-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
285 lines (212 loc) · 7.17 KB
/
database.py
File metadata and controls
285 lines (212 loc) · 7.17 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
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
from dataclasses import dataclass
import cx_Oracle
import psycopg2
import psycopg2.extras
from abstracts.database import IDatabase
@dataclass
class ConnectConfig:
host:str=""
port:int=5673
dbname:str=""
username:str=""
password:str=""
encode:str="utf-8"
tns:str=""
schema:str=""
class OracleDatabase(IDatabase):
def __init__(self, conn_config=None):
self.__conn_config = conn_config
self.__conn = None
self.__cursor = None
def open_conn(self):
try:
oralce_tns = self.__conn_config.tns
if not oralce_tns:
oralce_tns = cx_Oracle.makedsn(
self.__conn_config.host,
self.__conn_config.port,
self.__conn_config.dbname
)
self.__conn = cx_Oracle.connect(
self.__conn_config.username,
self.__conn_config.password,
oralce_tns,
encoding = self.__conn_config.encoding
)
except Exception as e:
raise e
def close_conn(self):
try:
if self.is_opened():
self.__conn.close()
except Exception as e:
raise e
def is_opened(self) -> bool:
try:
if self.__conn:
return self.__conn.ping() is None
return True
except cx_Oracle.InterfaceError:
return False
except Exception as e:
raise e
def __is_cursor_opened(self):
try:
if self.__cursor is not None:
return True
return False
except Exception as e:
return None
def query(self, sql="", dict_format=True):
try:
self.open_conn()
with self.__conn.cursor() as cur:
result = cur.execute(sql)
if dict_format:
result.rowfactory = lambda *args: dict(zip([d[0] for d in result.description], args))
return result.fetchall()
except Exception as e:
raise e
finally:
self.close_conn()
def execute(self, sql="", auto_commit=True):
try:
if auto_commit:
self.open_conn()
with self.__conn.cursor() as cur:
cur.execute(sql)
self.commit()
self.close_conn()
else:
if not self.is_opened():
self.open_conn()
if not self.__is_cursor_opended():
self.__cursor = self.__conn.cursor()
self.__cursor.execute(sql)
except Exception as e:
self.rollback()
raise e
def execute_many(self, sql="", data=[], auto_commit=True):
try:
errors = None
if auto_commit:
self.open_conn()
with self.__conn.cursor() as cur:
cur.executemany(sql, data, batcherrors=True, arraydmlrowcounts=True)
self.commit()
errors = cur.getbatcherrors()
self.close_conn()
else:
if not self.is_opened():
self.open_conn()
if not self.__is_cursor_opended():
self.__cursor = self.__conn.cursor()
self.__cursor.executemany(sql, data, batcherrors=True, arraydmlrowcounts=True)
errors = self.__cursor.getbatcherrors()
# if errors:
# for error in errors:
# print("number of errors whick took place:", len(errors))
# print("Error", error.message.rstrip(), "at row offset", error.offset)
except Exception as e:
self.rollback()
raise e
def commit(self):
try:
if self.__conn and self.is_opened():
self.__conn.commit()
if self.__is_cursor_opened():
self.__cursor.close()
self.__cursor = None
except Exception as e:
raise e
def rollback(self):
try:
if self.__conn and self.is_opened():
self.__conn.rollback()
if self.__is_cursor_opened():
self.__cursor.close()
self.__cursor = None
except Exception as e:
raise e
class PostgresDatabase(IDatabase):
def __init__(self, conn_config=None):
self.__conn_config = conn_config
self.__conn = None
self.__cursor = None
def open_conn(self):
try:
if not self.is_opened():
self.__conn = psycopg2.connect(
host=self.__conn_config.host,
user=self.__conn_config.username,
password=self.__conn_config.password,
port=self.__conn_config.port,
dbname=self.__conn_config.dbname,
options=f"-c search_path={self.__conn_config.schema}"
)
except Exception as e:
raise e
def close_conn(self):
try:
if self.is_opened():
self.__conn.close()
except Exception as e:
raise e
def is_opened(self) -> bool:
try:
if self.__conn is not None and self.__conn.closed == 0:
return True
return False
except Exception as e:
raise e
def query(self, sql="", dict_format=True):
try:
self.open_conn()
cursor_type = psycopg2.extras.RealDictCursor if dict_format else None
with self.__conn.cursor(cursor_factory=cursor_type) as cur:
cur.execute(sql)
result = cur.fetchall()
return result
except Exception as e:
raise e
finally:
self.close_conn()
def execute(self, sql="", auto_commit=True):
try:
self.open_conn()
if auto_commit:
with self.__conn.cursor() as cur:
cur.execute(sql)
self.commit()
self.close_conn()
else:
if self.__cursor is None or self.__cursor.closed:
self.__cursor = self.__conn.cursor()
self.__cursor.execute(sql)
except Exception as e:
raise e
def execute_many(self, sql="", data=[], auto_commit=True):
try:
pass
except Exception as e:
raise e
def commit(self):
try:
if self.__conn and self.is_opened():
self.__conn.commit()
if self.__cursor and not self.__cursor.closed:
self.__cursor.close()
except Exception as e:
raise e
finally:
self.close_conn()
def rollback(self):
try:
if self.__conn and self.is_opened():
self.__conn.rollback()
if self.__cursor and not self.__cursor.closed:
self.__cursor.close()
except Exception as e:
raise e
finally:
self.close_conn()