Fix: Only fetchall() when query has RETURNING clause or is SELECT
This commit is contained in:
parent
3f66bd07e6
commit
c8e005dd07
@ -64,12 +64,18 @@ def execute_query(query: str, params: tuple = None, fetch: bool = True):
|
|||||||
|
|
||||||
# Auto-detect write operations and commit
|
# Auto-detect write operations and commit
|
||||||
query_upper = query.strip().upper()
|
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()
|
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.fetchall()
|
||||||
|
elif is_write:
|
||||||
return cursor.rowcount
|
return cursor.rowcount
|
||||||
|
return []
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
logger.error(f"Query error: {e}")
|
logger.error(f"Query error: {e}")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user