641 self._record_tx_action(session, 'tx_relation_actions', 'A', |
641 self._record_tx_action(session, 'tx_relation_actions', 'A', |
642 eid_from=subject, rtype=rtype, eid_to=object) |
642 eid_from=subject, rtype=rtype, eid_to=object) |
643 |
643 |
644 def _add_relations(self, session, rtype, subj_obj_list, inlined=False): |
644 def _add_relations(self, session, rtype, subj_obj_list, inlined=False): |
645 """add a relation to the source""" |
645 """add a relation to the source""" |
|
646 sql = [] |
646 if inlined is False: |
647 if inlined is False: |
647 attrs = [{'eid_from': subject, 'eid_to': object} |
648 attrs = [{'eid_from': subject, 'eid_to': object} |
648 for subject, object in subj_obj_list] |
649 for subject, object in subj_obj_list] |
649 sql = self.sqlgen.insert('%s_relation' % rtype, attrs[0]) |
650 sql.append((self.sqlgen.insert('%s_relation' % rtype, attrs[0]), attrs)) |
650 else: # used by data import |
651 else: # used by data import |
651 etype = session.describe(subject)[0] |
652 etypes = {} |
652 attrs = [{'cw_eid': subject, SQL_PREFIX + rtype: object} |
653 for subject, object in subj_obj_list: |
653 for subject, object in subj_obj_list] |
654 etype = session.describe(subject)[0] |
654 sql = self.sqlgen.update(SQL_PREFIX + etype, attrs[0], |
655 if etype in etypes: |
655 ['cw_eid']) |
656 etypes[etype].append((subject, object)) |
656 self.doexecmany(session, sql, attrs) |
657 else: |
|
658 etypes[etype] = [(subject, object)] |
|
659 for subj_etype, subj_obj_list in etypes.iteritems(): |
|
660 attrs = [{'cw_eid': subject, SQL_PREFIX + rtype: object} |
|
661 for subject, object in subj_obj_list] |
|
662 sql.append((self.sqlgen.update(SQL_PREFIX + etype, attrs[0], |
|
663 ['cw_eid']), |
|
664 attrs)) |
|
665 for statement, attrs in sql: |
|
666 self.doexecmany(session, statement, attrs) |
657 |
667 |
658 def delete_relation(self, session, subject, rtype, object): |
668 def delete_relation(self, session, subject, rtype, object): |
659 """delete a relation from the source""" |
669 """delete a relation from the source""" |
660 rschema = self.schema.rschema(rtype) |
670 rschema = self.schema.rschema(rtype) |
661 self._delete_relation(session, subject, rtype, object, rschema.inlined) |
671 self._delete_relation(session, subject, rtype, object, rschema.inlined) |
1240 continue |
1250 continue |
1241 if not errors: |
1251 if not errors: |
1242 self.repo.hm.call_hooks('before_add_relation', session, |
1252 self.repo.hm.call_hooks('before_add_relation', session, |
1243 eidfrom=subj, rtype=rtype, eidto=obj) |
1253 eidfrom=subj, rtype=rtype, eidto=obj) |
1244 # add relation in the database |
1254 # add relation in the database |
1245 self._add_relation(session, subj, rtype, obj, rdef.rtype.inlined) |
1255 self._add_relations(session, rtype, [(subj, obj)], rdef.rtype.inlined) |
1246 # set related cache |
1256 # set related cache |
1247 session.update_rel_cache_add(subj, rtype, obj, rdef.rtype.symmetric) |
1257 session.update_rel_cache_add(subj, rtype, obj, rdef.rtype.symmetric) |
1248 self.repo.hm.call_hooks('after_add_relation', session, |
1258 self.repo.hm.call_hooks('after_add_relation', session, |
1249 eidfrom=subj, rtype=rtype, eidto=obj) |
1259 eidfrom=subj, rtype=rtype, eidto=obj) |
1250 return errors |
1260 return errors |