Repository navigation
Expand file tree
/
Copy pathdb.py
More file actions
117 lines (100 loc) · 4.07 KB
/
Copy pathdb.py
File metadata and controls
117 lines (100 loc) · 4.07 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import asyncio
import json
import logging
from typing import Any, Optional
import asyncpg
from config import XATA_DATABASE_URL
logger = logging.getLogger(__name__)
def json_decoder(value):
"""Safely decodes json/jsonb columns into native Python dictionaries."""
if value is None:
return None
return json.loads(value)
class Database:
def __init__(self):
self.pool: Optional[asyncpg.Pool] = None
self.poolInit = False
self.timeout = 10
async def no_op_reset(self, con):
"""A no-operation function to override asyncpg's default reset."""
pass
async def init_connection(self, con):
"""Sets statement timeout and registers JSON/JSONB type codecs."""
await con.execute("SET statement_timeout = '15s'")
# Register JSON and JSONB codecs globally on connection initialization
await con.set_type_codec(
'json',
encoder=json.dumps,
decoder=json_decoder,
schema='pg_catalog'
)
await con.set_type_codec(
'jsonb',
encoder=json.dumps,
decoder=json_decoder,
schema='pg_catalog'
)
async def create_pool(self):
self.poolInit = True
self.pool = await asyncpg.create_pool(
dsn=XATA_DATABASE_URL,
reset=self.no_op_reset,
command_timeout=60,
timeout=30,
init=self.init_connection,
)
logger.debug("Database pool created successfully.")
async def close_pool(self):
if self.pool:
await self.pool.close()
logger.debug("Database pool closed.")
self.pool = None
async def execute(self, query: str, *args, timeout: Optional[float] = None) -> Any:
"""
Executes a query with the provided arguments and returns the result.
"""
if self.pool and self.poolInit:
use_timeout = timeout if timeout is not None else self.timeout
async with self.pool.acquire() as con:
return await con.execute(query, *args, timeout=use_timeout)
else:
raise ConnectionError("Database pool not initialized.")
async def fetch_one(self, query: str, *args, timeout: Optional[float] = None) -> Optional[asyncpg.Record]:
"""
Executes a query and returns a single record, or None if no record is found.
"""
if self.pool and self.poolInit:
use_timeout = timeout if timeout is not None else self.timeout
async with self.pool.acquire() as con:
return await con.fetchrow(query, *args, timeout=use_timeout)
else:
raise ConnectionError("Database pool not initialized.")
async def fetch_rows(self, query: str, *args, timeout: Optional[float] = None) -> list[asyncpg.Record]:
"""
Executes a query and returns a list of records.
"""
if self.pool and self.poolInit:
use_timeout = timeout if timeout is not None else self.timeout
async with self.pool.acquire() as con:
return await con.fetch(query, *args, timeout=use_timeout)
else:
raise ConnectionError("Database pool not initialized.")
async def executemany(self, query: str, args: list[list[Any]], timeout: Optional[float] = None) -> None:
"""
Executes a query with multiple sets of arguments.
"""
if self.pool and self.poolInit:
use_timeout = timeout if timeout is not None else self.timeout
async with self.pool.acquire() as con:
await con.executemany(query, args, timeout=use_timeout)
else:
raise ConnectionError("Database pool not initialized.")
async def transaction(self):
"""
Returns the transaction context manager of the raw asyncpg connection.
"""
if self.pool and self.poolInit:
async with self.pool.acquire() as con:
return con.transaction()
else:
raise ConnectionError("Database pool not initialized.")