Skip to content

Commit 73e70a0

Browse files
authored
Merge pull request #302 from datachecks/with-view
(DCS-940) Add: fetch view along with table name
2 parents 02722ca + b109f0b commit 73e70a0

5 files changed

Lines changed: 163 additions & 31 deletions

File tree

dcs_core/integrations/databases/mssql.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,42 @@ 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,
131-
) -> List[str]:
129+
self, schema: str | None = None, with_view: bool = False
130+
) -> dict:
132131
"""
133132
Get the list of tables in the database.
134133
:param schema: optional schema name
135-
:return: list of table names
134+
:param with_view: whether to include views
135+
:return: dictionary with table names and optionally view names
136136
"""
137137
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"
138+
139+
if with_view:
140+
object_types = "IN ('U', 'V')"
141+
else:
142+
object_types = "= 'U'"
143+
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"
139145

140146
rows = self.fetchall(query)
141-
res = [row[0] for row in rows] if rows else []
142-
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
143165

144166
def query_get_table_columns(
145167
self, table: str, schema: str | None = None

dcs_core/integrations/databases/mysql.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,40 @@ def quote_column(self, column: str) -> str:
9191
def query_get_table_names(
9292
self,
9393
schema: str | None = None,
94-
) -> List[str]:
94+
with_view: bool = False,
95+
) -> dict:
9596
"""
9697
Get the list of tables in the database.
9798
:param schema: optional schema name
98-
:return: list of table names
99+
:param with_view: whether to include views
100+
:return: dictionary with table names and optionally view names
99101
"""
100102
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-
result = self.fetchall(query)
103-
return [row[0] for row in result]
103+
if with_view:
104+
table_type_condition = "TABLES.TABLE_TYPE IN ('BASE TABLE', 'VIEW')"
105+
else:
106+
table_type_condition = "TABLES.TABLE_TYPE = 'BASE TABLE'"
107+
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
104128

105129
def query_get_table_columns(
106130
self, table: str, schema: str | None = None

dcs_core/integrations/databases/oracle.py

Lines changed: 36 additions & 6 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:
@@ -86,16 +88,44 @@ def quote_column(self, column: str) -> str:
8688
def query_get_table_names(
8789
self,
8890
schema: str | None = None,
89-
) -> List[str]:
91+
with_view: bool = False,
92+
) -> dict:
9093
"""
9194
Get the list of tables in the database.
9295
:param schema: optional schema name
93-
:return: list of table names
96+
:param with_view: whether to include views
97+
:return: dictionary with table names and optionally view names
9498
"""
9599
schema = schema or self.schema_name
96-
query = f"SELECT TABLE_NAME FROM ALL_ALL_TABLES WHERE OWNER = '{schema}'"
97-
result = self.fetchall(query)
98-
return [row[0] for row in result]
100+
101+
if with_view:
102+
query = (
103+
f"SELECT TABLE_NAME, 'TABLE' AS OBJECT_TYPE FROM ALL_ALL_TABLES WHERE OWNER = '{schema}' "
104+
f"UNION "
105+
f"SELECT VIEW_NAME AS TABLE_NAME, 'VIEW' AS OBJECT_TYPE FROM ALL_VIEWS WHERE OWNER = '{schema}'"
106+
)
107+
else:
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
99129

100130
def query_get_table_columns(
101131
self,

dcs_core/integrations/databases/postgres.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,46 @@ def quote_column(self, column: str) -> str:
7474
def query_get_table_names(
7575
self,
7676
schema: str | None = None,
77-
) -> List[str]:
77+
with_view: bool = False,
78+
) -> dict:
7879
"""
7980
Get the list of tables in the database.
8081
:param schema: optional schema name
81-
:return: list of table names
82+
:param with_view: whether to include views
83+
:return: dictionary with table names and optionally view names
8284
"""
8385

8486
schema = schema or self.schema_name
8587
database = self.quote_database(self.database)
88+
89+
if with_view:
90+
table_type_condition = "table_type IN ('BASE TABLE', 'VIEW')"
91+
else:
92+
table_type_condition = "table_type = 'BASE TABLE'"
93+
8694
query = (
87-
f'SELECT table_name FROM {database}.information_schema.tables '
88-
f"WHERE table_schema = '{schema}' AND table_type = 'BASE TABLE'"
95+
f"SELECT table_name, table_type FROM {database}.information_schema.tables "
96+
f"WHERE table_schema = '{schema}' AND {table_type_condition}"
8997
)
90-
result = self.fetchall(query)
91-
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
92117

93118
def query_get_table_columns(
94119
self,

dcs_core/integrations/databases/sybase.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,27 +331,58 @@ def query_get_table_columns(
331331
def query_get_table_names(
332332
self,
333333
schema: str | None = None,
334-
) -> List[str]:
334+
with_view: bool = False,
335+
) -> dict:
335336
"""
336337
Get the list of tables in the database.
337338
:param schema: optional schema name
338-
:return: list of table names
339+
:param with_view: whether to include views
340+
:return: dictionary with table names and optionally view names
339341
"""
340342
schema = schema or self.schema_name
341343
database = self.database
344+
if with_view:
345+
type_condition = "IN ('U', 'V')"
346+
else:
347+
type_condition = "= 'U'"
342348

343349
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'"
350+
table_type_condition = (
351+
"table_type IN ('BASE', 'VIEW')" if with_view else "table_type = 'BASE'"
352+
)
353+
query = f"SELECT table_name, table_type FROM {database}.SYS.SYSTABLE WHERE creator = USER_ID('{schema}') AND {table_type_condition}"
345354
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}')"
355+
query = f"SELECT name AS table_name, type FROM {database}..sysobjects WHERE type {type_condition} AND uid = USER_ID('{schema}')"
347356
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}')"
357+
query = f"SELECT name AS table_name, type FROM {database}.dbo.sysobjects WHERE type {type_condition} AND uid = USER_ID('{schema}')"
349358
else:
350359
raise ValueError("Unknown Sybase driver type")
351360

352361
rows = self.fetchall(query)
353-
res = [row[0] for row in rows] if rows else []
354-
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
355386

356387
def convert_regex_to_sybase_pattern(self, regex_pattern: str) -> str:
357388
"""

0 commit comments

Comments
 (0)