-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path__init__.py
More file actions
551 lines (499 loc) · 23.5 KB
/
Copy path__init__.py
File metadata and controls
551 lines (499 loc) · 23.5 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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
from functools import reduce
from sqlobject import dbconnection
from sqlobject import classregistry
from sqlobject import events
from sqlobject import sqlbuilder
from sqlobject.col import StringCol, ForeignKey
from sqlobject.main import sqlmeta, SQLObject, SelectResults, \
makeProperties, unmakeProperties, getterName, setterName
from sqlobject.compat import string_type
from . import iteration
def tablesUsedSet(obj, db):
if hasattr(obj, "tablesUsedSet"):
return obj.tablesUsedSet(db)
elif isinstance(obj, (tuple, list, set, frozenset)):
s = set()
for component in obj:
s.update(tablesUsedSet(component, db))
return s
else:
return set()
class InheritableSelectResults(SelectResults):
IterationClass = iteration.InheritableIteration
def __init__(self, sourceClass, clause, clauseTables=None,
inheritedTables=None, **ops):
if clause is None or isinstance(clause, str) and clause == 'all':
clause = sqlbuilder.SQLTrueClause
dbName = (ops.get('connection', None)
or sourceClass._connection).dbName
tablesSet = tablesUsedSet(clause, dbName)
tablesSet.add(str(sourceClass.sqlmeta.table))
orderBy = ops.get('orderBy')
if inheritedTables:
for tableName in inheritedTables:
tablesSet.add(str(tableName))
if orderBy and not isinstance(orderBy, string_type):
tablesSet.update(tablesUsedSet(orderBy, dbName))
# DSM: if this class has a parent, we need to link it
# DSM: and be sure the parent is in the table list.
# DSM: The following code is before clauseTables
# DSM: because if the user uses clauseTables
# DSM: (and normal string SELECT), he must know what he wants
# DSM: and will do himself the relationship between classes.
if not isinstance(clause, str):
tableRegistry = {}
allClasses = classregistry.registry(
sourceClass.sqlmeta.registry).allClasses()
for registryClass in allClasses:
if str(registryClass.sqlmeta.table) in tablesSet:
# DSM: By default, no parents are needed for the clauses
tableRegistry[registryClass] = registryClass
tableRegistryCopy = tableRegistry.copy()
for childClass in tableRegistryCopy:
if childClass not in tableRegistry:
continue
currentClass = childClass
while currentClass:
if currentClass in tableRegistryCopy:
if currentClass in tableRegistry:
# DSM: Remove this class as it is a parent one
# DSM: of a needed children
del tableRegistry[currentClass]
# DSM: Must keep the last parent needed
# DSM: (to limit the number of join needed)
tableRegistry[childClass] = currentClass
currentClass = currentClass.sqlmeta.parentClass
# DSM: Table registry contains only the last children
# DSM: or standalone classes
parentClause = []
for (currentClass, minParentClass) in tableRegistry.items():
while (currentClass != minParentClass) \
and currentClass.sqlmeta.parentClass:
parentClass = currentClass.sqlmeta.parentClass
parentClause.append(currentClass.q.id == parentClass.q.id)
currentClass = parentClass
tablesSet.add(str(currentClass.sqlmeta.table))
clause = reduce(sqlbuilder.AND, parentClause, clause)
super(InheritableSelectResults, self).__init__(
sourceClass, clause, clauseTables, **ops)
def accumulateMany(self, *attributes, **kw):
if kw.get("skipInherited"):
return super(InheritableSelectResults, self).\
accumulateMany(*attributes)
tables = []
for func_name, attribute in attributes:
if not isinstance(attribute, string_type):
tables.append(attribute.tableName)
clone = self.__class__(self.sourceClass, self.clause,
self.clauseTables, inheritedTables=tables,
**self.ops)
return clone.accumulateMany(skipInherited=True, *attributes)
class InheritableSQLMeta(sqlmeta):
@classmethod
def addColumn(sqlmeta, columnDef, changeSchema=False, connection=None,
childUpdate=False):
soClass = sqlmeta.soClass
# DSM: Try to add parent properties to the current class
# DSM: Only do this once if possible at object creation and once for
# DSM: each new dynamic column to refresh the current class
if sqlmeta.parentClass:
for col in sqlmeta.parentClass.sqlmeta.columnList:
cname = col.name
if cname == 'childName':
continue
if cname.endswith("ID"):
cname = cname[:-2]
setattr(soClass, getterName(cname), eval(
'lambda self: self._parent.%s' % cname))
if not col.immutable:
def make_setfunc(cname):
def setfunc(self, val):
if not self.sqlmeta._creating and \
not getattr(self.sqlmeta,
"row_update_sig_suppress", False):
self.sqlmeta.send(events.RowUpdateSignal, self,
{cname: val})
setattr(self._parent, cname, val)
return setfunc
setfunc = make_setfunc(cname)
setattr(soClass, setterName(cname), setfunc)
if childUpdate:
makeProperties(soClass)
return
if columnDef:
super(InheritableSQLMeta, sqlmeta).addColumn(columnDef,
changeSchema,
connection)
# DSM: Update each child class if needed and existing (only for new
# DSM: dynamic column as no child classes exists at object creation)
if columnDef and hasattr(soClass, "q"):
q = getattr(soClass.q, columnDef.name, None)
else:
q = None
for c in sqlmeta.childClasses.values():
c.sqlmeta.addColumn(columnDef, connection=connection,
childUpdate=True)
if q:
setattr(c.q, columnDef.name, q)
@classmethod
def delColumn(sqlmeta, column, changeSchema=False, connection=None,
childUpdate=False):
if childUpdate:
soClass = sqlmeta.soClass
unmakeProperties(soClass)
makeProperties(soClass)
if isinstance(column, str):
name = column
else:
name = column.name
delattr(soClass, name)
delattr(soClass.q, name)
return
super(InheritableSQLMeta, sqlmeta).delColumn(column, changeSchema,
connection)
# DSM: Update each child class if needed
# DSM: and delete properties for this column
for c in sqlmeta.childClasses.values():
c.sqlmeta.delColumn(column, changeSchema=changeSchema,
connection=connection, childUpdate=True)
@classmethod
def addJoin(sqlmeta, joinDef, childUpdate=False):
soClass = sqlmeta.soClass
# DSM: Try to add parent properties to the current class
# DSM: Only do this once if possible at object creation and once for
# DSM: each new dynamic join to refresh the current class
if sqlmeta.parentClass:
for join in sqlmeta.parentClass.sqlmeta.joins:
jname = join.joinMethodName
jarn = join.addRemoveName
setattr(
soClass, getterName(jname),
eval('lambda self: self._parent.%s' % jname))
if hasattr(join, 'remove'):
setattr(
soClass, 'remove' + jarn,
eval('lambda self,o: self._parent.remove%s(o)' % jarn))
if hasattr(join, 'add'):
setattr(
soClass, 'add' + jarn,
eval('lambda self,o: self._parent.add%s(o)' % jarn))
if childUpdate:
makeProperties(soClass)
return
if joinDef:
super(InheritableSQLMeta, sqlmeta).addJoin(joinDef)
# DSM: Update each child class if needed and existing (only for new
# DSM: dynamic join as no child classes exists at object creation)
for c in sqlmeta.childClasses.values():
c.sqlmeta.addJoin(joinDef, childUpdate=True)
@classmethod
def delJoin(sqlmeta, joinDef, childUpdate=False):
if childUpdate:
soClass = sqlmeta.soClass
unmakeProperties(soClass)
makeProperties(soClass)
return
super(InheritableSQLMeta, sqlmeta).delJoin(joinDef)
# DSM: Update each child class if needed
# DSM: and delete properties for this join
for c in sqlmeta.childClasses.values():
c.sqlmeta.delJoin(joinDef, childUpdate=True)
@classmethod
def getAllColumns(sqlmeta):
columns = sqlmeta.columns.copy()
sm = sqlmeta
while sm.parentClass:
columns.update(sm.parentClass.sqlmeta.columns)
sm = sm.parentClass.sqlmeta
return columns
@classmethod
def getColumns(sqlmeta):
columns = sqlmeta.getAllColumns()
if 'childName' in columns:
del columns['childName']
return columns
class InheritableSQLObject(SQLObject):
sqlmeta = InheritableSQLMeta
_inheritable = True
SelectResultsClass = InheritableSelectResults
def set(self, **kw):
if self._parent:
SQLObject.set(self, _suppress_set_sig=True, **kw)
else:
SQLObject.set(self, **kw)
def __classinit__(cls, new_attrs):
SQLObject.__classinit__(cls, new_attrs)
# if we are a child class, add sqlbuilder fields from parents
currentClass = cls.sqlmeta.parentClass
while currentClass:
for column in currentClass.sqlmeta.columnDefinitions.values():
if column.name == 'childName':
continue
if isinstance(column, ForeignKey):
continue
setattr(cls.q, column.name,
getattr(currentClass.q, column.name))
currentClass = currentClass.sqlmeta.parentClass
@classmethod
def _SO_setupSqlmeta(cls, new_attrs, is_base):
# Note: cannot use super(InheritableSQLObject, cls)._SO_setupSqlmeta -
# InheritableSQLObject is not defined when it's __classinit__
# is run. Cannot use SQLObject._SO_setupSqlmeta, either:
# the method would be bound to wrong class.
if cls.__name__ == "InheritableSQLObject":
call_super = super(cls, cls)
else:
# InheritableSQLObject must be in globals yet
call_super = super(InheritableSQLObject, cls)
call_super._SO_setupSqlmeta(new_attrs, is_base)
sqlmeta = cls.sqlmeta
sqlmeta.childClasses = {}
# locate parent class and register this class in it's children
sqlmeta.parentClass = None
for superclass in cls.__bases__:
if getattr(superclass, '_inheritable', False) \
and (superclass.__name__ != 'InheritableSQLObject'):
if sqlmeta.parentClass:
# already have a parent class;
# cannot inherit from more than one
raise NotImplementedError(
"Multiple inheritance is not implemented")
sqlmeta.parentClass = superclass
superclass.sqlmeta.childClasses[cls.__name__] = cls
if sqlmeta.parentClass:
# remove inherited column definitions
cls.sqlmeta.columns = {}
cls.sqlmeta.columnList = []
cls.sqlmeta.columnDefinitions = {}
# default inheritance child name
if not sqlmeta.childName:
sqlmeta.childName = cls.__name__
@classmethod
def get(cls, id, connection=None, selectResults=None,
childResults=None, childUpdate=False):
val = super(InheritableSQLObject, cls).get(id, connection,
selectResults)
# DSM: If we are updating a child, we should never return a child...
if childUpdate:
return val
# DSM: If this class has a child, return the child
if 'childName' in cls.sqlmeta.columns:
childName = val.childName
if childName is not None:
childClass = cls.sqlmeta.childClasses[childName]
# If the class has no columns (which sometimes makes sense
# and may be true for non-inheritable (leaf) classes only),
# shunt the query to avoid almost meaningless SQL
# like "SELECT NULL FROM child WHERE id=1".
# This is based on assumption that child object exists
# if parent object exists. (If it doesn't your database
# is broken and that is a job for database maintenance.)
if not (childResults or childClass.sqlmeta.columns):
childResults = (None,)
return childClass.get(id, connection=connection,
selectResults=childResults)
# DSM: Now, we know we are alone or the last child in a family...
# DSM: It's time to find our parents
inst = val
while inst.sqlmeta.parentClass and not inst._parent:
inst._parent = inst.sqlmeta.parentClass.get(
id, connection=connection, childUpdate=True)
inst = inst._parent
# DSM: We can now return ourself
return val
@classmethod
def _notifyFinishClassCreation(cls):
sqlmeta = cls.sqlmeta
# verify names of added columns
if sqlmeta.parentClass:
# FIXME: this does not check for grandparent column overrides
parentCols = sqlmeta.parentClass.sqlmeta.columns.keys()
for column in sqlmeta.columnList:
if column.name == 'childName':
raise AttributeError(
"The column name 'childName' is reserved")
if column.name in parentCols:
raise AttributeError(
"The column '%s' is already defined "
"in an inheritable parent" % column.name)
# if this class is inheritable, add column for children distinction
if cls._inheritable and (cls.__name__ != 'InheritableSQLObject'):
sqlmeta.addColumn(
StringCol(name='childName',
# limit string length to get VARCHAR and not CLOB
length=255, default=None))
if not sqlmeta.columnList:
# There are no columns - call addColumn to propagate columns
# from parent classes to children
sqlmeta.addColumn(None)
if not sqlmeta.joins:
# There are no joins - call addJoin to propagate joins
# from parent classes to children
sqlmeta.addJoin(None)
def _create(self, id, **kw):
# DSM: If we were called by a children class,
# DSM: we must retreive the properties dictionary.
# DSM: Note: we can't use the ** call paremeter directly
# DSM: as we must be able to delete items from the dictionary
# DSM: (and our children must know that the items were removed!)
if 'kw' in kw:
kw = kw['kw']
# DSM: If we are the children of an inheritable class,
# DSM: we must first create our parent
if self.sqlmeta.parentClass:
parentClass = self.sqlmeta.parentClass
new_kw = {}
parent_kw = {}
for (name, value) in kw.items():
if (name != 'childName') and hasattr(parentClass, name):
parent_kw[name] = value
else:
new_kw[name] = value
kw = new_kw
# Need to check that we have enough data to sucesfully
# create the current subclass otherwise we will leave
# the database in an inconsistent state.
for col in self.sqlmeta.columnList:
if (col._default == sqlbuilder.NoDefault) and \
(col.name not in kw) and (col.foreignName not in kw):
raise TypeError(
"%s() did not get expected keyword argument "
"%s" % (self.__class__.__name__, col.name))
parent_kw['childName'] = self.sqlmeta.childName
self._parent = parentClass(kw=parent_kw,
connection=self._connection)
id = self._parent.id
# TC: Create this record and catch all exceptions in order to destroy
# TC: the parent if the child can not be created.
try:
super(InheritableSQLObject, self)._create(id, **kw)
except Exception:
# If we are outside a transaction and this is a child,
# destroy the parent
connection = self._connection
if (not isinstance(connection, dbconnection.Transaction)
and connection.autoCommit) and self.sqlmeta.parentClass:
self._parent.destroySelf()
# TC: Do we need to do this??
self._parent = None
# TC: Reraise the original exception
raise
@classmethod
def _findAlternateID(cls, name, dbName, value, connection=None):
result = list(cls.selectBy(connection, **{name: value}))
if not result:
return result, None
obj = result[0]
return [obj.id], obj
@classmethod
def select(cls, clause=None, *args, **kwargs):
parentClass = cls.sqlmeta.parentClass
childUpdate = kwargs.pop('childUpdate', None)
# childUpdate may have one of three values:
# True:
# select was issued by parent class to create child objects.
# Execute select without modifications.
# None (default):
# select is run by application. If this class is inheritance
# child, delegate query to the parent class to utilize
# InheritableIteration optimizations. Selected records
# are restricted to this (child) class by adding childName
# filter to the where clause.
# False:
# select is delegated from inheritance child which is parent
# of another class. Delegate the query to parent if possible,
# but don't add childName restriction: selected records
# will be filtered by join to the table filtered by childName.
if (not childUpdate) and parentClass:
if childUpdate is None:
# this is the first parent in deep hierarchy
addClause = parentClass.q.childName == cls.sqlmeta.childName
# if the clause was one of TRUE varians, replace it
if (clause is None) or (clause is sqlbuilder.SQLTrueClause) \
or (
isinstance(clause, string_type)
and (clause == 'all')):
clause = addClause
else:
# patch WHERE condition:
# change ID field of this class to ID of parent class
# XXX the clause is patched in place; it would be better
# to build a new one if we have to replace field
clsID = cls.q.id
parentID = parentClass.q.id
def _get_patched(clause):
if isinstance(clause, sqlbuilder.SQLOp):
_patch_id_clause(clause)
return None
elif not isinstance(clause, sqlbuilder.Field):
return None
elif (clause.tableName == clsID.tableName) \
and (clause.fieldName == clsID.fieldName):
return parentID
else:
return None
def _patch_id_clause(clause):
if not isinstance(clause, sqlbuilder.SQLOp):
return
expr = _get_patched(clause.expr1)
if expr:
clause.expr1 = expr
expr = _get_patched(clause.expr2)
if expr:
clause.expr2 = expr
_patch_id_clause(clause)
# add childName filter
clause = sqlbuilder.AND(clause, addClause)
return parentClass.select(clause, childUpdate=False,
*args, **kwargs)
else:
return super(InheritableSQLObject, cls).select(
clause, *args, **kwargs)
@classmethod
def selectBy(cls, connection=None, **kw):
clause = []
foreignColumns = {}
currentClass = cls
while currentClass:
foreignColumns.update(dict(
[(column.foreignName, name)
for (name, column) in currentClass.sqlmeta.columns.items()
if column.foreignKey
]))
currentClass = currentClass.sqlmeta.parentClass
for name, value in kw.items():
if name in foreignColumns:
name = foreignColumns[name] # translate "key" to "keyID"
if isinstance(value, SQLObject):
value = value.id
currentClass = cls
while currentClass:
try:
clause.append(getattr(currentClass.q, name) == value)
break
except AttributeError:
pass
currentClass = currentClass.sqlmeta.parentClass
else:
raise AttributeError(
"'%s' instance has no attribute '%s'" % (
cls.__name__, name))
if clause:
clause = reduce(sqlbuilder.AND, clause)
else:
clause = None # select all
conn = connection or cls._connection
return cls.SelectResultsClass(cls, clause, connection=conn)
def destroySelf(self):
# DSM: If this object has parents, recursivly kill them
if hasattr(self, '_parent') and self._parent:
self._parent.destroySelf()
super(InheritableSQLObject, self).destroySelf()
def _reprItems(self):
items = super(InheritableSQLObject, self)._reprItems()
# add parent attributes (if any)
if self.sqlmeta.parentClass:
items.extend(self._parent._reprItems())
# filter out our special column
return [item for item in items if item[0] != 'childName']
__all__ = ['InheritableSQLObject']