CMS 3D CMS Logo

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