/
db.py
401 lines (316 loc) · 11.1 KB
/
db.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# vim: set et ts=4 sw=4 fdm=marker
"""
MIT License
Copyright (c) 2016 Jesse Hogan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
# TODO Add Tests
from configfile import configfile
from entities import *
from MySQLdb.constants.ER import BAD_TABLE_ERROR
from pdb import set_trace; B=set_trace
from table import table
import MySQLdb
import uuid
class dbentities(entities):
def __init__(self, ress=None):
super().__init__()
self._isdirty = False
if ress:
for res in ress:
self += self.dbentity(res)
# The collection may have been added to above. If that is the case, the
# _isdirty flag will be set to True in the _self_onadd event handler.
# Set it back to False since we are just __init__'ing the object; it
# shouldn't be dirty at this point.
self._isdirty = False
def TRUNCATE(self):
conn = connections.getinstance().default
conn.query('truncate ' + self._table)
def ALL(self):
conn = connections.getinstance().default
ress = conn.query('select * from ' + self._table)
return type(self)(ress)
def _self_onadd(self, src, eargs):
super()._self_onadd(src, eargs)
self._isdirty = True
def _self_onremove(self, src, eargs):
super()._self_onremove(src, eargs)
self._isdirty = True
@property
def isdirty(self):
return self._isdirty or any([x.isdirty for x in self])
@property
def isnew(self):
return any([x.isnew for x in self])
@property
def _table(self):
msg = '_table must be overridden'
raise NotImplementedError(msg)
@property
def _create(self):
raise NotImplementedError('_create must be overridden')
@property
def connection(self):
return connections.getinstance().default
def query(self, sql, args=None, cur=None):
return self.connection.query(sql, args, cur=None)
def CREATE(self):
self.query(self._create)
def DROP(self):
self.query('drop table ' + self._table)
def RECREATE(self):
try:
self.DROP()
except MySQLdb.OperationalError as ex:
try:
errno = ex.args[0]
except:
raise
if errno != BAD_TABLE_ERROR: # 1051
raise
self.CREATE()
def save(self, cur=None):
# TODO Add reconnect logic or a connection pool
# Clone the default connection. Since transactions are commited
# by calling commit() on the connection object, we want a new
# connection to ensure this transaction is atomic. I'm not sure
# why MySQLDb uses the connection object for commits. It seems
# like there should be a transaction object that commit() could
# be called on. Either way, it seems like for now at least, we
# will have to connect to the database each time there is a need
# for atomic commits.
# https://stackoverflow.com/questions/50206523/can-you-perform-multiple-atomic-commits-using-the-same-connection-with-mysqldb
# If a cursor was not given, we will manage the atomicity of the
# transaction here. Otherwise, we will assuming the calling code is
# managing the atomicity.
tx = not bool(cur)
if not cur:
conn = connections.getinstance().default.clone()
cur = conn.createcursor()
states = []
try:
for e in self:
states.append((e._isnew, e._isdirty))
e.save(cur)
except Exception:
for i, st in enumerate(states):
e = self[i]
e._isnew, e._isdirty = st
if tx:
conn.rollback()
raise
else:
if tx:
conn.commit()
finally:
if tx:
cur.close()
conn.close()
def __getitem__(self, key):
if type(key) == int or type(key) == slice:
return self._ls[key]
for e in self._ls:
if hasattr(e, 'id') and type(key) == uuid.UUID :
if e.id == key: return e
elif hasattr(e, 'name'):
if e.name == key: return e
def _tostr(self, fn=str, includeHeader=True, props=None):
import functools
def getattr(obj, attr, *args):
def rgetattr(obj, attr):
if obj:
return builtins.getattr(obj, attr, *args)
return None
return functools.reduce(rgetattr, [obj] + attr.split('.'))
tbl = table()
if props:
props = ('ix', 'id') + tuple(props)
else:
props = ('ix', 'id')
if includeHeader:
r = tbl.newrow()
for prop in props:
r.newfield(prop)
for p in self:
r = tbl.newrow()
for prop in props:
if prop == 'ix':
v = self.getindex(p)
else:
v = getattr(p, prop)
v = v if v else ''
r.newfield(v)
return str(tbl)
def __str__(self):
return self._tostr()
class dbentity(entity):
# TODO Add Tests
def __init__(self, id=None):
super().__init__()
self._deleteme = False
self.onaftervaluechange += self._self_onaftervaluechange
def _marknew(self):
self._isnew = True
self._isdirty = False
def _markdirty(self):
self._isdirty = True
def _markold(self):
self._isnew = False
self._isdirty = False
def markfordeletion(self):
self._deleteme = True
def _self_onaftervaluechange(self, src, eargs):
if not self.isnew:
self._markdirty()
@property
def isnew(self):
return self._isnew
@property
def isdirty(self):
return self._isdirty
@property
def id(self):
return self._id
@property
def _table(self):
return self._collection()._table
@property
def connection(self):
return connections.getinstance().default
def query(self, sql, args, cur=None):
return self.connection.query(sql, args, cur)
def save(self, cur=None):
if not (self.isnew or self.isdirty or self._deleteme):
return
if self._deleteme:
if self._isnew:
raise Exception("Can't delete row that doesn't exist.")
else:
self._delete()
if self.isvalid:
if self.isnew:
self._insert(cur)
elif self.isdirty:
self._update()
self._isdirty = False
self._isnew = False
else:
raise brokenruleserror('Won\'t save invalid object', self)
def delete(self):
sql = 'delete from ' + self._table + ' where id = %s'
args = (self.id.bytes,)
ress = self.query(sql, args)
self._marknew()
return ress.rowcount
class connections(entities):
_instance = None
def __init__(self):
super().__init__()
cfg = configfile.getinstance()
accts = cfg.accounts.mysqlaccounts
for acct in accts:
self += connection(acct)
@classmethod
def getinstance(cls):
if cls._instance == None:
cls._instance = connections()
return cls._instance
@property
def default(self):
return self.first
class connection(entity):
def __init__(self, acct):
self._account = acct
self._conn = None
def clone(self):
return type(self)(self._account)
@property
def account(self):
return self._account
@property
def _connection(self):
if not self._conn:
acct = self.account
self._conn = MySQLdb.connect(acct.host, acct.username,
acct.password, acct.database,
port=acct.port)
return self._conn
def close(self):
return self._connection.close()
def commit(self):
return self._connection.commit()
def rollback(self):
return self._connection.rollback()
def createcursor(self):
return self._connection.cursor()
def _reconnect(self):
self._conn = None # force a reconnect
self._connection
def query(self, sql, args=None, cur=None):
if cur != None:
cur.execute(sql, args)
return dbresultset(cur)
for _ in range(2):
try:
conn = self._connection
cur = conn.cursor()
cur.execute(sql, args)
conn.commit()
return dbresultset(cur)
except MySQLdb.OperationalError as ex:
# Reconnect if the connection object has timed out and no
# longer holds a connection to the database.
# https://stackoverflow.com/questions/3335342/how-to-check-if-a-mysql-connection-is-closed-in-python
try:
errno = ex.args[0]
except:
errno = ''
isopen = conn.open
if errno == 2006 or not isopen:
msg = 'Reconnect[{0}]: errno: {1}; isopen: {2}'
msg = msg.format(_, errno, isopen)
self.log.debug('Reconnect ' + str(_))
self._reconnect()
else:
raise
class dbresultset(entities):
def __init__(self, cur):
super().__init__()
self._cur = cur
for r in self._cur:
self += dbresult(r)
@property
def lastrowid(self):
return self._cur.lastrowid
@property
def rowcount(self):
return self._cur.rowcount
def demandhasone(self):
if not self.hasone:
raise RecordNotFoundError('A single record was not found')
return self.first
class dbresult(entity):
def __init__(self, row):
self._row = row
def __iter__(self):
for f in self._row:
yield f
def __getitem__(self, i):
return self._row[0]
class RecordNotFoundError(Exception):
pass