-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathconnection.py
60 lines (48 loc) · 1.83 KB
/
connection.py
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
from psycopg.rows import dict_row
from uuid import uuid4
def exec_msar_func(conn, func_name, *args):
"""
Execute an msar function using a psycopg (3) connection.
Args:
conn: a psycopg connection or cursor
func_name: The unqualified msar_function name (danger; not sanitized)
*args: The list of parameters to pass
"""
# Returns a cursor
return conn.execute(
f"SELECT msar.{func_name}({','.join(['%s'] * len(args))})", args
)
def exec_msar_func_server_cursor(conn, func_name, *args):
"""
Execute an msar function using a psycopg (3) connection and a server cursor.
Args:
conn: a psycopg connection or cursor
func_name: The unqualified msar_function name (danger; not sanitized)
*args: The list of parameters to pass
Note:
The server cursor must be properly closed during usage.
Use the pattern:
with connection.exec_msar_func_server_cursor(...) as cursor:
...
since the with statement automatically closes the cursor.
"""
server_cursor = conn.cursor(name=str(uuid4()))
return server_cursor.execute(
f"SELECT msar.{func_name}({','.join(['%s'] * len(args))})", args
)
def select_from_msar_func(conn, func_name, *args):
"""
Select all records from an msar function using a psycopg (3) connection.
Args:
conn: a psycopg connection
func_name: The unqualified msar_function name (danger; not sanitized)
*args: The list of parameters to pass
"""
cursor = conn.execute(
f"SELECT * FROM msar.{func_name}({','.join(['%s'] * len(args))})", args
)
cursor.row_factory = dict_row
return cursor.fetchall()
def load_file_with_conn(conn, file_handle):
"""Run an SQL script from a file, using psycopg."""
conn.execute(file_handle.read())