annotate sqlpython/sqlpython.py @ 358:cdd403e73132

v 1.6.6
author catherine@cordelia
date Thu, 28 May 2009 17:37:20 -0400
parents 9960bece1e88
children 477f0bf652b2
rev   line source
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
1 #
358
cdd403e73132 v 1.6.6
catherine@cordelia
parents: 357
diff changeset
2 # SqlPython V1.6.6
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
3 # Author: Luca.Canali@cern.ch, Apr 2006
357
9960bece1e88 added defaultdict backport
catherine@cordelia
parents: 353
diff changeset
4 # Rev 1-May-09
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
5 #
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
6 # A python module to reproduce Oracle's command line 'sqlplus-like' within python
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
7 # Intended to allow easy customizations and extentions
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
8 # Best used with the companion modules sqlpyPlus and mysqlpy
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
9 # See also http://twiki.cern.ch/twiki/bin/view/PSSGroup/SqlPython
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
10
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
11 import cmd2,getpass,binascii,cx_Oracle,re,os
339
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
12 import sqlpyPlus, sqlalchemy, pyparsing
358
cdd403e73132 v 1.6.6
catherine@cordelia
parents: 357
diff changeset
13 __version__ = '1.6.6'
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
14
339
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
15 class Parser(object):
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
16 comment_def = "--" + ~ ('-' + pyparsing.CaselessKeyword('begin')) + pyparsing.ZeroOrMore(pyparsing.CharsNotIn("\n"))
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
17 def __init__(self, scanner, retainSeparator=True):
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
18 self.scanner = scanner
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
19 self.scanner.ignore(pyparsing.sglQuotedString)
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
20 self.scanner.ignore(pyparsing.dblQuotedString)
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
21 self.scanner.ignore(self.comment_def)
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
22 self.scanner.ignore(pyparsing.cStyleComment)
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
23 self.retainSeparator = retainSeparator
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
24 def separate(self, txt):
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
25 itms = []
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
26 for (sqlcommand, start, end) in self.scanner.scanString(txt):
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
27 if sqlcommand:
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
28 if type(sqlcommand[0]) == pyparsing.ParseResults:
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
29 if self.retainSeparator:
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
30 itms.append("".join(sqlcommand[0]))
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
31 else:
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
32 itms.append(sqlcommand[0][0])
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
33 else:
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
34 if sqlcommand[0]:
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
35 itms.append(sqlcommand[0])
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
36 return itms
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
37
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
38
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
39 class sqlpython(cmd2.Cmd):
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
40 '''A python module to reproduce Oracle's command line with focus on customization and extention'''
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
41
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
42 def __init__(self):
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
43 cmd2.Cmd.__init__(self)
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
44 self.no_connection()
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
45 self.maxfetch = 1000
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
46 self.terminator = ';'
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
47 self.timeout = 30
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
48 self.commit_on_exit = True
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
49 self.connections = {}
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
50
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
51 def no_connection(self):
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
52 self.prompt = 'SQL.No_Connection> '
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
53 self.curs = None
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
54 self.conn = None
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
55 self.connection_number = None
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
56
317
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
57 def make_connection_current(self, connection_number):
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
58 self.conn = self.connections[connection_number]['conn']
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
59 self.prompt = self.connections[connection_number]['prompt']
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
60 self.rdbms = self.connections[connection_number]['rdbms']
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
61 self.connection_number = connection_number
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
62 self.curs = self.conn.cursor()
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
63
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
64 def successful_connection_to_number(self, arg):
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
65 try:
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
66 connection_number = int(arg)
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
67 except ValueError:
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
68 return False
317
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
69 self.make_connection_current(connection_number)
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
70 if (self.rdbms == 'oracle') and self.serveroutput:
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
71 self.curs.callproc('dbms_output.enable', [])
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
72 return True
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
73
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
74 def list_connections(self):
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
75 self.stdout.write('Existing connections:\n')
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
76 self.stdout.write('\n'.join('%s (%s)' % (v['prompt'], v['rdbms'])
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
77 for (k,v) in sorted(self.connections.items())) + '\n')
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
78
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
79 def disconnect(self, arg):
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
80 try:
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
81 connection_number = int(arg)
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
82 connection = self.connections[connection_number]
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
83 except (ValueError, KeyError):
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
84 self.list_connections()
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
85 return
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
86 if self.commit_on_exit:
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
87 connection['conn'].commit()
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
88 self.connections.pop(connection_number)
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
89 if connection_number == self.connection_number:
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
90 self.no_connection()
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
91
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
92 def closeall(self):
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
93 for connection_number in self.connections.keys():
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
94 self.disconnect(connection_number)
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
95 self.curs = None
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
96 self.no_connection()
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
97
319
3c58df9bcf14 now relying entirely on sqlalchemy connections
Catherine Devlin <catherine.devlin@gmail.com>
parents: 317
diff changeset
98 def url_connect(self, arg):
3c58df9bcf14 now relying entirely on sqlalchemy connections
Catherine Devlin <catherine.devlin@gmail.com>
parents: 317
diff changeset
99 eng = sqlalchemy.create_engine(arg)
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
100 self.conn = eng.connect().connection
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
101 conn = {'conn': self.conn, 'prompt': self.prompt, 'dbname': eng.url.database,
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
102 'rdbms': eng.url.drivername, 'user': eng.url.username or '',
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
103 'eng': eng}
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
104 return conn
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
105 def ora_connect(self, arg):
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
106 modeval = 0
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
107 oraserv = None
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
108 for modere, modevalue in self.connection_modes.items():
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
109 if modere.search(arg):
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
110 arg = modere.sub('', arg)
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
111 modeval = modevalue
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
112 try:
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
113 orauser, oraserv = arg.split('@')
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
114 except ValueError:
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
115 try:
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
116 oraserv = os.environ['ORACLE_SID']
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
117 except KeyError:
333
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
118 self.perror('instance not specified and environment variable ORACLE_SID not set')
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
119 return
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
120 orauser = arg
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
121 sid = oraserv
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
122 try:
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
123 host, sid = oraserv.split('/')
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
124 try:
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
125 host, port = host.split(':')
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
126 port = int(port)
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
127 except ValueError:
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
128 port = 1521
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
129 oraserv = cx_Oracle.makedsn(host, port, sid)
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
130 except ValueError:
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
131 pass
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
132 try:
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
133 orauser, orapass = orauser.split('/')
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
134 except ValueError:
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
135 orapass = getpass.getpass('Password: ')
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
136 if orauser.upper() == 'SYS' and not modeval:
333
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
137 self.pfeedback('Privilege not specified for SYS, assuming SYSOPER')
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
138 modeval = cx_Oracle.SYSOPER
321
0c83ddee3a5c trail tnsname with slash to attach mode parameter to url login
Catherine Devlin <catherine.devlin@gmail.com>
parents: 319
diff changeset
139 result = self.url_connect('oracle://%s:%s@%s/?mode=%d' % (orauser, orapass, oraserv, modeval))
319
3c58df9bcf14 now relying entirely on sqlalchemy connections
Catherine Devlin <catherine.devlin@gmail.com>
parents: 317
diff changeset
140 result['dbname'] = oraserv
3c58df9bcf14 now relying entirely on sqlalchemy connections
Catherine Devlin <catherine.devlin@gmail.com>
parents: 317
diff changeset
141 return result
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
142
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
143 connection_modes = {re.compile(' AS SYSDBA', re.IGNORECASE): cx_Oracle.SYSDBA,
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
144 re.compile(' AS SYSOPER', re.IGNORECASE): cx_Oracle.SYSOPER}
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
145 @cmd2.options([cmd2.make_option('-a', '--add', action='store_true',
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
146 help='add connection (keep current connection)'),
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
147 cmd2.make_option('-c', '--close', action='store_true',
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
148 help='close connection {N} (or current)'),
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
149 cmd2.make_option('-C', '--closeall', action='store_true',
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
150 help='close all connections'),])
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
151 def do_connect(self, arg, opts):
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
152 '''Opens the DB connection'''
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
153 if opts.closeall:
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
154 self.closeall()
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
155 return
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
156 if opts.close:
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
157 if not arg:
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
158 arg = self.connection_number
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
159 self.disconnect(arg)
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
160 return
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
161 if not arg:
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
162 self.list_connections()
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
163 return
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
164 try:
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
165 if self.successful_connection_to_number(arg):
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
166 return
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
167 except IndexError:
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
168 self.list_connections()
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
169 return
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
170 try:
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
171 connect_info = self.url_connect(arg)
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
172 except sqlalchemy.exc.ArgumentError, e:
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
173 connect_info = self.ora_connect(arg)
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
174 if opts.add or (self.connection_number is None):
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
175 try:
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
176 self.connection_number = max(self.connections.keys()) + 1
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
177 except ValueError:
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
178 self.connection_number = 0
317
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
179 connect_info['prompt'] = '%d:%s@%s> ' % (self.connection_number, connect_info['user'], connect_info['dbname'])
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
180 self.connections[self.connection_number] = connect_info
317
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
181 self.make_connection_current(self.connection_number)
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
182 self.curs = self.conn.cursor()
317
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
183 if (self.rdbms == 'oracle') and self.serveroutput:
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
184 self.curs.callproc('dbms_output.enable', [])
335
00b183a103b3 bare prefix switches connection
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
185 if (self.rdbms == 'mysql'):
00b183a103b3 bare prefix switches connection
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
186 self.curs.execute('SET SQL_MODE=ANSI')
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
187 def postparsing_precmd(self, statement):
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
188 stop = 0
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
189 self.saved_connection_number = None
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
190 if statement.parsed.connection_number:
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
191 saved_connection_number = self.connection_number
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
192 try:
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
193 if self.successful_connection_to_number(statement.parsed.connection_number):
335
00b183a103b3 bare prefix switches connection
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
194 if statement.parsed.command:
00b183a103b3 bare prefix switches connection
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
195 self.saved_connection_number = saved_connection_number
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
196 except KeyError:
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
197 self.list_connections()
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
198 raise KeyError, 'No connection #%s' % statement.parsed.connection_number
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
199 return stop, statement
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
200 def postparsing_postcmd(self, stop):
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
201 if self.saved_connection_number is not None:
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
202 self.successful_connection_to_number(self.saved_connection_number)
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
203 return stop
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
204
256
d09f16b71f66 do_host
catherine@Elli.myhome.westell.com
parents: 255
diff changeset
205 do_host = cmd2.Cmd.do_shell
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
206
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
207 def emptyline(self):
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
208 pass
264
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
209
265
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
210 def _show_errors(self, all_users=False, limit=None, mintime=None, targets=[]):
264
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
211 if all_users:
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
212 user = ''
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
213 else:
265
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
214 user = "AND ao.owner = user\n"
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
215 if targets:
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
216 target = 'AND (%s)\n' % ' OR '.join("ae.type || '/' || ae.name LIKE '%s'" %
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
217 t.upper().replace('*','%') for t in targets)
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
218 else:
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
219 target = ''
264
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
220 self.curs.execute('''
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
221 SELECT ae.owner, ae.name, ae.type, ae.position, ae.line, ae.attribute,
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
222 ae.text error_text,
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
223 src.text object_text,
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
224 ao.last_ddl_time
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
225 FROM all_errors ae
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
226 JOIN all_objects ao ON ( ae.owner = ao.owner
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
227 AND ae.name = ao.object_name
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
228 AND ae.type = ao.object_type)
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
229 JOIN all_source src ON ( ae.owner = src.owner
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
230 AND ae.name = src.name
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
231 AND ae.type = src.type
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
232 AND ae.line = src.line)
265
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
233 WHERE 1=1
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
234 %s%sORDER BY ao.last_ddl_time DESC''' % (user, target))
264
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
235 if limit is None:
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
236 errors = self.curs.fetchall()
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
237 else:
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
238 errors = self.curs.fetchmany(numRows = limit)
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
239 for err in errors:
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
240 if (mintime is not None) and (err[8] < mintime):
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
241 break
333
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
242 self.poutput('%s at line %d of %s %s.%s:' % (err[5], err[4], err[2], err[0], err[1]))
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
243 self.poutput(err[7])
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
244 self.poutput((' ' * (err[3]-1)) + '^')
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
245 self.poutput(err[6])
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
246 self.poutput('\n')
264
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
247
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
248 def current_database_time(self):
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
249 self.curs.execute('select sysdate from dual')
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
250 return self.curs.fetchone()[0]
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
251
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
252 def do_terminators(self, arg):
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
253 """; standard Oracle format
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
254 \\c CSV (with headings)
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
255 \\C CSV (no headings)
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
256 \\g list
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
257 \\G aligned list
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
258 \\h HTML table
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
259 \\i INSERT statements
266
342e96de6de6 json not quite there
catherine@Elli.myhome.westell.com
parents: 265
diff changeset
260 \\j JSON
308
4d24fea42364 py scripts working again
catherine@Elli.myhome.westell.com
parents: 298
diff changeset
261 \\r ReStructured Text
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
262 \\s CSV (with headings)
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
263 \\S CSV (no headings)
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
264 \\t transposed
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
265 \\x XML
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
266 \\l line plot, with markers
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
267 \\L scatter plot (no lines)
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
268 \\b bar graph
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
269 \\p pie chart"""
333
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
270 self.poutput(self.do_terminators.__doc__)
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
271
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
272 terminatorSearchString = '|'.join('\\' + d.split()[0] for d in do_terminators.__doc__.splitlines())
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
273
339
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
274 bindScanner = {'oracle': Parser(pyparsing.Literal(':') + pyparsing.Word( pyparsing.alphanums + "_$#" )),
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
275 'postgres': Parser(pyparsing.Literal('%(') +
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
276 pyparsing.Word(pyparsing.alphanums + "_$#") + ')s')}
340
001d01eeac90 bind vars for postgres
Catherine Devlin <catherine.devlin@gmail.com>
parents: 339
diff changeset
277 def findBinds(self, target, givenBindVars = {}):
339
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
278 result = givenBindVars
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
279 if self.rdbms in self.bindScanner:
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
280 for finding, startat, endat in self.bindScanner[self.rdbms].scanner.scanString(target):
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
281 varname = finding[1]
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
282 try:
340
001d01eeac90 bind vars for postgres
Catherine Devlin <catherine.devlin@gmail.com>
parents: 339
diff changeset
283 result[varname] = self.binds[varname]
339
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
284 except KeyError:
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
285 if not givenBindVars.has_key(varname):
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
286 print 'Bind variable %s not defined.' % (varname)
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
287 return result
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
288
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
289 def default(self, arg):
340
001d01eeac90 bind vars for postgres
Catherine Devlin <catherine.devlin@gmail.com>
parents: 339
diff changeset
290 self.varsUsed = self.findBinds(arg, givenBindVars={})
247
f0f293d83337 begin docs
catherine@dellzilla
parents: 246
diff changeset
291 ending_args = arg.lower().split()[-2:]
f0f293d83337 begin docs
catherine@dellzilla
parents: 246
diff changeset
292 if 'end' in ending_args:
246
b5d4a122354a can round-trip a package now
catherine@dellzilla
parents: 242
diff changeset
293 command = '%s %s;'
b5d4a122354a can round-trip a package now
catherine@dellzilla
parents: 242
diff changeset
294 else:
264
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
295 command = '%s %s'
317
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
296 if self.rdbms == 'oracle':
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
297 current_time = self.current_database_time()
263
362db47c17b4 error reported
catherine@Elli.myhome.westell.com
parents: 261
diff changeset
298 self.curs.execute(command % (arg.parsed.command, arg.parsed.args), self.varsUsed)
276
0b7031c2229e autobind unit test failing
catherine@Elli.myhome.westell.com
parents: 266
diff changeset
299 executionmessage = '\nExecuted%s\n' % ((self.curs.rowcount > 0) and ' (%d rows)' % self.curs.rowcount or '')
317
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
300 if self.rdbms == 'oracle':
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
301 self._show_errors(all_users=True, limit=1, mintime=current_time)
333
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
302 self.pfeedback(executionmessage)
198
b2d8bf5f89db merged with changes from work
catherine@Elli.myhome.westell.com
parents: 197
diff changeset
303
b2d8bf5f89db merged with changes from work
catherine@Elli.myhome.westell.com
parents: 197
diff changeset
304 def do_commit(self, arg=''):
b2d8bf5f89db merged with changes from work
catherine@Elli.myhome.westell.com
parents: 197
diff changeset
305 self.default(self.parsed('commit %s;' % (arg)))
b2d8bf5f89db merged with changes from work
catherine@Elli.myhome.westell.com
parents: 197
diff changeset
306 def do_rollback(self, arg=''):
b2d8bf5f89db merged with changes from work
catherine@Elli.myhome.westell.com
parents: 197
diff changeset
307 self.default(self.parsed('rollback %s;' % (arg)))
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
308 def do_quit(self, arg):
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
309 if self.commit_on_exit:
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
310 self.closeall()
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
311 return cmd2.Cmd.do_quit(self, None)
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
312 do_exit = do_quit
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
313 do_q = do_quit
337
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
314 colorcodes = {'bold':{True:'\x1b[1m',False:'\x1b[22m'},
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
315 'red':{True:'\x1b[36m',False:'\x1b[39m'},
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
316 'cyan':{True:'\x1b[31m',False:'\x1b[39m'},
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
317 'underline':{True:'\x1b[4m',False:'\x1b[24m'}}
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
318 colors = True
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
319 def colorize(self, val, color):
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
320 if self.colors and (self.stdout == self.initial_stdout):
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
321 if color not in self.colorcodes:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
322 if (color % 2):
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
323 color = 'red'
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
324 else:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
325 color = 'cyan'
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
326 return self.colorcodes[color][True] + val + self.colorcodes[color][False]
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
327 return val
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
328 def pmatrix(self,rows,desc,maxlen=30,heading=True,restructuredtext=False):
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
329 '''prints a matrix, used by sqlpython to print queries' result sets'''
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
330 names = []
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
331 maxen = []
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
332 toprint = []
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
333 for d in desc:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
334 n = d[0]
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
335 names.append(n) # list col names
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
336 maxen.append(len(n)) # col length
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
337 rcols = range(len(desc))
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
338 rrows = range(len(rows))
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
339 for i in rrows: # loops for all rows
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
340 rowsi = map(str, rows[i]) # current row to process
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
341 split = [] # service var is row split is needed
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
342 mustsplit = 0 # flag
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
343 for j in rcols:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
344 if str(desc[j][1]) == "<type 'cx_Oracle.BINARY'>": # handles RAW columns
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
345 rowsi[j] = binascii.b2a_hex(rowsi[j])
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
346 maxen[j] = max(maxen[j], len(rowsi[j])) # computes max field length
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
347 if maxen[j] <= maxlen:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
348 split.append('')
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
349 else: # split the line is 2 because field is too long
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
350 mustsplit = 1
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
351 maxen[j] = maxlen
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
352 split.append(rowsi[j][maxlen-1:2*maxlen-1])
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
353 rowsi[j] = rowsi[j][0:maxlen-1] # this implem. truncates after maxlen*2
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
354 toprint.append(rowsi) # 'toprint' is a printable copy of rows
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
355 if mustsplit != 0:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
356 toprint.append(split)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
357 sepcols = []
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
358 for i in rcols:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
359 maxcol = maxen[i]
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
360 name = names[i]
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
361 sepcols.append("-" * maxcol) # formats column names (header)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
362 names[i] = name + (" " * (maxcol-len(name))) # formats separ line with --
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
363 rrows2 = range(len(toprint))
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
364 for j in rrows2:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
365 val = toprint[j][i]
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
366 if str(desc[i][1]) == "<type 'cx_Oracle.NUMBER'>": # right align numbers
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
367 toprint[j][i] = (" " * (maxcol-len(val))) + val
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
368 else:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
369 toprint[j][i] = val + (" " * (maxcol-len(val)))
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
370 toprint[j][i] = self.colorize(toprint[j][i], i)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
371 for j in rrows2:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
372 toprint[j] = ' '.join(toprint[j])
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
373 names = [self.colorize(name, n) for (n, name) in enumerate(names)]
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
374 names = ' '.join(names)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
375 names = self.colorize(names, 'bold')
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
376 sepcols = ' '.join(sepcols)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
377 if heading or restructuredtext:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
378 toprint.insert(0, sepcols)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
379 toprint.insert(0, names)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
380 if restructuredtext:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
381 toprint.insert(0, sepcols)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
382 toprint.append(sepcols)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
383 return '\n'.join(toprint)
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
384
337
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
385
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
386
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
387
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
388