Go to the documentation of this file.00001 import coral
00002 import nameDealer
00003 class idDealer(object):
00004 """Manages the autoincremental ID values.\n
00005 Input: coral.schema object
00006 """
00007 def __init__( self , schema ):
00008 self.__schema = schema
00009 self.__idTableColumnName = nameDealer.idTableColumnDefinition()[0]
00010 self.__idTableColumnType = nameDealer.idTableColumnDefinition()[1]
00011
00012 def getIDColumnDefinition( self ):
00013 return (self.__idTableColumnName, self.__idTableColumnType)
00014
00015 def getIDforTable( self, tableName ):
00016 """
00017 get the new id value to use for the given table
00018 """
00019 try:
00020 idtableName = nameDealer.idTableName(tableName)
00021 query = self.__schema.tableHandle(idtableName).newQuery()
00022 query.addToOutputList(self.__idTableColumnName)
00023 query.setForUpdate()
00024 cursor = query.execute()
00025 result = 0
00026 while ( cursor.next() ):
00027 result = cursor.currentRow()[self.__idTableColumnName].data()
00028 del query
00029 return result
00030 except Exception, e:
00031 raise Exception, str(e)
00032
00033 def generateNextIDForTable( self, tableName ):
00034 """
00035 Set the nextID in the IDTableName to current id value + 1 .\n
00036 Input: ID table name.
00037 """
00038 try:
00039 idtableName = nameDealer.idTableName(tableName)
00040 tableHandle = self.__schema.tableHandle(idtableName)
00041 query = tableHandle.newQuery()
00042 query.addToOutputList(self.__idTableColumnName)
00043 query.setForUpdate()
00044 cursor = query.execute()
00045 result = 0
00046 while ( cursor.next() ):
00047 result = cursor.currentRow()[0].data()
00048 dataEditor = tableHandle.dataEditor()
00049 inputData = coral.AttributeList()
00050 dataEditor.updateRows('NEXTID = NEXTID+1','',inputData)
00051 del query
00052 return result+1
00053 except Exception, e:
00054 raise Exception, str(e)
00055
00056 if __name__ == "__main__":
00057 fakeIDtableName='Fake_ID'
00058 svc=coral.ConnectionService()
00059 session=svc.connect('sqlite_file:fake.db')
00060 transaction=session.transaction()
00061 try:
00062 transaction.start(False)
00063 schema=session.nominalSchema()
00064 idor=idDealer(schema)
00065 if schema.existsTable(fakeIDtableName) is False:
00066 description=coral.TableDescription()
00067 description.setName(fakeIDtableName)
00068 description.setPrimaryKey(idor.getIDColumnDefinition()[0])
00069 description.insertColumn(idor.getIDColumnDefinition()[0],idor.getIDColumnDefinition()[1])
00070 idtableHandle=schema.createTable(description)
00071 idtableHandle.privilegeManager().grantToPublic(coral.privilege_Select)
00072 inputData=coral.AttributeList()
00073 editor=idtableHandle.dataEditor()
00074 editor.rowBuffer(inputData)
00075 inputData[ idor.getIDColumnDefinition()[0] ].setData(0)
00076 editor.insertRow(inputData)
00077 idor.generateNextIDForTable('Fake')
00078 print idor.getIDforTable('Fake')
00079 transaction.commit()
00080 del session
00081 except coral.Exception,e:
00082 transaction.rollback()
00083 del session
00084 except Exception, e:
00085 print 'failed in unit test'
00086 print str(e)
00087 del session