Update tools/sql_tool.py
Browse files- tools/sql_tool.py +11 -8
tools/sql_tool.py
CHANGED
|
@@ -22,8 +22,7 @@ def get_md_connection() -> duckdb.DuckDBPyConnection:
|
|
| 22 |
)
|
| 23 |
|
| 24 |
# 2. Connect to the MotherDuck service
|
| 25 |
-
# Note:
|
| 26 |
-
# otherwise it connects to the default MotherDuck endpoint.
|
| 27 |
conn = duckdb.connect(f'md:?motherduck_token={token}')
|
| 28 |
return conn
|
| 29 |
|
|
@@ -35,6 +34,7 @@ def run_duckdb_query(query: str) -> str:
|
|
| 35 |
Runs a read-only SQL query against the connected MotherDuck database and returns the results as a string.
|
| 36 |
The query must be valid DuckDB SQL. This tool only supports SELECT queries.
|
| 37 |
"""
|
|
|
|
| 38 |
try:
|
| 39 |
conn = get_md_connection()
|
| 40 |
|
|
@@ -52,12 +52,14 @@ def run_duckdb_query(query: str) -> str:
|
|
| 52 |
return result_df.to_string(index=False)
|
| 53 |
|
| 54 |
except ConnectionError as e:
|
|
|
|
| 55 |
return f"Connection Error: {e}"
|
| 56 |
except Exception as e:
|
|
|
|
| 57 |
return f"DuckDB Query Error: {e}"
|
| 58 |
finally:
|
| 59 |
-
# Always close the connection
|
| 60 |
-
if
|
| 61 |
conn.close()
|
| 62 |
|
| 63 |
@tool
|
|
@@ -66,15 +68,17 @@ def get_table_schema(table_name: str = "positions") -> str:
|
|
| 66 |
Returns the schema (column names and data types) for the specified table in the MotherDuck database.
|
| 67 |
Defaults to the 'positions' table.
|
| 68 |
"""
|
|
|
|
| 69 |
try:
|
| 70 |
conn = get_md_connection()
|
| 71 |
|
| 72 |
# Use PRAGMA table_info to get the schema details dynamically
|
|
|
|
| 73 |
query = f"PRAGMA table_info('{table_name}')"
|
| 74 |
schema_df = conn.execute(query).fetchdf()
|
| 75 |
|
| 76 |
if schema_df.empty:
|
| 77 |
-
return f"Error: Table '{table_name}' not found in the MotherDuck database."
|
| 78 |
|
| 79 |
# Format the schema into a simple string: name TYPE, name TYPE, ...
|
| 80 |
schema_parts = [f"{row['name']} {row['type']}" for index, row in schema_df.iterrows()]
|
|
@@ -85,6 +89,5 @@ def get_table_schema(table_name: str = "positions") -> str:
|
|
| 85 |
except Exception as e:
|
| 86 |
return f"DuckDB Schema Error: {e}"
|
| 87 |
finally:
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
conn.close()
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
# 2. Connect to the MotherDuck service
|
| 25 |
+
# Note: If you have a specific database name, you can adjust the connection string here.
|
|
|
|
| 26 |
conn = duckdb.connect(f'md:?motherduck_token={token}')
|
| 27 |
return conn
|
| 28 |
|
|
|
|
| 34 |
Runs a read-only SQL query against the connected MotherDuck database and returns the results as a string.
|
| 35 |
The query must be valid DuckDB SQL. This tool only supports SELECT queries.
|
| 36 |
"""
|
| 37 |
+
conn = None
|
| 38 |
try:
|
| 39 |
conn = get_md_connection()
|
| 40 |
|
|
|
|
| 52 |
return result_df.to_string(index=False)
|
| 53 |
|
| 54 |
except ConnectionError as e:
|
| 55 |
+
# Re-raise or handle specific connection errors
|
| 56 |
return f"Connection Error: {e}"
|
| 57 |
except Exception as e:
|
| 58 |
+
# Catch all other DuckDB or SQL execution errors
|
| 59 |
return f"DuckDB Query Error: {e}"
|
| 60 |
finally:
|
| 61 |
+
# Always close the connection if it was successfully opened
|
| 62 |
+
if conn:
|
| 63 |
conn.close()
|
| 64 |
|
| 65 |
@tool
|
|
|
|
| 68 |
Returns the schema (column names and data types) for the specified table in the MotherDuck database.
|
| 69 |
Defaults to the 'positions' table.
|
| 70 |
"""
|
| 71 |
+
conn = None
|
| 72 |
try:
|
| 73 |
conn = get_md_connection()
|
| 74 |
|
| 75 |
# Use PRAGMA table_info to get the schema details dynamically
|
| 76 |
+
# This is a standard DuckDB/SQLite way to get table schema
|
| 77 |
query = f"PRAGMA table_info('{table_name}')"
|
| 78 |
schema_df = conn.execute(query).fetchdf()
|
| 79 |
|
| 80 |
if schema_df.empty:
|
| 81 |
+
return f"Error: Table '{table_name}' not found in the MotherDuck database. Available tables: {conn.execute('SHOW TABLES;').fetchnames()}"
|
| 82 |
|
| 83 |
# Format the schema into a simple string: name TYPE, name TYPE, ...
|
| 84 |
schema_parts = [f"{row['name']} {row['type']}" for index, row in schema_df.iterrows()]
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
return f"DuckDB Schema Error: {e}"
|
| 91 |
finally:
|
| 92 |
+
if conn:
|
| 93 |
+
conn.close()
|
|
|