-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
psycopg
and asyncpg
drivers
This introduces the `crate+psycopg://`, `crate+asyncpg://`, and `crate+urllib3://` dialect identifiers. The asynchronous variant of `psycopg` is also supported.
- Loading branch information
Showing
5 changed files
with
237 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# -*- coding: utf-8; -*- | ||
# | ||
# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor | ||
# license agreements. See the NOTICE file distributed with this work for | ||
# additional information regarding copyright ownership. Crate licenses | ||
# this file to you under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. You may | ||
# obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
# However, if you have executed another commercial license agreement | ||
# with Crate these terms will supersede the license and you may use the | ||
# software solely pursuant to the terms of the relevant commercial agreement. | ||
from sqlalchemy.engine.reflection import Inspector | ||
from sqlalchemy_postgresql_relaxed.asyncpg import PGDialect_asyncpg_relaxed | ||
from sqlalchemy_postgresql_relaxed.base import PGDialect_relaxed | ||
from sqlalchemy_postgresql_relaxed.psycopg import ( | ||
PGDialect_psycopg_relaxed, | ||
PGDialectAsync_psycopg_relaxed, | ||
) | ||
|
||
from sqlalchemy_cratedb import dialect | ||
|
||
|
||
class CrateDialectPostgresAdapter(PGDialect_relaxed, dialect): | ||
""" | ||
Provide a dialect on top of the relaxed PostgreSQL dialect. | ||
""" | ||
|
||
inspector = Inspector | ||
|
||
# Need to manually override some methods because of polymorphic inheritance woes. | ||
# TODO: Investigate if this can be solved using metaprogramming or other techniques. | ||
has_schema = dialect.has_schema | ||
has_table = dialect.has_table | ||
get_schema_names = dialect.get_schema_names | ||
get_table_names = dialect.get_table_names | ||
get_view_names = dialect.get_view_names | ||
get_columns = dialect.get_columns | ||
get_pk_constraint = dialect.get_pk_constraint | ||
get_foreign_keys = dialect.get_foreign_keys | ||
get_indexes = dialect.get_indexes | ||
|
||
get_multi_columns = dialect.get_multi_columns | ||
get_multi_pk_constraint = dialect.get_multi_pk_constraint | ||
get_multi_foreign_keys = dialect.get_multi_foreign_keys | ||
|
||
# TODO: Those may want to go to dialect instead? | ||
def get_multi_indexes(self, *args, **kwargs): | ||
return [] | ||
|
||
def get_multi_unique_constraints(self, *args, **kwargs): | ||
return [] | ||
|
||
def get_multi_check_constraints(self, *args, **kwargs): | ||
return [] | ||
|
||
def get_multi_table_comment(self, *args, **kwargs): | ||
return [] | ||
|
||
|
||
class CrateDialect_psycopg(PGDialect_psycopg_relaxed, CrateDialectPostgresAdapter): | ||
driver = "psycopg" | ||
|
||
@classmethod | ||
def get_async_dialect_cls(cls, url): | ||
return CrateDialectAsync_psycopg | ||
|
||
@classmethod | ||
def import_dbapi(cls): | ||
import psycopg | ||
|
||
return psycopg | ||
|
||
|
||
class CrateDialectAsync_psycopg(PGDialectAsync_psycopg_relaxed, CrateDialectPostgresAdapter): | ||
driver = "psycopg_async" | ||
is_async = True | ||
|
||
|
||
class CrateDialect_asyncpg(PGDialect_asyncpg_relaxed, CrateDialectPostgresAdapter): | ||
driver = "asyncpg" | ||
|
||
# TODO: asyncpg may have `paramstyle="numeric_dollar"`. Review this! | ||
|
||
# TODO: AttributeError: module 'asyncpg' has no attribute 'paramstyle' | ||
""" | ||
@classmethod | ||
def import_dbapi(cls): | ||
import asyncpg | ||
return asyncpg | ||
""" | ||
|
||
|
||
dialect_urllib3 = dialect | ||
dialect_psycopg = CrateDialect_psycopg | ||
dialect_psycopg_async = CrateDialectAsync_psycopg | ||
dialect_asyncpg = CrateDialect_asyncpg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import pytest | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import registry as dialect_registry | ||
|
||
from sqlalchemy_cratedb import SA_VERSION, SA_2_0 | ||
|
||
if SA_VERSION < SA_2_0: | ||
raise pytest.skip("Only supported on SQLAlchemy 2.0 and higher", allow_module_level=True) | ||
|
||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine | ||
|
||
# Registering the additional dialects manually seems to be needed when running | ||
# under tests. Apparently, manual registration is not needed under regular | ||
# circumstances, as this is wired through the `sqlalchemy.dialects` entrypoint | ||
# registrations in `pyproject.toml`. It is definitively weird, but c'est la vie. | ||
dialect_registry.register("crate.urllib3", "sqlalchemy_cratedb.dialect_more", "dialect_urllib3") | ||
dialect_registry.register("crate.asyncpg", "sqlalchemy_cratedb.dialect_more", "dialect_asyncpg") | ||
dialect_registry.register("crate.psycopg", "sqlalchemy_cratedb.dialect_more", "dialect_psycopg") | ||
|
||
|
||
QUERY = sa.text("SELECT mountain, coordinates FROM sys.summits ORDER BY mountain LIMIT 3;") | ||
|
||
|
||
def test_engine_sync_vanilla(): | ||
""" | ||
crate:// -- Verify connectivity and data transport with vanilla HTTP-based driver. | ||
""" | ||
engine = sa.create_engine("crate://crate@localhost:4200/", echo=True) | ||
assert isinstance(engine, sa.engine.Engine) | ||
with engine.connect() as connection: | ||
result = connection.execute(QUERY) | ||
assert result.mappings().fetchone() == {'mountain': 'Acherkogel', 'coordinates': [10.95667, 47.18917]} | ||
|
||
|
||
def test_engine_sync_urllib3(): | ||
""" | ||
crate+urllib3:// -- Verify connectivity and data transport *explicitly* selecting the HTTP driver. | ||
""" | ||
engine = sa.create_engine("crate+urllib3://crate@localhost:4200/", isolation_level="AUTOCOMMIT", echo=True) | ||
assert isinstance(engine, sa.engine.Engine) | ||
with engine.connect() as connection: | ||
result = connection.execute(QUERY) | ||
assert result.mappings().fetchone() == {'mountain': 'Acherkogel', 'coordinates': [10.95667, 47.18917]} | ||
|
||
|
||
def test_engine_sync_psycopg(): | ||
""" | ||
crate+psycopg:// -- Verify connectivity and data transport using the psycopg driver (version 3). | ||
""" | ||
engine = sa.create_engine("crate+psycopg://crate@localhost:5432/", isolation_level="AUTOCOMMIT", echo=True) | ||
assert isinstance(engine, sa.engine.Engine) | ||
with engine.connect() as connection: | ||
result = connection.execute(QUERY) | ||
assert result.mappings().fetchone() == {'mountain': 'Acherkogel', 'coordinates': '(10.95667,47.18917)'} | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_engine_async_psycopg(): | ||
""" | ||
crate+psycopg:// -- Verify connectivity and data transport using the psycopg driver (version 3). | ||
This time, in asynchronous mode. | ||
""" | ||
engine = create_async_engine("crate+psycopg://crate@localhost:5432/", isolation_level="AUTOCOMMIT", echo=True) | ||
assert isinstance(engine, AsyncEngine) | ||
async with engine.begin() as conn: | ||
result = await conn.execute(QUERY) | ||
assert result.mappings().fetchone() == {'mountain': 'Acherkogel', 'coordinates': '(10.95667,47.18917)'} | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_engine_async_asyncpg(): | ||
""" | ||
crate+asyncpg:// -- Verify connectivity and data transport using the asyncpg driver. | ||
This exclusively uses asynchronous mode. | ||
""" | ||
from asyncpg.pgproto.types import Point | ||
engine = create_async_engine("crate+asyncpg://crate@localhost:5432/", isolation_level="AUTOCOMMIT", echo=True) | ||
assert isinstance(engine, AsyncEngine) | ||
async with engine.begin() as conn: | ||
result = await conn.execute(QUERY) | ||
assert result.mappings().fetchone() == {'mountain': 'Acherkogel', 'coordinates': Point(10.95667, 47.18917)} |