00001 import os
00002 import coral
00003 from multivaluedict import mseqdict
00004
00005 '''
00006 dumpobjectlist(schema)
00007 Dumps the list of tables and views grouped and ordered by hierarchy, specifying the existing constraints and indexes.
00008 Input parameter schema : a schema object, obtained by the sessionproxy object
00009 Output paramter : none
00010 '''
00011 def dumpobjectlist( schema ):
00012 try:
00013
00014 dTableInfo=listobjects( schema )
00015
00016 print "--------------------------------------"
00017 print "Listing Table Description "
00018 print "--------------------------------------"
00019 for key,value in dTableInfo.items():
00020 tableName= key
00021 _printTableInfo(schema,tableName)
00022
00023 print "Listing View Information"
00024 print "--------------------------------------"
00025 for viewName in schema.listViews():
00026 _printViewInfo(schema,viewName)
00027
00028 except Exception, e:
00029 raise Exception ("Error in dumpobjectlist method: " + str(e))
00030 return False
00031
00032
00033 def listobjects( schema ):
00034 try:
00035
00036 listOfTableNames = schema.listTables()
00037
00038
00039 dTable=mseqdict( [], {})
00040 dRefTable=mseqdict( [], {})
00041 dCopyTable=mseqdict( [], {})
00042
00043 for tableName in listOfTableNames:
00044
00045
00046 dTable.append(tableName,'')
00047 description = coral.TableDescription()
00048 description.setName( tableName )
00049 table = schema.tableHandle(tableName )
00050
00051 numberOfForeignKeys = table.description().numberOfForeignKeys()
00052 for i in range(0, numberOfForeignKeys):
00053 foreignKey = table.description().foreignKey( i )
00054 columnNames = foreignKey.columnNames()
00055
00056 dRefTable.append (tableName, foreignKey.referencedTableName())
00057 columnNamesR = foreignKey.referencedColumnNames()
00058
00059
00060 r1=mseqdict( [], {})
00061 r2=mseqdict( [], {})
00062
00063 for rTable, refTable in dRefTable.items():
00064 for table in refTable:
00065 r1.append(table,'')
00066 r1.append(rTable,'')
00067
00068 for rTable, refTable in r1.items():
00069 test=rTable
00070 for rTable1, refTable1 in dRefTable.items():
00071 if rTable1==test:
00072 for table in refTable1:
00073 if rTable1!=table:
00074 r2.append(table,'')
00075
00076 for key,value in r2.items():
00077 r1.remove(key,'')
00078 dTable.remove(key,'')
00079
00080 for key,value in r1.items():
00081 dTable.remove(key,'')
00082
00083 for key,value in dTable.items():
00084 dCopyTable.append(key,'')
00085
00086 for key,value in r2.items():
00087 dCopyTable.append(key,'')
00088
00089 for key,value in r1.items():
00090 dCopyTable.append(key,'')
00091
00092 return dCopyTable
00093
00094 except Exception, e:
00095 raise Exception (" " + str(e))
00096 return False
00097
00098
00099 def _printTableInfo( schema,tableName ):
00100 try:
00101
00102 description = coral.TableDescription()
00103 description.setName( tableName )
00104 table = schema.tableHandle(tableName )
00105
00106 numberOfColumns = table.description().numberOfColumns()
00107 print "Table " , tableName
00108 print "Columns : " , numberOfColumns
00109 for i in range(0, numberOfColumns):
00110 column = table.description().columnDescription( i )
00111 print "" , column.name() , " (" , column.type() , ")"
00112 if ( column.isUnique() ):
00113 print " UNIQUE";
00114 if ( column.isNotNull() ):
00115 print " NOT NULL"
00116
00117 if ( table.description().hasPrimaryKey() ):
00118 columnNames = table.description().primaryKey().columnNames()
00119 print ""
00120 print "Primary key defined for column :"
00121 for iColumn in columnNames:
00122 print " ",iColumn , " "
00123
00124 numberOfUniqueConstraints = table.description().numberOfUniqueConstraints()
00125 print ""
00126 print "Unique Constraints : " , numberOfUniqueConstraints
00127 for i in range( 0, numberOfUniqueConstraints ):
00128 uniqueConstraint = table.description().uniqueConstraint( i )
00129 print "" , uniqueConstraint.name() , " defined for column"
00130 columnNames = uniqueConstraint.columnNames()
00131 for iColumn in columnNames:
00132 print " ",iColumn
00133
00134 numberOfIndices = table.description().numberOfIndices()
00135 print ""
00136 print "Index : " , numberOfIndices
00137 for i in range(0, numberOfIndices ):
00138 index = table.description().index( i )
00139 print "" , index.name()
00140 if ( index.isUnique() ):
00141 print " (UNIQUE)"
00142 print " defined for column"
00143 columnNames = index.columnNames()
00144 for iColumn in columnNames:
00145 print " ",iColumn
00146
00147 numberOfForeignKeys = table.description().numberOfForeignKeys()
00148 print ""
00149 print "Foreign Keys : " , numberOfForeignKeys
00150 for i in range(0, numberOfForeignKeys):
00151 foreignKey = table.description().foreignKey( i )
00152 print "" , foreignKey.name() , " defined for column"
00153 columnNames = foreignKey.columnNames()
00154 for iColumn in columnNames:
00155 print " ",iColumn
00156 print " references -> " , foreignKey.referencedTableName() , "on Column ";
00157 columnNamesR = foreignKey.referencedColumnNames()
00158 for iColumn in columnNamesR:
00159 print " ",iColumn
00160
00161 print "--------------------------------------"
00162
00163 except Exception, e:
00164 raise Exception (" " + str(e))
00165 return False
00166
00167
00168 def _printViewInfo( schema,viewName ):
00169 try:
00170
00171 view = schema.viewHandle(viewName )
00172 numberOfColumns = view.numberOfColumns()
00173 print "View " , view.name()
00174 print "has", " ", numberOfColumns , " columns :"
00175 for i in range( 0,numberOfColumns ):
00176 column = view.column( i )
00177 print "" , column.name(), " (", column.type() , ")"
00178 if ( column.isUnique() ):
00179 print " UNIQUE"
00180 if ( column.isNotNull() ):
00181 print " NOT NULL"
00182
00183 print " definition string : " , view.definition()
00184
00185 print "--------------------------------------"
00186
00187 except Exception, e:
00188 raise Exception (" " + str(e))
00189 return False
00190
00191
00192 def listschema( schema ):
00193 try:
00194 listOfTableNames = schema.listTables()
00195
00196
00197 dTable=mseqdict( [], {})
00198 dRefTable=mseqdict( [], {})
00199 dCopyTable=mseqdict( [], {})
00200 dCircTable=mseqdict( [], {})
00201
00202 for tableName in listOfTableNames:
00203
00204
00205 dTable.append(tableName,'')
00206 description = coral.TableDescription()
00207 description.setName( tableName )
00208 table = schema.tableHandle(tableName )
00209
00210 numberOfForeignKeys = table.description().numberOfForeignKeys()
00211 for i in range(0, numberOfForeignKeys):
00212 foreignKey = table.description().foreignKey( i )
00213 columnNames = foreignKey.columnNames()
00214
00215 dRefTable.append (tableName, foreignKey.referencedTableName())
00216 dCircTable.append (tableName, foreignKey.referencedTableName())
00217 columnNamesR = foreignKey.referencedColumnNames()
00218
00219
00220 d1=mseqdict( [], {})
00221 d2=mseqdict( [], {})
00222
00223 for rTable, refTable in dCircTable.items():
00224 for table in refTable:
00225 d1.append(rTable,table)
00226
00227 dCircTable.swap()
00228 for rTable, refTable in dCircTable.items():
00229 for table in refTable:
00230 d2.append(rTable,table)
00231
00232 for key,value in d1.items():
00233 firsttable=key
00234 secondtable=value
00235 for key,value in d2.items():
00236 if key==firsttable and value==secondtable:
00237 raise Exception ("Circular Dependency exists between tables : "+firsttable,secondtable)
00238
00239
00240 r1=mseqdict( [], {})
00241 r2=mseqdict( [], {})
00242
00243 for rTable, refTable in dRefTable.items():
00244 for table in refTable:
00245 r1.append(table,'')
00246 r1.append(rTable,'')
00247
00248 for rTable, refTable in r1.items():
00249 test=rTable
00250 for rTable1, refTable1 in dRefTable.items():
00251 if rTable1==test:
00252 for table in refTable1:
00253 if rTable1!=table:
00254 r2.append(table,'')
00255
00256 for key,value in r2.items():
00257 r1.remove(key,'')
00258 dTable.remove(key,'')
00259
00260 for key,value in r1.items():
00261 dTable.remove(key,'')
00262
00263 for key,value in dTable.items():
00264 dCopyTable.append(key,'')
00265
00266 for key,value in r2.items():
00267 dCopyTable.append(key,'')
00268
00269 for key,value in r1.items():
00270 dCopyTable.append(key,'')
00271
00272 return dCopyTable
00273
00274 except Exception, e:
00275 raise Exception (" " + str(e))
00276 return False
00277
00278
00279 def listtables( schema,tablename ):
00280 try:
00281 listOfTableNames = schema.listTables()
00282
00283
00284 dTable=mseqdict( [], {})
00285 dCopyTable=mseqdict( [], {})
00286
00287 for tableName in listOfTableNames:
00288 if tablename==tableName:
00289
00290 dTable.append(tableName,'')
00291 description = coral.TableDescription()
00292 description.setName( tableName )
00293 table = schema.tableHandle(tableName )
00294
00295 numberOfForeignKeys = table.description().numberOfForeignKeys()
00296 for i in range(0, numberOfForeignKeys):
00297 foreignKey = table.description().foreignKey( i )
00298 columnNames = foreignKey.columnNames()
00299 columnNamesR = foreignKey.referencedColumnNames()
00300
00301 for key,value in dTable.items():
00302 dCopyTable.append(key,'')
00303
00304 return dCopyTable
00305
00306 except Exception, e:
00307 raise Exception (" " + str(e))
00308 return False
00309
00310
00311 def listtableset( schema,tableset ):
00312 try:
00313 listOfTableNames = schema.listTables()
00314
00315
00316 dTable=mseqdict( [], {})
00317 dCircTable=mseqdict( [], {})
00318 dCopyTable=mseqdict( [], {})
00319 dTempTable=mseqdict( [], {})
00320
00321 for table in listOfTableNames:
00322 for tableName in tableset:
00323 if tableName==table:
00324
00325 dTable.append(tableName,'')
00326 description = coral.TableDescription()
00327 description.setName( tableName )
00328 table = schema.tableHandle(tableName )
00329
00330 numberOfForeignKeys = table.description().numberOfForeignKeys()
00331 for i in range(0, numberOfForeignKeys):
00332 foreignKey = table.description().foreignKey( i )
00333 columnNames = foreignKey.columnNames()
00334
00335 dTable.append (tableName, foreignKey.referencedTableName())
00336 dCircTable.append (tableName, foreignKey.referencedTableName())
00337 columnNamesR = foreignKey.referencedColumnNames()
00338
00339
00340 d1=mseqdict( [], {})
00341 d2=mseqdict( [], {})
00342
00343 for rTable, refTable in dCircTable.items():
00344 for table in refTable:
00345 d1.append(rTable,table)
00346
00347 dCircTable.swap()
00348 for rTable, refTable in dCircTable.items():
00349 for table in refTable:
00350 d2.append(rTable,table)
00351
00352 for key,value in d1.items():
00353 firsttable=key
00354 secondtable=value
00355 for key,value in d2.items():
00356 if key==firsttable and value==secondtable:
00357 raise Exception ("Circular Dependency exists between tables : "+firsttable,secondtable)
00358
00359
00360 r1=mseqdict( [], {})
00361 r2=mseqdict( [], {})
00362
00363 for rTable, refTable in dTable.items():
00364 for table in refTable:
00365 r1.append(table,'')
00366 r1.append(rTable,'')
00367
00368 for rTable, refTable in r1.items():
00369 test=rTable
00370 for rTable1, refTable1 in dTable.items():
00371 if rTable1==test:
00372 for table in refTable1:
00373 if rTable1!=table:
00374 r2.append(table,'')
00375
00376 for key,value in r2.items():
00377 r1.remove(key,'')
00378
00379 for key,value in r2.items():
00380 dTempTable.append(key,'')
00381
00382 for key,value in r1.items():
00383 dTempTable.append(key,'')
00384
00385 for key,value in dTempTable.items():
00386 iTable= key
00387 for table in tableset:
00388 if table==iTable:
00389 dCopyTable.append(key,'')
00390
00391 return dCopyTable
00392
00393 except Exception, e:
00394 raise Exception (" " + str(e))
00395 return False
00396
00397 if __name__ == "__main__":
00398 svc = coral.ConnectionService()
00399 session = svc.connect( 'sqlite_file:source.db',
00400 accessMode = coral.access_Update )
00401 session.transaction().start(True)
00402 schema=session.nominalSchema()
00403 print 'TEST listobjects'
00404 print listobjects(schema)
00405 print 'TEST dumpobjectlist'
00406 dumpobjectlist(schema)
00407 print 'TEST listtables'
00408 print listtables(schema,'TAGINVENTORY_TABLE')
00409
00410 session.transaction().commit()
00411 del session