-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools_pgdao.py
320 lines (246 loc) · 8.3 KB
/
tools_pgdao.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""
This file is part of Giswater 3
The program is free software: you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
"""
# -*- coding: utf-8 -*-
import psycopg2
import psycopg2.extras
class GwPgDao(object):
def __init__(self):
self.last_error = None
self.set_search_path = None
self.conn = None
self.cursor = None
self.pid = None
def init_db(self):
""" Initializes database connection """
try:
self.conn = psycopg2.connect(self.conn_string)
self.cursor = self.get_cursor()
self.pid = self.conn.get_backend_pid()
status = True
except psycopg2.DatabaseError as e:
self.last_error = e
status = False
return status
def close_db(self):
""" Close database connection """
try:
status = True
if self.cursor:
self.cursor.close()
if self.conn:
self.conn.close()
del self.cursor
del self.conn
except Exception as e:
self.last_error = e
status = False
return status
def get_cursor(self, aux_conn=None):
if aux_conn:
cursor = aux_conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
else:
cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
return cursor
def reset_db(self):
""" Reset database connection """
if self.init_db():
if self.set_search_path:
self.execute_sql(self.set_search_path)
def check_cursor(self):
""" Check if cursor is closed """
status = True
if self.cursor.closed:
self.reset_db()
status = not self.cursor.closed
return status
def cursor_execute(self, sql):
""" Check if cursor is closed before execution """
if self.check_cursor():
self.cursor.execute(sql)
def get_poll(self):
""" Check if the connection is established """
status = True
try:
if self.check_cursor():
self.conn.poll()
except psycopg2.InterfaceError:
self.reset_db()
status = False
except psycopg2.OperationalError:
self.reset_db()
status = False
finally:
return status
def set_params(self, host, port, dbname, user, password, sslmode):
""" Set database parameters """
self.host = host
self.port = port
self.dbname = dbname
self.user = user
self.password = password
self.conn_string = f"host={self.host} port={self.port} dbname={self.dbname} user='{self.user}'"
if sslmode:
self.conn_string += f" sslmode={sslmode}"
if self.password is not None:
self.conn_string += f" password={self.password}"
def set_conn_string(self, conn_string):
""" Set connection string """
self.conn_string = conn_string
def set_service(self, service, sslmode=None):
""" Set service """
self.conn_string = f"service={service}"
if sslmode:
self.conn_string += f" sslmode={sslmode}"
def mogrify(self, sql, params):
""" Return a query string after arguments binding """
query = sql
try:
cursor = self.get_cursor()
query = cursor.mogrify(sql, params)
except Exception as e:
self.last_error = e
finally:
return query
def get_rows(self, sql, commit=False, aux_conn=None):
""" Get multiple rows from selected query """
self.last_error = None
rows = None
try:
cursor = self.get_cursor(aux_conn)
cursor.execute(sql)
rows = cursor.fetchall()
if commit:
self.commit(aux_conn)
except Exception as e:
self.last_error = e
if commit:
self.rollback(aux_conn)
finally:
return rows
def get_row(self, sql, commit=False, aux_conn=None):
""" Get single row from selected query """
self.last_error = None
row = None
try:
if aux_conn is not None:
cursor = self.get_cursor(aux_conn)
cursor.execute(sql)
row = cursor.fetchone()
else:
self.cursor_execute(sql)
row = self.cursor.fetchone()
if commit:
self.commit(aux_conn)
except Exception as e:
self.last_error = e
if commit:
self.rollback(aux_conn)
finally:
return row
def execute_sql(self, sql, commit=True, aux_conn=None):
""" Execute selected query """
self.last_error = None
status = True
try:
cursor = self.get_cursor(aux_conn)
cursor.execute(sql)
if commit:
self.commit(aux_conn)
except Exception as e:
self.last_error = e
status = False
if commit:
self.rollback(aux_conn)
finally:
return status
def execute_returning(self, sql, commit=True, aux_conn=None):
""" Execute selected query and return RETURNING field """
self.last_error = None
value = None
try:
cursor = self.get_cursor(aux_conn)
cursor.execute(sql)
value = cursor.fetchone()
if commit:
self.commit(aux_conn)
except Exception as e:
self.last_error = e
self.rollback(aux_conn)
finally:
return value
def commit(self, aux_conn=None):
""" Commit current database transaction """
try:
if aux_conn is not None:
aux_conn.commit()
return
self.conn.commit()
except Exception:
pass
def rollback(self, aux_conn=None):
""" Rollback current database transaction """
try:
if aux_conn is not None:
aux_conn.rollback()
return
self.conn.rollback()
except Exception:
pass
def export_to_csv(self, sql, csv_file):
""" Dumps contents of the query to selected CSV file """
try:
cursor = self.get_cursor()
cursor.export_to_csv(sql, csv_file)
return None
except Exception as e:
return e
def cancel_pid(self, pid):
""" Cancel one process by pid """
# Create an auxiliary connection with the intention of being able to cancel processes of the main connection
last_error = None
try:
aux_conn = psycopg2.connect(self.conn_string)
cursor = self.get_cursor(aux_conn)
cursor.execute(f"SELECT pg_cancel_backend({pid})")
status = True
cursor.close()
aux_conn.close()
del cursor
del aux_conn
except Exception as e:
last_error = e
status = False
return {'status': status, 'last_error': last_error}
def get_aux_conn(self):
try:
aux_conn = psycopg2.connect(self.conn_string)
cursor = self.get_cursor(aux_conn)
if self.set_search_path:
cursor.execute(self.set_search_path)
return aux_conn
except Exception as e:
last_error = e
status = False
return {'status': status, 'last_error': last_error}
def delete_aux_con(self, aux_conn):
try:
aux_conn.close()
del aux_conn
return
except Exception as e:
last_error = e
status = False
return {'status': status, 'last_error': last_error}
def check_connection(self):
""" Check database connection. Reconnect if needed """
was_closed = False
try:
self.cursor.execute("SELECT 1")
except psycopg2.OperationalError as e:
was_closed = True
self.init_db()
return was_closed