Fix: Only fetchall() when query has RETURNING clause or is SELECT

This commit is contained in:
Christian 2025-12-19 16:30:03 +01:00
parent 3f66bd07e6
commit c8e005dd07

View File

@ -64,12 +64,18 @@ def execute_query(query: str, params: tuple = None, fetch: bool = True):
# Auto-detect write operations and commit
query_upper = query.strip().upper()
if query_upper.startswith(('INSERT', 'UPDATE', 'DELETE')):
is_write = query_upper.startswith(('INSERT', 'UPDATE', 'DELETE'))
if is_write:
conn.commit()
if fetch:
# Only fetch if there are results to fetch
# (SELECT queries or INSERT/UPDATE/DELETE with RETURNING clause)
if fetch and (not is_write or 'RETURNING' in query_upper):
return cursor.fetchall()
return cursor.rowcount
elif is_write:
return cursor.rowcount
return []
except Exception as e:
conn.rollback()
logger.error(f"Query error: {e}")