server/repository.py
changeset 3720 5376aaadd16b
parent 3647 2941f4a0aab9
parent 3694 33dbb1da1db9
child 3722 c414f402cbff
equal deleted inserted replaced
3678:29f74716fd70 3720:5376aaadd16b
    88             session.repo.index_entity(session, container)
    88             session.repo.index_entity(session, container)
    89 
    89 
    90     def commit_event(self):
    90     def commit_event(self):
    91         pass
    91         pass
    92 
    92 
       
    93 
    93 def del_existing_rel_if_needed(session, eidfrom, rtype, eidto):
    94 def del_existing_rel_if_needed(session, eidfrom, rtype, eidto):
    94     """delete existing relation when adding a new one if card is 1 or ?
    95     """delete existing relation when adding a new one if card is 1 or ?
    95 
    96 
    96     have to be done once the new relation has been inserted to avoid having
    97     have to be done once the new relation has been inserted to avoid having
    97     an entity without a relation for some time
    98     an entity without a relation for some time
   102     # skip delete queries (only?) if session is an internal session. This is
   103     # skip delete queries (only?) if session is an internal session. This is
   103     # hooks responsability to ensure they do not violate relation's cardinality
   104     # hooks responsability to ensure they do not violate relation's cardinality
   104     if session.is_super_session:
   105     if session.is_super_session:
   105         return
   106         return
   106     ensure_card_respected(session.unsafe_execute, session, eidfrom, rtype, eidto)
   107     ensure_card_respected(session.unsafe_execute, session, eidfrom, rtype, eidto)
       
   108 
   107 
   109 
   108 def ensure_card_respected(execute, session, eidfrom, rtype, eidto):
   110 def ensure_card_respected(execute, session, eidfrom, rtype, eidto):
   109     card = session.schema_rproperty(rtype, eidfrom, eidto, 'cardinality')
   111     card = session.schema_rproperty(rtype, eidfrom, eidto, 'cardinality')
   110     # one may be tented to check for neweids but this may cause more than one
   112     # one may be tented to check for neweids but this may cause more than one
   111     # relation even with '1?'  cardinality if thoses relations are added in the
   113     # relation even with '1?'  cardinality if thoses relations are added in the
   119             execute('DELETE X %s Y WHERE X eid %%(x)s,NOT Y eid %%(y)s' % rtype,
   121             execute('DELETE X %s Y WHERE X eid %%(x)s,NOT Y eid %%(y)s' % rtype,
   120                     {'x': eidfrom, 'y': eidto}, 'x')
   122                     {'x': eidfrom, 'y': eidto}, 'x')
   121     if card[1] in '1?':
   123     if card[1] in '1?':
   122         execute('DELETE X %s Y WHERE NOT X eid %%(x)s, Y eid %%(y)s' % rtype,
   124         execute('DELETE X %s Y WHERE NOT X eid %%(x)s, Y eid %%(y)s' % rtype,
   123                 {'x': eidfrom, 'y': eidto}, 'y')
   125                 {'x': eidfrom, 'y': eidto}, 'y')
       
   126 
   124 
   127 
   125 class Repository(object):
   128 class Repository(object):
   126     """a repository provides access to a set of persistent storages for
   129     """a repository provides access to a set of persistent storages for
   127     entities and relations
   130     entities and relations
   128 
   131 
   982         if source.should_call_hooks:
   985         if source.should_call_hooks:
   983             self.hm.call_hooks('before_add_entity', session, entity=entity)
   986             self.hm.call_hooks('before_add_entity', session, entity=entity)
   984         # XXX use entity.keys here since edited_attributes is not updated for
   987         # XXX use entity.keys here since edited_attributes is not updated for
   985         # inline relations
   988         # inline relations
   986         for attr in entity.keys():
   989         for attr in entity.keys():
   987             rschema = eschema.subject_relation(attr)
   990             rschema = eschema.subjrels[attr]
   988             if not rschema.is_final(): # inlined relation
   991             if not rschema.final: # inlined relation
   989                 relations.append((attr, entity[attr]))
   992                 relations.append((attr, entity[attr]))
   990         entity.set_defaults()
   993         entity.set_defaults()
   991         entity.check(creation=True)
   994         entity.check(creation=True)
   992         source.add_entity(session, entity)
   995         source.add_entity(session, entity)
   993         if source.uri != 'system':
   996         if source.uri != 'system':
  1001         session.set_entity_cache(entity)
  1004         session.set_entity_cache(entity)
  1002         for rschema in eschema.subject_relations():
  1005         for rschema in eschema.subject_relations():
  1003             rtype = str(rschema)
  1006             rtype = str(rschema)
  1004             if rtype in schema.VIRTUAL_RTYPES:
  1007             if rtype in schema.VIRTUAL_RTYPES:
  1005                 continue
  1008                 continue
  1006             if rschema.is_final():
  1009             if rschema.final:
  1007                 entity.setdefault(rtype, None)
  1010                 entity.setdefault(rtype, None)
  1008             else:
  1011             else:
  1009                 entity.set_related_cache(rtype, 'subject', session.empty_rset())
  1012                 entity.set_related_cache(rtype, 'subject', session.empty_rset())
  1010         for rschema in eschema.object_relations():
  1013         for rschema in eschema.object_relations():
  1011             rtype = str(rschema)
  1014             rtype = str(rschema)
  1041         only_inline_rels, need_fti_update = True, False
  1044         only_inline_rels, need_fti_update = True, False
  1042         relations = []
  1045         relations = []
  1043         for attr in edited_attributes:
  1046         for attr in edited_attributes:
  1044             if attr == 'eid':
  1047             if attr == 'eid':
  1045                 continue
  1048                 continue
  1046             rschema = eschema.subject_relation(attr)
  1049             rschema = eschema.subjrels[attr]
  1047             if rschema.is_final():
  1050             if rschema.final:
  1048                 if eschema.rproperty(attr, 'fulltextindexed'):
  1051                 if eschema.rproperty(attr, 'fulltextindexed'):
  1049                     need_fti_update = True
  1052                     need_fti_update = True
  1050                 only_inline_rels = False
  1053                 only_inline_rels = False
  1051             else:
  1054             else:
  1052                 # inlined relation
  1055                 # inlined relation