CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
exporter.py
Go to the documentation of this file.
1 '''
2 exporter(sourceSession,destSession[,rowCachesize])
3 Input parameter sourceSession : session proxy for source schema providing logical service name & access mode
4 Input parameter destSession : session proxy for destination schema providing logical service name & access mode
5 Input parameter rowCachesize : the number of rows to be cached at the client side, default value =100
6 Output paramter : the exporter object
7 '''
8 
9 import os
10 import coral
11 import time
12 import math
13 from multivaluedict import mseqdict
14 from listobjects import listobjects,listschema,listtables,listtableset
15 
16 class exporter:
17  "exporter class for CoralTools"
18  m_sourceSession = 0
19  m_destSession = 0
20  m_rowCachesize = 100
21 
22  def __init__( self,sourceSession,destSession,rowCachesize=100 ):
23  try:
24 
25  self.m_sourceSession = sourceSession
26  self.m_destSession = destSession
27  self.m_rowCachesize = rowCachesize
28 
29  self.m_sourceSession.transaction().start()
30  self.m_destSession.transaction().start()
31 
32  except Exception, e:
33  raise Exception("Error in Initializer: " + str(e))
34 
35 #Copies the schema objects from source to destination, without copying data.
36  def copyschema(self ):
37  try:
38 
39  listsourceTable=listschema( self.m_sourceSession.nominalSchema() )
40  listdestTable=listobjects( self.m_destSession.nominalSchema() )
41  self._checktable(listsourceTable,listdestTable)
42 
43  for key,value in listsourceTable.items():
44  iTable = key
45  print iTable
46  self._copytablelayout(iTable)
47 
48  self.m_destSession.transaction().commit()
49  self.m_sourceSession.transaction().commit()
50  print "copyschema SUCCESS"
51 
52  return True
53 
54  except Exception, e:
55  self.m_destSession.transaction().rollback()
56  self.m_sourceSession.transaction().commit()
57  raise Exception ("Error in copyschema method: " + str(e))
58  return False
59 
60 #Copies the schema objects from source to destination, including data.
61  def copydata(self,rowCount=-1 ):
62  try:
63 
64  self.m_sourceSession.transaction().start()
65  self.m_destSession.transaction().start()
66 
67  listsourceTable=listschema( self.m_sourceSession.nominalSchema() )
68  listdestTable=listobjects( self.m_destSession.nominalSchema() )
69  self._checktable(listsourceTable,listdestTable)
70 
71  selectionclause=""
72  selectionparameters=coral.AttributeList()
73  for key,value in listsourceTable.items():
74  iTable = key
75  print iTable
76  currentCount = 0
77  self._copytablelayout(iTable)
78  self._copydatalayout(iTable,selectionclause,selectionparameters,currentCount,rowCount)
79 
80  self.m_destSession.transaction().commit()
81  self.m_sourceSession.transaction().commit()
82  print "copydata SUCCESS"
83  return True
84 
85  except Exception, e:
86  self.m_destSession.transaction().rollback()
87  self.m_sourceSession.transaction().commit()
88  raise Exception ("Error in copydata method: " + str(e))
89  return False
90 
91 #Copies the specified table schema without copying data.
92  def copytableschema(self,tablename ):
93  try:
94 
95  iTable=""
96  listsourceTable=listtables( self.m_sourceSession.nominalSchema(),tablename )
97  listdestTable=listtables(self.m_destSession.nominalSchema(),tablename )
98  self._checktable(listsourceTable,listdestTable)
99 
100  for key,value in listsourceTable.items():
101  iTable = key
102  print iTable
103  self._copytablelayout(iTable)
104 
105  self.m_destSession.transaction().commit()
106  self.m_sourceSession.transaction().commit()
107  print "copytableschema SUCCESS"
108  return True
109 
110  except Exception, e:
111  self.m_destSession.transaction().rollback()
112  self.m_sourceSession.transaction().commit()
113  raise Exception ("Error in copytableschema method: " + str(e)+" : "+iTable)
114  return False
115 
116 #Copies the specified table schema including data.
117  def copytabledata(self,tablename,selectionclause,selectionparameters,rowCount=-1 ):
118  try:
119 
120  iTable=""
121  listsourceTable=listtables( self.m_sourceSession.nominalSchema(),tablename )
122  listdestTable=listtables( self.m_destSession.nominalSchema(),tablename )
123 
124  currentCount = 0
125  for key,value in listsourceTable.items():
126  iTable = key
127  print iTable
128  tableexists = self._checkdata(iTable,listdestTable)
129 
130  if not tableexists:
131  self._copytablelayout(iTable)
132 
133  self._copydatalayout(iTable,selectionclause,selectionparameters,currentCount,rowCount)
134 
135  self.m_destSession.transaction().commit()
136  self.m_sourceSession.transaction().commit()
137  print "copytabledata SUCCESS"
138  return True
139 
140  except Exception, e:
141  self.m_destSession.transaction().rollback()
142  self.m_sourceSession.transaction().commit()
143  raise Exception ("Error in copytabledata method: " + str(e)+" : " + iTable)
144  return False
145 
146 #Copies the specified list of tables ordered by hierarchy without copying data.
147  def copytablelistschema(self,tableset ):
148  try:
149 
150  iTable=""
151  listsourceTable=listtableset( self.m_sourceSession.nominalSchema(),tableset )
152  listdestTable=listtableset( self.m_destSession.nominalSchema(),tableset )
153  self._checktable(listsourceTable,listdestTable)
154 
155  for key,value in listsourceTable.items():
156  iTable = key
157  print iTable
158  self._copytablelayout(iTable)
159 
160  self.m_destSession.transaction().commit()
161  self.m_sourceSession.transaction().commit()
162  print "copytablelistschema SUCCESS"
163  return True
164 
165  except Exception, e:
166  self.m_destSession.transaction().rollback()
167  self.m_sourceSession.transaction().commit()
168  raise Exception ("Error in copytablelistschema method: " + str(e)+" : "+iTable)
169  return False
170 
171 #Copies the specified list of tables ordered by hierarchy including data.
172  def copytablelistdata(self,tablelist,rowCount=-1):
173  try:
174 
175  iTable=""
176  tableset=[]
177  for table in tablelist:
178  i=0
179  for parameter in table:
180  if i==0:
181  tableset.append(parameter)
182  i=i+1
183 
184  listsourceTable=listtableset( self.m_sourceSession.nominalSchema(),tableset )
185  listdestTable=listtableset( self.m_destSession.nominalSchema(),tableset )
186  for key,value in listsourceTable.items():
187  iTable = key
188  print iTable
189  currentCount = 0
190  selectionclause=""
191  selectionparameters=coral.AttributeList()
192  for table in tablelist:
193  i=0
194  for parameter in table:
195  if i==0:
196  table=parameter
197  if table==iTable:
198  if i==1:
199  selectionclause = parameter
200  if i==2:
201  selectionparameters = parameter
202  i=i+1
203 
204  tableexists = self._checkdata(iTable,listdestTable)
205 
206  if not tableexists:
207  self._copytablelayout(iTable)
208 
209  self._copydatalayout(iTable,selectionclause,selectionparameters,currentCount,rowCount)
210 
211  self.m_destSession.transaction().commit()
212  self.m_sourceSession.transaction().commit()
213  print "copytablelistdata SUCCESS"
214  return True
215 
216  except Exception, e:
217  self.m_destSession.transaction().rollback()
218  self.m_sourceSession.transaction().commit()
219  raise Exception ("Error in copytablelistdata method: " + str(e) + " : "+ iTable)
220  return False
221 
222 #Copies the schema objects from source to destination
223  def _copytablelayout(self,iTable ):
224  try:
225 
226  description = self.m_sourceSession.nominalSchema().tableHandle( iTable ).description()
227  table = self.m_destSession.nominalSchema().createTable( description )
228 
229  return True
230 
231  except Exception, e:
232  raise Exception (" " + str(e))
233  return False
234 
235 #Copies the data from source to destination
236  def _copydatalayout(self,iTable,selectionclause,selectionparameters,currentCount,rowCount ):
237  try:
238  data=coral.AttributeList()
239  sourceEditor = self.m_sourceSession.nominalSchema().tableHandle( iTable ).dataEditor()
240  destEditor = self.m_destSession.nominalSchema().tableHandle( iTable ).dataEditor()
241 
242  sourceEditor.rowBuffer(data)
243  sourcequery = self.m_sourceSession.nominalSchema().tableHandle( iTable ).newQuery()
244  sourcequery.setCondition(selectionclause,selectionparameters)
245  sourcequery.setRowCacheSize(self.m_rowCachesize)
246  sourcequery.defineOutput(data)
247 
248  bulkOperation = destEditor.bulkInsert(data,self.m_rowCachesize )
249 
250  cursor=sourcequery.execute()
251 
252  for row in cursor:
253  currentCount = currentCount+1
254  bulkOperation.processNextIteration()
255  if currentCount == rowCount:
256  bulkOperation.flush()
257  self.m_destSession.transaction().commit()
258  self.m_destSession.transaction().start()
259  currentCount = 0
260  bulkOperation.flush()
261  del bulkOperation
262  del sourcequery
263 
264  return True
265 
266  except Exception, e:
267  raise Exception (" " + str(e))
268  return False
269 
270  #Checks if table exists in destination schema
271  def _checktable(self,listsourceTable,listdestTable ):
272  try:
273 
274  for key,value in listsourceTable.items():
275  table=key
276  for key,value in listdestTable.items():
277  if key==table:
278  raise Exception( "Table exists in Destination Schema : " )
279 
280  return True
281 
282  except Exception, e:
283  raise Exception (" " + str(e) + table)
284  return False
285 
286 #Checks if data exists in the table in destination schema
287  def _checkdata(self,iTable,listdestTable ):
288  try:
289 
290  foundtable=False
291  founddata=False
292  for key,value in listdestTable.items():
293  if key==iTable:
294  counter=0
295  # Run a query on the destination table
296  query = self.m_destSession.nominalSchema().tableHandle( iTable ).newQuery()
297  cursor = query.execute()
298  foundtable=True
299  for row in cursor:
300  counter=counter+1
301  del query
302  if (counter > 0):
303  founddata=True
304 
305  return foundtable
306 
307  except Exception, e:
308  raise Exception (" " + str(e) + iTable)
309  return False
310 
311  def __del__( self ):
312  print ""
Definition: start.py:1
tuple description
Definition: idDealer.py:66