req.py
changeset 2968 0e3460341023
parent 2825 87ac03aed941
child 3163 edfe43ceaa35
equal deleted inserted replaced
2902:dd9f2dd02f85 2968:0e3460341023
   102     def entity_cache(self, eid):
   102     def entity_cache(self, eid):
   103         raise KeyError
   103         raise KeyError
   104 
   104 
   105     def set_entity_cache(self, entity):
   105     def set_entity_cache(self, entity):
   106         pass
   106         pass
       
   107 
       
   108     # XXX move to CWEntityManager or even better as factory method (unclear
       
   109     # where yet...)
       
   110     def create_entity(self, etype, *args, **kwargs):
       
   111         """add a new entity of the given type"""
       
   112         rql = 'INSERT %s X' % etype
       
   113         relations = []
       
   114         restrictions = []
       
   115         cachekey = []
       
   116         for rtype, rvar in args:
       
   117             relations.append('X %s %s' % (rtype, rvar))
       
   118             restrictions.append('%s eid %%(%s)s' % (rvar, rvar))
       
   119             cachekey.append(rvar)
       
   120         for attr in kwargs:
       
   121             if attr in cachekey:
       
   122                 continue
       
   123             relations.append('X %s %%(%s)s' % (attr, attr))
       
   124         if relations:
       
   125             rql = '%s: %s' % (rql, ', '.join(relations))
       
   126         if restrictions:
       
   127             rql = '%s WHERE %s' % (rql, ', '.join(restrictions))
       
   128         return self.execute(rql, kwargs, cachekey).get_entity(0, 0)
   107 
   129 
   108     def ensure_ro_rql(self, rql):
   130     def ensure_ro_rql(self, rql):
   109         """raise an exception if the given rql is not a select query"""
   131         """raise an exception if the given rql is not a select query"""
   110         first = rql.split(' ', 1)[0].lower()
   132         first = rql.split(' ', 1)[0].lower()
   111         if first in ('insert', 'set', 'delete'):
   133         if first in ('insert', 'set', 'delete'):