CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/CondCore/TagCollection/python/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     svc = coral.ConnectionService()
00082     session = svc.connect( 'sqlite_file:data.db', accessMode = coral.access_Update )
00083     transaction = session.transaction()
00084     try:
00085         transaction.start()
00086         schema = session.nominalSchema()
00087         generator=IdGenerator(schema)
00088         generator.createIDTable( idtableName )
00089         transaction.commit()
00090         transaction.start(True)
00091         result=generator.getNewID(idtableName)
00092         print 'new id ',result
00093         transaction.commit()
00094         transaction.start(False)
00095         generator.incrementNextID(idtableName)
00096         print 'new id ',generator.getNewID(idtableName)
00097         transaction.commit()
00098         del session
00099     except coral.Exception, e:
00100         transaction.rollback()
00101         print str(e)
00102         del session
00103     except Exception, e:
00104         print "Failed in unit test"
00105         print str(e)
00106         del session