schema.py
changeset 3589 a5432f99f2d9
parent 3471 8c57c71b859c
parent 3554 26e586f3c15c
child 3720 5376aaadd16b
equal deleted inserted replaced
3536:f6c9a5df80fb 3589:a5432f99f2d9
   555     """
   555     """
   556 
   556 
   557     def __init__(self, restriction):
   557     def __init__(self, restriction):
   558         self.restriction = restriction
   558         self.restriction = restriction
   559 
   559 
   560     def check_consistency(self, subjschema, objschema, rdef):
       
   561         if objschema.is_final():
       
   562             raise BadSchemaDefinition("unique constraint doesn't apply to "
       
   563                                       "final entity type")
       
   564 
       
   565     def serialize(self):
   560     def serialize(self):
   566         return self.restriction
   561         return self.restriction
   567 
   562 
   568     def deserialize(cls, value):
   563     def deserialize(cls, value):
   569         return cls(value)
   564         return cls(value)
   575         return 1
   570         return 1
   576 
   571 
   577     def repo_check(self, session, eidfrom, rtype, eidto):
   572     def repo_check(self, session, eidfrom, rtype, eidto):
   578         """raise ValidationError if the relation doesn't satisfy the constraint
   573         """raise ValidationError if the relation doesn't satisfy the constraint
   579         """
   574         """
   580         pass # this is a vocabulary constraint, not enforce
   575         pass # this is a vocabulary constraint, not enforce XXX why?
   581 
   576 
   582     def __str__(self):
   577     def __str__(self):
   583         return self.restriction
   578         return self.restriction
   584 
   579 
   585     def __repr__(self):
   580     def __repr__(self):
   589 class RQLConstraint(RQLVocabularyConstraint):
   584 class RQLConstraint(RQLVocabularyConstraint):
   590     """the rql constraint is similar to the RQLVocabularyConstraint but
   585     """the rql constraint is similar to the RQLVocabularyConstraint but
   591     are also enforced at the repository level
   586     are also enforced at the repository level
   592     """
   587     """
   593     def exec_query(self, session, eidfrom, eidto):
   588     def exec_query(self, session, eidfrom, eidto):
       
   589         if eidto is None:
       
   590             rql = 'Any S WHERE S eid %(s)s, ' + self.restriction
       
   591             return session.unsafe_execute(rql, {'s': eidfrom}, 's',
       
   592                                           build_descr=False)
   594         rql = 'Any S,O WHERE S eid %(s)s, O eid %(o)s, ' + self.restriction
   593         rql = 'Any S,O WHERE S eid %(s)s, O eid %(o)s, ' + self.restriction
   595         return session.unsafe_execute(rql, {'s': eidfrom, 'o': eidto},
   594         return session.unsafe_execute(rql, {'s': eidfrom, 'o': eidto},
   596                                       ('s', 'o'), build_descr=False)
   595                                       ('s', 'o'), build_descr=False)
   597     def error(self, eid, rtype, msg):
   596     def error(self, eid, rtype, msg):
   598         raise ValidationError(eid, {rtype: msg})
   597         raise ValidationError(eid, {rtype: msg})
   599 
   598 
   600     def repo_check(self, session, eidfrom, rtype, eidto):
   599     def repo_check(self, session, eidfrom, rtype, eidto=None):
   601         """raise ValidationError if the relation doesn't satisfy the constraint
   600         """raise ValidationError if the relation doesn't satisfy the constraint
   602         """
   601         """
   603         if not self.exec_query(session, eidfrom, eidto):
   602         if not self.exec_query(session, eidfrom, eidto):
   604             # XXX at this point dunno if the validation error `occured` on
   603             # XXX at this point dunno if the validation error `occured` on
   605             #     eidfrom or eidto (from user interface point of view)
   604             #     eidfrom or eidto (from user interface point of view)
   608 
   607 
   609 class RQLUniqueConstraint(RQLConstraint):
   608 class RQLUniqueConstraint(RQLConstraint):
   610     """the unique rql constraint check that the result of the query isn't
   609     """the unique rql constraint check that the result of the query isn't
   611     greater than one
   610     greater than one
   612     """
   611     """
   613     def repo_check(self, session, eidfrom, rtype, eidto):
   612     def repo_check(self, session, eidfrom, rtype, eidto=None):
   614         """raise ValidationError if the relation doesn't satisfy the constraint
   613         """raise ValidationError if the relation doesn't satisfy the constraint
   615         """
   614         """
   616         if len(self.exec_query(session, eidfrom, eidto)) > 1:
   615         if len(self.exec_query(session, eidfrom, eidto)) > 1:
   617             # XXX at this point dunno if the validation error `occured` on
   616             # XXX at this point dunno if the validation error `occured` on
   618             #     eidfrom or eidto (from user interface point of view)
   617             #     eidfrom or eidto (from user interface point of view)