Spaces:
Sleeping
Sleeping
fix
Browse files- backend/env/database.py +12 -1
backend/env/database.py
CHANGED
|
@@ -476,6 +476,17 @@ def _execute_sqlite(sql: str) -> tuple[list[dict], str | None]:
|
|
| 476 |
conn.close()
|
| 477 |
|
| 478 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
def _execute_postgres(sql: str) -> tuple[list[dict], str | None]:
|
| 480 |
import psycopg2 # type: ignore[import]
|
| 481 |
import psycopg2.extras # type: ignore[import]
|
|
@@ -484,7 +495,7 @@ def _execute_postgres(sql: str) -> tuple[list[dict], str | None]:
|
|
| 484 |
cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
|
| 485 |
cur.execute(sql)
|
| 486 |
if cur.description is not None:
|
| 487 |
-
rows = [dict(row) for row in cur.fetchall()]
|
| 488 |
else:
|
| 489 |
rows = []
|
| 490 |
conn.commit()
|
|
|
|
| 476 |
conn.close()
|
| 477 |
|
| 478 |
|
| 479 |
+
def _pg_safe(v: object) -> object:
|
| 480 |
+
"""Convert PostgreSQL-specific types to JSON-serializable equivalents."""
|
| 481 |
+
from decimal import Decimal
|
| 482 |
+
import datetime
|
| 483 |
+
if isinstance(v, Decimal):
|
| 484 |
+
return float(v)
|
| 485 |
+
if isinstance(v, (datetime.date, datetime.datetime, datetime.time)):
|
| 486 |
+
return v.isoformat()
|
| 487 |
+
return v
|
| 488 |
+
|
| 489 |
+
|
| 490 |
def _execute_postgres(sql: str) -> tuple[list[dict], str | None]:
|
| 491 |
import psycopg2 # type: ignore[import]
|
| 492 |
import psycopg2.extras # type: ignore[import]
|
|
|
|
| 495 |
cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
|
| 496 |
cur.execute(sql)
|
| 497 |
if cur.description is not None:
|
| 498 |
+
rows = [{k: _pg_safe(v) for k, v in dict(row).items()} for row in cur.fetchall()]
|
| 499 |
else:
|
| 500 |
rows = []
|
| 501 |
conn.commit()
|