Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 126 lines (112 sloc) 4.27 kb
dfd22ee kchiogio Added function to read commands from file
kchiogio authored
1 #!/usr/bin/python
2
1885a43 kchiogio added author comment
kchiogio authored
3 #Remy Baumgarten
4 #Kevin Chiogioji
5
a490264 Remy broke down program into assignments and uncoupled functions
authored
6 import ibm_db
04e5312 Remy Grabbing connections from dtables that match a given table named parsed ...
authored
7 import antlr3
8 import antlr3.tree
9 from sqlLexer import sqlLexer
10 from sqlParser import sqlParser
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
11
12 #show stats on database
35b1188 Remy added multiple instances variables to connect to, SAMPLE database can be...
authored
13 def client_print(db):
14 client = ibm_db.client_info(db)
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
15 print "DRIVER_NAME: string(%d) \"%s\"" % (len(client.DRIVER_NAME), client.DRIVER_NAME)
16 print "DRIVER_VER: string(%d) \"%s\"" % (len(client.DRIVER_VER), client.DRIVER_VER)
17 print "DATA_SOURCE_NAME: string(%d) \"%s\"" % (len(client.DATA_SOURCE_NAME), client.DATA_SOURCE_NAME)
18 print "DRIVER_ODBC_VER: string(%d) \"%s\"" % (len(client.DRIVER_ODBC_VER), client.DRIVER_ODBC_VER)
19 print "ODBC_VER: string(%d) \"%s\"" % (len(client.ODBC_VER), client.ODBC_VER)
7dc30ba Remy added some sample config reading functions
authored
20 print "ODBC_SQL_CONFORMANCE: string(%d) \"%s\"" % (len(client.ODBC_SQL_CONFORMANCE), client.ODBC_SQL_CONFORMANCE)
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
21 print "APPL_CODEPAGE: int(%s)" % client.APPL_CODEPAGE
22 print "CONN_CODEPAGE: int(%s)" % client.CONN_CODEPAGE
23
eee166a Remy added Threading for remote connections and real configuration parsing an...
authored
24 # execute query
25 def exec_query(db, query):
26 try:
27 ibm_db.exec_immediate(db,query)
28 except:
2f663b8 Remy Catalog creation and section added to config
authored
29 print "[*] The transaction could not be completed:", query #ibm_db.stmt_errormsg()
eee166a Remy added Threading for remote connections and real configuration parsing an...
authored
30 else:
2f663b8 Remy Catalog creation and section added to config
authored
31 print "[*] Transaction complete: ",query
ef32a2b Remy I added some sample functions to interact with the DB2 instance
authored
32
2f663b8 Remy Catalog creation and section added to config
authored
33 # check to see if dtables in CATALOG exists, if not, create it
34 def create_catalog(cat, catalog):
35 try:
36 ibm_db.exec_immediate(cat,"CREATE TABLE "+catalog['table'])
37 except:
b465195 Remy Added cataloging, it was missing, fixed issue with catalog insertion not...
authored
38 print "[*] NOTICE catalog table exists, continuing..."
2f663b8 Remy Catalog creation and section added to config
authored
39
40 # insert metadata
9168ef5 kchiogio Added function to read DDLs
kchiogio authored
41 def insert_catalog_row(query, conn, node_conf):
765f7c4 kchiogio Edited ddl.txt file
kchiogio authored
42 index = query.split()
43 if index[0].upper() == "CREATE" or index[0].upper() == "DROP":
44 tableName = index[2]
45 elif index[0].upper() == "SELECT":
46 tableName = index[3]
fc3e0cd kchiogio Fixed table name reading bug
kchiogio authored
47
765f7c4 kchiogio Edited ddl.txt file
kchiogio authored
48 if tableName.find("(") != -1:
49 tableName = tableName[0:tableName.find("(")]
fc3e0cd kchiogio Fixed table name reading bug
kchiogio authored
50
765f7c4 kchiogio Edited ddl.txt file
kchiogio authored
51 cat_row = "INSERT INTO dtables (tname, nodedriver, nodeurl, nodeuser," \
b465195 Remy Added cataloging, it was missing, fixed issue with catalog insertion not...
authored
52 " nodepasswd, partmtd, partparam1, partparam2) VALUES ('%s', '%s', '%s', '%s', '%s', %s, '%s', '%s');" % (tableName.rstrip(";"), node_conf["driver"], node_conf ["hostname"], node_conf["username"], node_conf["passwd"], "NULL", "NULL", "NULL")
a138164 Remy removed extra functions and cleaned up debug
authored
53 #print cat_row
b465195 Remy Added cataloging, it was missing, fixed issue with catalog insertion not...
authored
54 stmt = ibm_db.exec_immediate(conn,cat_row)
55 print "[*] Cataloging transaction...done"
eee166a Remy added Threading for remote connections and real configuration parsing an...
authored
56
dfd22ee kchiogio Added function to read commands from file
kchiogio authored
57 # read DDLs from file
9168ef5 kchiogio Added function to read DDLs
kchiogio authored
58 def readDDL(fileName):
dfd22ee kchiogio Added function to read commands from file
kchiogio authored
59 try:
9168ef5 kchiogio Added function to read DDLs
kchiogio authored
60 f = open(fileName,'r')
dfd22ee kchiogio Added function to read commands from file
kchiogio authored
61 commands = f.readlines()
62 for command in commands:
63 command = command.strip()
64 return commands
65 except:
66 print "the file could not be read\n"
67 else:
68 f.close()
eee166a Remy added Threading for remote connections and real configuration parsing an...
authored
69
04e5312 Remy Grabbing connections from dtables that match a given table named parsed ...
authored
70 # Prints the contents of the Query
b52381e Remy finished assignment part 3
authored
71 def print_results(conn,sql):
72 #sql = "SELECT " + column + " FROM " + table + ";"
73 #print sql
8a7028d checking in part3 processor and addition of query printer for main funct...
Remy authored
74 stmt = ibm_db.exec_immediate(conn,sql)
75 dictionary = ibm_db.fetch_assoc(stmt)
76 while dictionary != False:
77 print '==================='
36fc430 Remy changed table contents for row interator called via part3 grammer query
authored
78 print "ISBN: ", dictionary["ISBN"]
79 print "Title: ",dictionary["TITLE"]
80 print "Author: ",dictionary["AUTHOR"]
8a7028d checking in part3 processor and addition of query printer for main funct...
Remy authored
81 dictionary = ibm_db.fetch_assoc(stmt)
82
04e5312 Remy Grabbing connections from dtables that match a given table named parsed ...
authored
83 # Get nodes from catalog for a specific table
84 # and return it as a list (url, user,passwd)
85 def get_nodes(conn,tablename):
86 sql = "SELECT DISTINCT * FROM DTABLES WHERE TNAME = '" + tablename + "';"
87 print "\t",sql
88 stmt = ibm_db.exec_immediate(conn,sql)
89 dictionary = ibm_db.fetch_assoc(stmt)
90 nodes = []
91 while dictionary != False:
92 url = dictionary["NODEURL"].rstrip()
93 user = dictionary["NODEUSER"]
94 passwd = dictionary["NODEPASSWD"]
95 node = (url,user,passwd)
96 nodes.append(node)
97 dictionary = ibm_db.fetch_assoc(stmt)
98 return nodes
99
100 # returns a list of tuples for each line in the csv file
101 def loadCSV(filename):
102 contents = []
103 for line in open(filename):
104 fields = line.split(',');
105 number = fields[0]
106 title = fields[1]
107 author = fields[2]
108 book = (number,title,author)
109 contents.append(book)
110 return contents
e9407f5 changed main.py to be loaded as module
Remy authored
111
04e5312 Remy Grabbing connections from dtables that match a given table named parsed ...
authored
112 # parse to obtain tablename
113 def get_table(sql):
114 stream = antlr3.ANTLRStringStream(sql)
115 lexer = sqlLexer(stream)
116 tokens = antlr3.CommonTokenStream(lexer)
117 parser = sqlParser(tokens)
118 r = parser.sqlstmt()
119 tree = r.tree.toStringTree()
120 if( r.tree.children[0].toString().lower() == "create"):
121 return r.tree.children[2].toString()
122 elif( r.tree.children[0].toString().lower() == "select"):
123 return r.tree.children[3].toString()
124 elif( r.tree.children[0].toString().lower() == "drop"):
125 return r.tree.children[1].toString()
Something went wrong with that request. Please try again.