server/schemahooks.py
changeset 1990 59507a12a7f4
parent 1977 606923dff11b
parent 1981 e6eed4324357
child 2011 758ccc0a9d16
equal deleted inserted replaced
1980:35394365b6c1 1990:59507a12a7f4
   186     session.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s',
   186     session.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s',
   187                     {'x': eid})
   187                     {'x': eid})
   188     DeleteCWRTypeOp(session, name)
   188     DeleteCWRTypeOp(session, name)
   189 
   189 
   190 
   190 
   191 class DelErdefOp(SchemaOperation):
   191 class DelRelationDefOp(SchemaOperation):
   192     """actually remove the relation definition from the application's schema"""
   192     """actually remove the relation definition from the application's schema"""
   193     def commit_event(self):
   193     def commit_event(self):
   194         subjtype, rtype, objtype = self.kobj
   194         subjtype, rtype, objtype = self.kobj
   195         try:
   195         try:
   196             self.schema.del_relation_def(subjtype, rtype, objtype)
   196             self.schema.del_relation_def(subjtype, rtype, objtype)
   235     elif lastrel:
   235     elif lastrel:
   236         DropTableOp(session, table='%s_relation' % rschema.type)
   236         DropTableOp(session, table='%s_relation' % rschema.type)
   237     # if this is the last instance, drop associated relation type
   237     # if this is the last instance, drop associated relation type
   238     if lastrel and not rteid in pendings:
   238     if lastrel and not rteid in pendings:
   239         execute('DELETE CWRType X WHERE X eid %(x)s', {'x': rteid}, 'x')
   239         execute('DELETE CWRType X WHERE X eid %(x)s', {'x': rteid}, 'x')
   240     DelErdefOp(session, (subjschema, rschema, objschema))
   240     DelRelationDefOp(session, (subjschema, rschema, objschema))
   241 
   241 
   242 
   242 
   243 # addition ####################################################################
   243 # addition ####################################################################
   244 
   244 
   245 class AddCWETypeOp(EarlySchemaOperation):
   245 class AddCWETypeOp(EarlySchemaOperation):
   560 
   560 
   561     def commit_event(self):
   561     def commit_event(self):
   562         self.session.repo.schema.rename_entity_type(self.oldname, self.newname)
   562         self.session.repo.schema.rename_entity_type(self.oldname, self.newname)
   563 
   563 
   564 
   564 
   565 class UpdateRdefOp(SchemaOperation):
   565 class UpdateRelationDefOp(SchemaOperation):
   566     """actually update some properties of a relation definition"""
   566     """actually update some properties of a relation definition"""
   567     rschema = values = None # make pylint happy
   567     rschema = values = None # make pylint happy
   568 
   568 
   569     def precommit_event(self):
   569     def precommit_event(self):
   570         if 'indexed' in self.values:
   570         if 'indexed' in self.values:
   574             column = SQL_PREFIX + rtype
   574             column = SQL_PREFIX + rtype
   575             if self.values['indexed']:
   575             if self.values['indexed']:
   576                 sysource.create_index(self.session, table, column)
   576                 sysource.create_index(self.session, table, column)
   577             else:
   577             else:
   578                 sysource.drop_index(self.session, table, column)
   578                 sysource.drop_index(self.session, table, column)
       
   579         if 'cardinality' in self.values and self.rschema.is_final():
       
   580             if self.session.pool.source('system').dbdriver == 'sqlite':
       
   581                 # not supported (and NOT NULL not set by yams in that case, so
       
   582                 # no worry)
       
   583                 return
       
   584             sqlexec = self.session.system_sql
       
   585             etype, rtype = self.kobj[0], self.rschema.type
       
   586             if self.values['cardinality'][0] == '1':
       
   587                 cmd = 'SET'
       
   588             else:
       
   589                 cmd = 'DROP'
       
   590             sqlexec('ALTER TABLE %s ALTER COLUMN %s %s NOT NULL' % (
       
   591                 table, SQL_PREFIX + etype, SQL_PREFIX + rtype))
   579 
   592 
   580     def commit_event(self):
   593     def commit_event(self):
   581         # structure should be clean, not need to remove entity's relations
   594         # structure should be clean, not need to remove entity's relations
   582         # at this point
   595         # at this point
   583         self.rschema._rproperties[self.kobj].update(self.values)
   596         self.rschema._rproperties[self.kobj].update(self.values)
   594             prop = 'ordernum'
   607             prop = 'ordernum'
   595         if prop in entity:
   608         if prop in entity:
   596             newvalues[prop] = entity[prop]
   609             newvalues[prop] = entity[prop]
   597     if newvalues:
   610     if newvalues:
   598         subjtype = entity.from_entity[0].name
   611         subjtype = entity.from_entity[0].name
   599         UpdateRdefOp(session, (subjtype, desttype), rschema=rschema,
   612         UpdateRelationDefOp(session, (subjtype, desttype),
   600                      values=newvalues)
   613                             rschema=rschema, values=newvalues)
   601 
   614 
   602 
   615 
   603 class UpdateRtypeOp(SchemaOperation):
   616 class UpdateRtypeOp(SchemaOperation):
   604     """actually update some properties of a relation definition"""
   617     """actually update some properties of a relation definition"""
   605     rschema = values = entity = None # make pylint happy
   618     rschema = values = entity = None # make pylint happy