forked from cms-tsg-fog/RateMon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbexplorer.py
71 lines (58 loc) · 2.68 KB
/
dbexplorer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# Author: Nathaniel Rupprecht
# Created: June 29, 2015
# Last Modified: August 17, 2015
import cx_Oracle
# FILE NAME
fileName = "out.txt"
# Use: Prints all the table and column data available to the given cursor to the given file
def getData(curs, file):
# Get pairs { table name, column name }
curs.execute("select owner, table_name, column_name from all_tab_columns")
columns = {} # A dictionary
# Put our data into the dictionary
count = 0
for owner, table_name, column_name in curs.fetchall():
if not columns.has_key(table_name): # Create the entry if it does not exist
columns[table_name]=[owner, [column_name]]
elif not column_name in columns[table_name][1]:
columns[table_name][1].append(column_name)
else: continue # Make sure we aren't double counting columns
count += 1
# Do some counting
nColumns = count
nTables = len(columns)
# Write DB table info
for table_name in sorted(columns):
number = len(columns[table_name][1])
owner = columns[table_name][0]
file.write(table_name + ":\nThere are %s columns. Table Owner: %s\nFull Name: %s.%s\n| " % (number, owner, owner, table_name))
for col in columns[table_name][1]:
file.write(col+" | ")
file.write("\n\n")
return [nTables, nColumns]
# main function
if __name__ == "__main__":
file = open(fileName, "wb") # Open file
# Get the HLT cursor
orcl = cx_Oracle.connect(user='cms_hlt_r',password='convertMe!',dsn='cms_omds_lb')
curs = orcl.cursor()
file.write("***********************************************************************************\n")
file.write("CMS HLT DATABASE\n")
file.write("***********************************************************************************\n\n\n")
nHLTTables, nHLTColumns = getData(curs, file)
# Get the trigger cursor
orcl = cx_Oracle.connect(user='cms_trg_r',password='X3lmdvu4',dsn='cms_omds_lb')
curs = orcl.cursor()
file.write("***********************************************************************************\n")
file.write("CMS TRG DATABASE\n")
file.write("***********************************************************************************\n\n\n")
nTrgTables, nTrgColumns = getData(curs, file)
# Find totals
nTables = nHLTTables + nTrgTables
nColumns = nHLTColumns + nTrgColumns
# Print footer
file.write("Fun facts: There are %s tables in the system, with a total of %s columns.\n\n\n" % (nTables, nColumns))
file.write("Thank you for reading. That is all.")
file.close() # Close file
print "Database summary written to %s" % (fileName)