entity.py
changeset 7377 d8083b2ae4d6
parent 7314 d3104c697adb
parent 7376 38524ca653e5
child 7429 20ef21926774
equal deleted inserted replaced
7368:f1d9e3b9e51e 7377:d8083b2ae4d6
   268         rql = 'INSERT %s X' % cls.__regid__
   268         rql = 'INSERT %s X' % cls.__regid__
   269         relations = []
   269         relations = []
   270         restrictions = set()
   270         restrictions = set()
   271         pending_relations = []
   271         pending_relations = []
   272         eschema = cls.e_schema
   272         eschema = cls.e_schema
       
   273         qargs = {}
   273         for attr, value in kwargs.items():
   274         for attr, value in kwargs.items():
   274             if attr.startswith('reverse_'):
   275             if attr.startswith('reverse_'):
   275                 attr = attr[len('reverse_'):]
   276                 attr = attr[len('reverse_'):]
   276                 role = 'object'
   277                 role = 'object'
   277             else:
   278             else:
   281             if not rschema.final and isinstance(value, (tuple, list, set, frozenset)):
   282             if not rschema.final and isinstance(value, (tuple, list, set, frozenset)):
   282                 if len(value) == 1:
   283                 if len(value) == 1:
   283                     value = iter(value).next()
   284                     value = iter(value).next()
   284                 else:
   285                 else:
   285                     # prepare IN clause
   286                     # prepare IN clause
   286                     del kwargs[attr]
   287                     pending_relations.append( (attr, role, value) )
   287                     pending_relations.append( (attr, value) )
       
   288                     continue
   288                     continue
   289             if hasattr(value, 'eid'): # non final relation
   289             if rschema.final: # attribute
       
   290                 relations.append('X %s %%(%s)s' % (attr, attr))
       
   291             else:
   290                 rvar = attr.upper()
   292                 rvar = attr.upper()
   291                 if role == 'object':
   293                 if role == 'object':
   292                     relations.append('%s %s X' % (rvar, attr))
   294                     relations.append('%s %s X' % (rvar, attr))
   293                 else:
   295                 else:
   294                     relations.append('X %s %s' % (attr, rvar))
   296                     relations.append('X %s %s' % (attr, rvar))
   295                 restriction = '%s eid %%(%s)s' % (rvar, attr)
   297                 restriction = '%s eid %%(%s)s' % (rvar, attr)
   296                 if not restriction in restrictions:
   298                 if not restriction in restrictions:
   297                     restrictions.add(restriction)
   299                     restrictions.add(restriction)
   298                 kwargs[attr] = value.eid
   300                 if hasattr(value, 'eid'):
   299             else: # attribute
   301                     value = value.eid
   300                 relations.append('X %s %%(%s)s' % (attr, attr))
   302             qargs[attr] = value
   301         if relations:
   303         if relations:
   302             rql = '%s: %s' % (rql, ', '.join(relations))
   304             rql = '%s: %s' % (rql, ', '.join(relations))
   303         if restrictions:
   305         if restrictions:
   304             rql = '%s WHERE %s' % (rql, ', '.join(restrictions))
   306             rql = '%s WHERE %s' % (rql, ', '.join(restrictions))
   305         created = execute(rql, kwargs).get_entity(0, 0)
   307         created = execute(rql, qargs).get_entity(0, 0)
   306         for attr, values in pending_relations:
   308         for attr, role, values in pending_relations:
   307             if attr.startswith('reverse_'):
   309             if role == 'object':
   308                 restr = 'Y %s X' % attr[len('reverse_'):]
   310                 restr = 'Y %s X' % attr
   309             else:
   311             else:
   310                 restr = 'X %s Y' % attr
   312                 restr = 'X %s Y' % attr
   311             execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % (
   313             execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % (
   312                 restr, ','.join(str(r.eid) for r in values)),
   314                 restr, ','.join(str(getattr(r, 'eid', r)) for r in values)),
   313                     {'x': created.eid}, build_descr=False)
   315                     {'x': created.eid}, build_descr=False)
   314         return created
   316         return created
   315 
   317 
   316     def __init__(self, req, rset=None, row=None, col=0):
   318     def __init__(self, req, rset=None, row=None, col=0):
   317         AppObject.__init__(self, req, rset=rset, row=row, col=col)
   319         AppObject.__init__(self, req, rset=rset, row=row, col=col)