|
9 | 9 | from fastapi import APIRouter, Depends, HTTPException, status, Response, Request |
10 | 10 | from sqlalchemy import select, delete, func, text, desc, and_ |
11 | 11 | from sqlalchemy.ext.asyncio import AsyncSession |
| 12 | +import sqlparse |
12 | 13 |
|
13 | 14 | from ..database import get_database |
14 | 15 | from ..admin_auth import ( |
@@ -1084,8 +1085,23 @@ async def execute_query( |
1084 | 1085 | admin_username = admin_session.github_username |
1085 | 1086 |
|
1086 | 1087 | try: |
1087 | | - # Execute the query |
1088 | | - result = await db.execute(text(query)) |
| 1088 | + # Split statements using sqlparse and execute each separately |
| 1089 | + statements = [stmt.strip() for stmt in sqlparse.split(query) if stmt.strip()] |
| 1090 | + |
| 1091 | + # Execute all statements |
| 1092 | + total_affected_rows = 0 |
| 1093 | + for stmt in statements: |
| 1094 | + stmt_result = await db.execute(text(stmt)) |
| 1095 | + total_affected_rows += stmt_result.rowcount |
| 1096 | + await db.commit() |
| 1097 | + |
| 1098 | + # Create simple result object |
| 1099 | + class MultiStatementResult: |
| 1100 | + rowcount = total_affected_rows |
| 1101 | + def fetchall(self): return [] |
| 1102 | + def keys(self): return [] |
| 1103 | + |
| 1104 | + result = MultiStatementResult() |
1089 | 1105 |
|
1090 | 1106 | if query_upper.startswith("SELECT") or query_upper.startswith("WITH"): |
1091 | 1107 | # For SELECT queries, fetch all results |
|
0 commit comments