250 |
250 |
251 Example (in a shell session): |
251 Example (in a shell session): |
252 |
252 |
253 >>> companycls = vreg['etypes'].etype_class(('Company') |
253 >>> companycls = vreg['etypes'].etype_class(('Company') |
254 >>> personcls = vreg['etypes'].etype_class(('Person') |
254 >>> personcls = vreg['etypes'].etype_class(('Person') |
255 >>> c = companycls.cw_instantiate(req.execute, name=u'Logilab') |
255 >>> c = companycls.cw_instantiate(session.execute, name=u'Logilab') |
256 >>> personcls.cw_instantiate(req.execute, firstname=u'John', lastname=u'Doe', |
256 >>> p = personcls.cw_instantiate(session.execute, firstname=u'John', lastname=u'Doe', |
257 ... works_for=c) |
257 ... works_for=c) |
258 |
258 |
|
259 You can also set relation where the entity has 'object' role by |
|
260 prefixing the relation by 'reverse_'. |
259 """ |
261 """ |
260 rql = 'INSERT %s X' % cls.__regid__ |
262 rql = 'INSERT %s X' % cls.__regid__ |
261 relations = [] |
263 relations = [] |
262 restrictions = set() |
264 restrictions = set() |
263 pending_relations = [] |
265 pending_relations = [] |
272 rschema = eschema.subjrels[attr] if role == 'subject' else eschema.objrels[attr] |
274 rschema = eschema.subjrels[attr] if role == 'subject' else eschema.objrels[attr] |
273 if not rschema.final and isinstance(value, (tuple, list, set, frozenset)): |
275 if not rschema.final and isinstance(value, (tuple, list, set, frozenset)): |
274 if len(value) == 1: |
276 if len(value) == 1: |
275 value = iter(value).next() |
277 value = iter(value).next() |
276 else: |
278 else: |
|
279 # prepare IN clause |
277 del kwargs[attr] |
280 del kwargs[attr] |
278 pending_relations.append( (attr, value) ) |
281 pending_relations.append( (attr, value) ) |
279 continue |
282 continue |
280 if hasattr(value, 'eid'): # non final relation |
283 if hasattr(value, 'eid'): # non final relation |
281 rvar = attr.upper() |
284 rvar = attr.upper() |
282 # XXX safer detection of object relation |
285 if role == 'object': |
283 if attr.startswith('reverse_'): |
286 relations.append('%s %s X' % (rvar, attr)) |
284 relations.append('%s %s X' % (rvar, attr[len('reverse_'):])) |
|
285 else: |
287 else: |
286 relations.append('X %s %s' % (attr, rvar)) |
288 relations.append('X %s %s' % (attr, rvar)) |
287 restriction = '%s eid %%(%s)s' % (rvar, attr) |
289 restriction = '%s eid %%(%s)s' % (rvar, attr) |
288 if not restriction in restrictions: |
290 if not restriction in restrictions: |
289 restrictions.add(restriction) |
291 restrictions.add(restriction) |