2 from RecoLuminosity.LumiDB
import nameDealer
4 """Class wrap up all the database operations.\n
8 Input: coral schema handle.
13 mytable=self.__schema.tableHandle(tablename)
14 print 'numberofindices ', mytable.description().numberOfIndices()
15 for i
in range(0,mytable.description().numberOfIndices()):
16 index=mytable.description().
index(i)
17 print ' ', index.name(),
' -> '
18 for iColumn
in index.columnNames():
20 print ' (tablespace : ',index.tableSpaceName(),
')'
23 Print out the overview of the schema
26 tablelist=self.__schema.listTables()
28 table = self.__schema.tableHandle(t)
29 print 'table ===',t,
'==='
30 n=table.description().numberOfColumns()
32 columndesp=table.description().columnDescription(i)
33 print '\t',columndesp.name(),columndesp.type()
34 if table.description().hasPrimaryKey():
35 print 'Primary Key : '
36 print '\t',table.description().primaryKey().columnNames()
39 viewlist=self.__schema.listViews()
41 myview = self.__schema.viewHandle(
'V0')
42 print 'definition : ',myview.definition()
43 n=myview.numberOfColumns()
45 columndesp=view.column(i)
46 print '\t',columndesp.name(),columndesp.type()
48 raise Exception, str(e)
49 def existRow( self, tableName, condition, conditionDefDict,conditionDict):
51 Return true if one row fulfills the selection criteria
54 tableHandle = self.__schema.tableHandle(tableName)
55 query = tableHandle.newQuery()
56 queryBind=coral.AttributeList()
57 for colname,coltype
in conditionDefDict.items():
58 queryBind.extend(colname,coltype)
59 queryBind[colname].setData(conditionDict[colname])
60 query.setCondition(condition,queryBind)
61 cursor = query.execute()
63 while ( cursor.next() ):
69 raise Exception, str(e)
70 def insertOneRow( self, tableName, tabrowDefDict, tabrowValueDict ):
75 tableHandle = self.__schema.tableHandle(tableName)
76 editor = tableHandle.dataEditor()
77 inputData = coral.AttributeList()
78 for name,type
in tabrowDefDict.items():
80 inputData.extend( name, type )
81 inputData[name].setData(tabrowValueDict[name])
82 editor.insertRow( inputData )
84 raise Exception,
'dbUtil.insertOneRow:'+str(e)
86 def singleUpdate( self,tableName,setClause,updateCondition,inputData):
88 dataEditor=self.__schema.tableHandle(tableName).dataEditor()
89 n=dataEditor.updateRows(setClause,updateCondition,inputData)
92 raise RuntimeError(
'dbUtil.updateOneRow:'+str(e))
94 def updateRows( self,tableName,updateAction,updateCondition,bindvarDef,bulkinput):
96 update rows, note update must be ordered
99 updateAction,string e.g. flag=:newflag
100 conditionstring, string ,e.g. runnum=:runnum and cmslsnum=:cmslsnum
101 bindvarDef,[('newflag','string'),('runnum','unsigned int'),('cmslsnum','unsigned int')]
102 bulkinput,[[('newflag','GOOD'),('runnum',1234),('cmslsnum',1)],[]]
105 dataEditor=self.__schema.tableHandle(tableName).dataEditor()
106 updateData=coral.AttributeList()
107 for (columnname,columntype)
in bindvarDef:
108 updateData.extend(columnname,columntype)
109 bulkOperation=dataEditor.bulkUpdateRows(updateAction,updateCondition,updateData,len(bulkinput))
110 for valuelist
in bulkinput:
111 for (columnname,columnvalue)
in valuelist:
112 updateData[columnname].setData(columnvalue)
113 bulkOperation.processNextIteration()
114 bulkOperation.flush()
117 raise Exception,
'dbUtil.updateRows:'+str(e)
122 tabrowDef,[('RUNNUM':'unsigned int'),('CMSLSNUM','unsigned int'),('FLAG','string'),('COMMENT','string')]
123 bulkinput,[[('RUNNUM',1234),('CMSLSNUM',1234),('FLAG','GOOD'),('COMMENT','coment')],[]]
126 dataEditor=self.__schema.tableHandle(tableName).dataEditor()
127 insertdata=coral.AttributeList()
128 for (columnname,columntype)
in tabrowDef:
129 insertdata.extend(columnname,columntype)
130 bulkOperation=dataEditor.bulkInsert(insertdata,len(bulkinput))
131 for valuelist
in bulkinput:
132 for (columnname,columnvalue)
in valuelist:
134 insertdata[columnname].setData(columnvalue)
135 bulkOperation.processNextIteration()
136 bulkOperation.flush()
141 def deleteRows( self, tableName, condition, conditionbindDict ):
146 tableHandle = self.__schema.tableHandle(tableName)
147 editor = tableHandle.dataEditor()
148 editor.deleteRows( condition, conditionbindDict )
150 raise Exception, str(e)
154 Drop specified table.If associated Id table exists, drop also Id table
157 self.__schema.dropIfExistsTable( tableName )
160 raise Exception, str(e)
164 Drop all tables can be listed by schema.listTables
167 for t
in self.__schema.listTables():
168 self.__schema.dropTable(t)
170 raise Exception, str(e)
172 def createTable( self,description,withIdTable=False,withEntryTables=False,withRevMapTable=False):
174 Create table if non-existing, create Id table if required
177 tableHandle=self.__schema.createTable(description)
178 tableName=tableHandle.description().
name()
179 tableHandle.privilegeManager().grantToPublic(coral.privilege_Select)
180 if withIdTable
is True:
182 if withEntryTables
is True:
186 if withRevMapTable
is True:
189 raise RuntimeError(
'dbUtil.createTable'+str(e))
193 Tell whether table exists
196 self.__schema.tableHandle(tableName)
198 except coral.Exception, e:
203 Create ID table for the given table.\n
204 Input: name of the table which needs new associated id table
205 Output: name of the id table created
209 if deleteOld
is True:
210 self.__schema.dropIfExistsTable(idtableName)
212 if self.__schema.existsTable(idtableName):
213 print 'table '+idtableName+
' exists, do nothing'
215 description = coral.TableDescription()
216 description.setName( idtableName )
219 idtableHandle=self.__schema.createTable( description )
220 idtableHandle.privilegeManager().grantToPublic( coral.privilege_Select )
221 inputData = coral.AttributeList()
222 editor = idtableHandle.dataEditor()
223 editor.rowBuffer( inputData )
225 editor.insertRow( inputData )
227 raise RuntimeError(
'dbUtil.createIDTable'+str(e))
231 Create Entry table for the given table.\n
232 Input: name of the table which needs new associated id table
233 Output: name of the id table created
237 if deleteOld
is True:
238 self.__schema.dropIfExistsTable(entrytableName)
240 if self.__schema.existsTable(entrytableName):
241 print 'table '+entrytableName+
' exists, do nothing'
243 description = coral.TableDescription()
244 description.setName( entrytableName )
245 description.insertColumn(
'ENTRY_ID' ,
'unsigned long long')
246 description.insertColumn(
'REVISION_ID' ,
'unsigned long long')
247 description.insertColumn(
'NAME' ,
'string',56,
False)
248 tableHandle=self.__schema.createTable( description )
249 tableHandle.privilegeManager().grantToPublic(coral.privilege_Select)
251 raise RuntimeError(
' dbUtil.createEntryTable '+str(e))
255 Create Rev table for the given table.\n
256 Input: name of the table
257 Output: name of the id table
261 if deleteOld
is True:
262 self.__schema.dropIfExistsTable(revmaptableName)
264 if self.__schema.existsTable(revmaptableName):
265 print 'table '+revmaptableName+
' exists, do nothing'
267 description = coral.TableDescription()
268 description.setName( revmaptableName )
269 description.insertColumn(
'DATA_ID',
'unsigned long long')
270 description.insertColumn(
'REVISION_ID' ,
'unsigned long long')
271 tableHandle=self.__schema.createTable( description )
272 tableHandle.privilegeManager().grantToPublic(coral.privilege_Select)
274 raise RuntimeError(
' dbUtil.createRevMapTable '+str(e))
276 if __name__ ==
"__main__":
def idTableColumnDefinition