Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions dcs_core/core/datasource/sql_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ def query_get_table_metadata(self) -> List[str]:
"""
return inspect(self.connection.engine).get_table_names()

def query_get_database_version(self) -> str:
"""
Get the database version
:return: version number
"""
query = "SELECT @@version"
result = self.fetchone(query)[0]
Comment on lines +182 to +183

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Hard-coding SELECT @@version limits engine support

@@version is SQL-Server / Sybase-specific. SQLDataSource is dialect-agnostic, so subclasses pointing at Postgres, MySQL, etc. will break.
Consider:

  1. Inspecting the SQLAlchemy dialect (self.connection.engine.name) and issuing the appropriate query (SELECT version() for Postgres/MySQL, PRAGMA user_version for SQLite, etc.).
  2. Providing an overridable method in each concrete data-source subclass instead of implementing a one-size-fits-all query here.
🤖 Prompt for AI Agents
In dcs_core/core/datasource/sql_datasource.py around lines 182 to 183, the query
"SELECT @@version" is hard-coded, which only works for SQL Server and Sybase,
breaking compatibility with other databases. To fix this, detect the database
dialect using self.connection.engine.name and run the appropriate version query
for each dialect (e.g., "SELECT version()" for Postgres/MySQL, "PRAGMA
user_version" for SQLite). Alternatively, create an overridable method in
subclasses to provide the correct version query per database type, ensuring
dialect-agnostic behavior.


if result:
return result
Comment on lines +182 to +186

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Defensive-programming: guard against None rows and empty tuples

self.fetchone(query)[0] will raise TypeError if the query returns None, and IndexError if an empty tuple is returned. Fetch the row first, validate it, then unwrap.

-        result = self.fetchone(query)[0]
-
-        if result:
-            return result
+        row = self.fetchone(query)
+        if row and len(row) > 0 and row[0]:
+            return row[0]
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
query = "SELECT @@version"
result = self.fetchone(query)[0]
if result:
return result
query = "SELECT @@version"
row = self.fetchone(query)
if row and len(row) > 0 and row[0]:
return row[0]
🤖 Prompt for AI Agents
In dcs_core/core/datasource/sql_datasource.py around lines 182 to 186, the code
directly accesses the first element of the result from self.fetchone(query)
without checking if the result is None or an empty tuple, which can cause
TypeError or IndexError. Modify the code to first assign the result of
self.fetchone(query) to a variable, then check if this variable is not None and
contains at least one element before accessing the first element. Return the
first element only after these validations.


return "Unknown version"

def query_get_row_count(self, table: str, filters: str = None) -> int:
"""
Get the row count
Expand Down
Loading