CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
IdGenerator.py
Go to the documentation of this file.
1 import coral
2 import DBImpl
4  """Manages the autoincremental ID values.\n
5  Input: coral.schema object
6  """
7  def __init__( self , schema ):
8  self.__schema = schema
9  self.__idTableColumnName = 'nextID'
10  self.__idTableColumnType = 'unsigned long'
11  def getNewID( self, IDtableName):
12  """Return the ID value in the specified ID table.\n
13  Input: ID table name
14  """
15  try:
16  query = self.__schema.tableHandle(IDtableName).newQuery()
17  query.addToOutputList(self.__idTableColumnName)
18  query.setForUpdate() #lock it
19  cursor = query.execute()
20  result = 0
21  while ( cursor.next() ):
22  result = cursor.currentRow()[self.__idTableColumnName].data()
23  del query
24  return result
25  except Exception, e:
26  raise Exception, str(e)
27  def incrementNextID( self, IDtableName ):
28  """Set the nextID in the IDTableName to current id value + 1 .\n
29  Input: ID table name.
30  """
31  try:
32  tableHandle = self.__schema.tableHandle(IDtableName)
33  query = tableHandle.newQuery()
34  query.addToOutputList(self.__idTableColumnName)
35  query.setForUpdate() #lock it
36  cursor = query.execute()
37  result = 0
38  while ( cursor.next() ):
39  result = cursor.currentRow()[0].data()
40  del query
41  dataEditor = tableHandle.dataEditor()
42  inputData = coral.AttributeList()
43  inputData.extend( 'newid', self.__idTableColumnType )
44  inputData['newid'].setData(result+1)
45  dataEditor.updateRows('nextID = :newid','',inputData)
46  except Exception, e:
47  raise Exception, str(e)
48  #def getIDTableName( self, tableName ):
49  # """Returns the ID table name associated with given table.\n
50  # No check on the existence of the table.\n
51  # Input: data table name
52  # Output: ID table name
53  # """
54  # return tableName+'_IDs'
55  def createIDTable( self, idtableName, deleteOld=True ):
56  """Create ID table 'tableName_ID' for the given table.\n
57  Input: name of the table which needs new associated id table
58  Output: name of the id table created
59  """
60  dbop=DBImpl.DBImpl(self.__schema)
61  try:
62  if dbop.tableExists(idtableName) is True:
63  if deleteOld is True:
64  dbop.dropTable(idtableName)
65  else:
66  return
67  description = coral.TableDescription();
68  description.setName(idtableName)
69  description.insertColumn(self.__idTableColumnName, self.__idTableColumnType)
70  idtableHandle=self.__schema.createTable( description )
71  idtableHandle.privilegeManager().grantToPublic( coral.privilege_Select )
72  inputData = coral.AttributeList()
73  editor = idtableHandle.dataEditor()
74  editor.rowBuffer( inputData )
75  inputData[self.__idTableColumnName].setData(1)
76  editor.insertRow( inputData )
77  except Exception, e:
78  raise Exception, str(e)
79 if __name__ == "__main__":
80  idtableName = 'TagTreeTable_IDS'
81  svc = coral.ConnectionService()
82  session = svc.connect( 'sqlite_file:data.db', accessMode = coral.access_Update )
83  transaction = session.transaction()
84  try:
85  transaction.start()
86  schema = session.nominalSchema()
87  generator=IdGenerator(schema)
88  generator.createIDTable( idtableName )
89  transaction.commit()
90  transaction.start(True)
91  result=generator.getNewID(idtableName)
92  print 'new id ',result
93  transaction.commit()
94  transaction.start(False)
95  generator.incrementNextID(idtableName)
96  print 'new id ',generator.getNewID(idtableName)
97  transaction.commit()
98  del session
99  except coral.Exception, e:
100  transaction.rollback()
101  print str(e)
102  del session
103  except Exception, e:
104  print "Failed in unit test"
105  print str(e)
106  del session
list object
Definition: dbtoconf.py:77
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82