Skip to content

Commit b109f0b

Browse files
committed
add: view and table return to query_get_table_names
1 parent cdc8b4b commit b109f0b

5 files changed

Lines changed: 128 additions & 30 deletions

File tree

dcs_core/integrations/databases/mssql.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,12 @@ def quote_column(self, column: str) -> str:
127127

128128
def query_get_table_names(
129129
self, schema: str | None = None, with_view: bool = False
130-
) -> List[str]:
130+
) -> dict:
131131
"""
132132
Get the list of tables in the database.
133133
:param schema: optional schema name
134-
:return: list of table names
134+
:param with_view: whether to include views
135+
:return: dictionary with table names and optionally view names
135136
"""
136137
schema = schema or self.schema_name
137138

@@ -140,11 +141,27 @@ def query_get_table_names(
140141
else:
141142
object_types = "= 'U'"
142143

143-
query = f"SELECT o.name AS table_name FROM sys.objects o JOIN sys.schemas s ON o.schema_id = s.schema_id WHERE o.type {object_types} AND s.name = '{schema}' ORDER BY o.name"
144+
query = f"SELECT o.name AS table_name, o.type FROM sys.objects o JOIN sys.schemas s ON o.schema_id = s.schema_id WHERE o.type {object_types} AND s.name = '{schema}' ORDER BY o.name"
144145

145146
rows = self.fetchall(query)
146-
res = [row[0] for row in rows] if rows else []
147-
return res
147+
148+
if with_view:
149+
result = {"table": [], "view": []}
150+
if rows:
151+
for row in rows:
152+
object_name = row[0]
153+
object_type = row[1].strip() if row[1] else row[1]
154+
155+
if object_type == "U":
156+
result["table"].append(object_name)
157+
elif object_type == "V":
158+
result["view"].append(object_name)
159+
else:
160+
result = {"table": []}
161+
if rows:
162+
result["table"] = [row[0] for row in rows]
163+
164+
return result
148165

149166
def query_get_table_columns(
150167
self, table: str, schema: str | None = None

dcs_core/integrations/databases/mysql.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,39 @@ def query_get_table_names(
9292
self,
9393
schema: str | None = None,
9494
with_view: bool = False,
95-
) -> List[str]:
95+
) -> dict:
9696
"""
9797
Get the list of tables in the database.
9898
:param schema: optional schema name
99-
:return: list of table names
99+
:param with_view: whether to include views
100+
:return: dictionary with table names and optionally view names
100101
"""
101102
database = self.database
102103
if with_view:
103104
table_type_condition = "TABLES.TABLE_TYPE IN ('BASE TABLE', 'VIEW')"
104105
else:
105106
table_type_condition = "TABLES.TABLE_TYPE = 'BASE TABLE'"
106107

107-
query = f"SELECT TABLES.TABLE_NAME FROM information_schema.tables WHERE TABLES.TABLE_SCHEMA = '{database}' and {table_type_condition}"
108-
result = self.fetchall(query)
109-
return [row[0] for row in result]
108+
query = f"SELECT TABLES.TABLE_NAME, TABLES.TABLE_TYPE FROM information_schema.tables WHERE TABLES.TABLE_SCHEMA = '{database}' and {table_type_condition}"
109+
rows = self.fetchall(query)
110+
111+
if with_view:
112+
result = {"table": [], "view": []}
113+
if rows:
114+
for row in rows:
115+
table_name = row[0]
116+
table_type = row[1].strip() if row[1] else row[1]
117+
118+
if table_type == "BASE TABLE":
119+
result["table"].append(table_name)
120+
elif table_type == "VIEW":
121+
result["view"].append(table_name)
122+
else:
123+
result = {"table": []}
124+
if rows:
125+
result["table"] = [row[0] for row in rows]
126+
127+
return result
110128

111129
def query_get_table_columns(
112130
self, table: str, schema: str | None = None

dcs_core/integrations/databases/oracle.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ def connect(self) -> Any:
5757
"service_name": self.data_connection.get("service_name"),
5858
},
5959
)
60-
self.schema_name = self.data_connection.get("username")
60+
self.schema_name = self.data_connection.get(
61+
"schema"
62+
) or self.data_connection.get("username")
6163
self.connection = engine.connect()
6264
return self.connection
6365
except Exception as e:
@@ -87,23 +89,43 @@ def query_get_table_names(
8789
self,
8890
schema: str | None = None,
8991
with_view: bool = False,
90-
) -> List[str]:
92+
) -> dict:
9193
"""
9294
Get the list of tables in the database.
9395
:param schema: optional schema name
94-
:return: list of table names
96+
:param with_view: whether to include views
97+
:return: dictionary with table names and optionally view names
9598
"""
9699
schema = schema or self.schema_name
100+
97101
if with_view:
98102
query = (
99-
f"SELECT TABLE_NAME FROM ALL_ALL_TABLES WHERE OWNER = '{schema}' "
103+
f"SELECT TABLE_NAME, 'TABLE' AS OBJECT_TYPE FROM ALL_ALL_TABLES WHERE OWNER = '{schema}' "
100104
f"UNION "
101-
f"SELECT VIEW_NAME AS TABLE_NAME FROM ALL_VIEWS WHERE OWNER = '{schema}'"
105+
f"SELECT VIEW_NAME AS TABLE_NAME, 'VIEW' AS OBJECT_TYPE FROM ALL_VIEWS WHERE OWNER = '{schema}'"
102106
)
103107
else:
104-
query = f"SELECT TABLE_NAME FROM ALL_ALL_TABLES WHERE OWNER = '{schema}'"
105-
result = self.fetchall(query)
106-
return [row[0] for row in result]
108+
query = f"SELECT TABLE_NAME, 'TABLE' AS OBJECT_TYPE FROM ALL_ALL_TABLES WHERE OWNER = '{schema}'"
109+
110+
rows = self.fetchall(query)
111+
112+
if with_view:
113+
result = {"table": [], "view": []}
114+
if rows:
115+
for row in rows:
116+
object_name = row[0]
117+
object_type = row[1].strip() if row[1] else row[1]
118+
119+
if object_type == "TABLE":
120+
result["table"].append(object_name)
121+
elif object_type == "VIEW":
122+
result["view"].append(object_name)
123+
else:
124+
result = {"table": []}
125+
if rows:
126+
result["table"] = [row[0] for row in rows]
127+
128+
return result
107129

108130
def query_get_table_columns(
109131
self,

dcs_core/integrations/databases/postgres.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ def query_get_table_names(
7575
self,
7676
schema: str | None = None,
7777
with_view: bool = False,
78-
) -> List[str]:
78+
) -> dict:
7979
"""
8080
Get the list of tables in the database.
8181
:param schema: optional schema name
82-
:return: list of table names
82+
:param with_view: whether to include views
83+
:return: dictionary with table names and optionally view names
8384
"""
8485

8586
schema = schema or self.schema_name
@@ -91,11 +92,28 @@ def query_get_table_names(
9192
table_type_condition = "table_type = 'BASE TABLE'"
9293

9394
query = (
94-
f"SELECT table_name FROM {database}.information_schema.tables "
95+
f"SELECT table_name, table_type FROM {database}.information_schema.tables "
9596
f"WHERE table_schema = '{schema}' AND {table_type_condition}"
9697
)
97-
result = self.fetchall(query)
98-
return [row[0] for row in result]
98+
rows = self.fetchall(query)
99+
100+
if with_view:
101+
result = {"table": [], "view": []}
102+
if rows:
103+
for row in rows:
104+
table_name = row[0]
105+
table_type = row[1].strip() if row[1] else row[1]
106+
107+
if table_type == "BASE TABLE":
108+
result["table"].append(table_name)
109+
elif table_type == "VIEW":
110+
result["view"].append(table_name)
111+
else:
112+
result = {"table": []}
113+
if rows:
114+
result["table"] = [row[0] for row in rows]
115+
116+
return result
99117

100118
def query_get_table_columns(
101119
self,

dcs_core/integrations/databases/sybase.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,12 @@ def query_get_table_names(
332332
self,
333333
schema: str | None = None,
334334
with_view: bool = False,
335-
) -> List[str]:
335+
) -> dict:
336336
"""
337337
Get the list of tables in the database.
338338
:param schema: optional schema name
339-
:return: list of table names
339+
:param with_view: whether to include views
340+
:return: dictionary with table names and optionally view names
340341
"""
341342
schema = schema or self.schema_name
342343
database = self.database
@@ -349,17 +350,39 @@ def query_get_table_names(
349350
table_type_condition = (
350351
"table_type IN ('BASE', 'VIEW')" if with_view else "table_type = 'BASE'"
351352
)
352-
query = f"SELECT table_name FROM {database}.SYS.SYSTABLE WHERE creator = USER_ID('{schema}') AND {table_type_condition}"
353+
query = f"SELECT table_name, table_type FROM {database}.SYS.SYSTABLE WHERE creator = USER_ID('{schema}') AND {table_type_condition}"
353354
elif self.sybase_driver_type.is_ase:
354-
query = f"SELECT name AS table_name FROM {database}..sysobjects WHERE type {type_condition} AND uid = USER_ID('{schema}')"
355+
query = f"SELECT name AS table_name, type FROM {database}..sysobjects WHERE type {type_condition} AND uid = USER_ID('{schema}')"
355356
elif self.sybase_driver_type.is_freetds:
356-
query = f"SELECT name AS table_name FROM {database}.dbo.sysobjects WHERE type {type_condition} AND uid = USER_ID('{schema}')"
357+
query = f"SELECT name AS table_name, type FROM {database}.dbo.sysobjects WHERE type {type_condition} AND uid = USER_ID('{schema}')"
357358
else:
358359
raise ValueError("Unknown Sybase driver type")
359360

360361
rows = self.fetchall(query)
361-
res = [row[0] for row in rows] if rows else []
362-
return res
362+
363+
if with_view:
364+
result = {"table": [], "view": []}
365+
if rows:
366+
for row in rows:
367+
table_name = row[0]
368+
table_type = row[1].strip() if row[1] else row[1]
369+
370+
if self.sybase_driver_type.is_iq:
371+
if table_type == "BASE":
372+
result["table"].append(table_name)
373+
elif table_type == "VIEW":
374+
result["view"].append(table_name)
375+
else: # ASE or FreeTDS
376+
if table_type == "U":
377+
result["table"].append(table_name)
378+
elif table_type == "V":
379+
result["view"].append(table_name)
380+
else:
381+
result = {"table": []}
382+
if rows:
383+
result["table"] = [row[0] for row in rows]
384+
385+
return result
363386

364387
def convert_regex_to_sybase_pattern(self, regex_pattern: str) -> str:
365388
"""

0 commit comments

Comments
 (0)