diff --git a/duckdb_engine/__init__.py b/duckdb_engine/__init__.py index 85350b2c..b5f84346 100644 --- a/duckdb_engine/__init__.py +++ b/duckdb_engine/__init__.py @@ -17,7 +17,7 @@ import duckdb import sqlalchemy -from sqlalchemy import pool, text, util +from sqlalchemy import pool, text, util, select, sql from sqlalchemy import types as sqltypes from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.dialects.postgresql.base import ( @@ -25,6 +25,7 @@ PGIdentifierPreparer, PGInspector, PGTypeCompiler, + pg_catalog, ) from sqlalchemy.dialects.postgresql.psycopg2 import PGDialect_psycopg2 from sqlalchemy.engine.default import DefaultDialect @@ -34,6 +35,7 @@ from sqlalchemy.exc import NoSuchTableError from sqlalchemy.ext.compiler import compiles from sqlalchemy.sql.selectable import Select +from sqlalchemy.sql import bindparam from ._supports import has_comment_support from .config import apply_config, get_core_config @@ -601,6 +603,31 @@ def get_multi_columns( return columns.items() + def _comment_query(self, schema, has_filter_names, scope, kind): + relkinds = self._kind_to_relkinds(kind) + query = ( + select( + pg_catalog.pg_class.c.relname, + pg_catalog.pg_description.c.description, + ) + .select_from(pg_catalog.pg_class) + .outerjoin( + pg_catalog.pg_description, + sql.and_( + pg_catalog.pg_class.c.oid + == pg_catalog.pg_description.c.objoid, + pg_catalog.pg_description.c.objsubid == 0, + ), + ) + .where(self._pg_class_relkind_condition(relkinds)) + ) + query = self._pg_class_filter_scope_schema(query, schema, scope) + if has_filter_names: + query = query.where( + pg_catalog.pg_class.c.relname.in_(bindparam("filter_names")) + ) + return query + if sqlalchemy.__version__ >= "2.0.14": from sqlalchemy import TryCast # type: ignore[attr-defined] diff --git a/duckdb_engine/tests/test_reflection.py b/duckdb_engine/tests/test_reflection.py new file mode 100644 index 00000000..cea54d52 --- /dev/null +++ b/duckdb_engine/tests/test_reflection.py @@ -0,0 +1,12 @@ +from sqlalchemy import MetaData, create_engine, text + +engine = create_engine("duckdb:///:memory:") +metadata = MetaData() +with engine.connect() as conn: + conn.execute(text("CREATE TABLE tbl(col1 INTEGER)")) + conn.commit() +try: + metadata.reflect(engine) +except Exception as e: + print("Exception: " + str(e)) + raise AssertionError \ No newline at end of file