22 from collections import defaultdict |
22 from collections import defaultdict |
23 from itertools import chain |
23 from itertools import chain |
24 from base64 import b64encode |
24 from base64 import b64encode |
25 |
25 |
26 from six.moves import range |
26 from six.moves import range |
27 |
|
28 from yams.constraints import SizeConstraint |
|
29 |
27 |
30 from cubicweb.dataimport import stores, pgstore |
28 from cubicweb.dataimport import stores, pgstore |
31 |
29 |
32 |
30 |
33 class MassiveObjectStore(stores.RQLObjectStore): |
31 class MassiveObjectStore(stores.RQLObjectStore): |
332 self.sql("INSERT INTO %s (eid_from, eid_to) SELECT cw_eid, %s FROM cw_%s " |
330 self.sql("INSERT INTO %s (eid_from, eid_to) SELECT cw_eid, %s FROM cw_%s " |
333 "WHERE NOT EXISTS (SELECT 1 FROM entities WHERE eid=cw_eid)" |
331 "WHERE NOT EXISTS (SELECT 1 FROM entities WHERE eid=cw_eid)" |
334 % (rtype, eid_to, etype.lower())) |
332 % (rtype, eid_to, etype.lower())) |
335 |
333 |
336 |
334 |
337 def get_size_constraints(schema): |
|
338 """analyzes yams ``schema`` and returns the list of size constraints. |
|
339 |
|
340 The returned value is a dictionary mapping entity types to a |
|
341 sub-dictionnaries mapping attribute names -> max size. |
|
342 """ |
|
343 size_constraints = {} |
|
344 # iterates on all entity types |
|
345 for eschema in schema.entities(): |
|
346 # for each entity type, iterates on attribute definitions |
|
347 size_constraints[eschema.type] = eschema_constraints = {} |
|
348 for rschema, aschema in eschema.attribute_definitions(): |
|
349 # for each attribute, if a size constraint is found, |
|
350 # append it to the size constraint list |
|
351 maxsize = None |
|
352 rdef = rschema.rdef(eschema, aschema) |
|
353 for constraint in rdef.constraints: |
|
354 if isinstance(constraint, SizeConstraint): |
|
355 maxsize = constraint.max |
|
356 eschema_constraints[rschema.type] = maxsize |
|
357 return size_constraints |
|
358 |
|
359 |
|
360 def get_default_values(schema): |
335 def get_default_values(schema): |
361 """analyzes yams ``schema`` and returns the list of default values. |
336 """analyzes yams ``schema`` and returns the list of default values. |
362 |
337 |
363 The returned value is a dictionary mapping entity types to a |
338 The returned value is a dictionary mapping entity types to a |
364 sub-dictionnaries mapping attribute names -> default values. |
339 sub-dictionnaries mapping attribute names -> default values. |