Skip to main content

A SQLAlchemy dialect for connecting to a [GizmoSQL](https://github.com/gizmodata/GizmoSQL) server with ADBC

Project description

SQLAlchemy GizmoSQL ADBC Dialect

sqlalchemy-gizmosql-adbc-dialect-ci Supported Python Versions PyPI version PyPI Downloads

Basic SQLAlchemy dialect for GizmoSQL

Installation

Option 1 - from PyPi

$ pip install sqlalchemy-gizmosql-adbc-dialect

Option 2 - from source - for development

git clone https://github.com/gizmodata/sqlalchemy-gizmosql-adbc-dialect

cd sqlalchemy-gizmosql-adbc-dialect

# Create the virtual environment
python3 -m venv .venv

# Activate the virtual environment
. .venv/bin/activate

# Upgrade pip, setuptools, and wheel
pip install --upgrade pip setuptools wheel

# Install SQLAlchemy GizmoSQL ADBC Dialect - in editable mode with dev dependencies
pip install --editable .[dev]

Note

For the following commands - if you are running from source and using --editable mode (for development purposes) - you will need to set the PYTHONPATH environment variable as follows:

export PYTHONPATH=$(pwd)/src

Usage

Once you've installed this package, you should be able to just use it, as SQLAlchemy does a python path search

Start a GizmoSQL Server - example below - see https://github.com/gizmodata/GizmoSQL for more details

docker run --name gizmosql \
           --detach \
           --rm \
           --tty \
           --init \
           --publish 31337:31337 \
           --env TLS_ENABLED="1" \
           --env GIZMOSQL_PASSWORD="gizmosql_password" \
           --env PRINT_QUERIES="1" \
           --pull missing \
           gizmodata/gizmosql:latest

Connect with the SQLAlchemy GizmoSQL ADBC Dialect

import os
import logging

from sqlalchemy import create_engine, MetaData, Table, select, Column, text, Integer, String, Sequence
from sqlalchemy.orm import Session
from sqlalchemy.orm import declarative_base
from sqlalchemy.engine.url import URL

# Setup logging
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)


Base = declarative_base()


class FakeModel(Base):  # type: ignore
    __tablename__ = "fake"

    id = Column(Integer, Sequence("fakemodel_id_sequence"), primary_key=True)
    name = Column(String)


def main():
    # Build the URL
    url = URL.create(drivername="gizmosql",
                     host="localhost",
                     port=31337,
                     username=os.getenv("GIZMOSQL_USERNAME", "gizmosql_username"),
                     password=os.getenv("GIZMOSQL_PASSWORD", "gizmosql_password"),
                     query={"disableCertificateVerification": "True",
                            "useEncryption": "True"
                            }
                     )

    print(f"Database URL: {url}")

    engine = create_engine(url=url)
    Base.metadata.create_all(bind=engine)

    metadata = MetaData()
    metadata.reflect(bind=engine)

    for table_name in metadata.tables:
        print(f"Table name: {table_name}")

    with Session(bind=engine) as session:

        # Try ORM
        session.add(FakeModel(id=1, name="Joe"))
        session.commit()

        joe = session.query(FakeModel).filter(FakeModel.name == "Joe").first()

        assert joe.name == "Joe"

        # Execute some raw SQL
        results = session.execute(statement=text("SELECT * FROM fake")).fetchall()
        print(results)

        # Try a SQLAlchemy table select
        fake: Table = metadata.tables["fake"]
        stmt = select(fake.c.name)

        results = session.execute(statement=stmt).fetchall()
        print(results)


if __name__ == "__main__":
    main()

Credits

Much code and inspiration was taken from repo: https://github.com/Mause/duckdb_engine

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy_gizmosql_adbc_dialect-0.0.17.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file sqlalchemy_gizmosql_adbc_dialect-0.0.17.tar.gz.

File metadata

File hashes

Hashes for sqlalchemy_gizmosql_adbc_dialect-0.0.17.tar.gz
Algorithm Hash digest
SHA256 65d2d611dfaa33eecf19c0f5a6152f42a28bfb1c828a93f57be221b80aee0f47
MD5 e29890ea022d67d15a485b08b8a24349
BLAKE2b-256 5e1c282ac1203d81519333bb4e5ba36465a73183ecb1be87c1f6ee588aa3ee5e

See more details on using hashes here.

File details

Details for the file sqlalchemy_gizmosql_adbc_dialect-0.0.17-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_gizmosql_adbc_dialect-0.0.17-py3-none-any.whl
Algorithm Hash digest
SHA256 e0cbd47aa8786dc543aed4bc5e0e303c56575b989709a437eb60445f8064d2fc
MD5 fdefe72535243725a11f81134e8cc3b4
BLAKE2b-256 394de61ffa613d37f111a05d2653227b4845acac1c8ef397d7e81638255ea7ae

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page