annotate sqlpython/sqlpython.py @ 409:5b88ce5f31ff

ugh, trying to separate -- comments from --flags
author catherine@DellZilla
date Thu, 15 Oct 2009 17:39:54 -0400
parents 188c86d4a11e
children 3f566f30d14d
rev   line source
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
1 #
368
477f0bf652b2 version to 1.6.7
catherine@cordelia
parents: 358
diff changeset
2 # SqlPython V1.6.7
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
3 # Author: Luca.Canali@cern.ch, Apr 2006
380
catherine@cordelia
parents: 375
diff changeset
4 # Rev 2-Sep-09
368
477f0bf652b2 version to 1.6.7
catherine@cordelia
parents: 358
diff changeset
5
189
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
375
9d0a3ab7f573 set colors off for Windows
devlinjs@A0266D4FVTK81.wrightpatterson.afmc.ds.af.mil
parents: 374
diff changeset
11 import cmd2,getpass,binascii,cx_Oracle,re,os,platform
384
b9fedbfbec79 ls now works with suggested changes to Gerald
catherine@cordelia
parents: 383
diff changeset
12 import sqlalchemy, pyparsing, schemagroup
380
catherine@cordelia
parents: 375
diff changeset
13 __version__ = '1.6.8'
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):
409
5b88ce5f31ff ugh, trying to separate -- comments from --flags
catherine@DellZilla
parents: 407
diff changeset
16 comment_def = "--" + pyparsing.NotAny('-' + pyparsing.CaselessKeyword('begin')) + pyparsing.ZeroOrMore(pyparsing.CharsNotIn("\n"))
339
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
378
05d2de3e7ea8 more gerald generalization
catherine@cordelia
parents: 377
diff changeset
101 user = eng.url.username or ''
05d2de3e7ea8 more gerald generalization
catherine@cordelia
parents: 377
diff changeset
102 rdbms = eng.url.drivername
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
103 conn = {'conn': self.conn, 'prompt': self.prompt, 'dbname': eng.url.database,
378
05d2de3e7ea8 more gerald generalization
catherine@cordelia
parents: 377
diff changeset
104 'rdbms': rdbms, 'user': user, 'eng': eng,
388
841573c48676 cleaning up persistent hg fail
catherine@DellZilla
parents: 387
diff changeset
105 'schemas': schemagroup.SchemaDict({},
405
1e3179b31a1e nice progress on tab completion
catherine@cordelia
parents: 400
diff changeset
106 rdbms=rdbms, user=user, connection=self.conn, connection_string=arg)}
378
05d2de3e7ea8 more gerald generalization
catherine@cordelia
parents: 377
diff changeset
107 s = conn['schemas']
389
53ee70e9417e oops, one more change propagated from hg messup
catherine@DellZilla
parents: 388
diff changeset
108 s.refresh_asynch()
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
109 return conn
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
110
407
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
111 legal_sql_word = pyparsing.Word(pyparsing.alphanums + '_$#')
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
112 legal_hostname = pyparsing.Word(pyparsing.alphanums + '_-.')('host') + pyparsing.Optional(
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
113 ':' + pyparsing.Word(pyparsing.nums)('port'))
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
114 oracle_connect_parser = legal_sql_word('username') + (
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
115 pyparsing.Optional('/' + pyparsing.CharsNotIn('@')("password")) +
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
116 pyparsing.Optional('@' + pyparsing.Optional(legal_hostname + '/') +
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
117 legal_sql_word('db_name')) +
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
118 pyparsing.Optional(pyparsing.CaselessKeyword('as') +
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
119 (pyparsing.CaselessKeyword('sysoper') ^
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
120 pyparsing.CaselessKeyword('sysdba'))('mode')))
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
121 postgresql_connect_parser = (legal_sql_word('db_name') +
409
5b88ce5f31ff ugh, trying to separate -- comments from --flags
catherine@DellZilla
parents: 407
diff changeset
122 pyparsing.Optional(legal_sql_word('username')))
407
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
123
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
124 def connect_url(self, arg, opts):
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
125 rdbms = opts.rdbms or self.default_rdbms
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
126
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
127 mode = 0
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
128 host = None
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
129 port = None
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
130
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
131 if rdbms == 'oracle':
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
132 result = self.oracle_connect_parser.parseString(arg)
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
133 if result.mode == 'sysdba':
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
134 mode = cx_Oracle.SYSDBA
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
135 elif result.mode == 'sysoper':
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
136 mode = cx_Oracle.SYSOPER
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
137 else:
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
138 mode = 0
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
139 elif rdbms == 'postgres':
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
140 result = self.postgresql_connect_parser.parseString(arg)
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
141 port = opts.port or os.environ.get('PGPORT') or 5432
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
142 host = opts.host or os.environ.get('PGHOST') or 'localhost'
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
143
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
144 username = result.username or opts.username
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
145 if not username and rdbms == 'postgres':
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
146 username = os.environ.get('PGUSER') or os.environ.get('USER')
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
147
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
148 db_name = result.db_name or opts.database
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
149 if not db_name:
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
150 if rdbms == 'oracle':
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
151 db_name = os.environ.get('ORACLE_SID')
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
152 elif rdbms == 'postgres':
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
153 db_name = os.environ.get('PGDATABASE') or username
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
154
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
155 password = result.password or getpass.getpass('Password: ')
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
156
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
157 if host:
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
158 if port:
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
159 host = '%s:%s' % (host, port)
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
160 db_name = '%s/%s' % (host, db_name)
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
161
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
162 url = '%s://%s:%s@%s' % (rdbms, username, password, db_name)
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
163 if mode:
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
164 url = '%s/?mode=%d' % mode
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
165 return url
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
166
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
167 @cmd2.options([cmd2.make_option('-a', '--add', action='store_true',
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
168 help='add connection (keep current connection)'),
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
169 cmd2.make_option('-c', '--close', action='store_true',
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
170 help='close connection {N} (or current)'),
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
171 cmd2.make_option('-C', '--closeall', action='store_true',
407
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
172 help='close all connections'),
409
5b88ce5f31ff ugh, trying to separate -- comments from --flags
catherine@DellZilla
parents: 407
diff changeset
173 cmd2.make_option('--postgres', action='store_true', help='Connect to a postgreSQL database'),
5b88ce5f31ff ugh, trying to separate -- comments from --flags
catherine@DellZilla
parents: 407
diff changeset
174 cmd2.make_option('--oracle', action='store_true', help='Connect to an Oracle database'),
5b88ce5f31ff ugh, trying to separate -- comments from --flags
catherine@DellZilla
parents: 407
diff changeset
175 cmd2.make_option('--mysql', action='store_true', help='Connect to a MySQL database'),
407
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
176 cmd2.make_option('-r', '--rdbms', type='string',
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
177 help='Type of database to connect to (oracle, postgres, mysql)'),
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
178 cmd2.make_option('-H', '--host', type='string',
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
179 help='Host to connect to (postgresql only)'),
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
180 cmd2.make_option('-p', '--port', type='int',
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
181 help='Port to connect to (postgresql only)'),
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
182 cmd2.make_option('-d', '--database', type='string',
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
183 help='Database name to connect to'),
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
184 cmd2.make_option('-U', '--username', type='string',
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
185 help='Database user name to connect as')
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
186 ])
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
187 def do_connect(self, arg, opts):
407
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
188
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
189 '''Opens the DB connection'''
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
190 if opts.closeall:
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
191 self.closeall()
409
5b88ce5f31ff ugh, trying to separate -- comments from --flags
catherine@DellZilla
parents: 407
diff changeset
192 return
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
193 if opts.close:
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
194 if not arg:
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
195 arg = self.connection_number
260
fc106df4606b need to work on connection closing
catherine@dellzilla
parents: 259
diff changeset
196 self.disconnect(arg)
409
5b88ce5f31ff ugh, trying to separate -- comments from --flags
catherine@DellZilla
parents: 407
diff changeset
197 return
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
198 if not arg:
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
199 self.list_connections()
409
5b88ce5f31ff ugh, trying to separate -- comments from --flags
catherine@DellZilla
parents: 407
diff changeset
200 return
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
201 try:
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
202 if self.successful_connection_to_number(arg):
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
203 return
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
204 except IndexError:
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
205 self.list_connections()
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
206 return
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
207 try:
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
208 connect_info = self.url_connect(arg)
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
209 except sqlalchemy.exc.ArgumentError, e:
409
5b88ce5f31ff ugh, trying to separate -- comments from --flags
catherine@DellZilla
parents: 407
diff changeset
210 url = self.connect_url(arg, opts)
5b88ce5f31ff ugh, trying to separate -- comments from --flags
catherine@DellZilla
parents: 407
diff changeset
211 connect_info = self.url_connect(url)
407
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
212 except Exception, e:
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
213 self.perror(r'URL connection format: rdbms://username:password@host/database')
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
214 return
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
215 if opts.add or (self.connection_number is None):
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
216 try:
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
217 self.connection_number = max(self.connections.keys()) + 1
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
218 except ValueError:
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
219 self.connection_number = 0
317
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
220 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
221 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
222 self.make_connection_current(self.connection_number)
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
223 self.curs = self.conn.cursor()
317
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
224 if (self.rdbms == 'oracle') and self.serveroutput:
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
225 self.curs.callproc('dbms_output.enable', [])
335
00b183a103b3 bare prefix switches connection
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
226 if (self.rdbms == 'mysql'):
00b183a103b3 bare prefix switches connection
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
227 self.curs.execute('SET SQL_MODE=ANSI')
409
5b88ce5f31ff ugh, trying to separate -- comments from --flags
catherine@DellZilla
parents: 407
diff changeset
228 return
5b88ce5f31ff ugh, trying to separate -- comments from --flags
catherine@DellZilla
parents: 407
diff changeset
229
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
230 def postparsing_precmd(self, statement):
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
231 stop = 0
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
232 self.saved_connection_number = None
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
233 if statement.parsed.connection_number:
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
234 saved_connection_number = self.connection_number
258
a94fec8155da multiple connections
catherine@Elli.myhome.westell.com
parents: 257
diff changeset
235 try:
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
236 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
237 if statement.parsed.command:
00b183a103b3 bare prefix switches connection
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
238 self.saved_connection_number = saved_connection_number
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
239 except KeyError:
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
240 self.list_connections()
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
241 raise KeyError, 'No connection #%s' % statement.parsed.connection_number
259
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
242 return stop, statement
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
243 def postparsing_postcmd(self, stop):
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
244 if self.saved_connection_number is not None:
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
245 self.successful_connection_to_number(self.saved_connection_number)
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
246 return stop
c0847a4c7f49 one-shot connection changes
catherine@Elli.myhome.westell.com
parents: 258
diff changeset
247
256
d09f16b71f66 do_host
catherine@Elli.myhome.westell.com
parents: 255
diff changeset
248 do_host = cmd2.Cmd.do_shell
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
249
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
250 def emptyline(self):
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
251 pass
264
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
252
265
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
253 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
254 if all_users:
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
255 user = ''
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
256 else:
265
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
257 user = "AND ao.owner = user\n"
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
258 if targets:
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
259 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
260 t.upper().replace('*','%') for t in targets)
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
261 else:
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
262 target = ''
264
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
263 self.curs.execute('''
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
264 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
265 ae.text error_text,
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
266 src.text object_text,
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
267 ao.last_ddl_time
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
268 FROM all_errors ae
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
269 JOIN all_objects ao ON ( ae.owner = ao.owner
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
270 AND ae.name = ao.object_name
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
271 AND ae.type = ao.object_type)
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
272 JOIN all_source src ON ( ae.owner = src.owner
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
273 AND ae.name = src.name
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
274 AND ae.type = src.type
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
275 AND ae.line = src.line)
265
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
276 WHERE 1=1
041c656dc8e5 show err working nicely now
catherine@Elli.myhome.westell.com
parents: 264
diff changeset
277 %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
278 if limit is None:
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
279 errors = self.curs.fetchall()
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
280 else:
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
281 errors = self.curs.fetchmany(numRows = limit)
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
282 for err in errors:
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
283 if (mintime is not None) and (err[8] < mintime):
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
284 break
333
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
285 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
286 self.poutput(err[7])
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
287 self.poutput((' ' * (err[3]-1)) + '^')
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
288 self.poutput(err[6])
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
289 self.poutput('\n')
264
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
290
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
291 def current_database_time(self):
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
292 self.curs.execute('select sysdate from dual')
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
293 return self.curs.fetchone()[0]
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
294
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
295 def do_terminators(self, arg):
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
296 """; standard Oracle format
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
297 \\c CSV (with headings)
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
298 \\C CSV (no headings)
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
299 \\g list
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
300 \\G aligned list
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
301 \\h HTML table
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
302 \\i INSERT statements
266
342e96de6de6 json not quite there
catherine@Elli.myhome.westell.com
parents: 265
diff changeset
303 \\j JSON
308
4d24fea42364 py scripts working again
catherine@Elli.myhome.westell.com
parents: 298
diff changeset
304 \\r ReStructured Text
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
305 \\s CSV (with headings)
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
306 \\S CSV (no headings)
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
307 \\t transposed
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
308 \\x XML
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
309 \\l line plot, with markers
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
310 \\L scatter plot (no lines)
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
311 \\b bar graph
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
312 \\p pie chart"""
333
1cde0ec62e61 send up
Catherine Devlin <catherine.devlin@gmail.com>
parents: 324
diff changeset
313 self.poutput(self.do_terminators.__doc__)
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
314
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
315 terminatorSearchString = '|'.join('\\' + d.split()[0] for d in do_terminators.__doc__.splitlines())
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
316
339
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
317 bindScanner = {'oracle': Parser(pyparsing.Literal(':') + pyparsing.Word( pyparsing.alphanums + "_$#" )),
407
188c86d4a11e struggling with option parsing
catherine@DellZilla
parents: 405
diff changeset
318 'postgres': Parser(pyparsing.Literal('%(') + legal_sql_word + ')s')}
340
001d01eeac90 bind vars for postgres
Catherine Devlin <catherine.devlin@gmail.com>
parents: 339
diff changeset
319 def findBinds(self, target, givenBindVars = {}):
339
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
320 result = givenBindVars
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
321 if self.rdbms in self.bindScanner:
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
322 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
323 varname = finding[1]
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
324 try:
340
001d01eeac90 bind vars for postgres
Catherine Devlin <catherine.devlin@gmail.com>
parents: 339
diff changeset
325 result[varname] = self.binds[varname]
339
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
326 except KeyError:
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
327 if not givenBindVars.has_key(varname):
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
328 print 'Bind variable %s not defined.' % (varname)
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
329 return result
545f63b6ef42 supports bind variables in postgresql
Catherine Devlin <catherine.devlin@gmail.com>
parents: 338
diff changeset
330
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
331 def default(self, arg):
340
001d01eeac90 bind vars for postgres
Catherine Devlin <catherine.devlin@gmail.com>
parents: 339
diff changeset
332 self.varsUsed = self.findBinds(arg, givenBindVars={})
247
f0f293d83337 begin docs
catherine@dellzilla
parents: 246
diff changeset
333 ending_args = arg.lower().split()[-2:]
f0f293d83337 begin docs
catherine@dellzilla
parents: 246
diff changeset
334 if 'end' in ending_args:
246
b5d4a122354a can round-trip a package now
catherine@dellzilla
parents: 242
diff changeset
335 command = '%s %s;'
b5d4a122354a can round-trip a package now
catherine@dellzilla
parents: 242
diff changeset
336 else:
264
a8deaa38f11e show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents: 263
diff changeset
337 command = '%s %s'
317
f200a222a936 beginning to set up metadata.py
Catherine Devlin <catherine.devlin@gmail.com>
parents: 315
diff changeset
338 if self.rdbms == 'oracle':
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
339 current_time = self.current_database_time()
263
362db47c17b4 error reported
catherine@Elli.myhome.westell.com
parents: 261
diff changeset
340 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
341 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
342 if self.rdbms == 'oracle':
315
a0a36232983a url_connect
catherine@Elli.myhome.westell.com
parents: 313
diff changeset
343 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
344 self.pfeedback(executionmessage)
198
b2d8bf5f89db merged with changes from work
catherine@Elli.myhome.westell.com
parents: 197
diff changeset
345
b2d8bf5f89db merged with changes from work
catherine@Elli.myhome.westell.com
parents: 197
diff changeset
346 def do_commit(self, arg=''):
b2d8bf5f89db merged with changes from work
catherine@Elli.myhome.westell.com
parents: 197
diff changeset
347 self.default(self.parsed('commit %s;' % (arg)))
b2d8bf5f89db merged with changes from work
catherine@Elli.myhome.westell.com
parents: 197
diff changeset
348 def do_rollback(self, arg=''):
b2d8bf5f89db merged with changes from work
catherine@Elli.myhome.westell.com
parents: 197
diff changeset
349 self.default(self.parsed('rollback %s;' % (arg)))
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
350 def do_quit(self, arg):
261
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
351 if self.commit_on_exit:
0044cfa5867a disconnections worked out
catherine@dellzilla
parents: 260
diff changeset
352 self.closeall()
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
353 return cmd2.Cmd.do_quit(self, None)
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
354 do_exit = do_quit
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
355 do_q = do_quit
337
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
356 colorcodes = {'bold':{True:'\x1b[1m',False:'\x1b[22m'},
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
357 'red':{True:'\x1b[36m',False:'\x1b[39m'},
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
358 'cyan':{True:'\x1b[31m',False:'\x1b[39m'},
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
359 'underline':{True:'\x1b[4m',False:'\x1b[24m'}}
375
9d0a3ab7f573 set colors off for Windows
devlinjs@A0266D4FVTK81.wrightpatterson.afmc.ds.af.mil
parents: 374
diff changeset
360 colors = (platform.system() != 'Windows')
337
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
361 def colorize(self, val, color):
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
362 if self.colors and (self.stdout == self.initial_stdout):
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
363 if color not in self.colorcodes:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
364 if (color % 2):
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
365 color = 'red'
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
366 else:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
367 color = 'cyan'
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
368 return self.colorcodes[color][True] + val + self.colorcodes[color][False]
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
369 return val
400
8903d24575f0 desc works on code
catherine@DellZilla
parents: 396
diff changeset
370 def pmatrix(self,rows,maxlen=30,heading=True,restructuredtext=False):
337
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
371 '''prints a matrix, used by sqlpython to print queries' result sets'''
400
8903d24575f0 desc works on code
catherine@DellZilla
parents: 396
diff changeset
372 names = self.colnames
8903d24575f0 desc works on code
catherine@DellZilla
parents: 396
diff changeset
373 maxen = [len(n) for n in self.colnames]
337
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
374 toprint = []
400
8903d24575f0 desc works on code
catherine@DellZilla
parents: 396
diff changeset
375 rcols = range(len(self.colnames))
337
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
376 rrows = range(len(rows))
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
377 for i in rrows: # loops for all rows
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
378 rowsi = map(str, rows[i]) # current row to process
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
379 split = [] # service var is row split is needed
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
380 mustsplit = 0 # flag
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
381 for j in rcols:
400
8903d24575f0 desc works on code
catherine@DellZilla
parents: 396
diff changeset
382 if str(self.coltypes[j]) == "<type 'cx_Oracle.BINARY'>": # handles RAW columns
337
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
383 rowsi[j] = binascii.b2a_hex(rowsi[j])
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
384 maxen[j] = max(maxen[j], len(rowsi[j])) # computes max field length
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
385 if maxen[j] <= maxlen:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
386 split.append('')
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
387 else: # split the line is 2 because field is too long
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
388 mustsplit = 1
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
389 maxen[j] = maxlen
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
390 split.append(rowsi[j][maxlen-1:2*maxlen-1])
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
391 rowsi[j] = rowsi[j][0:maxlen-1] # this implem. truncates after maxlen*2
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
392 toprint.append(rowsi) # 'toprint' is a printable copy of rows
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
393 if mustsplit != 0:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
394 toprint.append(split)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
395 sepcols = []
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
396 for i in rcols:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
397 maxcol = maxen[i]
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
398 name = names[i]
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
399 sepcols.append("-" * maxcol) # formats column names (header)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
400 names[i] = name + (" " * (maxcol-len(name))) # formats separ line with --
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
401 rrows2 = range(len(toprint))
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
402 for j in rrows2:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
403 val = toprint[j][i]
400
8903d24575f0 desc works on code
catherine@DellZilla
parents: 396
diff changeset
404 #import pdb; pdb.set_trace()
8903d24575f0 desc works on code
catherine@DellZilla
parents: 396
diff changeset
405 if str(self.coltypes[i]) == "<type 'cx_Oracle.NUMBER'>": # right align numbers - but must generalize!
337
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
406 toprint[j][i] = (" " * (maxcol-len(val))) + val
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
407 else:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
408 toprint[j][i] = val + (" " * (maxcol-len(val)))
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
409 toprint[j][i] = self.colorize(toprint[j][i], i)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
410 for j in rrows2:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
411 toprint[j] = ' '.join(toprint[j])
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
412 names = [self.colorize(name, n) for (n, name) in enumerate(names)]
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
413 names = ' '.join(names)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
414 names = self.colorize(names, 'bold')
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
415 sepcols = ' '.join(sepcols)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
416 if heading or restructuredtext:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
417 toprint.insert(0, sepcols)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
418 toprint.insert(0, names)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
419 if restructuredtext:
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
420 toprint.insert(0, sepcols)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
421 toprint.append(sepcols)
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
422 return '\n'.join(toprint)
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
423
337
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
424
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
425
Catherine Devlin <catherine.devlin@gmail.com>
parents: 333
diff changeset
426
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
427