Skip to content

Commit da43ed3

Browse files
refactor: route schema queries through adapter methods
- list_schemas() uses adapter.list_schemas_sql() - make_classes() uses adapter.list_tables_sql() instead of SHOW TABLES - Schema.exists uses new adapter.schema_exists_sql() method - Added schema_exists_sql() to base, MySQL, and PostgreSQL adapters Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 33ef072 commit da43ed3

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

src/datajoint/adapters/base.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,23 @@ def list_schemas_sql(self) -> str:
615615
"""
616616
...
617617

618+
@abstractmethod
619+
def schema_exists_sql(self, schema_name: str) -> str:
620+
"""
621+
Generate query to check if a schema exists.
622+
623+
Parameters
624+
----------
625+
schema_name : str
626+
Name of schema to check.
627+
628+
Returns
629+
-------
630+
str
631+
SQL query that returns a row if the schema exists.
632+
"""
633+
...
634+
618635
@abstractmethod
619636
def list_tables_sql(self, schema_name: str, pattern: str | None = None) -> str:
620637
"""

src/datajoint/adapters/mysql.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,10 @@ def list_schemas_sql(self) -> str:
611611
"""Query to list all databases in MySQL."""
612612
return "SELECT schema_name FROM information_schema.schemata"
613613

614+
def schema_exists_sql(self, schema_name: str) -> str:
615+
"""Query to check if a database exists in MySQL."""
616+
return f"SELECT schema_name FROM information_schema.schemata WHERE schema_name = {self.quote_string(schema_name)}"
617+
614618
def list_tables_sql(self, schema_name: str, pattern: str | None = None) -> str:
615619
"""Query to list tables in a database."""
616620
sql = f"SHOW TABLES IN {self.quote_identifier(schema_name)}"

src/datajoint/adapters/postgres.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,10 @@ def list_schemas_sql(self) -> str:
721721
"WHERE schema_name NOT IN ('pg_catalog', 'information_schema')"
722722
)
723723

724+
def schema_exists_sql(self, schema_name: str) -> str:
725+
"""Query to check if a schema exists in PostgreSQL."""
726+
return f"SELECT schema_name FROM information_schema.schemata WHERE schema_name = {self.quote_string(schema_name)}"
727+
724728
def list_tables_sql(self, schema_name: str, pattern: str | None = None) -> str:
725729
"""Query to list tables in a schema."""
726730
sql = (

src/datajoint/schemas.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def make_classes(self, into: dict[str, Any] | None = None) -> None:
345345
del frame
346346
tables = [
347347
row[0]
348-
for row in self.connection.query("SHOW TABLES in `%s`" % self.database)
348+
for row in self.connection.query(self.connection.adapter.list_tables_sql(self.database))
349349
if lookup_class_name("`{db}`.`{tab}`".format(db=self.database, tab=row[0]), into, 0) is None
350350
]
351351
master_classes = (Lookup, Manual, Imported, Computed)
@@ -423,13 +423,7 @@ def exists(self) -> bool:
423423
"""
424424
if self.database is None:
425425
raise DataJointError("Schema must be activated first.")
426-
return bool(
427-
self.connection.query(
428-
"SELECT schema_name FROM information_schema.schemata WHERE schema_name = '{database}'".format(
429-
database=self.database
430-
)
431-
).rowcount
432-
)
426+
return bool(self.connection.query(self.connection.adapter.schema_exists_sql(self.database)).rowcount)
433427

434428
@property
435429
def lineage_table_exists(self) -> bool:
@@ -838,12 +832,8 @@ def list_schemas(connection: Connection | None = None) -> list[str]:
838832
list[str]
839833
Names of all accessible schemas.
840834
"""
841-
return [
842-
r[0]
843-
for r in (connection or _get_singleton_connection()).query(
844-
'SELECT schema_name FROM information_schema.schemata WHERE schema_name <> "information_schema"'
845-
)
846-
]
835+
conn = connection or _get_singleton_connection()
836+
return [r[0] for r in conn.query(conn.adapter.list_schemas_sql())]
847837

848838

849839
def virtual_schema(

0 commit comments

Comments
 (0)