CMS 3D CMS Logo

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