MCPcopy Create free account
hub / github.com/dbt-labs/ade-bench / execute_sql

Function execute_sql

shared/scripts/run_sql.py:203–251  ·  view source on GitHub ↗

Execute SQL query against the database.

(conn, sql: str, db_type: str)

Source from the content-addressed store, hash-verified

201
202
203def execute_sql(conn, sql: str, db_type: str) -> None:
204 """Execute SQL query against the database."""
205 cursor = None
206 try:
207 # Split SQL by semicolons to handle multiple statements
208 statements = [s.strip() for s in sql.split(";") if s.strip()]
209
210 if not statements:
211 print("Error: No SQL statements found", file=sys.stderr)
212 sys.exit(1)
213
214 cursor = conn.cursor()
215
216 for statement in statements:
217 if statement:
218 cursor.execute(statement)
219
220 # For SELECT queries, fetch and print results (optional)
221 # For DDL/DML, just execute
222 try:
223 results = cursor.fetchall()
224 if results:
225 # Print results if any
226 for row in results:
227 print(row)
228 except Exception:
229 # Not a SELECT query, that's fine
230 pass
231
232 # Commit if the connection supports it
233 if hasattr(conn, "commit"):
234 conn.commit()
235
236 if cursor:
237 cursor.close()
238
239 except Exception as e:
240 if cursor:
241 try:
242 cursor.close()
243 except Exception:
244 pass
245 print(f"Error executing SQL: {e}", file=sys.stderr)
246 if db_type == "snowflake" and hasattr(conn, "rollback"):
247 try:
248 conn.rollback()
249 except Exception:
250 pass
251 sys.exit(1)
252
253
254def main():

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected