1 from __future__
import print_function
2 from builtins
import range
4 from RecoLuminosity.LumiDB
import nameDealer
6 """Class wrap up all the database operations.\n 10 Input: coral schema handle. 15 mytable=self.__schema.tableHandle(tablename)
16 print(
'numberofindices ', mytable.description().numberOfIndices())
17 for i
in range(0,mytable.description().numberOfIndices()):
18 index=mytable.description().
index(i)
19 print(
' ', index.name(),
' -> ')
20 for iColumn
in index.columnNames():
22 print(
' (tablespace : ',index.tableSpaceName(),
')')
25 Print out the overview of the schema 28 tablelist=self.__schema.listTables()
30 table = self.__schema.tableHandle(t)
31 print(
'table ===',t,
'===')
32 n=table.description().numberOfColumns()
34 columndesp=table.description().columnDescription(i)
35 print(
'\t',columndesp.name(),columndesp.type())
36 if table.description().hasPrimaryKey():
37 print(
'Primary Key : ')
38 print(
'\t',table.description().primaryKey().columnNames())
41 viewlist=self.__schema.listViews()
43 myview = self.__schema.viewHandle(
'V0')
44 print(
'definition : ',myview.definition())
45 n=myview.numberOfColumns()
47 columndesp=view.column(i)
48 print(
'\t',columndesp.name(),columndesp.type())
49 except Exception
as e:
51 def existRow( self, tableName, condition, conditionDefDict,conditionDict):
53 Return true if one row fulfills the selection criteria 56 tableHandle = self.__schema.tableHandle(tableName)
57 query = tableHandle.newQuery()
58 queryBind=coral.AttributeList()
59 for colname,coltype
in conditionDefDict.items():
60 queryBind.extend(colname,coltype)
61 queryBind[colname].setData(conditionDict[colname])
62 query.setCondition(condition,queryBind)
63 cursor = query.execute()
65 while (
next(cursor) ):
70 except Exception
as e:
72 def insertOneRow( self, tableName, tabrowDefDict, tabrowValueDict ):
77 tableHandle = self.__schema.tableHandle(tableName)
78 editor = tableHandle.dataEditor()
79 inputData = coral.AttributeList()
80 for name,type
in tabrowDefDict.items():
82 inputData.extend( name, type )
83 inputData[name].setData(tabrowValueDict[name])
84 editor.insertRow( inputData )
85 except Exception
as e:
88 def singleUpdate( self,tableName,setClause,updateCondition,inputData):
90 dataEditor=self.__schema.tableHandle(tableName).dataEditor()
91 n=dataEditor.updateRows(setClause,updateCondition,inputData)
93 except Exception
as e:
94 raise RuntimeError(
'dbUtil.updateOneRow:'+
str(e))
96 def updateRows( self,tableName,updateAction,updateCondition,bindvarDef,bulkinput):
98 update rows, note update must be ordered 101 updateAction,string e.g. flag=:newflag 102 conditionstring, string ,e.g. runnum=:runnum and cmslsnum=:cmslsnum 103 bindvarDef,[('newflag','string'),('runnum','unsigned int'),('cmslsnum','unsigned int')] 104 bulkinput,[[('newflag','GOOD'),('runnum',1234),('cmslsnum',1)],[]] 107 dataEditor=self.__schema.tableHandle(tableName).dataEditor()
108 updateData=coral.AttributeList()
109 for (columnname,columntype)
in bindvarDef:
110 updateData.extend(columnname,columntype)
111 bulkOperation=dataEditor.bulkUpdateRows(updateAction,updateCondition,updateData,len(bulkinput))
112 for valuelist
in bulkinput:
113 for (columnname,columnvalue)
in valuelist:
114 updateData[columnname].setData(columnvalue)
115 bulkOperation.processNextIteration()
116 bulkOperation.flush()
118 except Exception
as e:
124 tabrowDef,[('RUNNUM':'unsigned int'),('CMSLSNUM','unsigned int'),('FLAG','string'),('COMMENT','string')] 125 bulkinput,[[('RUNNUM',1234),('CMSLSNUM',1234),('FLAG','GOOD'),('COMMENT','coment')],[]] 128 dataEditor=self.__schema.tableHandle(tableName).dataEditor()
129 insertdata=coral.AttributeList()
130 for (columnname,columntype)
in tabrowDef:
131 insertdata.extend(columnname,columntype)
132 bulkOperation=dataEditor.bulkInsert(insertdata,len(bulkinput))
133 for valuelist
in bulkinput:
134 for (columnname,columnvalue)
in valuelist:
136 insertdata[columnname].setData(columnvalue)
137 bulkOperation.processNextIteration()
138 bulkOperation.flush()
143 def deleteRows( self, tableName, condition, conditionbindDict ):
148 tableHandle = self.__schema.tableHandle(tableName)
149 editor = tableHandle.dataEditor()
150 editor.deleteRows( condition, conditionbindDict )
151 except Exception
as e:
156 Drop specified table.If associated Id table exists, drop also Id table 159 self.__schema.dropIfExistsTable( tableName )
161 except Exception
as e:
166 Drop all tables can be listed by schema.listTables 169 for t
in self.__schema.listTables():
170 self.__schema.dropTable(t)
171 except Exception
as e:
174 def createTable( self,description,withIdTable=False,withEntryTables=False,withRevMapTable=False):
176 Create table if non-existing, create Id table if required 179 tableHandle=self.__schema.createTable(description)
180 tableName=tableHandle.description().
name()
181 tableHandle.privilegeManager().grantToPublic(coral.privilege_Select)
182 if withIdTable
is True:
184 if withEntryTables
is True:
188 if withRevMapTable
is True:
190 except Exception
as e:
191 raise RuntimeError(
'dbUtil.createTable'+
str(e))
195 Tell whether table exists 198 self.__schema.tableHandle(tableName)
200 except coral.Exception
as e:
205 Create ID table for the given table.\n 206 Input: name of the table which needs new associated id table 207 Output: name of the id table created 211 if deleteOld
is True:
212 self.__schema.dropIfExistsTable(idtableName)
214 if self.__schema.existsTable(idtableName):
215 print(
'table '+idtableName+
' exists, do nothing')
217 description = coral.TableDescription()
218 description.setName( idtableName )
221 idtableHandle=self.__schema.createTable( description )
222 idtableHandle.privilegeManager().grantToPublic( coral.privilege_Select )
223 inputData = coral.AttributeList()
224 editor = idtableHandle.dataEditor()
225 editor.rowBuffer( inputData )
227 editor.insertRow( inputData )
228 except Exception
as e:
229 raise RuntimeError(
'dbUtil.createIDTable'+
str(e))
233 Create Entry table for the given table.\n 234 Input: name of the table which needs new associated id table 235 Output: name of the id table created 239 if deleteOld
is True:
240 self.__schema.dropIfExistsTable(entrytableName)
242 if self.__schema.existsTable(entrytableName):
243 print(
'table '+entrytableName+
' exists, do nothing')
245 description = coral.TableDescription()
246 description.setName( entrytableName )
247 description.insertColumn(
'ENTRY_ID' ,
'unsigned long long')
248 description.insertColumn(
'REVISION_ID' ,
'unsigned long long')
249 description.insertColumn(
'NAME' ,
'string',56,
False)
250 tableHandle=self.__schema.createTable( description )
251 tableHandle.privilegeManager().grantToPublic(coral.privilege_Select)
252 except Exception
as e:
253 raise RuntimeError(
' dbUtil.createEntryTable '+
str(e))
257 Create Rev table for the given table.\n 258 Input: name of the table 259 Output: name of the id table 263 if deleteOld
is True:
264 self.__schema.dropIfExistsTable(revmaptableName)
266 if self.__schema.existsTable(revmaptableName):
267 print(
'table '+revmaptableName+
' exists, do nothing')
269 description = coral.TableDescription()
270 description.setName( revmaptableName )
271 description.insertColumn(
'DATA_ID',
'unsigned long long')
272 description.insertColumn(
'REVISION_ID' ,
'unsigned long long')
273 tableHandle=self.__schema.createTable( description )
274 tableHandle.privilegeManager().grantToPublic(coral.privilege_Select)
275 except Exception
as e:
276 raise RuntimeError(
' dbUtil.createRevMapTable '+
str(e))
278 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)