Skip to content

Commit cdc8b4b

Browse files
committed
add: fetch view along with table name
1 parent 02722ca commit cdc8b4b

5 files changed

Lines changed: 44 additions & 10 deletions

File tree

dcs_core/integrations/databases/mssql.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,21 @@ def quote_column(self, column: str) -> str:
126126
return f"[{column}]"
127127

128128
def query_get_table_names(
129-
self,
130-
schema: str | None = None,
129+
self, schema: str | None = None, with_view: bool = False
131130
) -> List[str]:
132131
"""
133132
Get the list of tables in the database.
134133
:param schema: optional schema name
135134
:return: list of table names
136135
"""
137136
schema = schema or self.schema_name
138-
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 = 'U' AND s.name = '{schema}' ORDER BY o.name"
137+
138+
if with_view:
139+
object_types = "IN ('U', 'V')"
140+
else:
141+
object_types = "= 'U'"
142+
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"
139144

140145
rows = self.fetchall(query)
141146
res = [row[0] for row in rows] if rows else []

dcs_core/integrations/databases/mysql.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,20 @@ def quote_column(self, column: str) -> str:
9191
def query_get_table_names(
9292
self,
9393
schema: str | None = None,
94+
with_view: bool = False,
9495
) -> List[str]:
9596
"""
9697
Get the list of tables in the database.
9798
:param schema: optional schema name
9899
:return: list of table names
99100
"""
100101
database = self.database
101-
query = f"SELECT TABLES.TABLE_NAME FROM information_schema.tables WHERE TABLES.TABLE_SCHEMA = '{database}' and TABLES.TABLE_TYPE = 'BASE TABLE'"
102+
if with_view:
103+
table_type_condition = "TABLES.TABLE_TYPE IN ('BASE TABLE', 'VIEW')"
104+
else:
105+
table_type_condition = "TABLES.TABLE_TYPE = 'BASE TABLE'"
106+
107+
query = f"SELECT TABLES.TABLE_NAME FROM information_schema.tables WHERE TABLES.TABLE_SCHEMA = '{database}' and {table_type_condition}"
102108
result = self.fetchall(query)
103109
return [row[0] for row in result]
104110

dcs_core/integrations/databases/oracle.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,22 @@ def quote_column(self, column: str) -> str:
8686
def query_get_table_names(
8787
self,
8888
schema: str | None = None,
89+
with_view: bool = False,
8990
) -> List[str]:
9091
"""
9192
Get the list of tables in the database.
9293
:param schema: optional schema name
9394
:return: list of table names
9495
"""
9596
schema = schema or self.schema_name
96-
query = f"SELECT TABLE_NAME FROM ALL_ALL_TABLES WHERE OWNER = '{schema}'"
97+
if with_view:
98+
query = (
99+
f"SELECT TABLE_NAME FROM ALL_ALL_TABLES WHERE OWNER = '{schema}' "
100+
f"UNION "
101+
f"SELECT VIEW_NAME AS TABLE_NAME FROM ALL_VIEWS WHERE OWNER = '{schema}'"
102+
)
103+
else:
104+
query = f"SELECT TABLE_NAME FROM ALL_ALL_TABLES WHERE OWNER = '{schema}'"
97105
result = self.fetchall(query)
98106
return [row[0] for row in result]
99107

dcs_core/integrations/databases/postgres.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def quote_column(self, column: str) -> str:
7474
def query_get_table_names(
7575
self,
7676
schema: str | None = None,
77+
with_view: bool = False,
7778
) -> List[str]:
7879
"""
7980
Get the list of tables in the database.
@@ -83,9 +84,15 @@ def query_get_table_names(
8384

8485
schema = schema or self.schema_name
8586
database = self.quote_database(self.database)
87+
88+
if with_view:
89+
table_type_condition = "table_type IN ('BASE TABLE', 'VIEW')"
90+
else:
91+
table_type_condition = "table_type = 'BASE TABLE'"
92+
8693
query = (
87-
f'SELECT table_name FROM {database}.information_schema.tables '
88-
f"WHERE table_schema = '{schema}' AND table_type = 'BASE TABLE'"
94+
f"SELECT table_name FROM {database}.information_schema.tables "
95+
f"WHERE table_schema = '{schema}' AND {table_type_condition}"
8996
)
9097
result = self.fetchall(query)
9198
return [row[0] for row in result]

dcs_core/integrations/databases/sybase.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ def query_get_table_columns(
331331
def query_get_table_names(
332332
self,
333333
schema: str | None = None,
334+
with_view: bool = False,
334335
) -> List[str]:
335336
"""
336337
Get the list of tables in the database.
@@ -339,13 +340,20 @@ def query_get_table_names(
339340
"""
340341
schema = schema or self.schema_name
341342
database = self.database
343+
if with_view:
344+
type_condition = "IN ('U', 'V')"
345+
else:
346+
type_condition = "= 'U'"
342347

343348
if self.sybase_driver_type.is_iq:
344-
query = f"SELECT table_name FROM {database}.SYS.SYSTABLE WHERE creator = USER_ID('{schema}') AND table_type = 'BASE'"
349+
table_type_condition = (
350+
"table_type IN ('BASE', 'VIEW')" if with_view else "table_type = 'BASE'"
351+
)
352+
query = f"SELECT table_name FROM {database}.SYS.SYSTABLE WHERE creator = USER_ID('{schema}') AND {table_type_condition}"
345353
elif self.sybase_driver_type.is_ase:
346-
query = f"SELECT name AS table_name FROM {database}..sysobjects WHERE type = 'U' AND uid = USER_ID('{schema}')"
354+
query = f"SELECT name AS table_name FROM {database}..sysobjects WHERE type {type_condition} AND uid = USER_ID('{schema}')"
347355
elif self.sybase_driver_type.is_freetds:
348-
query = f"SELECT name AS table_name FROM {database}.dbo.sysobjects WHERE type = 'U' AND uid = USER_ID('{schema}')"
356+
query = f"SELECT name AS table_name FROM {database}.dbo.sysobjects WHERE type {type_condition} AND uid = USER_ID('{schema}')"
349357
else:
350358
raise ValueError("Unknown Sybase driver type")
351359

0 commit comments

Comments
 (0)