CMS 3D CMS Logo

IdGenerator.py

Go to the documentation of this file.
00001 import coral
00002 import DBImpl
00003 class IdGenerator(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 = 'nextID'
00010         self.__idTableColumnType = 'unsigned long'
00011     def getNewID( self, IDtableName):
00012         """Return the ID value in the specified ID table.\n
00013         Input: ID table name
00014         """
00015         try:
00016             query = self.__schema.tableHandle(IDtableName).newQuery()
00017             query.addToOutputList(self.__idTableColumnName)
00018             query.setForUpdate() #lock it
00019             cursor = query.execute()
00020             result = 0
00021             while ( cursor.next() ):
00022                 result = cursor.currentRow()[self.__idTableColumnName].data()
00023             del query
00024             return result
00025         except Exception, e:
00026             raise Exception, str(e)
00027     def incrementNextID( self, IDtableName ):
00028         """Set the nextID in the IDTableName to current id value + 1 .\n
00029         Input: ID table name.
00030         """
00031         try:
00032             tableHandle = self.__schema.tableHandle(IDtableName)
00033             query = tableHandle.newQuery()
00034             query.addToOutputList(self.__idTableColumnName)
00035             query.setForUpdate() #lock it
00036             cursor = query.execute()
00037             result = 0
00038             while ( cursor.next() ):
00039                 result = cursor.currentRow()[0].data()
00040             del query
00041             dataEditor = tableHandle.dataEditor()
00042             inputData = coral.AttributeList()
00043             inputData.extend( 'newid', self.__idTableColumnType )
00044             inputData['newid'].setData(result+1)
00045             dataEditor.updateRows('nextID = :newid','',inputData)
00046         except Exception, e:
00047             raise Exception, str(e)
00048     #def getIDTableName( self, tableName ):
00049     #    """Returns the ID table name associated with given table.\n
00050     #    No check on the existence of the table.\n
00051     #    Input: data table name
00052     #    Output: ID table name
00053     #    """
00054     #    return tableName+'_IDs'
00055     def createIDTable( self, idtableName, deleteOld=True ):
00056         """Create ID table 'tableName_ID' for the given table.\n
00057         Input: name of the table which needs new associated id table
00058         Output: name of the id table created
00059         """
00060         dbop=DBImpl.DBImpl(self.__schema)
00061         try:
00062             if dbop.tableExists(idtableName) is True:
00063                 if deleteOld is True:
00064                     dbop.dropTable(idtableName)
00065                 else:
00066                     return
00067             description = coral.TableDescription();
00068             description.setName(idtableName)
00069             description.insertColumn(self.__idTableColumnName, self.__idTableColumnType)
00070             idtableHandle=self.__schema.createTable( description )
00071             idtableHandle.privilegeManager().grantToPublic( coral.privilege_Select )
00072             inputData = coral.AttributeList()
00073             editor = idtableHandle.dataEditor()
00074             editor.rowBuffer( inputData )
00075             inputData[self.__idTableColumnName].setData(1)
00076             editor.insertRow( inputData )
00077         except Exception, e:
00078             raise Exception, str(e)
00079 if __name__ == "__main__":
00080     idtableName = 'TagTreeTable_IDS'
00081     context = coral.Context()
00082     context.setVerbosityLevel( 'ERROR' )
00083     svc = coral.ConnectionService( context )
00084     session = svc.connect( 'sqlite_file:data.db', accessMode = coral.access_Update )
00085     transaction = session.transaction()
00086     try:
00087         transaction.start()
00088         schema = session.nominalSchema()
00089         generator=IdGenerator(schema)
00090         generator.createIDTable( idtableName )
00091         transaction.commit()
00092         transaction.start(True)
00093         result=generator.getNewID(idtableName)
00094         print 'new id ',result
00095         transaction.commit()
00096         transaction.start(False)
00097         generator.incrementNextID(idtableName)
00098         print 'new id ',generator.getNewID(idtableName)
00099         transaction.commit()
00100         del session
00101     except coral.Exception, e:
00102         transaction.rollback()
00103         print str(e)
00104         del session
00105     except Exception, e:
00106         print "Failed in unit test"
00107         print str(e)
00108         del session

Generated on Tue Jun 9 17:26:14 2009 for CMSSW by  doxygen 1.5.4