Repository navigation
Expand file tree
/
Copy pathpsycopg_client.py
More file actions
70 lines (52 loc) · 1.76 KB
/
Copy pathpsycopg_client.py
File metadata and controls
70 lines (52 loc) · 1.76 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
import psycopg
import typing
from psycopg.rows import dict_row
class PsycoPGClient(object):
def __init__(self, connection_string :str):
self._connection_string :str = connection_string
self._connection = psycopg.connect(
connection_string,
autocommit = True,
row_factory=dict_row
)
def _exec_query(self, query :str) -> typing.List[typing.Dict]:
rv = []
try:
with self._connection.execute(query) as r:
for row in r:
rv.append(row)
return rv
except Exception as err:
return []
def _exec_query_strict(self, query :str) -> typing.List[typing.Dict]:
rv = []
try:
with self._connection.execute(query) as r:
for row in r:
rv.append(row)
return rv
except Exception as err:
print(err)
raise err
def _run_queries(self, queries :typing.List[str]) -> typing.List[typing.Dict]:
ret_val = []
for q in queries:
nv = self._exec_query(q)
if nv:
ret_val += nv
return ret_val
def _run_queries_strict(self, queries :typing.List[str]) -> typing.List[typing.Dict]:
ret_val = []
for q in queries:
nv = self._exec_query_strict(q)
if nv:
ret_val += nv
return ret_val
def run_queries(self, queries :typing.List[str]) -> typing.List[typing.Dict]:
return self._run_queries(queries)
def run_queries_strict(self, queries :typing.List[str]) -> typing.List[typing.Dict]:
return self._run_queries_strict(queries)
def exec_bound_query(self, query :str, params :tuple) -> typing.List[typing.Dict]:
"""Execute with server side binding; psycopg (v3) sends Parse/Bind/Execute when params are supplied."""
with self._connection.execute(query, params) as r:
return [row for row in r]