CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
dbUtil.dbUtil Class Reference
Inheritance diagram for dbUtil.dbUtil:

Public Member Functions

def __init__ (self, schema)
 
def bulkInsert (self, tableName, tabrowDef, bulkinput)
 
def createEntryTable (self, tableName, deleteOld=True)
 
def createIDTable (self, tableName, deleteOld=True)
 
def createRevMapTable (self, tableName, deleteOld=True)
 
def createTable (self, description, withIdTable=False, withEntryTables=False, withRevMapTable=False)
 
def deleteRows (self, tableName, condition, conditionbindDict)
 
def describeSchema (self)
 
def dropAllTables (self)
 
def dropTable (self, tableName)
 
def existRow (self, tableName, condition, conditionDefDict, conditionDict)
 
def insertOneRow (self, tableName, tabrowDefDict, tabrowValueDict)
 
def listIndex (self, tablename)
 
def singleUpdate (self, tableName, setClause, updateCondition, inputData)
 
def tableExists (self, tableName)
 
def updateRows (self, tableName, updateAction, updateCondition, bindvarDef, bulkinput)
 

Private Attributes

 __schema
 

Detailed Description

Class wrap up all the database operations.\n

Definition at line 4 of file dbUtil.py.

Constructor & Destructor Documentation

def dbUtil.dbUtil.__init__ (   self,
  schema 
)
Input: coral schema handle.

Definition at line 7 of file dbUtil.py.

7  def __init__( self , schema):
8  """
9  Input: coral schema handle.
10  """
11  self.__schema = schema
12 
def __init__(self, schema)
Definition: dbUtil.py:7

Member Function Documentation

def dbUtil.dbUtil.bulkInsert (   self,
  tableName,
  tabrowDef,
  bulkinput 
)
input:
   tableName, string
   tabrowDef,[('RUNNUM':'unsigned int'),('CMSLSNUM','unsigned int'),('FLAG','string'),('COMMENT','string')]
   bulkinput,[[('RUNNUM',1234),('CMSLSNUM',1234),('FLAG','GOOD'),('COMMENT','coment')],[]]

Definition at line 119 of file dbUtil.py.

119  def bulkInsert( self, tableName, tabrowDef, bulkinput):
120  """
121  input:
122  tableName, string
123  tabrowDef,[('RUNNUM':'unsigned int'),('CMSLSNUM','unsigned int'),('FLAG','string'),('COMMENT','string')]
124  bulkinput,[[('RUNNUM',1234),('CMSLSNUM',1234),('FLAG','GOOD'),('COMMENT','coment')],[]]
125  """
126  try:
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:
134 # print 'columnname ',columnname,' columnvalue ',columnvalue
135  insertdata[columnname].setData(columnvalue)
136  bulkOperation.processNextIteration()
137  bulkOperation.flush()
138  del bulkOperation
139  except:
140  raise
141 
def bulkInsert(self, tableName, tabrowDef, bulkinput)
Definition: dbUtil.py:119
def dbUtil.dbUtil.createEntryTable (   self,
  tableName,
  deleteOld = True 
)
Create Entry table  for the given table.\n
Input: name of the table which needs new associated id table
Output: name of the id table created

Definition at line 230 of file dbUtil.py.

References nameDealer.entryTableName(), edm.print(), and str.

Referenced by dbUtil.dbUtil.createTable().

230  def createEntryTable( self, tableName, deleteOld=True ):
231  """
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
235  """
236  try:
237  entrytableName=nameDealer.entryTableName(tableName)
238  if deleteOld is True:
239  self.__schema.dropIfExistsTable(entrytableName)
240  else:
241  if self.__schema.existsTable(entrytableName):
242  print('table '+entrytableName+' exists, do nothing')
243  return
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))
253 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def entryTableName(dataTableName)
Definition: nameDealer.py:103
def createEntryTable(self, tableName, deleteOld=True)
Definition: dbUtil.py:230
#define str(s)
def dbUtil.dbUtil.createIDTable (   self,
  tableName,
  deleteOld = True 
)
Create ID table  for the given table.\n
Input: name of the table which needs new associated id table
Output: name of the id table created

Definition at line 202 of file dbUtil.py.

References nameDealer.idTableColumnDefinition(), nameDealer.idTableName(), edm.print(), and str.

Referenced by dbUtil.dbUtil.createTable().

202  def createIDTable( self, tableName, deleteOld=True ):
203  """
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
207  """
208  try:
209  idtableName=nameDealer.idTableName(tableName)
210  if deleteOld is True:
211  self.__schema.dropIfExistsTable(idtableName)
212  else:
213  if self.__schema.existsTable(idtableName):
214  print('table '+idtableName+' exists, do nothing')
215  return
216  description = coral.TableDescription()
217  description.setName( idtableName )
218  description.setPrimaryKey( nameDealer.idTableColumnDefinition()[0] )
219  description.insertColumn( nameDealer.idTableColumnDefinition()[0], nameDealer.idTableColumnDefinition()[1])
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 )
225  inputData[ nameDealer.idTableColumnDefinition()[0] ].setData(0)
226  editor.insertRow( inputData )
227  except Exception as e:
228  raise RuntimeError('dbUtil.createIDTable'+str(e))
229 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def createIDTable(self, tableName, deleteOld=True)
Definition: dbUtil.py:202
def idTableColumnDefinition()
Definition: nameDealer.py:97
def idTableName(dataTableName)
Definition: nameDealer.py:94
#define str(s)
def dbUtil.dbUtil.createRevMapTable (   self,
  tableName,
  deleteOld = True 
)
Create Rev table  for the given table.\n
Input: name of the table
Output: name of the id table 

Definition at line 254 of file dbUtil.py.

References edm.print(), nameDealer.revmapTableName(), and str.

Referenced by dbUtil.dbUtil.createTable().

254  def createRevMapTable( self, tableName, deleteOld=True ):
255  """
256  Create Rev table for the given table.\n
257  Input: name of the table
258  Output: name of the id table
259  """
260  try:
261  revmaptableName=nameDealer.revmapTableName(tableName)
262  if deleteOld is True:
263  self.__schema.dropIfExistsTable(revmaptableName)
264  else:
265  if self.__schema.existsTable(revmaptableName):
266  print('table '+revmaptableName+' exists, do nothing')
267  return
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))
276 
def createRevMapTable(self, tableName, deleteOld=True)
Definition: dbUtil.py:254
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def revmapTableName(dataTableName)
Definition: nameDealer.py:100
#define str(s)
def dbUtil.dbUtil.createTable (   self,
  description,
  withIdTable = False,
  withEntryTables = False,
  withRevMapTable = False 
)
Create table if non-existing, create Id table if required

Definition at line 173 of file dbUtil.py.

References dbUtil.dbUtil.createEntryTable(), dbUtil.dbUtil.createIDTable(), dbUtil.dbUtil.createRevMapTable(), nameDealer.entryTableName(), dataset.name, and str.

173  def createTable( self,description,withIdTable=False,withEntryTables=False,withRevMapTable=False):
174  """
175  Create table if non-existing, create Id table if required
176  """
177  try:
178  tableHandle=self.__schema.createTable(description)
179  tableName=tableHandle.description().name()
180  tableHandle.privilegeManager().grantToPublic(coral.privilege_Select)
181  if withIdTable is True:
182  self.createIDTable(tableName,True)
183  if withEntryTables is True:
184  entrytableName=nameDealer.entryTableName(tableName)
185  self.createEntryTable(tableName,True)
186  self.createIDTable(entrytableName,True)
187  if withRevMapTable is True:
188  self.createRevMapTable(tableName,True)
189  except Exception as e:
190  raise RuntimeError('dbUtil.createTable'+str(e))
191 
def createRevMapTable(self, tableName, deleteOld=True)
Definition: dbUtil.py:254
def createIDTable(self, tableName, deleteOld=True)
Definition: dbUtil.py:202
def entryTableName(dataTableName)
Definition: nameDealer.py:103
def createTable(self, description, withIdTable=False, withEntryTables=False, withRevMapTable=False)
Definition: dbUtil.py:173
def createEntryTable(self, tableName, deleteOld=True)
Definition: dbUtil.py:230
#define str(s)
def dbUtil.dbUtil.deleteRows (   self,
  tableName,
  condition,
  conditionbindDict 
)
Delete row(s)

Definition at line 142 of file dbUtil.py.

References str.

142  def deleteRows( self, tableName, condition, conditionbindDict ):
143  """
144  Delete row(s)
145  """
146  try:
147  tableHandle = self.__schema.tableHandle(tableName)
148  editor = tableHandle.dataEditor()
149  editor.deleteRows( condition, conditionbindDict )
150  except Exception as e:
151  raise Exception(str(e))
152 
def deleteRows(self, tableName, condition, conditionbindDict)
Definition: dbUtil.py:142
#define str(s)
def dbUtil.dbUtil.describeSchema (   self)
Print out the overview of the schema

Definition at line 22 of file dbUtil.py.

References dbUtil.dbUtil.listIndex(), edm.print(), and str.

22  def describeSchema(self):
23  """
24  Print out the overview of the schema
25  """
26  try:
27  tablelist=self.__schema.listTables()
28  for t in tablelist:
29  table = self.__schema.tableHandle(t)
30  print('table ===',t,'===')
31  n=table.description().numberOfColumns()
32  for i in range(0,n):
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())
38  print('Indices : ')
39  self.listIndex(t)
40  viewlist=self.__schema.listViews()
41  for v in viewlist:
42  myview = self.__schema.viewHandle('V0')
43  print('definition : ',myview.definition())
44  n=myview.numberOfColumns()
45  for i in range(0,n):
46  columndesp=view.column(i)
47  print('\t',columndesp.name(),columndesp.type())
48  except Exception as e:
49  raise Exception(str(e))
def listIndex(self, tablename)
Definition: dbUtil.py:13
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def describeSchema(self)
Definition: dbUtil.py:22
#define str(s)
def dbUtil.dbUtil.dropAllTables (   self)
Drop all tables can be listed by schema.listTables

Definition at line 163 of file dbUtil.py.

References str.

163  def dropAllTables( self ):
164  """
165  Drop all tables can be listed by schema.listTables
166  """
167  try:
168  for t in self.__schema.listTables():
169  self.__schema.dropTable(t)
170  except Exception as e:
171  raise Exception(str(e))
172 
def dropAllTables(self)
Definition: dbUtil.py:163
#define str(s)
def dbUtil.dbUtil.dropTable (   self,
  tableName 
)
Drop specified table.If associated Id table exists, drop also Id table

Definition at line 153 of file dbUtil.py.

References nameDealer.idTableName(), and str.

153  def dropTable( self, tableName ):
154  """
155  Drop specified table.If associated Id table exists, drop also Id table
156  """
157  try:
158  self.__schema.dropIfExistsTable( tableName )
159  self.__schema.dropIfExistsTable( nameDealer.idTableName(tableName) )
160  except Exception as e:
161  raise Exception(str(e))
162 
def dropTable(self, tableName)
Definition: dbUtil.py:153
def idTableName(dataTableName)
Definition: nameDealer.py:94
#define str(s)
def dbUtil.dbUtil.existRow (   self,
  tableName,
  condition,
  conditionDefDict,
  conditionDict 
)
Return true if one row fulfills the selection criteria

Definition at line 50 of file dbUtil.py.

References GetRecoTauVFromDQM_MC_cff.next, and str.

50  def existRow( self, tableName, condition, conditionDefDict,conditionDict):
51  """
52  Return true if one row fulfills the selection criteria
53  """
54  try:
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()
63  result=False
64  while ( next(cursor) ):
65  result=True
66  cursor.close()
67  del query
68  return result
69  except Exception as e:
70  raise Exception(str(e))
def existRow(self, tableName, condition, conditionDefDict, conditionDict)
Definition: dbUtil.py:50
#define str(s)
def dbUtil.dbUtil.insertOneRow (   self,
  tableName,
  tabrowDefDict,
  tabrowValueDict 
)
Insert row 

Definition at line 71 of file dbUtil.py.

References str.

71  def insertOneRow( self, tableName, tabrowDefDict, tabrowValueDict ):
72  """
73  Insert row
74  """
75  try:
76  tableHandle = self.__schema.tableHandle(tableName)
77  editor = tableHandle.dataEditor()
78  inputData = coral.AttributeList()
79  for name,type in tabrowDefDict.items():
80  # print name, type
81  inputData.extend( name, type )
82  inputData[name].setData(tabrowValueDict[name])
83  editor.insertRow( inputData )
84  except Exception as e:
85  raise Exception('dbUtil.insertOneRow:'+str(e))
86 
def insertOneRow(self, tableName, tabrowDefDict, tabrowValueDict)
Definition: dbUtil.py:71
#define str(s)
def dbUtil.dbUtil.listIndex (   self,
  tablename 
)

Definition at line 13 of file dbUtil.py.

References edm.print().

Referenced by dbUtil.dbUtil.describeSchema().

13  def listIndex(self,tablename):
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():
20  print(iColumn)
21  print(' (tablespace : ',index.tableSpaceName(),')')
def listIndex(self, tablename)
Definition: dbUtil.py:13
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def dbUtil.dbUtil.singleUpdate (   self,
  tableName,
  setClause,
  updateCondition,
  inputData 
)

Definition at line 87 of file dbUtil.py.

References str.

87  def singleUpdate( self,tableName,setClause,updateCondition,inputData):
88  try:
89  dataEditor=self.__schema.tableHandle(tableName).dataEditor()
90  n=dataEditor.updateRows(setClause,updateCondition,inputData)
91  return n
92  except Exception as e:
93  raise RuntimeError('dbUtil.updateOneRow:'+str(e))
94 
def singleUpdate(self, tableName, setClause, updateCondition, inputData)
Definition: dbUtil.py:87
#define str(s)
def dbUtil.dbUtil.tableExists (   self,
  tableName 
)
Tell whether table exists

Definition at line 192 of file dbUtil.py.

192  def tableExists( self,tableName ):
193  """
194  Tell whether table exists
195  """
196  try:
197  self.__schema.tableHandle(tableName)
198  return True
199  except coral.Exception as e:
200  return False
201 
def tableExists(self, tableName)
Definition: dbUtil.py:192
def dbUtil.dbUtil.updateRows (   self,
  tableName,
  updateAction,
  updateCondition,
  bindvarDef,
  bulkinput 
)
update rows, note update must be ordered
input :
   tableName, string
   updateAction,string  e.g. flag=:newflag
   conditionstring, string ,e.g. runnum=:runnum and cmslsnum=:cmslsnum
   bindvarDef,[('newflag','string'),('runnum','unsigned int'),('cmslsnum','unsigned int')]
   bulkinput,[[('newflag','GOOD'),('runnum',1234),('cmslsnum',1)],[]]

Definition at line 95 of file dbUtil.py.

References str.

95  def updateRows( self,tableName,updateAction,updateCondition,bindvarDef,bulkinput):
96  '''
97  update rows, note update must be ordered
98  input :
99  tableName, string
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)],[]]
104  '''
105  try:
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()
116  del bulkOperation
117  except Exception as e:
118  raise Exception('dbUtil.updateRows:'+str(e))
def updateRows(self, tableName, updateAction, updateCondition, bindvarDef, bulkinput)
Definition: dbUtil.py:95
#define str(s)

Member Data Documentation

dbUtil.dbUtil.__schema
private

Definition at line 11 of file dbUtil.py.