CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DBImpl.py
Go to the documentation of this file.
1 import coral
2 import IdGenerator
3 
4 class DBImpl(object):
5  """Class wrap up all the database operations.\n
6  """
7  def __init__( self , schema):
8  """Input: coral schema handle.
9  """
10  self.__schema = schema
11  def existRow( self, tableName, condition, conditionbindDict):
12  """Return true if one row fulfills the selection criteria
13  """
14  try:
15  tableHandle = self.__schema.tableHandle(tableName)
16  query = tableHandle.newQuery()
17  query.setCondition(condition,conditionbindDict)
18  cursor = query.execute()
19  result=False
20  while ( cursor.next() ):
21  result=True
22  cursor.close()
23  del query
24  return result
25  except Exception, e:
26  raise Exception, str(e)
27  def insertOneRow( self, tableName, tabrowDefDict, tabrowValueDict ):
28  """Insert row
29  """
30  try:
31  tableHandle = self.__schema.tableHandle(tableName)
32  editor = tableHandle.dataEditor()
33  inputData = coral.AttributeList()
34  for name,type in tabrowDefDict.items():
35  # print name, type
36  inputData.extend( name, type )
37  inputData[name].setData(tabrowValueDict[name])
38  editor.insertRow( inputData )
39  except Exception, e:
40  raise Exception, str(e)
41  def bulkInsert( self, tableName, tabrowDefDict, bulkinput):
42  """Bulk insert bulkinput=[{}]
43  """
44  try:
45  dataEditor=self.__schema.tableHandle(tableName).dataEditor()
46  insertdata=coral.AttributeList()
47  for (columnname,columntype) in tabrowDefDict.items():
48  insertdata.extend(columnname,columntype)
49 
50  bulkOperation=dataEditor.bulkInsert(insertdata,len(bulkinput))
51  for valuedict in bulkinput:
52  for (columnname,columnvalue) in valuedict.items():
53  insertdata[columnname].setData(columnvalue)
54  bulkOperation.processNextIteration()
55  bulkOperation.flush()
56  del bulkOperation
57  except Exception, e:
58  raise Exception, str(e)
59  def deleteRows( self, tableName, condition, conditionbindDict ):
60  """Delete row(s)
61  """
62  try:
63  tableHandle = self.__schema.tableHandle(tableName)
64  editor = tableHandle.dataEditor()
65  editor.deleteRows( condition, conditionbindDict )
66  except Exception, e:
67  raise Exception, str(e)
68 
69  def dropTable( self, tableName ):
70  """Drop specified table.
71  """
72  self.__schema.dropIfExistsTable( tableName )
73  def tableExists( self, tableName ):
74  """Tell whether table exists
75  """
76  try:
77  self.__schema.tableHandle(tableName)
78  return True
79  except coral.Exception, e:
80  return False
81 
82 if __name__ == "__main__":
83  pass
list object
Definition: dbtoconf.py:77