-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
84 lines (70 loc) · 2.43 KB
/
Copy pathdatabase.py
File metadata and controls
84 lines (70 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
database.py — PostgreSQL connection and safe query execution
"""
import os
import psycopg
import time
import logging
from typing import Optional
logger = logging.getLogger(__name__)
# ── Connection config — read from environment variables with sane defaults ──
# Update these environment variables instead of hardcoding credentials:
# DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD
DB_CONFIG = {
"host": os.getenv("DB_HOST", "localhost"),
"port": int(os.getenv("DB_PORT", "5432")),
"dbname": os.getenv("DB_NAME", "classicmodels"),
"user": os.getenv("DB_USER", "postgres"),
"password": os.getenv("DB_PASSWORD", "password"),
}
def get_connection():
"""Return a live psycopg (psycopg3) connection."""
# psycopg.connect returns a connection usable as a context manager
return psycopg.connect(**DB_CONFIG)
def execute_query(sql: str, timeout_ms: int = 10_000) -> dict:
"""
Execute a single SQL query safely.
Returns:
{
"columns": [...],
"rows": [...],
"row_count": int,
"execution_time_ms": float,
"error": None | str
}
"""
start = time.perf_counter()
result = {
"columns": [],
"rows": [],
"row_count": 0,
"execution_time_ms": 0.0,
"error": None,
}
try:
with get_connection() as conn:
# Set statement timeout so runaway queries don't hang
with conn.cursor() as cur:
cur.execute(f"SET statement_timeout = {timeout_ms};")
# Use dict row factory to get rows as mappings
with conn.cursor(row_factory=psycopg.rows.dict_row) as cur:
cur.execute(sql)
rows = cur.fetchall()
result["columns"] = list(rows[0].keys()) if rows else []
result["rows"] = [dict(r) for r in rows]
result["row_count"] = len(rows)
except Exception as e:
result["error"] = str(e)
logger.error(f"Query execution error: {e}\nSQL: {sql}")
result["execution_time_ms"] = round((time.perf_counter() - start) * 1000, 2)
return result
def test_connection() -> bool:
"""Ping the database."""
try:
with get_connection() as conn:
with conn.cursor() as cur:
cur.execute("SELECT 1;")
return True
except Exception as e:
logger.error(f"DB connection failed: {e}")
return False