CMS 3D CMS Logo

idDealer.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import coral
3 import nameDealer
4 class idDealer(object):
5  """Manages the autoincremental ID values.\n
6  Input: coral.schema object
7  """
8  def __init__( self , schema ):
9  self.__schema = schema
12 
13  def getIDColumnDefinition( self ):
14  return (self.__idTableColumnName, self.__idTableColumnType)
15 
16  def getIDforTable( self, tableName ):
17  """
18  get the new id value to use for the given table
19  """
20  try:
21  idtableName = nameDealer.idTableName(tableName)
22  query = self.__schema.tableHandle(idtableName).newQuery()
23  query.addToOutputList(self.__idTableColumnName)
24  query.setForUpdate() #lock it
25  cursor = query.execute()
26  result = 0
27  while ( next(cursor) ):
28  result = cursor.currentRow()[self.__idTableColumnName].data()
29  del query
30  return result
31  except Exception as e:
32  raise Exception(str(e))
33 
34  def generateNextIDForTable( self, tableName ):
35  """
36  Set the nextID in the IDTableName to current id value + 1 .\n
37  Input: ID table name.
38  """
39  try:
40  idtableName = nameDealer.idTableName(tableName)
41  tableHandle = self.__schema.tableHandle(idtableName)
42  query = tableHandle.newQuery()
43  query.addToOutputList(self.__idTableColumnName)
44  query.setForUpdate() #lock it
45  cursor = query.execute()
46  result = 0
47  while ( next(cursor) ):
48  result = cursor.currentRow()[0].data()
49  dataEditor = tableHandle.dataEditor()
50  inputData = coral.AttributeList()
51  dataEditor.updateRows('NEXTID = NEXTID+1','',inputData)
52  del query
53  return result+1
54  except Exception as e:
55  raise Exception(str(e))
56 
57 if __name__ == "__main__":
58  fakeIDtableName='Fake_ID'
59  svc=coral.ConnectionService()
60  session=svc.connect('sqlite_file:fake.db')
61  transaction=session.transaction()
62  try:
63  transaction.start(False)
64  schema=session.nominalSchema()
65  idor=idDealer(schema)
66  if schema.existsTable(fakeIDtableName) is False:
67  description=coral.TableDescription()
68  description.setName(fakeIDtableName)
69  description.setPrimaryKey(idor.getIDColumnDefinition()[0])
70  description.insertColumn(idor.getIDColumnDefinition()[0],idor.getIDColumnDefinition()[1])
71  idtableHandle=schema.createTable(description)
72  idtableHandle.privilegeManager().grantToPublic(coral.privilege_Select)
73  inputData=coral.AttributeList()
74  editor=idtableHandle.dataEditor()
75  editor.rowBuffer(inputData)
76  inputData[ idor.getIDColumnDefinition()[0] ].setData(0)
77  editor.insertRow(inputData)
78  idor.generateNextIDForTable('Fake')
79  print(idor.getIDforTable('Fake'))
80  transaction.commit()
81  del session
82  except coral.Exception as e:
83  transaction.rollback()
84  del session
85  except Exception as e:
86  print('failed in unit test')
87  print(str(e))
88  del session
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def __init__(self, schema)
Definition: idDealer.py:8
def getIDforTable(self, tableName)
Definition: idDealer.py:16
def idTableColumnDefinition()
Definition: nameDealer.py:97
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
def getIDColumnDefinition(self)
Definition: idDealer.py:13
def idTableName(dataTableName)
Definition: nameDealer.py:94
#define str(s)
def generateNextIDForTable(self, tableName)
Definition: idDealer.py:34