CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/CondCore/TagCollection/python/DBImpl.py

Go to the documentation of this file.
00001 import coral
00002 import IdGenerator
00003         
00004 class DBImpl(object):
00005     """Class wrap up all the database operations.\n
00006     """
00007     def __init__( self , schema):
00008         """Input: coral schema handle.
00009         """
00010         self.__schema = schema
00011     def existRow( self, tableName, condition, conditionbindDict):
00012         """Return true if one row fulfills the selection criteria
00013         """
00014         try:
00015             tableHandle = self.__schema.tableHandle(tableName)
00016             query = tableHandle.newQuery()
00017             query.setCondition(condition,conditionbindDict)
00018             cursor = query.execute()
00019             result=False
00020             while ( cursor.next() ):
00021                 result=True
00022                 cursor.close()
00023             del query
00024             return result
00025         except Exception, e:
00026             raise Exception, str(e)
00027     def insertOneRow( self, tableName, tabrowDefDict, tabrowValueDict ):
00028         """Insert row 
00029         """
00030         try:
00031             tableHandle = self.__schema.tableHandle(tableName)
00032             editor = tableHandle.dataEditor()
00033             inputData = coral.AttributeList()
00034             for name,type in tabrowDefDict.items():
00035                # print name, type
00036                 inputData.extend( name, type )
00037                 inputData[name].setData(tabrowValueDict[name])
00038             editor.insertRow( inputData )
00039         except Exception, e:
00040             raise Exception, str(e)
00041     def bulkInsert( self, tableName, tabrowDefDict, bulkinput):
00042         """Bulk insert bulkinput=[{}]
00043         """
00044         try:
00045             dataEditor=self.__schema.tableHandle(tableName).dataEditor()
00046             insertdata=coral.AttributeList()
00047             for (columnname,columntype) in tabrowDefDict.items():
00048                 insertdata.extend(columnname,columntype)
00049                 
00050             bulkOperation=dataEditor.bulkInsert(insertdata,len(bulkinput))
00051             for valuedict in bulkinput:
00052                 for (columnname,columnvalue) in valuedict.items():
00053                     insertdata[columnname].setData(columnvalue)
00054                 bulkOperation.processNextIteration()
00055             bulkOperation.flush()
00056             del bulkOperation
00057         except Exception, e:
00058             raise Exception, str(e)
00059     def deleteRows( self, tableName, condition, conditionbindDict ):
00060         """Delete row(s)
00061         """
00062         try:
00063             tableHandle = self.__schema.tableHandle(tableName)
00064             editor = tableHandle.dataEditor()
00065             editor.deleteRows( condition, conditionbindDict )
00066         except Exception, e:
00067             raise Exception, str(e)
00068         
00069     def dropTable( self, tableName ):
00070         """Drop specified table.
00071         """
00072         self.__schema.dropIfExistsTable( tableName )
00073     def tableExists( self, tableName ):
00074         """Tell whether table exists
00075         """
00076         try:
00077             self.__schema.tableHandle(tableName)
00078             return True
00079         except coral.Exception, e:
00080             return False
00081 
00082 if __name__ == "__main__":
00083     pass