CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
entryComment.py
Go to the documentation of this file.
1 import coral
2 import DBImpl
3 import CommonUtils
4 
6  """Class add optional comment on given entry in a given table\n
7  """
8  def __init__( self, session ):
9  """Input: coral schema handle.
10  """
11  self.__session = session
12  self.__entryCommentTableColumns={'entryid':'unsigned long','tablename':'string','comment':'string'}
13  self.__entryCommentTableNotNullColumns=['entryid','tablename']
14  self.__entryCommentTablePK=('entryid','tablename')
15 
16  def existCommentTable(self):
17  """Check if entry comment table exists
18  """
19  try:
20  transaction=self.__session.transaction()
21  transaction.start(True)
22  schema = self.__session.nominalSchema()
23  result=schema.existsTable(CommonUtils.commentTableName())
24  transaction.commit()
25  #print result
26  except Exception, er:
27  transaction.rollback()
28  raise Exception, str(er)
29  return result
30 
32  """Create entry comment able.Existing table will be deleted.
33  """
34  try:
35  transaction=self.__session.transaction()
36  transaction.start()
37  schema = self.__session.nominalSchema()
38  schema.dropIfExistsTable(CommonUtils.commentTableName())
39  description = coral.TableDescription()
40  description.setName(CommonUtils.commentTableName())
41  for columnName, columnType in self.__entryCommentTableColumns.items():
42  description.insertColumn(columnName,columnType)
43  for columnName in self.__entryCommentTableNotNullColumns:
44  description.setNotNullConstraint(columnName,True)
45  description.setPrimaryKey(self.__entryCommentTablePK)
46  tablehandle=schema.createTable(description)
47  tablehandle.privilegeManager().grantToPublic(coral.privilege_Select)
48  transaction.commit()
49  except Exception, e:
50  transaction.rollback()
51  raise Exception, str(e)
52  def insertComment( self, tablename, entryid,comment ):
53  """insert comment on the given entry of given table
54  """
55  transaction=self.__session.transaction()
56  try:
57  transaction.start(False)
58  tabrowValueDict={'entryid':entryid,'tablename':tablename,'comment':comment}
59  schema = self.__session.nominalSchema()
60  dbop=DBImpl.DBImpl(schema)
61  dbop.insertOneRow(CommonUtils.commentTableName(),self.__entryCommentTableColumns,tabrowValueDict)
62  transaction.commit()
63  except Exception, e:
64  transaction.rollback()
65  raise Exception, str(e)
66  def bulkinsertComments( self, tableName,bulkinput):
67  """bulk insert comments for a given table
68  bulkinput [{'entryid':unsigned long, 'tablename':string,'comment':string}]
69  """
70  transaction=self.__session.transaction()
71  try:
72  transaction.start(False)
73  schema = self.__session.nominalSchema()
74  dbop=DBImpl.DBImpl(schema)
75  dbop.bulkInsert(CommonUtils.commentTableName(),self.__entryCommentTableColumns,bulkinput)
76  transaction.commit()
77  except Exception, e:
78  transaction.rollback()
79  raise Exception, str(e)
80 
81  def getCommentForId( self, tableName, entryid ):
82  """get comment for given id in given table
83  """
84  transaction=self.__session.transaction()
85  comment=''
86  try:
87  transaction.start(True)
88  schema = self.__session.nominalSchema()
89  query = schema.tableHandle(CommonUtils.commentTableName()).newQuery()
90  condition='entryid = :entryid AND tablename = :tablename'
91  conditionbindDict=coral.AttributeList()
92  conditionbindDict.extend('entryid','unsigned long')
93  conditionbindDict.extend('tablename','string')
94  conditionbindDict['entryid'].setData(entryid)
95  conditionbindDict['tablename'].setData(tableName)
96  query.addToOutputList('comment')
97  query.setCondition(condition,conditionbindDict)
98  cursor=query.execute()
99  if cursor.next():
100  comment=cursor.currentRow()['comment'].data()
101  cursor.close()
102  transaction.commit()
103  del query
104  return comment
105  except Exception, e:
106  transaction.rollback()
107  raise Exception, str(e)
108  def getCommentsForTable( self, tableName ):
109  """get all comments for given table
110  result=[(entryid,comment)]
111  """
112  transaction=self.__session.transaction()
113  result=[]
114 
115  try:
116  transaction.start(True)
117  schema = self.__session.nominalSchema()
118  query = schema.tableHandle(CommonUtils.commentTableName()).newQuery()
119  condition='tablename = :tablename'
120  conditionbindDict=coral.AttributeList()
121  conditionbindDict.extend('tablename','string')
122  conditionbindDict['tablename'].setData(tableName)
123  query.addToOutputList('entryid')
124  query.addToOutputList('comment')
125  query.setCondition(condition,conditionbindDict)
126  cursor=query.execute()
127  while cursor.next():
128  comment=cursor.currentRow()['comment'].data()
129  entryid=cursor.currentRow()['entryid'].data()
130  result.append((entryid,comment))
131  cursor.close()
132  transaction.commit()
133  del query
134  return result
135  except Exception, e:
136  transaction.rollback()
137  raise Exception, str(e)
138 
139  def modifyCommentForId( self, tableName, entryid, newcomment ):
140  """replace comment for given entry for given table
141  """
142  transaction=self.__session.transaction()
143  try:
144  transaction.start(False)
145  editor = self.__session.nominalSchema().tableHandle(CommonUtils.commentTableName()).dataEditor()
146  inputData = coral.AttributeList()
147  inputData.extend('newcomment','string')
148  inputData.extend('entryid','unsigned long')
149  inputData.extend('tablename','string')
150  inputData['newcomment'].setData(newcomment)
151  inputData['entryid'].setData(entryid)
152  inputData['tablename'].setData(tableName)
153  editor.updateRows( "comment = :newcomment", "entryid = :entryid AND tablename = :tablename", inputData )
154  transaction.commit()
155  except Exception, e:
156  transaction.rollback()
157  raise Exception, str(e)
158 
159  def replaceId( self, tableName, oldentryid, newentryid ):
160  """replace entryid in given table
161  """
162  transaction=self.__session.transaction()
163  try:
164  transaction.start(False)
165  editor = self.__session.nominalSchema().tableHandle(CommonUtils.commentTableName()).dataEditor()
166  inputData = coral.AttributeList()
167  inputData.extend('newentryid','unsigned long')
168  inputData.extend('oldentryid','unsigned long')
169  inputData.extend('tablename','string')
170  inputData['newentryid'].setData(newentryid)
171  inputData['oldentryid'].setData(oldentryid)
172  inputData['tablename'].setData(tableName)
173  editor.updateRows( "entryid = :newentryid", "entryid = :oldentryid AND tablename = :tablename", inputData )
174  transaction.commit()
175  except Exception, e:
176  transaction.rollback()
177  raise Exception, str(e)
178 
179  def deleteCommentForId( self, tablename, entryid):
180  """delete selected comment entry
181  """
182  transaction=self.__session.transaction()
183  try:
184  transaction.start(False)
185  dbop=DBImpl.DBImpl(self.__session.nominalSchema())
186  condition='tablename = :tablename AND entryid = :entryid'
187  conditionbindDict=coral.AttributeList()
188  conditionbindDict.extend('tablename','string')
189  conditionbindDict.extend('entryid','unsigned long')
190  conditionbindDict['tablename'].setData(tablename)
191  conditionbindDict['entryid'].setData(entryid)
192  dbop.deleteRows(CommonUtils.commentTableName(),condition,conditionbindDict)
193  transaction.commit()
194  except Exception, e:
195  transaction.rollback()
196  raise Exception, str(e)
197 
198  def clearAllEntriesForTable( self, tablename ):
199  """delete all entries related with given table
200  """
201  transaction=self.__session.transaction()
202  try:
203  transaction.start(False)
204  dbop=DBImpl.DBImpl(self.__session.nominalSchema())
205  condition='tablename = :tablename'
206  conditionbindDict=coral.AttributeList()
207  conditionbindDict.extend('tablename','string')
208  conditionbindDict['tablename'].setData(tablename)
209  dbop.deleteRows(CommonUtils.commentTableName(),condition,conditionbindDict)
210  transaction.commit()
211  except Exception, e:
212  transaction.rollback()
213  raise Exception, str(e)
214 
215 
216 if __name__ == "__main__":
217  svc = coral.ConnectionService()
218  session = svc.connect( 'sqlite_file:testentryComment.db',
219  accessMode = coral.access_Update )
220  try:
221  entrycomment=entryComment(session)
222  print "test create entrycomment table"
223  entrycomment.createEntryCommentTable()
224  print "test insert one comment"
225  entrycomment.insertComment(CommonUtils.inventoryTableName(),12,'comment1')
226  entrycomment.insertComment(CommonUtils.treeTableName('ABCTREE'),12,'comment1')
227  print "test bulk insert"
228  bulkinput=[]
229  bulkinput.append({'entryid':21,'tablename':CommonUtils.inventoryTableName(),'comment':'mycomment'})
230  bulkinput.append({'entryid':22,'tablename':CommonUtils.inventoryTableName(),'comment':'mycomment2'})
231  bulkinput.append({'entryid':23,'tablename':CommonUtils.inventoryTableName(),'comment':'mycomment3'})
232  bulkinput.append({'entryid':24,'tablename':CommonUtils.inventoryTableName(),'comment':'mycomment4'})
233  entrycomment.bulkinsertComments(CommonUtils.inventoryTableName(),bulkinput)
234  print "test getCommentsForTable ",CommonUtils.inventoryTableName()
235  results=entrycomment.getCommentsForTable(CommonUtils.inventoryTableName())
236  print results
237  result=entrycomment.getCommentForId(CommonUtils.inventoryTableName(),23)
238  print result
239  entrycomment.modifyCommentForId(CommonUtils.inventoryTableName(),23, 'mynewcomment' )
240  print entrycomment.getCommentForId(CommonUtils.inventoryTableName(),23)
241  print 'test replaceid'
242  entrycomment.replaceId(CommonUtils.inventoryTableName(),23,33 )
243  print entrycomment.getCommentForId(CommonUtils.inventoryTableName(),33)
244  print 'test deletecomment for id'
245  entrycomment.deleteCommentForId(CommonUtils.inventoryTableName(), 24)
246  print entrycomment.getCommentsForTable(CommonUtils.inventoryTableName())
247  print 'clearAllEntriesForTable'
248  entrycomment.clearAllEntriesForTable(CommonUtils.inventoryTableName())
249  print entrycomment.getCommentsForTable(CommonUtils.inventoryTableName())
250  del session
251  except Exception, e:
252  print "Failed in unit test"
253  print str(e)
254  del session
255 
list object
Definition: dbtoconf.py:77
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82