121 def entity_cache(self, eid): |
121 def entity_cache(self, eid): |
122 raise KeyError |
122 raise KeyError |
123 def set_entity_cache(self, entity): |
123 def set_entity_cache(self, entity): |
124 pass |
124 pass |
125 |
125 |
126 def create_entity(self, etype, **kwargs): |
126 def create_entity(self, etype, _cw_unsafe=False, **kwargs): |
127 """add a new entity of the given type |
127 """add a new entity of the given type |
128 |
128 |
129 Example (in a shell session): |
129 Example (in a shell session): |
130 |
130 |
131 c = create_entity('Company', name=u'Logilab') |
131 c = create_entity('Company', name=u'Logilab') |
132 create_entity('Person', works_for=c, firstname=u'John', lastname=u'Doe') |
132 create_entity('Person', works_for=c, firstname=u'John', lastname=u'Doe') |
133 |
133 |
134 """ |
134 """ |
|
135 if _cw_unsafe: |
|
136 execute = self.unsafe_execute |
|
137 else: |
|
138 execute = self.execute |
135 rql = 'INSERT %s X' % etype |
139 rql = 'INSERT %s X' % etype |
136 relations = [] |
140 relations = [] |
137 restrictions = set() |
141 restrictions = set() |
138 cachekey = [] |
142 cachekey = [] |
139 pending_relations = [] |
143 pending_relations = [] |
160 relations.append('X %s %%(%s)s' % (attr, attr)) |
164 relations.append('X %s %%(%s)s' % (attr, attr)) |
161 if relations: |
165 if relations: |
162 rql = '%s: %s' % (rql, ', '.join(relations)) |
166 rql = '%s: %s' % (rql, ', '.join(relations)) |
163 if restrictions: |
167 if restrictions: |
164 rql = '%s WHERE %s' % (rql, ', '.join(restrictions)) |
168 rql = '%s WHERE %s' % (rql, ', '.join(restrictions)) |
165 created = self.execute(rql, kwargs, cachekey).get_entity(0, 0) |
169 created = execute(rql, kwargs, cachekey).get_entity(0, 0) |
166 for attr, values in pending_relations: |
170 for attr, values in pending_relations: |
167 if attr.startswith('reverse_'): |
171 if attr.startswith('reverse_'): |
168 restr = 'Y %s X' % attr[len('reverse_'):] |
172 restr = 'Y %s X' % attr[len('reverse_'):] |
169 else: |
173 else: |
170 restr = 'X %s Y' % attr |
174 restr = 'X %s Y' % attr |
171 self.execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % ( |
175 execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % ( |
172 restr, ','.join(str(r.eid) for r in values)), |
176 restr, ','.join(str(r.eid) for r in values)), |
173 {'x': created.eid}, 'x') |
177 {'x': created.eid}, 'x') |
174 return created |
178 return created |
175 |
179 |
176 # url generation methods ################################################## |
180 # url generation methods ################################################## |