995 # if inlined relations are specified, fill entity's related cache to |
995 # if inlined relations are specified, fill entity's related cache to |
996 # avoid unnecessary queries |
996 # avoid unnecessary queries |
997 for attr in entity.keys(): |
997 for attr in entity.keys(): |
998 rschema = eschema.subject_relation(attr) |
998 rschema = eschema.subject_relation(attr) |
999 if not rschema.is_final(): # inlined relation |
999 if not rschema.is_final(): # inlined relation |
1000 entity.set_related_cache(attr, 'subject', |
|
1001 entity.req.eid_rset(entity[attr])) |
|
1002 relations.append((attr, entity[attr])) |
1000 relations.append((attr, entity[attr])) |
1003 if source.should_call_hooks: |
1001 if source.should_call_hooks: |
1004 self.hm.call_hooks('before_add_entity', etype, session, entity) |
1002 self.hm.call_hooks('before_add_entity', etype, session, entity) |
|
1003 entity.edited_attributes = entity.keys() |
1005 entity.set_defaults() |
1004 entity.set_defaults() |
1006 entity.check(creation=True) |
1005 entity.check(creation=True) |
1007 source.add_entity(session, entity) |
1006 source.add_entity(session, entity) |
1008 if source.uri != 'system': |
1007 if source.uri != 'system': |
1009 extid = source.get_extid(entity) |
1008 extid = source.get_extid(entity) |
1010 self._extid_cache[(str(extid), source.uri)] = entity.eid |
1009 self._extid_cache[(str(extid), source.uri)] = entity.eid |
1011 else: |
1010 else: |
1012 extid = None |
1011 extid = None |
1013 self.add_info(session, entity, source, extid, complete=False) |
1012 self.add_info(session, entity, source, extid, complete=False) |
1014 entity._is_saved = True # entity has an eid and is saved |
1013 entity._is_saved = True # entity has an eid and is saved |
|
1014 # prefill entity relation caches |
|
1015 session.set_entity_cache(entity) |
|
1016 for rschema in eschema.subject_relations(): |
|
1017 rtype = str(rschema) |
|
1018 if rtype in VIRTUAL_RTYPES: |
|
1019 continue |
|
1020 if rschema.is_final(): |
|
1021 entity.setdefault(rtype, None) |
|
1022 else: |
|
1023 entity.set_related_cache(rtype, 'subject', session.empty_rset()) |
|
1024 for rschema in eschema.object_relations(): |
|
1025 rtype = str(rschema) |
|
1026 if rtype in VIRTUAL_RTYPES: |
|
1027 continue |
|
1028 entity.set_related_cache(rtype, 'object', session.empty_rset()) |
1015 # trigger after_add_entity after after_add_relation |
1029 # trigger after_add_entity after after_add_relation |
1016 if source.should_call_hooks: |
1030 if source.should_call_hooks: |
1017 self.hm.call_hooks('after_add_entity', etype, session, entity) |
1031 self.hm.call_hooks('after_add_entity', etype, session, entity) |
1018 # call hooks for inlined relations |
1032 # call hooks for inlined relations |
1019 for attr, value in relations: |
1033 for attr, value in relations: |
1020 self.hm.call_hooks('before_add_relation', attr, session, |
1034 self.hm.call_hooks('before_add_relation', attr, session, |
1021 entity.eid, attr, value) |
1035 entity.eid, attr, value) |
|
1036 session.update_rel_cache_add(entity.eid, attr, value) |
1022 self.hm.call_hooks('after_add_relation', attr, session, |
1037 self.hm.call_hooks('after_add_relation', attr, session, |
1023 entity.eid, attr, value) |
1038 entity.eid, attr, value) |
1024 return entity.eid |
1039 return entity.eid |
1025 |
1040 |
1026 def glob_update_entity(self, session, entity): |
1041 def glob_update_entity(self, session, entity): |
1045 else: |
1061 else: |
1046 # inlined relation |
1062 # inlined relation |
1047 previous_value = entity.related(attr) |
1063 previous_value = entity.related(attr) |
1048 if previous_value: |
1064 if previous_value: |
1049 previous_value = previous_value[0][0] # got a result set |
1065 previous_value = previous_value[0][0] # got a result set |
1050 self.hm.call_hooks('before_delete_relation', attr, session, |
1066 if previous_value == entity[attr]: |
1051 entity.eid, attr, previous_value) |
1067 previous_value = None |
1052 entity.set_related_cache(attr, 'subject', |
1068 else: |
1053 entity.req.eid_rset(entity[attr])) |
1069 self.hm.call_hooks('before_delete_relation', attr, |
|
1070 session, entity.eid, attr, |
|
1071 previous_value) |
1054 relations.append((attr, entity[attr], previous_value)) |
1072 relations.append((attr, entity[attr], previous_value)) |
1055 source = self.source_from_eid(entity.eid, session) |
1073 source = self.source_from_eid(entity.eid, session) |
1056 if source.should_call_hooks: |
1074 if source.should_call_hooks: |
1057 # call hooks for inlined relations |
1075 # call hooks for inlined relations |
1058 for attr, value, _ in relations: |
1076 for attr, value, _ in relations: |
1070 if source.should_call_hooks: |
1088 if source.should_call_hooks: |
1071 self.hm.call_hooks('after_update_entity', etype, session, |
1089 self.hm.call_hooks('after_update_entity', etype, session, |
1072 entity) |
1090 entity) |
1073 if source.should_call_hooks: |
1091 if source.should_call_hooks: |
1074 for attr, value, prevvalue in relations: |
1092 for attr, value, prevvalue in relations: |
|
1093 # if the relation is already cached, update existant cache |
|
1094 relcache = entity.relation_cached(attr, 'subject') |
1075 if prevvalue: |
1095 if prevvalue: |
1076 self.hm.call_hooks('after_delete_relation', attr, session, |
1096 self.hm.call_hooks('after_delete_relation', attr, session, |
1077 entity.eid, attr, prevvalue) |
1097 entity.eid, attr, prevvalue) |
|
1098 if relcache is not None: |
|
1099 session.update_rel_cache_del(entity.eid, attr, prevvalue) |
1078 del_existing_rel_if_needed(session, entity.eid, attr, value) |
1100 del_existing_rel_if_needed(session, entity.eid, attr, value) |
|
1101 if relcache is not None: |
|
1102 session.update_rel_cache_add(entity.eid, attr, value) |
|
1103 else: |
|
1104 entity.set_related_cache(attr, 'subject', |
|
1105 session.eid_rset(value)) |
1079 self.hm.call_hooks('after_add_relation', attr, session, |
1106 self.hm.call_hooks('after_add_relation', attr, session, |
1080 entity.eid, attr, value) |
1107 entity.eid, attr, value) |
1081 |
1108 |
1082 def glob_delete_entity(self, session, eid): |
1109 def glob_delete_entity(self, session, eid): |
1083 """delete an entity and all related entities from the repository""" |
1110 """delete an entity and all related entities from the repository""" |
1084 # call delete_info before hooks |
1111 # call delete_info before hooks |
1085 self._prepare_delete_info(session, eid) |
1112 self._prepare_delete_info(session, eid) |
1086 etype, uri, extid = self.type_and_source_from_eid(eid, session) |
1113 etype, uri, extid = self.type_and_source_from_eid(eid, session) |
1087 if server.DEBUG & server.DBG_REPO: |
1114 if server.DEBUG & server.DBG_REPO: |
1088 print 'DELETE entity', etype, eid |
1115 print 'DELETE entity', etype, eid |
|
1116 if eid == 937: |
|
1117 server.DEBUG |= (server.DBG_SQL | server.DBG_RQL | server.DBG_MORE) |
1089 source = self.sources_by_uri[uri] |
1118 source = self.sources_by_uri[uri] |
1090 if source.should_call_hooks: |
1119 if source.should_call_hooks: |
1091 self.hm.call_hooks('before_delete_entity', etype, session, eid) |
1120 self.hm.call_hooks('before_delete_entity', etype, session, eid) |
1092 self._delete_info(session, eid) |
1121 self._delete_info(session, eid) |
1093 source.delete_entity(session, etype, eid) |
1122 source.delete_entity(session, etype, eid) |
1103 if source.should_call_hooks: |
1132 if source.should_call_hooks: |
1104 del_existing_rel_if_needed(session, subject, rtype, object) |
1133 del_existing_rel_if_needed(session, subject, rtype, object) |
1105 self.hm.call_hooks('before_add_relation', rtype, session, |
1134 self.hm.call_hooks('before_add_relation', rtype, session, |
1106 subject, rtype, object) |
1135 subject, rtype, object) |
1107 source.add_relation(session, subject, rtype, object) |
1136 source.add_relation(session, subject, rtype, object) |
|
1137 rschema = self.schema.rschema(rtype) |
|
1138 session.update_rel_cache_add(subject, rtype, object, rschema.symetric) |
1108 if source.should_call_hooks: |
1139 if source.should_call_hooks: |
1109 self.hm.call_hooks('after_add_relation', rtype, session, |
1140 self.hm.call_hooks('after_add_relation', rtype, session, |
1110 subject, rtype, object) |
1141 subject, rtype, object) |
1111 |
1142 |
1112 def glob_delete_relation(self, session, subject, rtype, object): |
1143 def glob_delete_relation(self, session, subject, rtype, object): |
1116 source = self.locate_relation_source(session, subject, rtype, object) |
1147 source = self.locate_relation_source(session, subject, rtype, object) |
1117 if source.should_call_hooks: |
1148 if source.should_call_hooks: |
1118 self.hm.call_hooks('before_delete_relation', rtype, session, |
1149 self.hm.call_hooks('before_delete_relation', rtype, session, |
1119 subject, rtype, object) |
1150 subject, rtype, object) |
1120 source.delete_relation(session, subject, rtype, object) |
1151 source.delete_relation(session, subject, rtype, object) |
1121 if self.schema.rschema(rtype).symetric: |
1152 rschema = self.schema.rschema(rtype) |
|
1153 session.update_rel_cache_del(subject, rtype, object, rschema.symetric) |
|
1154 if rschema.symetric: |
1122 # on symetric relation, we can't now in which sense it's |
1155 # on symetric relation, we can't now in which sense it's |
1123 # stored so try to delete both |
1156 # stored so try to delete both |
1124 source.delete_relation(session, object, rtype, subject) |
1157 source.delete_relation(session, object, rtype, subject) |
1125 if source.should_call_hooks: |
1158 if source.should_call_hooks: |
1126 self.hm.call_hooks('after_delete_relation', rtype, session, |
1159 self.hm.call_hooks('after_delete_relation', rtype, session, |