-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
74 lines (62 loc) · 2.15 KB
/
database.py
File metadata and controls
74 lines (62 loc) · 2.15 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
"""
Utility functions for database interactions (connection setup, reusable queries).
Connection management (get_db_connection).
Low-level database operations (execute_query).
"""
from typing import Generator
from contextlib import contextmanager
import psycopg
from psycopg.connection import Connection
from config import POSTGRES_HOST, POSTGRES_DB, POSTGRES_USER, POSTGRES_PASSWORD
from utils.logging import configure_logging
logger = configure_logging(__name__)
@contextmanager
def get_db_connection() -> Generator[Connection, None, None]:
"""
Establish a connection to the Postgres database and ensure cleanup.
Yields:
Connection: A psycopg Connection object.
"""
connection = None
try:
connection = psycopg.connect(
host=POSTGRES_HOST,
dbname=POSTGRES_DB,
user=POSTGRES_USER,
password=POSTGRES_PASSWORD,
)
yield connection
except Exception as e:
logger.error("Error establishing database connection: %s", e)
raise
finally:
if connection and not connection.closed:
connection.close()
logger.debug("Database connection closed.")
def build_insert_query(table_name, columns, values):
"""
Build a parameterized SQL INSERT query.
Parameters:
- table_name (str): Name of the target database table.
- columns (list): List of column names to insert values into.
- values (list): List of corresponding values to insert.
Returns:
- tuple: (query, values) where `query` is the constructed SQL string
and `values` is the list of values for parameterized execution.
"""
query = f"""
INSERT INTO {table_name} ({', '.join(columns)})
VALUES ({', '.join(['%s'] * len(values))})
"""
return query, values
def execute_query(cursor, query, values):
"""
Execute a SQL query with the given values.
"""
try:
resolved_query = query % tuple(values)
logger.debug("Executing query: %s", resolved_query)
cursor.execute(query, values)
except psycopg.errors.DatabaseError as e:
logger.error("Database error: %s", e)
raise