Skip to content

Commit 390f592

Browse files
house SQL commands in constants
Signed-off-by: varun-edachali-dbx <varun.edachali@databricks.com>
1 parent 22dc252 commit 390f592

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

src/databricks/sql/backend/sea/backend.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
ResultDisposition,
1111
ResultCompression,
1212
WaitTimeout,
13+
MetadataCommands,
1314
)
1415

1516
if TYPE_CHECKING:
@@ -635,7 +636,7 @@ def get_catalogs(
635636
) -> "ResultSet":
636637
"""Get available catalogs by executing 'SHOW CATALOGS'."""
637638
result = self.execute_command(
638-
operation="SHOW CATALOGS",
639+
operation=MetadataCommands.SHOW_CATALOGS.value,
639640
session_id=session_id,
640641
max_rows=max_rows,
641642
max_bytes=max_bytes,
@@ -662,10 +663,10 @@ def get_schemas(
662663
if not catalog_name:
663664
raise ValueError("Catalog name is required for get_schemas")
664665

665-
operation = f"SHOW SCHEMAS IN {catalog_name}"
666+
operation = MetadataCommands.SHOW_SCHEMAS.value.format(catalog_name)
666667

667668
if schema_name:
668-
operation += f" LIKE '{schema_name}'"
669+
operation += MetadataCommands.LIKE_PATTERN.value.format(schema_name)
669670

670671
result = self.execute_command(
671672
operation=operation,
@@ -697,17 +698,19 @@ def get_tables(
697698
if not catalog_name:
698699
raise ValueError("Catalog name is required for get_tables")
699700

700-
operation = "SHOW TABLES IN " + (
701-
"ALL CATALOGS"
701+
operation = (
702+
MetadataCommands.SHOW_TABLES_ALL_CATALOGS.value
702703
if catalog_name in [None, "*", "%"]
703-
else f"CATALOG {catalog_name}"
704+
else MetadataCommands.SHOW_TABLES.value.format(
705+
MetadataCommands.CATALOG_SPECIFIC.value.format(catalog_name)
706+
)
704707
)
705708

706709
if schema_name:
707-
operation += f" SCHEMA LIKE '{schema_name}'"
710+
operation += MetadataCommands.SCHEMA_LIKE_PATTERN.value.format(schema_name)
708711

709712
if table_name:
710-
operation += f" LIKE '{table_name}'"
713+
operation += MetadataCommands.LIKE_PATTERN.value.format(table_name)
711714

712715
result = self.execute_command(
713716
operation=operation,
@@ -745,16 +748,16 @@ def get_columns(
745748
if not catalog_name:
746749
raise ValueError("Catalog name is required for get_columns")
747750

748-
operation = f"SHOW COLUMNS IN CATALOG {catalog_name}"
751+
operation = MetadataCommands.SHOW_COLUMNS.value.format(catalog_name)
749752

750753
if schema_name:
751-
operation += f" SCHEMA LIKE '{schema_name}'"
754+
operation += MetadataCommands.SCHEMA_LIKE_PATTERN.value.format(schema_name)
752755

753756
if table_name:
754-
operation += f" TABLE LIKE '{table_name}'"
757+
operation += MetadataCommands.TABLE_LIKE_PATTERN.value.format(table_name)
755758

756759
if column_name:
757-
operation += f" LIKE '{column_name}'"
760+
operation += MetadataCommands.LIKE_PATTERN.value.format(column_name)
758761

759762
result = self.execute_command(
760763
operation=operation,

src/databricks/sql/backend/sea/utils/constants.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,23 @@ class WaitTimeout(Enum):
4545

4646
ASYNC = "0s"
4747
SYNC = "10s"
48+
49+
50+
class MetadataCommands(Enum):
51+
"""SQL commands used in the SEA backend.
52+
53+
These constants are used for metadata operations and other SQL queries
54+
to ensure consistency and avoid string literal duplication.
55+
"""
56+
57+
SHOW_CATALOGS = "SHOW CATALOGS"
58+
SHOW_SCHEMAS = "SHOW SCHEMAS IN {}"
59+
SHOW_TABLES = "SHOW TABLES IN {}"
60+
SHOW_TABLES_ALL_CATALOGS = "SHOW TABLES IN ALL CATALOGS"
61+
SHOW_COLUMNS = "SHOW COLUMNS IN CATALOG {}"
62+
63+
SCHEMA_LIKE_PATTERN = " SCHEMA LIKE '{}'"
64+
TABLE_LIKE_PATTERN = " TABLE LIKE '{}'"
65+
LIKE_PATTERN = " LIKE '{}'"
66+
67+
CATALOG_SPECIFIC = "CATALOG {}"

0 commit comments

Comments
 (0)