CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
listobjects.py
Go to the documentation of this file.
1 import os
2 import coral
3 from multivaluedict import mseqdict
4 
5 '''
6 dumpobjectlist(schema)
7 Dumps the list of tables and views grouped and ordered by hierarchy, specifying the existing constraints and indexes.
8 Input parameter schema : a schema object, obtained by the sessionproxy object
9 Output paramter : none
10 '''
11 def dumpobjectlist( schema ):
12  try:
13 
14  dTableInfo=listobjects( schema )
15 
16  print "--------------------------------------"
17  print "Listing Table Description "
18  print "--------------------------------------"
19  for key,value in dTableInfo.items():
20  tableName= key
21  _printTableInfo(schema,tableName)
22 
23  print "Listing View Information"
24  print "--------------------------------------"
25  for viewName in schema.listViews():
26  _printViewInfo(schema,viewName)
27 
28  except Exception, e:
29  raise Exception ("Error in dumpobjectlist method: " + str(e))
30  return False
31 
32 #Returns the list of tables ordered by hierarchy
33 def listobjects( schema ):
34  try:
35 
36  listOfTableNames = schema.listTables()
37 
38  #Dictionaries are created for resolving table dependencies
39  dTable=mseqdict( [], {})
40  dRefTable=mseqdict( [], {})
41  dCopyTable=mseqdict( [], {})
42 
43  for tableName in listOfTableNames:
44 
45  #Add tablename to dictionary
46  dTable.append(tableName,'')
47  description = coral.TableDescription()
48  description.setName( tableName )
49  table = schema.tableHandle(tableName )
50 
51  numberOfForeignKeys = table.description().numberOfForeignKeys()
52  for i in range(0, numberOfForeignKeys):
53  foreignKey = table.description().foreignKey( i )
54  columnNames = foreignKey.columnNames()
55  #Add referenced tablename to dictionary
56  dRefTable.append (tableName, foreignKey.referencedTableName())
57  columnNamesR = foreignKey.referencedColumnNames()
58 
59  #For retrieving the tables in order of dependency
60  r1=mseqdict( [], {})
61  r2=mseqdict( [], {})
62 
63  for rTable, refTable in dRefTable.items():
64  for table in refTable:
65  r1.append(table,'')
66  r1.append(rTable,'')
67 
68  for rTable, refTable in r1.items():
69  test=rTable
70  for rTable1, refTable1 in dRefTable.items():
71  if rTable1==test:
72  for table in refTable1:
73  if rTable1!=table:
74  r2.append(table,'')
75 
76  for key,value in r2.items():
77  r1.remove(key,'')
78  dTable.remove(key,'')
79 
80  for key,value in r1.items():
81  dTable.remove(key,'')
82 
83  for key,value in dTable.items():
84  dCopyTable.append(key,'')
85 
86  for key,value in r2.items():
87  dCopyTable.append(key,'')
88 
89  for key,value in r1.items():
90  dCopyTable.append(key,'')
91 
92  return dCopyTable
93 
94  except Exception, e:
95  raise Exception (" " + str(e))
96  return False
97 
98 #For printing the Table Information
99 def _printTableInfo( schema,tableName ):
100  try:
101 
102  description = coral.TableDescription()
103  description.setName( tableName )
104  table = schema.tableHandle(tableName )
105 
106  numberOfColumns = table.description().numberOfColumns()
107  print "Table " , tableName
108  print "Columns : " , numberOfColumns
109  for i in range(0, numberOfColumns):
110  column = table.description().columnDescription( i )
111  print "" , column.name() , " (" , column.type() , ")"
112  if ( column.isUnique() ):
113  print " UNIQUE";
114  if ( column.isNotNull() ):
115  print " NOT NULL"
116 
117  if ( table.description().hasPrimaryKey() ):
118  columnNames = table.description().primaryKey().columnNames()
119  print ""
120  print "Primary key defined for column :"
121  for iColumn in columnNames:
122  print " ",iColumn , " "
123 
124  numberOfUniqueConstraints = table.description().numberOfUniqueConstraints()
125  print ""
126  print "Unique Constraints : " , numberOfUniqueConstraints
127  for i in range( 0, numberOfUniqueConstraints ):
128  uniqueConstraint = table.description().uniqueConstraint( i )
129  print "" , uniqueConstraint.name() , " defined for column"
130  columnNames = uniqueConstraint.columnNames()
131  for iColumn in columnNames:
132  print " ",iColumn
133 
134  numberOfIndices = table.description().numberOfIndices()
135  print ""
136  print "Index : " , numberOfIndices
137  for i in range(0, numberOfIndices ):
138  index = table.description().index( i )
139  print "" , index.name()
140  if ( index.isUnique() ):
141  print " (UNIQUE)"
142  print " defined for column"
143  columnNames = index.columnNames()
144  for iColumn in columnNames:
145  print " ",iColumn
146 
147  numberOfForeignKeys = table.description().numberOfForeignKeys()
148  print ""
149  print "Foreign Keys : " , numberOfForeignKeys
150  for i in range(0, numberOfForeignKeys):
151  foreignKey = table.description().foreignKey( i )
152  print "" , foreignKey.name() , " defined for column"
153  columnNames = foreignKey.columnNames()
154  for iColumn in columnNames:
155  print " ",iColumn
156  print " references -> " , foreignKey.referencedTableName() , "on Column ";
157  columnNamesR = foreignKey.referencedColumnNames()
158  for iColumn in columnNamesR:
159  print " ",iColumn
160 
161  print "--------------------------------------"
162 
163  except Exception, e:
164  raise Exception (" " + str(e))
165  return False
166 
167 #For printing the View Information
168 def _printViewInfo( schema,viewName ):
169  try:
170 
171  view = schema.viewHandle(viewName )
172  numberOfColumns = view.numberOfColumns()
173  print "View " , view.name()
174  print "has", " ", numberOfColumns , " columns :"
175  for i in range( 0,numberOfColumns ):
176  column = view.column( i )
177  print "" , column.name(), " (", column.type() , ")"
178  if ( column.isUnique() ):
179  print " UNIQUE"
180  if ( column.isNotNull() ):
181  print " NOT NULL"
182 
183  print " definition string : " , view.definition()
184 
185  print "--------------------------------------"
186 
187  except Exception, e:
188  raise Exception (" " + str(e))
189  return False
190 
191 #Returns the list of tables ordered by hierarchy and checks for circular dependency between tables in source schema
192 def listschema( schema ):
193  try:
194  listOfTableNames = schema.listTables()
195 
196  #Dictionaries are created for resolving table dependencies
197  dTable=mseqdict( [], {})
198  dRefTable=mseqdict( [], {})
199  dCopyTable=mseqdict( [], {})
200  dCircTable=mseqdict( [], {})
201 
202  for tableName in listOfTableNames:
203 
204  #Add tablename to dictionary
205  dTable.append(tableName,'')
206  description = coral.TableDescription()
207  description.setName( tableName )
208  table = schema.tableHandle(tableName )
209 
210  numberOfForeignKeys = table.description().numberOfForeignKeys()
211  for i in range(0, numberOfForeignKeys):
212  foreignKey = table.description().foreignKey( i )
213  columnNames = foreignKey.columnNames()
214  #Add referenced tablename to dictionary
215  dRefTable.append (tableName, foreignKey.referencedTableName())
216  dCircTable.append (tableName, foreignKey.referencedTableName())
217  columnNamesR = foreignKey.referencedColumnNames()
218 
219  #For checking circular dependency between the tables
220  d1=mseqdict( [], {})
221  d2=mseqdict( [], {})
222 
223  for rTable, refTable in dCircTable.items():
224  for table in refTable:
225  d1.append(rTable,table)
226 
227  dCircTable.swap()
228  for rTable, refTable in dCircTable.items():
229  for table in refTable:
230  d2.append(rTable,table)
231 
232  for key,value in d1.items():
233  firsttable=key
234  secondtable=value
235  for key,value in d2.items():
236  if key==firsttable and value==secondtable:
237  raise Exception ("Circular Dependency exists between tables : "+firsttable,secondtable)
238 
239  #For retrieving the tables in order of dependency
240  r1=mseqdict( [], {})
241  r2=mseqdict( [], {})
242 
243  for rTable, refTable in dRefTable.items():
244  for table in refTable:
245  r1.append(table,'')
246  r1.append(rTable,'')
247 
248  for rTable, refTable in r1.items():
249  test=rTable
250  for rTable1, refTable1 in dRefTable.items():
251  if rTable1==test:
252  for table in refTable1:
253  if rTable1!=table:
254  r2.append(table,'')
255 
256  for key,value in r2.items():
257  r1.remove(key,'')
258  dTable.remove(key,'')
259 
260  for key,value in r1.items():
261  dTable.remove(key,'')
262 
263  for key,value in dTable.items():
264  dCopyTable.append(key,'')
265 
266  for key,value in r2.items():
267  dCopyTable.append(key,'')
268 
269  for key,value in r1.items():
270  dCopyTable.append(key,'')
271 
272  return dCopyTable
273 
274  except Exception, e:
275  raise Exception (" " + str(e))
276  return False
277 
278 #Returns the tablename for the specified table schema
279 def listtables( schema,tablename ):
280  try:
281  listOfTableNames = schema.listTables()
282 
283  #Dictionaries are created for resolving table dependencies
284  dTable=mseqdict( [], {})
285  dCopyTable=mseqdict( [], {})
286 
287  for tableName in listOfTableNames:
288  if tablename==tableName:
289  #Add tablename to dictionary
290  dTable.append(tableName,'')
291  description = coral.TableDescription()
292  description.setName( tableName )
293  table = schema.tableHandle(tableName )
294 
295  numberOfForeignKeys = table.description().numberOfForeignKeys()
296  for i in range(0, numberOfForeignKeys):
297  foreignKey = table.description().foreignKey( i )
298  columnNames = foreignKey.columnNames()
299  columnNamesR = foreignKey.referencedColumnNames()
300 
301  for key,value in dTable.items():
302  dCopyTable.append(key,'')
303 
304  return dCopyTable
305 
306  except Exception, e:
307  raise Exception (" " + str(e))
308  return False
309 
310 #Returns the list of tables ordered by hierarchy for the specified list of tables and also checks for circular dependency between the tables
311 def listtableset( schema,tableset ):
312  try:
313  listOfTableNames = schema.listTables()
314 
315  #Dictionaries are created for resolving table dependencies
316  dTable=mseqdict( [], {})
317  dCircTable=mseqdict( [], {})
318  dCopyTable=mseqdict( [], {})
319  dTempTable=mseqdict( [], {})
320 
321  for table in listOfTableNames:
322  for tableName in tableset:
323  if tableName==table:
324  #Add tablename to dictionary
325  dTable.append(tableName,'')
326  description = coral.TableDescription()
327  description.setName( tableName )
328  table = schema.tableHandle(tableName )
329 
330  numberOfForeignKeys = table.description().numberOfForeignKeys()
331  for i in range(0, numberOfForeignKeys):
332  foreignKey = table.description().foreignKey( i )
333  columnNames = foreignKey.columnNames()
334  #Add referenced tablename to dictionary
335  dTable.append (tableName, foreignKey.referencedTableName())
336  dCircTable.append (tableName, foreignKey.referencedTableName())
337  columnNamesR = foreignKey.referencedColumnNames()
338 
339  #For checking circular dependency between the tables
340  d1=mseqdict( [], {})
341  d2=mseqdict( [], {})
342 
343  for rTable, refTable in dCircTable.items():
344  for table in refTable:
345  d1.append(rTable,table)
346 
347  dCircTable.swap()
348  for rTable, refTable in dCircTable.items():
349  for table in refTable:
350  d2.append(rTable,table)
351 
352  for key,value in d1.items():
353  firsttable=key
354  secondtable=value
355  for key,value in d2.items():
356  if key==firsttable and value==secondtable:
357  raise Exception ("Circular Dependency exists between tables : "+firsttable,secondtable)
358 
359  #For retrieving the tables in order of dependency
360  r1=mseqdict( [], {})
361  r2=mseqdict( [], {})
362 
363  for rTable, refTable in dTable.items():
364  for table in refTable:
365  r1.append(table,'')
366  r1.append(rTable,'')
367 
368  for rTable, refTable in r1.items():
369  test=rTable
370  for rTable1, refTable1 in dTable.items():
371  if rTable1==test:
372  for table in refTable1:
373  if rTable1!=table:
374  r2.append(table,'')
375 
376  for key,value in r2.items():
377  r1.remove(key,'')
378 
379  for key,value in r2.items():
380  dTempTable.append(key,'')
381 
382  for key,value in r1.items():
383  dTempTable.append(key,'')
384 
385  for key,value in dTempTable.items():
386  iTable= key
387  for table in tableset:
388  if table==iTable:
389  dCopyTable.append(key,'')
390 
391  return dCopyTable
392 
393  except Exception, e:
394  raise Exception (" " + str(e))
395  return False
396 
397 if __name__ == "__main__":
398  svc = coral.ConnectionService()
399  session = svc.connect( 'sqlite_file:source.db',
400  accessMode = coral.access_Update )
401  session.transaction().start(True)
402  schema=session.nominalSchema()
403  print 'TEST listobjects'
404  print listobjects(schema)
405  print 'TEST dumpobjectlist'
406  dumpobjectlist(schema)
407  print 'TEST listtables'
408  print listtables(schema,'TAGINVENTORY_TABLE')
409 
410  session.transaction().commit()
411  del session
Definition: start.py:1