-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathutils.py
More file actions
46 lines (35 loc) · 1.29 KB
/
utils.py
File metadata and controls
46 lines (35 loc) · 1.29 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
from dataclasses import dataclass
from sqlalchemy import event
from app import db
@dataclass
class QueryInfo:
statement: str
parameters: tuple | dict | None
bind_key: str | None
class QueryRecorder:
def __init__(self):
self.queries: list[QueryInfo] = []
self._listeners = []
def __enter__(self):
# Register listeners for all engines to capture bind_key
for bind_key, engine in db.engines.items():
listener = self._listener(bind_key)
event.listen(engine, "before_cursor_execute", listener)
self._listeners.append((engine, listener))
return self
def __exit__(self, exc_type, exc_val, exc_tb):
# Remove all listeners
for engine, listener in self._listeners:
event.remove(engine, "before_cursor_execute", listener)
self._listeners.clear()
def _listener(self, bind_key):
"""Create a listener function that captures the bind_key in its closure."""
def listener(conn, cursor, statement, parameters, context, executemany):
self.queries.append(
QueryInfo(
statement=statement,
parameters=parameters,
bind_key=bind_key,
)
)
return listener