Skip to content

Commit f746eb2

Browse files
authored
feat: add fetch_sample_values_from_database method (#332)
* feat: add fetch_sample_values_from_database method * fix: update fetch_rows method to enforce column name requirement and adjust return type
1 parent 1e6e5a6 commit f746eb2

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

dcs_core/integrations/databases/postgres.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,28 @@ def fetch_rows(
284284
return rows, list(column_names)
285285
else:
286286
return rows, None
287+
288+
def fetch_sample_values_from_database(
289+
self,
290+
table_name: str,
291+
column_names: list[str],
292+
limit: int = 5,
293+
) -> List[Tuple]:
294+
"""
295+
Fetch sample rows for specific columns from the given table.
296+
297+
:param table_name: The name of the table.
298+
:param column_names: List of column names to fetch.
299+
:param limit: Number of rows to fetch.
300+
:return: List of row tuples.
301+
"""
302+
table_name = self.qualified_table_name(table_name)
303+
304+
if not column_names:
305+
raise ValueError("At least one column name must be provided")
306+
307+
columns = ", ".join([self.quote_column(col) for col in column_names])
308+
query = f"SELECT {columns} FROM {table_name} LIMIT {limit}"
309+
result = self.connection.execute(text(query))
310+
rows = result.fetchall()
311+
return rows

dcs_core/integrations/databases/sybase.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,22 @@ def fetch_rows(
603603
else:
604604
return rows, None
605605

606+
def fetch_sample_values_from_database(
607+
self,
608+
table_name: str,
609+
column_names: list[str],
610+
limit: int = 5,
611+
) -> list[Tuple]:
612+
table_name = self.qualified_table_name(table_name)
613+
if not column_names:
614+
raise ValueError("At least one column name must be provided")
615+
columns = ", ".join([self.quote_column(col) for col in column_names])
616+
query = f"SELECT TOP {limit} {columns} FROM {table_name}"
617+
cursor = self.connection.cursor()
618+
cursor.execute(query)
619+
rows = cursor.fetchmany(limit)
620+
return rows
621+
606622
def convert_regex_to_sybase_pattern(self, regex_pattern: str) -> str:
607623
"""
608624
Convert a regex pattern into a Sybase-compatible LIKE pattern.

0 commit comments

Comments
 (0)