1 from __future__
import print_function
3 from RecoLuminosity.LumiDB
import nameDealer
5 """Class wrap up all the database operations.\n 9 Input: coral schema handle. 14 mytable=self.__schema.tableHandle(tablename)
15 print(
'numberofindices ', mytable.description().numberOfIndices())
16 for i
in range(0,mytable.description().numberOfIndices()):
17 index=mytable.description().
index(i)
18 print(
' ', index.name(),
' -> ')
19 for iColumn
in index.columnNames():
21 print(
' (tablespace : ',index.tableSpaceName(),
')')
24 Print out the overview of the schema 27 tablelist=self.__schema.listTables()
29 table = self.__schema.tableHandle(t)
30 print(
'table ===',t,
'===')
31 n=table.description().numberOfColumns()
33 columndesp=table.description().columnDescription(i)
34 print(
'\t',columndesp.name(),columndesp.type())
35 if table.description().hasPrimaryKey():
36 print(
'Primary Key : ')
37 print(
'\t',table.description().primaryKey().columnNames())
40 viewlist=self.__schema.listViews()
42 myview = self.__schema.viewHandle(
'V0')
43 print(
'definition : ',myview.definition())
44 n=myview.numberOfColumns()
46 columndesp=view.column(i)
47 print(
'\t',columndesp.name(),columndesp.type())
48 except Exception
as e:
50 def existRow( self, tableName, condition, conditionDefDict,conditionDict):
52 Return true if one row fulfills the selection criteria 55 tableHandle = self.__schema.tableHandle(tableName)
56 query = tableHandle.newQuery()
57 queryBind=coral.AttributeList()
58 for colname,coltype
in conditionDefDict.items():
59 queryBind.extend(colname,coltype)
60 queryBind[colname].setData(conditionDict[colname])
61 query.setCondition(condition,queryBind)
62 cursor = query.execute()
64 while (
next(cursor) ):
69 except Exception
as e:
71 def insertOneRow( self, tableName, tabrowDefDict, tabrowValueDict ):
76 tableHandle = self.__schema.tableHandle(tableName)
77 editor = tableHandle.dataEditor()
78 inputData = coral.AttributeList()
79 for name,type
in tabrowDefDict.items():
81 inputData.extend( name, type )
82 inputData[name].setData(tabrowValueDict[name])
83 editor.insertRow( inputData )
84 except Exception
as e:
87 def singleUpdate( self,tableName,setClause,updateCondition,inputData):
89 dataEditor=self.__schema.tableHandle(tableName).dataEditor()
90 n=dataEditor.updateRows(setClause,updateCondition,inputData)
92 except Exception
as e:
93 raise RuntimeError(
'dbUtil.updateOneRow:'+
str(e))
95 def updateRows( self,tableName,updateAction,updateCondition,bindvarDef,bulkinput):
97 update rows, note update must be ordered 100 updateAction,string e.g. flag=:newflag 101 conditionstring, string ,e.g. runnum=:runnum and cmslsnum=:cmslsnum 102 bindvarDef,[('newflag','string'),('runnum','unsigned int'),('cmslsnum','unsigned int')] 103 bulkinput,[[('newflag','GOOD'),('runnum',1234),('cmslsnum',1)],[]] 106 dataEditor=self.__schema.tableHandle(tableName).dataEditor()
107 updateData=coral.AttributeList()
108 for (columnname,columntype)
in bindvarDef:
109 updateData.extend(columnname,columntype)
110 bulkOperation=dataEditor.bulkUpdateRows(updateAction,updateCondition,updateData,len(bulkinput))
111 for valuelist
in bulkinput:
112 for (columnname,columnvalue)
in valuelist:
113 updateData[columnname].setData(columnvalue)
114 bulkOperation.processNextIteration()
115 bulkOperation.flush()
117 except Exception
as e:
123 tabrowDef,[('RUNNUM':'unsigned int'),('CMSLSNUM','unsigned int'),('FLAG','string'),('COMMENT','string')] 124 bulkinput,[[('RUNNUM',1234),('CMSLSNUM',1234),('FLAG','GOOD'),('COMMENT','coment')],[]] 127 dataEditor=self.__schema.tableHandle(tableName).dataEditor()
128 insertdata=coral.AttributeList()
129 for (columnname,columntype)
in tabrowDef:
130 insertdata.extend(columnname,columntype)
131 bulkOperation=dataEditor.bulkInsert(insertdata,len(bulkinput))
132 for valuelist
in bulkinput:
133 for (columnname,columnvalue)
in valuelist:
135 insertdata[columnname].setData(columnvalue)
136 bulkOperation.processNextIteration()
137 bulkOperation.flush()
142 def deleteRows( self, tableName, condition, conditionbindDict ):
147 tableHandle = self.__schema.tableHandle(tableName)
148 editor = tableHandle.dataEditor()
149 editor.deleteRows( condition, conditionbindDict )
150 except Exception
as e:
155 Drop specified table.If associated Id table exists, drop also Id table 158 self.__schema.dropIfExistsTable( tableName )
160 except Exception
as e:
165 Drop all tables can be listed by schema.listTables 168 for t
in self.__schema.listTables():
169 self.__schema.dropTable(t)
170 except Exception
as e:
173 def createTable( self,description,withIdTable=False,withEntryTables=False,withRevMapTable=False):
175 Create table if non-existing, create Id table if required 178 tableHandle=self.__schema.createTable(description)
179 tableName=tableHandle.description().
name()
180 tableHandle.privilegeManager().grantToPublic(coral.privilege_Select)
181 if withIdTable
is True:
183 if withEntryTables
is True:
187 if withRevMapTable
is True:
189 except Exception
as e:
190 raise RuntimeError(
'dbUtil.createTable'+
str(e))
194 Tell whether table exists 197 self.__schema.tableHandle(tableName)
199 except coral.Exception
as e:
204 Create ID table for the given table.\n 205 Input: name of the table which needs new associated id table 206 Output: name of the id table created 210 if deleteOld
is True:
211 self.__schema.dropIfExistsTable(idtableName)
213 if self.__schema.existsTable(idtableName):
214 print(
'table '+idtableName+
' exists, do nothing')
216 description = coral.TableDescription()
217 description.setName( idtableName )
220 idtableHandle=self.__schema.createTable( description )
221 idtableHandle.privilegeManager().grantToPublic( coral.privilege_Select )
222 inputData = coral.AttributeList()
223 editor = idtableHandle.dataEditor()
224 editor.rowBuffer( inputData )
226 editor.insertRow( inputData )
227 except Exception
as e:
228 raise RuntimeError(
'dbUtil.createIDTable'+
str(e))
232 Create Entry table for the given table.\n 233 Input: name of the table which needs new associated id table 234 Output: name of the id table created 238 if deleteOld
is True:
239 self.__schema.dropIfExistsTable(entrytableName)
241 if self.__schema.existsTable(entrytableName):
242 print(
'table '+entrytableName+
' exists, do nothing')
244 description = coral.TableDescription()
245 description.setName( entrytableName )
246 description.insertColumn(
'ENTRY_ID' ,
'unsigned long long')
247 description.insertColumn(
'REVISION_ID' ,
'unsigned long long')
248 description.insertColumn(
'NAME' ,
'string',56,
False)
249 tableHandle=self.__schema.createTable( description )
250 tableHandle.privilegeManager().grantToPublic(coral.privilege_Select)
251 except Exception
as e:
252 raise RuntimeError(
' dbUtil.createEntryTable '+
str(e))
256 Create Rev table for the given table.\n 257 Input: name of the table 258 Output: name of the id table 262 if deleteOld
is True:
263 self.__schema.dropIfExistsTable(revmaptableName)
265 if self.__schema.existsTable(revmaptableName):
266 print(
'table '+revmaptableName+
' exists, do nothing')
268 description = coral.TableDescription()
269 description.setName( revmaptableName )
270 description.insertColumn(
'DATA_ID',
'unsigned long long')
271 description.insertColumn(
'REVISION_ID' ,
'unsigned long long')
272 tableHandle=self.__schema.createTable( description )
273 tableHandle.privilegeManager().grantToPublic(coral.privilege_Select)
274 except Exception
as e:
275 raise RuntimeError(
' dbUtil.createRevMapTable '+
str(e))
277 if __name__ ==
"__main__":
def listIndex(self, tablename)
def createRevMapTable(self, tableName, deleteOld=True)
def bulkInsert(self, tableName, tabrowDef, bulkinput)
S & print(S &os, JobReport::InputFile const &f)
def tableExists(self, tableName)
def dropTable(self, tableName)
def createIDTable(self, tableName, deleteOld=True)
def entryTableName(dataTableName)
def __init__(self, schema)
def existRow(self, tableName, condition, conditionDefDict, conditionDict)
def revmapTableName(dataTableName)
def idTableColumnDefinition()
def singleUpdate(self, tableName, setClause, updateCondition, inputData)
def deleteRows(self, tableName, condition, conditionbindDict)
def updateRows(self, tableName, updateAction, updateCondition, bindvarDef, bulkinput)
def idTableName(dataTableName)
def createTable(self, description, withIdTable=False, withEntryTables=False, withRevMapTable=False)
def insertOneRow(self, tableName, tabrowDefDict, tabrowValueDict)
def createEntryTable(self, tableName, deleteOld=True)