Mercurial > sqlpython
annotate sqlpython/sqlpyPlus.py @ 285:316abf2191a4
substvar define working now
author | catherine@dellzilla |
---|---|
date | Fri, 20 Mar 2009 09:47:22 -0400 |
parents | ad20675a17f7 |
children | abb4c6524113 3cade02da892 |
rev | line source |
---|---|
189 | 1 """sqlpyPlus - extra features (inspired by Oracle SQL*Plus) for Luca Canali's sqlpython.py |
2 | |
3 Features include: | |
4 - SQL*Plus-style bind variables | |
5 - `set autobind on` stores single-line result sets in bind variables automatically | |
6 - SQL buffer with list, run, ed, get, etc.; unlike SQL*Plus, buffer stores session's full history | |
7 - @script.sql loads and runs (like SQL*Plus) | |
8 - ! runs operating-system command | |
9 - show and set to control sqlpython parameters | |
10 - SQL*Plus-style describe, spool | |
11 - write sends query result directly to file | |
12 - comments shows table and column comments | |
13 - compare ... to ... graphically compares results of two queries | |
14 - commands are case-insensitive | |
15 - context-sensitive tab-completion for table names, column names, etc. | |
16 | |
17 Use 'help' within sqlpython for details. | |
18 | |
19 Set bind variables the hard (SQL*Plus) way | |
20 exec :b = 3 | |
21 or with a python-like shorthand | |
22 :b = 3 | |
23 | |
24 - catherinedevlin.blogspot.com May 31, 2006 | |
25 """ | |
220 | 26 import sys, os, re, sqlpython, cx_Oracle, pyparsing, re, completion, datetime, pickle, binascii, subprocess |
189 | 27 from cmd2 import Cmd, make_option, options, Statekeeper, Cmd2TestCase |
28 from output_templates import output_templates | |
29 from plothandler import Plot | |
30 try: | |
31 import pylab | |
198
b2d8bf5f89db
merged with changes from work
catherine@Elli.myhome.westell.com
parents:
196
diff
changeset
|
32 except (RuntimeError, ImportError): |
189 | 33 pass |
34 | |
35 descQueries = { | |
36 'TABLE': (""" | |
193 | 37 SELECT atc.column_name, |
189 | 38 CASE atc.nullable WHEN 'Y' THEN 'NULL' ELSE 'NOT NULL' END "Null?", |
39 atc.data_type || | |
40 CASE atc.data_type WHEN 'DATE' THEN '' | |
41 ELSE '(' || | |
42 CASE atc.data_type WHEN 'NUMBER' THEN TO_CHAR(atc.data_precision) || | |
43 CASE atc.data_scale WHEN 0 THEN '' | |
44 ELSE ',' || TO_CHAR(atc.data_scale) END | |
45 ELSE TO_CHAR(atc.data_length) END | |
46 END || | |
47 CASE atc.data_type WHEN 'DATE' THEN '' ELSE ')' END | |
48 data_type | |
49 FROM all_tab_columns atc | |
50 WHERE atc.table_name = :object_name | |
51 AND atc.owner = :owner | |
193 | 52 ORDER BY atc.column_id;""",), |
189 | 53 'PROCEDURE': (""" |
193 | 54 SELECT NVL(argument_name, 'Return Value') argument_name, |
189 | 55 data_type, |
56 in_out, | |
57 default_value | |
58 FROM all_arguments | |
59 WHERE object_name = :object_name | |
60 AND owner = :owner | |
61 AND package_name IS NULL | |
193 | 62 ORDER BY sequence;""",), |
189 | 63 'PackageObjects':(""" |
64 SELECT DISTINCT object_name | |
65 FROM all_arguments | |
66 WHERE package_name = :package_name | |
67 AND owner = :owner""",), | |
68 'PackageObjArgs':(""" | |
193 | 69 SELECT object_name, |
189 | 70 argument_name, |
71 data_type, | |
72 in_out, | |
73 default_value | |
74 FROM all_arguments | |
75 WHERE package_name = :package_name | |
76 AND object_name = :object_name | |
77 AND owner = :owner | |
78 AND argument_name IS NOT NULL | |
193 | 79 ORDER BY sequence;""",), |
189 | 80 'TRIGGER':(""" |
193 | 81 SELECT description |
189 | 82 FROM all_triggers |
83 WHERE owner = :owner | |
193 | 84 AND trigger_name = :object_name; |
189 | 85 """, |
86 """ | |
193 | 87 SELECT table_owner, |
189 | 88 base_object_type, |
89 table_name, | |
90 column_name, | |
91 when_clause, | |
92 status, | |
93 action_type, | |
94 crossedition | |
95 FROM all_triggers | |
96 WHERE owner = :owner | |
97 AND trigger_name = :object_name | |
98 \\t | |
99 """, | |
100 ), | |
101 'INDEX':(""" | |
193 | 102 SELECT index_type, |
189 | 103 table_owner, |
104 table_name, | |
105 table_type, | |
106 uniqueness, | |
107 compression, | |
108 partitioned, | |
109 temporary, | |
110 generated, | |
111 secondary, | |
112 dropped, | |
113 visibility | |
114 FROM all_indexes | |
115 WHERE owner = :owner | |
116 AND index_name = :object_name | |
117 \\t | |
118 """,) | |
119 } | |
120 descQueries['VIEW'] = descQueries['TABLE'] | |
121 descQueries['FUNCTION'] = descQueries['PROCEDURE'] | |
122 | |
123 queries = { | |
124 'resolve': """ | |
125 SELECT object_type, object_name, owner FROM ( | |
126 SELECT object_type, object_name, user owner, 1 priority | |
127 FROM user_objects | |
128 WHERE object_name = :objName | |
129 UNION ALL | |
130 SELECT ao.object_type, ao.object_name, ao.owner, 2 priority | |
131 FROM all_objects ao | |
132 JOIN user_synonyms us ON (us.table_owner = ao.owner AND us.table_name = ao.object_name) | |
133 WHERE us.synonym_name = :objName | |
134 AND ao.object_type != 'SYNONYM' | |
135 UNION ALL | |
136 SELECT ao.object_type, ao.object_name, ao.owner, 3 priority | |
137 FROM all_objects ao | |
138 JOIN all_synonyms asyn ON (asyn.table_owner = ao.owner AND asyn.table_name = ao.object_name) | |
139 WHERE asyn.synonym_name = :objName | |
140 AND ao.object_type != 'SYNONYM' | |
141 AND asyn.owner = 'PUBLIC' | |
142 UNION ALL | |
143 SELECT 'DIRECTORY' object_type, dir.directory_name, dir.owner, 6 priority | |
144 FROM all_directories dir | |
145 WHERE dir.directory_name = :objName | |
146 UNION ALL | |
147 SELECT 'DATABASE LINK' object_type, db_link, owner, 7 priority | |
148 FROM all_db_links dbl | |
149 WHERE dbl.db_link = :objName | |
150 ) ORDER BY priority ASC, | |
151 length(object_type) ASC, | |
152 object_type DESC""", # preference: PACKAGE before PACKAGE BODY, TABLE before INDEX | |
153 'tabComments': """ | |
154 SELECT comments | |
155 FROM all_tab_comments | |
156 WHERE owner = :owner | |
157 AND table_name = :table_name""", | |
158 'colComments': """ | |
193 | 159 SELECT |
189 | 160 atc.column_name, |
161 acc.comments | |
162 FROM all_tab_columns atc | |
163 JOIN all_col_comments acc ON (atc.owner = acc.owner and atc.table_name = acc.table_name and atc.column_name = acc.column_name) | |
164 WHERE atc.table_name = :object_name | |
165 AND atc.owner = :owner | |
193 | 166 ORDER BY atc.column_id;""", |
189 | 167 'oneColComments': """ |
193 | 168 SELECTatc.column_name, |
189 | 169 acc.comments |
170 FROM all_tab_columns atc | |
171 JOIN all_col_comments acc ON (atc.owner = acc.owner and atc.table_name = acc.table_name and atc.column_name = acc.column_name) | |
172 WHERE atc.table_name = :object_name | |
173 AND atc.owner = :owner | |
174 AND acc.column_name = :column_name | |
193 | 175 ORDER BY atc.column_id;""", |
189 | 176 #thanks to Senora.pm for "refs" |
177 'refs': """ | |
178 NULL referenced_by, | |
179 c2.table_name references, | |
180 c1.constraint_name constraint | |
181 FROM | |
182 user_constraints c1, | |
183 user_constraints c2 | |
184 WHERE | |
185 c1.table_name = :object_name | |
186 and c1.constraint_type ='R' | |
187 and c1.r_constraint_name = c2.constraint_name | |
188 and c1.r_owner = c2.owner | |
189 and c1.owner = :owner | |
190 UNION | |
191 SELECT c1.table_name referenced_by, | |
192 NULL references, | |
193 c1.constraint_name constraint | |
194 FROM | |
195 user_constraints c1, | |
196 user_constraints c2 | |
197 WHERE | |
198 c2.table_name = :object_name | |
199 and c1.constraint_type ='R' | |
200 and c1.r_constraint_name = c2.constraint_name | |
201 and c1.r_owner = c2.owner | |
202 and c1.owner = :owner | |
203 """ | |
204 } | |
205 | |
206 class SoftwareSearcher(object): | |
207 def __init__(self, softwareList, purpose): | |
208 self.softwareList = softwareList | |
209 self.purpose = purpose | |
210 self.software = None | |
211 def invoke(self, *args): | |
212 if not self.software: | |
213 (self.software, self.invokeString) = self.find() | |
214 argTuple = tuple([self.software] + list(args)) | |
215 os.system(self.invokeString % argTuple) | |
216 def find(self): | |
217 if self.purpose == 'text editor': | |
218 software = os.environ.get('EDITOR') | |
219 if software: | |
220 return (software, '%s %s') | |
221 for (n, (software, invokeString)) in enumerate(self.softwareList): | |
222 if os.path.exists(software): | |
223 if n > (len(self.softwareList) * 0.7): | |
224 print """ | |
225 | |
226 Using %s. Note that there are better options available for %s, | |
227 but %s couldn't find a better one in your PATH. | |
228 Feel free to open up %s | |
229 and customize it to find your favorite %s program. | |
230 | |
231 """ % (software, self.purpose, __file__, __file__, self.purpose) | |
232 return (software, invokeString) | |
233 stem = os.path.split(software)[1] | |
234 for p in os.environ['PATH'].split(os.pathsep): | |
235 if os.path.exists(os.sep.join([p, stem])): | |
236 return (stem, invokeString) | |
237 raise (OSError, """Could not find any %s programs. You will need to install one, | |
238 or customize %s to make it aware of yours. | |
239 Looked for these programs: | |
240 %s""" % (self.purpose, __file__, "\n".join([s[0] for s in self.softwareList]))) | |
241 | |
242 softwareLists = { | |
243 'diff/merge': [ | |
244 ('/usr/bin/meld',"%s %s %s"), | |
245 ('/usr/bin/kdiff3',"%s %s %s"), | |
246 (r'C:\Program Files\Araxis\Araxis Merge v6.5\Merge.exe','"%s" %s %s'), | |
247 (r'C:\Program Files\TortoiseSVN\bin\TortoiseMerge.exe', '"%s" /base:"%s" /mine:"%s"'), | |
248 ('FileMerge','%s %s %s'), | |
249 ('kompare','%s %s %s'), | |
250 ('WinMerge','%s %s %s'), | |
251 ('xxdiff','%s %s %s'), | |
252 ('fldiff','%s %s %s'), | |
253 ('gtkdiff','%s %s %s'), | |
254 ('tkdiff','%s %s %s'), | |
255 ('gvimdiff','%s %s %s'), | |
256 ('diff',"%s %s %s"), | |
257 (r'c:\windows\system32\comp.exe',"%s %s %s")], | |
258 'text editor': [ | |
259 ('gedit', '%s %s'), | |
260 ('textpad', '%s %s'), | |
261 ('notepad.exe', '%s %s'), | |
262 ('pico', '%s %s'), | |
263 ('emacs', '%s %s'), | |
264 ('vim', '%s %s'), | |
265 ('vi', '%s %s'), | |
266 ('ed', '%s %s'), | |
267 ('edlin', '%s %s') | |
268 ] | |
269 } | |
270 | |
271 diffMergeSearcher = SoftwareSearcher(softwareLists['diff/merge'],'diff/merge') | |
272 editSearcher = SoftwareSearcher(softwareLists['text editor'], 'text editor') | |
273 editor = os.environ.get('EDITOR') | |
274 if editor: | |
275 editSearcher.find = lambda: (editor, "%s %s") | |
276 | |
277 class CaselessDict(dict): | |
278 """dict with case-insensitive keys. | |
279 | |
280 Posted to ASPN Python Cookbook by Jeff Donner - http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66315""" | |
281 def __init__(self, other=None): | |
282 if other: | |
283 # Doesn't do keyword args | |
284 if isinstance(other, dict): | |
285 for k,v in other.items(): | |
286 dict.__setitem__(self, k.lower(), v) | |
287 else: | |
288 for k,v in other: | |
289 dict.__setitem__(self, k.lower(), v) | |
290 def __getitem__(self, key): | |
291 return dict.__getitem__(self, key.lower()) | |
292 def __setitem__(self, key, value): | |
273
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
293 try: |
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
294 key = key.lower() |
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
295 except AttributeError: |
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
296 pass |
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
297 dict.__setitem__(self, key, value) |
189 | 298 def __contains__(self, key): |
299 return dict.__contains__(self, key.lower()) | |
300 def has_key(self, key): | |
301 return dict.has_key(self, key.lower()) | |
302 def get(self, key, def_val=None): | |
303 return dict.get(self, key.lower(), def_val) | |
304 def setdefault(self, key, def_val=None): | |
305 return dict.setdefault(self, key.lower(), def_val) | |
306 def update(self, other): | |
307 for k,v in other.items(): | |
308 dict.__setitem__(self, k.lower(), v) | |
309 def fromkeys(self, iterable, value=None): | |
310 d = CaselessDict() | |
311 for k in iterable: | |
312 dict.__setitem__(d, k.lower(), value) | |
313 return d | |
314 def pop(self, key, def_val=None): | |
315 return dict.pop(self, key.lower(), def_val) | |
316 | |
317 class Parser(object): | |
246 | 318 comment_def = "--" + ~ ('-' + pyparsing.CaselessKeyword('begin')) + pyparsing.ZeroOrMore(pyparsing.CharsNotIn("\n")) |
189 | 319 def __init__(self, scanner, retainSeparator=True): |
320 self.scanner = scanner | |
321 self.scanner.ignore(pyparsing.sglQuotedString) | |
322 self.scanner.ignore(pyparsing.dblQuotedString) | |
323 self.scanner.ignore(self.comment_def) | |
324 self.scanner.ignore(pyparsing.cStyleComment) | |
325 self.retainSeparator = retainSeparator | |
326 def separate(self, txt): | |
327 itms = [] | |
328 for (sqlcommand, start, end) in self.scanner.scanString(txt): | |
329 if sqlcommand: | |
330 if type(sqlcommand[0]) == pyparsing.ParseResults: | |
331 if self.retainSeparator: | |
332 itms.append("".join(sqlcommand[0])) | |
333 else: | |
334 itms.append(sqlcommand[0][0]) | |
335 else: | |
336 if sqlcommand[0]: | |
337 itms.append(sqlcommand[0]) | |
338 return itms | |
339 | |
340 bindScanner = Parser(pyparsing.Literal(':') + pyparsing.Word( pyparsing.alphanums + "_$#" )) | |
341 | |
342 def findBinds(target, existingBinds, givenBindVars = {}): | |
343 result = givenBindVars | |
344 for finding, startat, endat in bindScanner.scanner.scanString(target): | |
345 varname = finding[1] | |
346 try: | |
347 result[varname] = existingBinds[varname] | |
348 except KeyError: | |
349 if not givenBindVars.has_key(varname): | |
350 print 'Bind variable %s not defined.' % (varname) | |
351 return result | |
192
6bb8a112af6b
accept special terminators on most anything
catherine@dellzilla
parents:
191
diff
changeset
|
352 |
271 | 353 class ResultSet(list): |
354 pass | |
355 | |
356 class Result(tuple): | |
357 def __str__(self): | |
272 | 358 return '\n'.join('%s: %s' % (colname, self[idx]) |
359 for (idx, colname) in enumerate(self.resultset.colnames)) | |
271 | 360 def __getattr__(self, attr): |
361 attr = attr.lower() | |
362 try: | |
363 return self[self.resultset.colnames.index(attr)] | |
364 except ValueError: | |
365 if attr in ('colnames', 'statement', 'bindvars'): | |
366 return getattr(self.resultset, attr) | |
367 else: | |
368 raise AttributeError, "available columns are: " + ", ".join(self.resultset.colnames) | |
273
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
369 def bind(self): |
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
370 for (idx, colname) in enumerate(self.resultset.colnames): |
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
371 self.resultset.pystate['binds'][colname] = self[idx] |
277 | 372 self.resultset.pystate['binds'][str(idx+1)] = self[idx] |
271 | 373 |
189 | 374 class sqlpyPlus(sqlpython.sqlpython): |
375 defaultExtension = 'sql' | |
376 sqlpython.sqlpython.shortcuts.update({':': 'setbind', | |
377 '\\': 'psql', | |
378 '@': '_load'}) | |
379 multilineCommands = '''select insert update delete tselect | |
380 create drop alter _multiline_comment'''.split() | |
381 sqlpython.sqlpython.noSpecialParse.append('spool') | |
382 commentGrammars = pyparsing.Or([pyparsing.Literal('--') + pyparsing.restOfLine, pyparsing.cStyleComment]) | |
246 | 383 commentGrammars = pyparsing.Or([Parser.comment_def, pyparsing.cStyleComment]) |
259
c0847a4c7f49
one-shot connection changes
catherine@Elli.myhome.westell.com
parents:
257
diff
changeset
|
384 prefixParser = pyparsing.Optional(pyparsing.Word(pyparsing.nums)('connection_number') |
c0847a4c7f49
one-shot connection changes
catherine@Elli.myhome.westell.com
parents:
257
diff
changeset
|
385 + ':') |
247 | 386 default_file_name = 'afiedt.buf' |
189 | 387 def __init__(self): |
388 sqlpython.sqlpython.__init__(self) | |
389 self.binds = CaselessDict() | |
281 | 390 self.settable += 'autobind commit_on_exit maxfetch maxtselctrows scan serveroutput sql_echo store_results timeout heading wildsql'.split() |
247 | 391 self.settable.remove('case_insensitive') |
230 | 392 self.settable.sort() |
189 | 393 self.stdoutBeforeSpool = sys.stdout |
249 | 394 self.sql_echo = False |
189 | 395 self.spoolFile = None |
396 self.autobind = False | |
229 | 397 self.heading = True |
240 | 398 self.wildsql = False |
257
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
399 self.serveroutput = True |
271 | 400 self.scan = True |
401 self.substvars = {} | |
402 self.result_history = [] | |
281 | 403 self.store_results = True |
284 | 404 self.pystate = {'r': [], 'binds': self.binds, 'substs': self.substvars} |
257
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
405 |
189 | 406 # overrides cmd's parseline |
407 def parseline(self, line): | |
408 """Parse the line into a command name and a string containing | |
409 the arguments. Returns a tuple containing (command, args, line). | |
410 'command' and 'args' may be None if the line couldn't be parsed. | |
411 Overrides cmd.cmd.parseline to accept variety of shortcuts..""" | |
412 | |
413 cmd, arg, line = sqlpython.sqlpython.parseline(self, line) | |
414 if cmd in ('select', 'sleect', 'insert', 'update', 'delete', 'describe', | |
415 'desc', 'comments', 'pull', 'refs', 'desc', 'triggers', 'find') \ | |
416 and not hasattr(self, 'curs'): | |
417 print 'Not connected.' | |
418 return '', '', '' | |
419 return cmd, arg, line | |
257
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
420 |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
421 def dbms_output(self): |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
422 "Dumps contents of Oracle's DBMS_OUTPUT buffer (where PUT_LINE goes)" |
261 | 423 try: |
424 line = self.curs.var(cx_Oracle.STRING) | |
425 status = self.curs.var(cx_Oracle.NUMBER) | |
257
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
426 self.curs.callproc('dbms_output.get_line', [line, status]) |
261 | 427 while not status.getvalue(): |
428 self.stdout.write(line.getvalue()) | |
429 self.stdout.write('\n') | |
430 self.curs.callproc('dbms_output.get_line', [line, status]) | |
431 except AttributeError: | |
432 pass | |
257
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
433 |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
434 def postcmd(self, stop, line): |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
435 """Hook method executed just after a command dispatch is finished.""" |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
436 if self.serveroutput: |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
437 self.dbms_output() |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
438 return stop |
241 | 439 |
245
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
440 def do_remark(self, line): |
242 | 441 ''' |
245
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
442 REMARK is one way to denote a comment in SQL*Plus. |
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
443 |
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
444 Wrapping a *single* SQL or PL/SQL statement in `REMARK BEGIN` and `REMARK END` |
241 | 445 tells sqlpython to submit the enclosed code directly to Oracle as a single |
242 | 446 unit of code. |
241 | 447 |
448 Without these markers, sqlpython fails to properly distinguish the beginning | |
449 and end of all but the simplest PL/SQL blocks, causing errors. sqlpython also | |
450 slows down when parsing long SQL statements as it tries to determine whether | |
245
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
451 the statement has ended yet; `REMARK BEGIN` and `REMARK END` allow it to skip this |
241 | 452 parsing. |
453 | |
245
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
454 Standard SQL*Plus interprets REMARK BEGIN and REMARK END as comments, so it is |
242 | 455 safe to include them in SQL*Plus scripts. |
241 | 456 ''' |
245
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
457 if not line.lower().strip().startswith('begin'): |
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
458 return |
241 | 459 statement = [] |
247 | 460 next = self.pseudo_raw_input(self.continuation_prompt) |
245
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
461 while next.lower().split()[:2] != ['remark','end']: |
241 | 462 statement.append(next) |
247 | 463 next = self.pseudo_raw_input(self.continuation_prompt) |
242 | 464 return self.onecmd('\n'.join(statement)) |
272 | 465 |
466 def do_py(self, arg): | |
467 ''' | |
468 py <command>: Executes a Python command. | |
469 py: Enters interactive Python mode (end with `\py`). | |
275 | 470 Past SELECT results are stored in list `r`; |
471 most recent resultset is `r[-1]`. | |
284 | 472 SQL bind variables can be accessed/changed via `binds`; |
473 substitution variables via `substs`. | |
272 | 474 ''' |
273
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
475 return Cmd.do_py(self, arg) |
284 | 476 |
477 def do_get(self, args): | |
478 """ | |
479 `get {script.sql}` or `@{script.sql}` runs the command(s) in {script.sql}. | |
480 If additional arguments are supplied, they are assigned to &1, &2, etc. | |
481 """ | |
482 fname, args = args.split()[0], args.split()[1:] | |
483 for (idx, arg) in enumerate(args): | |
484 self.substvars[str(idx+1)] = arg | |
485 return Cmd.do__load(self, fname) | |
486 | |
189 | 487 def onecmd_plus_hooks(self, line): |
488 line = self.precmd(line) | |
489 stop = self.onecmd(line) | |
490 stop = self.postcmd(stop, line) | |
491 | |
257
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
492 def _onchange_serveroutput(self, old, new): |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
493 if new: |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
494 self.curs.callproc('dbms_output.enable', []) |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
495 else: |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
496 self.curs.callproc('dbms_output.disable', []) |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
497 |
189 | 498 def do_shortcuts(self,arg): |
499 """Lists available first-character shortcuts | |
500 (i.e. '!dir' is equivalent to 'shell dir')""" | |
501 for (scchar, scto) in self.shortcuts.items(): | |
502 print '%s: %s' % (scchar, scto) | |
503 | |
254
b61e21386383
oops, restore lines of code after sql_format_item
catherine@Elli.myhome.westell.com
parents:
253
diff
changeset
|
504 tableNameFinder = re.compile(r'from\s+([\w$#_"]+)', re.IGNORECASE | re.MULTILINE | re.DOTALL) |
b61e21386383
oops, restore lines of code after sql_format_item
catherine@Elli.myhome.westell.com
parents:
253
diff
changeset
|
505 inputStatementFormatters = { |
b61e21386383
oops, restore lines of code after sql_format_item
catherine@Elli.myhome.westell.com
parents:
253
diff
changeset
|
506 cx_Oracle.STRING: "'%s'", |
b61e21386383
oops, restore lines of code after sql_format_item
catherine@Elli.myhome.westell.com
parents:
253
diff
changeset
|
507 cx_Oracle.DATETIME: "TO_DATE('%s', 'YYYY-MM-DD HH24:MI:SS')"} |
b61e21386383
oops, restore lines of code after sql_format_item
catherine@Elli.myhome.westell.com
parents:
253
diff
changeset
|
508 inputStatementFormatters[cx_Oracle.CLOB] = inputStatementFormatters[cx_Oracle.STRING] |
b61e21386383
oops, restore lines of code after sql_format_item
catherine@Elli.myhome.westell.com
parents:
253
diff
changeset
|
509 inputStatementFormatters[cx_Oracle.TIMESTAMP] = inputStatementFormatters[cx_Oracle.DATETIME] |
189 | 510 def output(self, outformat, rowlimit): |
511 self.tblname = self.tableNameFinder.search(self.curs.statement).group(1) | |
512 self.colnames = [d[0] for d in self.curs.description] | |
513 if outformat in output_templates: | |
514 self.colnamelen = max(len(colname) for colname in self.colnames) | |
515 self.coltypes = [d[1] for d in self.curs.description] | |
516 self.formatters = [self.inputStatementFormatters.get(typ, '%s') for typ in self.coltypes] | |
517 result = output_templates[outformat].generate(**self.__dict__) | |
518 elif outformat == '\\t': # transposed | |
519 rows = [self.colnames] | |
520 rows.extend(list(self.rows)) | |
521 transpr = [[rows[y][x] for y in range(len(rows))]for x in range(len(rows[0]))] # matrix transpose | |
522 newdesc = [['ROW N.'+str(y),10] for y in range(len(rows))] | |
523 for x in range(len(self.curs.description)): | |
524 if str(self.curs.description[x][1]) == "<type 'cx_Oracle.BINARY'>": # handles RAW columns | |
525 rname = transpr[x][0] | |
526 transpr[x] = map(binascii.b2a_hex, transpr[x]) | |
527 transpr[x][0] = rname | |
528 newdesc[0][0] = 'COLUMN NAME' | |
529 result = '\n' + sqlpython.pmatrix(transpr,newdesc) | |
530 elif outformat in ('\\l', '\\L', '\\p', '\\b'): | |
531 plot = Plot() | |
532 plot.build(self, outformat) | |
533 plot.shelve() | |
534 plot.draw() | |
535 return '' | |
536 else: | |
229 | 537 result = sqlpython.pmatrix(self.rows, self.curs.description, self.maxfetch, heading=self.heading) |
189 | 538 return result |
539 | |
540 legalOracle = re.compile('[a-zA-Z_$#]') | |
541 | |
542 def select_scalar_list(self, sql, binds={}): | |
249 | 543 self._execute(sql, binds) |
189 | 544 return [r[0] for r in self.curs.fetchall()] |
545 | |
546 columnNameRegex = re.compile( | |
547 r'select\s+(.*)from', | |
548 re.IGNORECASE | re.DOTALL | re.MULTILINE) | |
549 def completedefault(self, text, line, begidx, endidx): | |
550 segment = completion.whichSegment(line) | |
551 text = text.upper() | |
552 completions = [] | |
553 if segment == 'select': | |
554 stmt = "SELECT column_name FROM user_tab_columns WHERE column_name LIKE '%s%%'" | |
555 completions = self.select_scalar_list(stmt % (text)) | |
556 if not completions: | |
557 stmt = "SELECT column_name FROM all_tab_columns WHERE column_name LIKE '%s%%'" | |
558 completions = self.select_scalar_list(stmt % (text)) | |
559 if segment == 'from': | |
560 columnNames = self.columnNameRegex.search(line) | |
561 if columnNames: | |
562 columnNames = columnNames.group(1) | |
563 columnNames = [c.strip().upper() for c in columnNames.split(',')] | |
564 stmt1 = "SELECT table_name FROM all_tab_columns WHERE column_name = '%s' AND table_name LIKE '%s%%'" | |
565 for columnName in columnNames: | |
566 # and if columnName is * ? | |
567 completions.extend(self.select_scalar_list(stmt1 % (columnName, text))) | |
568 if segment in ('from', 'update', 'insert into') and (not completions): | |
569 stmt = "SELECT table_name FROM user_tables WHERE table_name LIKE '%s%%'" | |
570 completions = self.select_scalar_list(stmt % (text)) | |
571 if not completions: | |
572 stmt = """SELECT table_name FROM user_tables WHERE table_name LIKE '%s%%' | |
573 UNION | |
574 SELECT DISTINCT owner FROM all_tables WHERE owner LIKE '%%%s'""" | |
575 completions = self.select_scalar_list(stmt % (text, text)) | |
576 if segment in ('where', 'group by', 'order by', 'having', 'set'): | |
577 tableNames = completion.tableNamesFromFromClause(line) | |
578 if tableNames: | |
579 stmt = """SELECT column_name FROM all_tab_columns | |
580 WHERE table_name IN (%s)""" % \ | |
581 (','.join("'%s'" % (t) for t in tableNames)) | |
582 stmt = "%s AND column_name LIKE '%s%%'" % (stmt, text) | |
583 completions = self.select_scalar_list(stmt) | |
584 if not segment: | |
585 stmt = "SELECT object_name FROM all_objects WHERE object_name LIKE '%s%%'" | |
586 completions = self.select_scalar_list(stmt % (text)) | |
587 return completions | |
233 | 588 |
234
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
589 columnlistPattern = pyparsing.SkipTo(pyparsing.CaselessKeyword('from'))('columns') + \ |
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
590 pyparsing.SkipTo(pyparsing.stringEnd)('remainder') |
233 | 591 |
236 | 592 negator = pyparsing.Literal('!')('exclude') |
242 | 593 colNumber = pyparsing.Optional(negator) + pyparsing.Literal('#') + pyparsing.Word('-' + pyparsing.nums, pyparsing.nums)('column_number') |
238
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
594 colName = negator + pyparsing.Word('$_#' + pyparsing.alphas, '$_#' + pyparsing.alphanums)('column_name') |
241 | 595 wildColName = pyparsing.Optional(negator) + pyparsing.Word('?*%$_#' + pyparsing.alphas, '?*%$_#' + pyparsing.alphanums, min=2)('column_name') |
242 | 596 colNumber.ignore(pyparsing.cStyleComment).ignore(Parser.comment_def). \ |
597 ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString) | |
235 | 598 wildSqlParser = colNumber ^ colName ^ wildColName |
599 wildSqlParser.ignore(pyparsing.cStyleComment).ignore(Parser.comment_def). \ | |
236 | 600 ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString) |
601 emptyCommaRegex = re.compile(',\s*,', re.DOTALL) | |
602 deadStarterCommaRegex = re.compile('^\s*,', re.DOTALL) | |
603 deadEnderCommaRegex = re.compile(',\s*$', re.DOTALL) | |
233 | 604 def expandWildSql(self, arg): |
605 try: | |
234
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
606 columnlist = self.columnlistPattern.parseString(arg) |
233 | 607 except pyparsing.ParseException: |
608 return arg | |
236 | 609 parseresults = list(self.wildSqlParser.scanString(columnlist.columns)) |
239
4c563c2218e6
catching standard names caught
catherine@Elli.myhome.westell.com
parents:
238
diff
changeset
|
610 # I would rather exclude non-wild column names in the grammar, |
4c563c2218e6
catching standard names caught
catherine@Elli.myhome.westell.com
parents:
238
diff
changeset
|
611 # but can't figure out how |
4c563c2218e6
catching standard names caught
catherine@Elli.myhome.westell.com
parents:
238
diff
changeset
|
612 parseresults = [p for p in parseresults if |
4c563c2218e6
catching standard names caught
catherine@Elli.myhome.westell.com
parents:
238
diff
changeset
|
613 p[0].column_number or |
4c563c2218e6
catching standard names caught
catherine@Elli.myhome.westell.com
parents:
238
diff
changeset
|
614 '*' in p[0].column_name or |
4c563c2218e6
catching standard names caught
catherine@Elli.myhome.westell.com
parents:
238
diff
changeset
|
615 '%' in p[0].column_name or |
241 | 616 '?' in p[0].column_name or |
239
4c563c2218e6
catching standard names caught
catherine@Elli.myhome.westell.com
parents:
238
diff
changeset
|
617 p[0].exclude] |
236 | 618 if not parseresults: |
619 return arg | |
234
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
620 self.curs.execute('select * ' + columnlist.remainder, self.varsUsed) |
236 | 621 columns_available = [d[0] for d in self.curs.description] |
234
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
622 replacers = {} |
236 | 623 included = set() |
624 excluded = set() | |
625 for (col, startpos, endpos) in parseresults: | |
238
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
626 replacers[arg[startpos:endpos]] = [] |
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
627 if col.column_name: |
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
628 finder = col.column_name.replace('*','.*') |
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
629 finder = finder.replace('%','.*') |
241 | 630 finder = finder.replace('?','.') |
631 colnames = [c for c in columns_available if re.match(finder + '$', c, re.IGNORECASE)] | |
238
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
632 elif col.column_number: |
236 | 633 idx = int(col.column_number) |
634 if idx > 0: | |
635 idx -= 1 | |
238
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
636 colnames = [columns_available[idx]] |
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
637 if not colnames: |
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
638 print 'No columns found matching criteria.' |
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
639 return 'null from dual' |
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
640 for colname in colnames: |
236 | 641 if col.exclude: |
642 included.discard(colname) | |
643 include_here = columns_available[:] | |
644 include_here.remove(colname) | |
240 | 645 replacers[arg[startpos:endpos]].extend(i for i in include_here if i not in replacers[arg[startpos:endpos]]) |
236 | 646 excluded.add(colname) |
647 else: | |
648 excluded.discard(colname) | |
238
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
649 replacers[arg[startpos:endpos]].append(colname) |
236 | 650 |
234
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
651 replacers = sorted(replacers.items(), key=len, reverse=True) |
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
652 result = columnlist.columns |
236 | 653 for (target, replacement) in replacers: |
242 | 654 cols = [r for r in replacement if r not in excluded and r not in included] |
238
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
655 replacement = ', '.join(cols) |
254fb9d3f4c3
must fix catching regular cols as wilds, repeating on eqdbw/mtndew@orcl
catherine@Elli.myhome.westell.com
parents:
237
diff
changeset
|
656 included.update(cols) |
236 | 657 result = result.replace(target, replacement) |
242 | 658 # some column names could get wiped out completely, so we fix their dangling commas |
236 | 659 result = self.emptyCommaRegex.sub(',', result) |
660 result = self.deadStarterCommaRegex.sub('', result) | |
661 result = self.deadEnderCommaRegex.sub('', result) | |
242 | 662 if not result.strip(): |
663 print 'No columns found matching criteria.' | |
664 return 'null from dual' | |
237 | 665 return result + ' ' + columnlist.remainder |
284 | 666 |
667 def do_prompt(self, args): | |
668 print args | |
233 | 669 |
284 | 670 def do_accept(self, args): |
671 try: | |
672 prompt = args[args.lower().index('prompt ')+7:] | |
673 except ValueError: | |
674 prompt = '' | |
675 varname = args.lower().split()[0] | |
676 self.substvars[varname] = self.pseudo_raw_input(prompt) | |
677 | |
281 | 678 def ampersand_substitution(self, raw, regexpr, isglobal): |
271 | 679 subst = regexpr.search(raw) |
680 while subst: | |
681 fullexpr, var = subst.group(1), subst.group(2) | |
682 print 'Substitution variable %s found in:' % fullexpr | |
683 print raw[max(subst.start()-20, 0):subst.end()+20] | |
284 | 684 if var in self.substvars: |
271 | 685 val = self.substvars[var] |
686 else: | |
687 val = raw_input('Substitution for %s (SET SCAN OFF to halt substitution): ' % fullexpr) | |
688 if val.lower().split() == ['set','scan','off']: | |
689 self.scan = False | |
690 return raw | |
691 if isglobal: | |
692 self.substvars[var] = val | |
693 raw = raw.replace(fullexpr, val) | |
694 print 'Substituted %s for %s' % (val, fullexpr) | |
695 subst = regexpr.search(raw) # do not FINDALL b/c we don't want to ask twice | |
696 return raw | |
284 | 697 |
698 numericampre = re.compile('(&(\d+))') | |
699 doubleampre = re.compile('(&&([a-zA-Z\d_$#]+))', re.IGNORECASE) | |
700 singleampre = re.compile( '(&([a-zA-Z\d_$#]+))', re.IGNORECASE) | |
271 | 701 def preparse(self, raw, **kwargs): |
702 if self.scan: | |
284 | 703 raw = self.ampersand_substitution(raw, regexpr=self.numericampre, isglobal=False) |
704 if self.scan: | |
281 | 705 raw = self.ampersand_substitution(raw, regexpr=self.doubleampre, isglobal=True) |
271 | 706 if self.scan: |
281 | 707 raw = self.ampersand_substitution(raw, regexpr=self.singleampre, isglobal=False) |
271 | 708 return raw |
709 | |
189 | 710 rowlimitPattern = pyparsing.Word(pyparsing.nums)('rowlimit') |
204 | 711 terminators = '; \\C \\t \\i \\p \\l \\L \\b '.split() + output_templates.keys() |
199
09592342a33d
ugh - parsing stripping command causes real trouble
catherine@dellzilla
parents:
198
diff
changeset
|
712 |
281 | 713 @options([make_option('-r', '--row', type="int", default=-1, |
714 help='Bind row #ROW instead of final row (zero-based)')]) | |
715 def do_bind(self, arg=None, opts={}): | |
716 ''' | |
717 Inserts the results from the final row in the last completed SELECT statement | |
718 into bind variables with names corresponding to the column names. When the optional | |
719 `autobind` setting is on, this will be issued automatically after every query that | |
720 returns exactly one row. | |
721 ''' | |
722 try: | |
723 self.pystate['r'][-1][opts.row].bind() | |
724 except IndexError: | |
725 print self.do_bind.__doc__ | |
271 | 726 |
192
6bb8a112af6b
accept special terminators on most anything
catherine@dellzilla
parents:
191
diff
changeset
|
727 def do_select(self, arg, bindVarsIn=None, terminator=None): |
189 | 728 """Fetch rows from a table. |
729 | |
730 Limit the number of rows retrieved by appending | |
731 an integer after the terminator | |
732 (example: SELECT * FROM mytable;10 ) | |
733 | |
734 Output may be formatted by choosing an alternative terminator | |
735 ("help terminators" for details) | |
736 """ | |
737 bindVarsIn = bindVarsIn or {} | |
196 | 738 try: |
739 rowlimit = int(arg.parsed.suffix or 0) | |
740 except ValueError: | |
741 rowlimit = 0 | |
206 | 742 print "Specify desired number of rows after terminator (not '%s')" % arg.parsed.suffix |
281 | 743 if arg.parsed.terminator == '\\t': |
744 rowlimit = rowlimit or self.maxtselctrows | |
194 | 745 self.varsUsed = findBinds(arg, self.binds, bindVarsIn) |
233 | 746 if self.wildsql: |
234
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
747 selecttext = self.expandWildSql(arg) |
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
748 else: |
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
749 selecttext = arg |
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
750 self.curs.execute('select ' + selecttext, self.varsUsed) |
194 | 751 self.rows = self.curs.fetchmany(min(self.maxfetch, (rowlimit or self.maxfetch))) |
189 | 752 self.rc = self.curs.rowcount |
753 if self.rc > 0: | |
271 | 754 resultset = ResultSet() |
755 resultset.colnames = [d[0].lower() for d in self.curs.description] | |
281 | 756 resultset.pystate = self.pystate |
271 | 757 resultset.statement = 'select ' + selecttext |
273
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
758 resultset.varsUsed = self.varsUsed |
271 | 759 resultset.extend([Result(r) for r in self.rows]) |
760 for row in resultset: | |
761 row.resultset = resultset | |
281 | 762 self.pystate['r'].append(resultset) |
194 | 763 self.stdout.write('\n%s\n' % (self.output(arg.parsed.terminator, rowlimit))) |
189 | 764 if self.rc == 0: |
765 print '\nNo rows Selected.\n' | |
766 elif self.rc == 1: | |
767 print '\n1 row selected.\n' | |
768 if self.autobind: | |
281 | 769 self.do_bind() |
189 | 770 elif self.rc < self.maxfetch: |
771 print '\n%d rows selected.\n' % self.rc | |
772 else: | |
773 print '\nSelected Max Num rows (%d)' % self.rc | |
193 | 774 |
775 def do_cat(self, arg): | |
226 | 776 '''Shortcut for SELECT * FROM''' |
200 | 777 return self.do_select(self.parsed('SELECT * FROM %s;' % arg, |
778 terminator = arg.parsed.terminator or ';', | |
779 suffix = arg.parsed.suffix)) | |
220 | 780 |
781 def _pull(self, arg, opts, vc=None): | |
782 """Displays source code.""" | |
783 if opts.dump: | |
784 statekeeper = Statekeeper(self, ('stdout',)) | |
785 try: | |
786 for (owner, object_type, object_name) in self.resolve_many(arg, opts): | |
787 if object_type in self.supported_ddl_types: | |
788 object_type = {'DATABASE LINK': 'DB_LINK', 'JAVA CLASS': 'JAVA_SOURCE' | |
789 }.get(object_type) or object_type | |
790 object_type = object_type.replace(' ', '_') | |
791 if opts.dump: | |
792 try: | |
793 os.makedirs(os.path.join(owner.lower(), object_type.lower())) | |
794 except OSError: | |
795 pass | |
796 filename = os.path.join(owner.lower(), object_type.lower(), '%s.sql' % object_name.lower()) | |
797 self.stdout = open(filename, 'w') | |
798 if vc: | |
799 subprocess.call(vc + [filename]) | |
248 | 800 if object_type == 'PACKAGE': |
801 ddl = [['PACKAGE_SPEC', object_name, owner],['PACKAGE_BODY', object_name, owner]] | |
802 elif object_type in ['CONTEXT', 'DIRECTORY', 'JOB']: | |
803 ddl = [[object_type, object_name]] | |
804 else: | |
805 ddl = [[object_type, object_name, owner]] | |
806 for ddlargs in ddl: | |
807 try: | |
246 | 808 self.stdout.write('REMARK BEGIN %s\n%s\nREMARK END\n\n' % (object_name, str(self.curs.callfunc('DBMS_METADATA.GET_DDL', cx_Oracle.CLOB, ddlargs)))) |
248 | 809 except cx_Oracle.DatabaseError, errmsg: |
810 if object_type == 'JOB': | |
811 print '%s: DBMS_METADATA.GET_DDL does not support JOBs (MetaLink DocID 567504.1)' % object_name | |
812 elif 'ORA-31603' in str(errmsg): # not found, as in package w/o package body | |
813 pass | |
814 else: | |
815 raise | |
220 | 816 if opts.full: |
817 for dependent_type in ('OBJECT_GRANT', 'CONSTRAINT', 'TRIGGER'): | |
818 try: | |
245
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
819 self.stdout.write('REMARK BEGIN\n%s\nREMARK END\n\n' % str(self.curs.callfunc('DBMS_METADATA.GET_DEPENDENT_DDL', cx_Oracle.CLOB, |
220 | 820 [dependent_type, object_name, owner]))) |
821 except cx_Oracle.DatabaseError: | |
822 pass | |
823 if opts.dump: | |
824 self.stdout.close() | |
825 except: | |
826 if opts.dump: | |
827 statekeeper.restore() | |
828 raise | |
829 if opts.dump: | |
830 statekeeper.restore() | |
831 | |
221 | 832 def do_show(self, arg): |
833 ''' | |
834 show - display value of all sqlpython parameters | |
835 show (parameter name) - display value of a sqlpython parameter | |
836 show parameter (parameter name) - display value of an ORACLE parameter | |
265
041c656dc8e5
show err working nicely now
catherine@Elli.myhome.westell.com
parents:
264
diff
changeset
|
837 show err (object type/name) - errors from latest PL/SQL object compilation. |
041c656dc8e5
show err working nicely now
catherine@Elli.myhome.westell.com
parents:
264
diff
changeset
|
838 show all err (type/name) - all compilation errors from the user's PL/SQL objects. |
221 | 839 ''' |
840 if arg.startswith('param'): | |
841 try: | |
842 paramname = arg.split()[1].lower() | |
843 except IndexError: | |
844 paramname = '' | |
845 self.onecmd("""SELECT name, | |
846 CASE type WHEN 1 THEN 'BOOLEAN' | |
847 WHEN 2 THEN 'STRING' | |
848 WHEN 3 THEN 'INTEGER' | |
849 WHEN 4 THEN 'PARAMETER FILE' | |
850 WHEN 5 THEN 'RESERVED' | |
851 WHEN 6 THEN 'BIG INTEGER' END type, | |
852 value FROM v$parameter WHERE name LIKE '%%%s%%';""" % paramname) | |
853 else: | |
264
a8deaa38f11e
show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents:
261
diff
changeset
|
854 argpieces = arg.lower().split() |
a8deaa38f11e
show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents:
261
diff
changeset
|
855 try: |
a8deaa38f11e
show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents:
261
diff
changeset
|
856 if argpieces[0][:3] == 'err': |
265
041c656dc8e5
show err working nicely now
catherine@Elli.myhome.westell.com
parents:
264
diff
changeset
|
857 return self._show_errors(all_users=False, limit=1, targets=argpieces[1:]) |
264
a8deaa38f11e
show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents:
261
diff
changeset
|
858 elif (argpieces[0], argpieces[1][:3]) == ('all','err'): |
265
041c656dc8e5
show err working nicely now
catherine@Elli.myhome.westell.com
parents:
264
diff
changeset
|
859 return self._show_errors(all_users=False, limit=None, targets=argpieces[2:]) |
264
a8deaa38f11e
show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents:
261
diff
changeset
|
860 except IndexError: |
a8deaa38f11e
show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents:
261
diff
changeset
|
861 pass |
221 | 862 return Cmd.do_show(self, arg) |
264
a8deaa38f11e
show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents:
261
diff
changeset
|
863 do_sho = do_show |
221 | 864 |
218
397979c7f6d6
dumping working but not for wildcards
catherine@Elli.myhome.westell.com
parents:
217
diff
changeset
|
865 @options([make_option('-d', '--dump', action='store_true', help='dump results to files'), |
397979c7f6d6
dumping working but not for wildcards
catherine@Elli.myhome.westell.com
parents:
217
diff
changeset
|
866 make_option('-f', '--full', action='store_true', help='get dependent objects as well'), |
217
a65b98938596
multi-pull working pretty well
catherine@Elli.myhome.westell.com
parents:
216
diff
changeset
|
867 make_option('-a', '--all', action='store_true', help="all schemas' objects"), |
218
397979c7f6d6
dumping working but not for wildcards
catherine@Elli.myhome.westell.com
parents:
217
diff
changeset
|
868 make_option('-x', '--exact', action='store_true', help="match object name exactly")]) |
189 | 869 def do_pull(self, arg, opts): |
870 """Displays source code.""" | |
220 | 871 self._pull(arg, opts) |
872 | |
247 | 873 supported_ddl_types = 'CLUSTER, CONTEXT, DATABASE LINK, DIRECTORY, FUNCTION, INDEX, JOB, LIBRARY, MATERIALIZED VIEW, PACKAGE, PACKAGE BODY, PACKAGE SPEC, OPERATOR, PACKAGE, PROCEDURE, SEQUENCE, SYNONYM, TABLE, TRIGGER, VIEW, TYPE, TYPE BODY, XML SCHEMA' |
220 | 874 do_pull.__doc__ += '\n\nSupported DDL types: ' + supported_ddl_types |
875 supported_ddl_types = supported_ddl_types.split(', ') | |
189 | 876 |
224 | 877 def _vc(self, arg, opts, program): |
248 | 878 if not os.path.exists('.%s' % program): |
879 create = raw_input('%s repository not yet in current directory (%s). Create (y/N)? ' % | |
880 (program, os.getcwd())) | |
881 if not create.strip().lower().startswith('y'): | |
882 return | |
224 | 883 subprocess.call([program, 'init']) |
220 | 884 opts.dump = True |
885 self._pull(arg, opts, vc=[program, 'add']) | |
886 subprocess.call([program, 'commit', '-m', '"%s"' % opts.message or 'committed from sqlpython']) | |
887 | |
888 @options([ | |
889 make_option('-f', '--full', action='store_true', help='get dependent objects as well'), | |
890 make_option('-a', '--all', action='store_true', help="all schemas' objects"), | |
891 make_option('-x', '--exact', action='store_true', help="match object name exactly"), | |
892 make_option('-m', '--message', action='store', type='string', dest='message', help="message to save to hg log during commit")]) | |
893 def do_hg(self, arg, opts): | |
894 '''hg (opts) (objects): | |
226 | 895 Stores DDL on disk and puts files under Mercurial version control. |
896 Args specify which objects to store, same format as `ls`.''' | |
224 | 897 self._vc(arg, opts, 'hg') |
189 | 898 |
220 | 899 @options([ |
900 make_option('-f', '--full', action='store_true', help='get dependent objects as well'), | |
901 make_option('-a', '--all', action='store_true', help="all schemas' objects"), | |
902 make_option('-x', '--exact', action='store_true', help="match object name exactly"), | |
903 make_option('-m', '--message', action='store', type='string', dest='message', help="message to save to hg log during commit")]) | |
904 def do_bzr(self, arg, opts): | |
905 '''bzr (opts) (objects): | |
226 | 906 Stores DDL on disk and puts files under Bazaar version control. |
907 Args specify which objects to store, same format as `ls`.''' | |
224 | 908 self._vc(arg, opts, 'bzr') |
220 | 909 |
910 @options([ | |
911 make_option('-f', '--full', action='store_true', help='get dependent objects as well'), | |
912 make_option('-a', '--all', action='store_true', help="all schemas' objects"), | |
913 make_option('-x', '--exact', action='store_true', help="match object name exactly"), | |
914 make_option('-m', '--message', action='store', type='string', dest='message', help="message to save to hg log during commit")]) | |
224 | 915 def do_git(self, arg, opts): |
916 '''git (opts) (objects): | |
226 | 917 Stores DDL on disk and puts files under git version control. |
918 Args specify which objects to store, same format as `ls`.''' | |
224 | 919 self._vc(arg, opts, 'git') |
220 | 920 |
196 | 921 all_users_option = make_option('-a', action='store_const', dest="scope", |
222 | 922 default={'col':'', 'view':'user', 'schemas':'user', 'firstcol': ''}, |
923 const={'col':', owner', 'view':'all', 'schemas':'all', 'firstcol': 'owner, '}, | |
194 | 924 help='Describe all objects (not just my own)') |
925 @options([all_users_option, | |
189 | 926 make_option('-c', '--col', action='store_true', help='find column'), |
927 make_option('-t', '--table', action='store_true', help='find table')]) | |
928 def do_find(self, arg, opts): | |
929 """Finds argument in source code or (with -c) in column definitions.""" | |
193 | 930 |
931 capArg = arg.upper() | |
189 | 932 |
933 if opts.col: | |
222 | 934 sql = "SELECT table_name, column_name %s FROM %s_tab_columns where column_name like '%%%s%%' ORDER BY %s table_name, column_name;" \ |
935 % (opts.scope['col'], opts.scope['view'], capArg, opts.scope['firstcol']) | |
189 | 936 elif opts.table: |
222 | 937 sql = "SELECT table_name %s from %s_tables where table_name like '%%%s%%' ORDER BY %s table_name;" \ |
938 % (opts.scope['col'], opts.scope['view'], capArg, opts.scope['firstcol']) | |
189 | 939 else: |
196 | 940 sql = "SELECT * from %s_source where UPPER(text) like '%%%s%%';" % (opts.scope['view'], capArg) |
200 | 941 self.do_select(self.parsed(sql, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
193 | 942 |
194 | 943 @options([all_users_option]) |
189 | 944 def do_describe(self, arg, opts): |
945 "emulates SQL*Plus's DESCRIBE" | |
193 | 946 target = arg.upper() |
947 if not target: | |
194 | 948 return self.do_select(self.parsed("""SELECT object_name, object_type%s |
949 FROM %s_objects | |
950 WHERE object_type IN ('TABLE','VIEW','INDEX') | |
196 | 951 ORDER BY object_name;""" % (opts.scope['col'], opts.scope['view']), |
200 | 952 terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
193 | 953 object_type, owner, object_name = self.resolve(target) |
189 | 954 if not object_type: |
194 | 955 return self.do_select(self.parsed("""SELECT object_name, object_type%s FROM %s_objects |
956 WHERE object_type IN ('TABLE','VIEW','INDEX') | |
957 AND object_name LIKE '%%%s%%' | |
958 ORDER BY object_name;""" % | |
200 | 959 (opts.scope['col'], opts.scope['view'], target), |
960 terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) | |
189 | 961 self.stdout.write("%s %s.%s\n" % (object_type, owner, object_name)) |
962 descQ = descQueries.get(object_type) | |
963 if descQ: | |
964 for q in descQ: | |
204 | 965 self.do_select(self.parsed(q, terminator=arg.parsed.terminator or ';' , suffix=arg.parsed.suffix), |
200 | 966 bindVarsIn={'object_name':object_name, 'owner':owner}) |
189 | 967 elif object_type == 'PACKAGE': |
968 packageContents = self.select_scalar_list(descQueries['PackageObjects'][0], {'package_name':object_name, 'owner':owner}) | |
969 for packageObj_name in packageContents: | |
970 self.stdout.write('Arguments to %s\n' % (packageObj_name)) | |
200 | 971 sql = self.parsed(descQueries['PackageObjArgs'][0], terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix) |
194 | 972 self.do_select(sql, bindVarsIn={'package_name':object_name, 'owner':owner, 'object_name':packageObj_name}) |
189 | 973 do_desc = do_describe |
974 | |
975 def do_deps(self, arg): | |
193 | 976 target = arg.upper() |
977 object_type, owner, object_name = self.resolve(target) | |
189 | 978 if object_type == 'PACKAGE BODY': |
979 q = "and (type != 'PACKAGE BODY' or name != :object_name)'" | |
980 object_type = 'PACKAGE' | |
981 else: | |
982 q = "" | |
193 | 983 q = """SELECT name, |
189 | 984 type |
985 from user_dependencies | |
986 where | |
987 referenced_name like :object_name | |
988 and referenced_type like :object_type | |
989 and referenced_owner like :owner | |
193 | 990 %s;""" % (q) |
200 | 991 self.do_select(self.parsed(q, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix), |
992 bindVarsIn={'object_name':object_name, 'object_type':object_type, 'owner':owner}) | |
189 | 993 |
994 def do_comments(self, arg): | |
995 'Prints comments on a table and its columns.' | |
193 | 996 target = arg.upper() |
997 object_type, owner, object_name, colName = self.resolve_with_column(target) | |
189 | 998 if object_type: |
249 | 999 self._execute(queries['tabComments'], {'table_name':object_name, 'owner':owner}) |
189 | 1000 self.stdout.write("%s %s.%s: %s\n" % (object_type, owner, object_name, self.curs.fetchone()[0])) |
1001 if colName: | |
194 | 1002 sql = queries['oneColComments'] |
1003 bindVarsIn={'owner':owner, 'object_name': object_name, 'column_name': colName} | |
189 | 1004 else: |
194 | 1005 sql = queries['colComments'] |
1006 bindVarsIn={'owner':owner, 'object_name': object_name} | |
200 | 1007 self.do_select(self.parsed(sql, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix), |
1008 bindVarsIn=bindVarsIn) | |
189 | 1009 |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1010 def _resolve(self, identifier): |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1011 parts = identifier.split('.') |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1012 if len(parts) == 2: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1013 owner, object_name = parts |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1014 object_type = self.select_scalar_list('SELECT object_type FROM all_objects WHERE owner = :owner AND object_name = :object_name', |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1015 {'owner': owner, 'object_name': object_name.upper()} |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1016 )[0] |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1017 elif len(parts) == 1: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1018 object_name = parts[0] |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1019 self._execute(queries['resolve'], {'objName':object_name.upper()}) |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1020 object_type, object_name, owner = self.curs.fetchone() |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1021 return object_type, owner, object_name |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1022 |
189 | 1023 def resolve(self, identifier): |
1024 """Checks (my objects).name, (my synonyms).name, (public synonyms).name | |
1025 to resolve a database object's name. """ | |
1026 try: | |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1027 return self._resolve(identifier) |
189 | 1028 except (TypeError, IndexError): |
1029 print 'Could not resolve object %s.' % identifier | |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1030 return '', '', '' |
189 | 1031 |
1032 def resolve_with_column(self, identifier): | |
1033 colName = None | |
1034 object_type, owner, object_name = self.resolve(identifier) | |
1035 if not object_type: | |
1036 parts = identifier.split('.') | |
1037 if len(parts) > 1: | |
1038 colName = parts[-1] | |
1039 identifier = '.'.join(parts[:-1]) | |
1040 object_type, owner, object_name = self.resolve(identifier) | |
1041 return object_type, owner, object_name, colName | |
1042 | |
1043 def do_resolve(self, arg): | |
195 | 1044 target = arg.upper() |
1045 self.stdout.write(','.join(self.resolve(target))+'\n') | |
189 | 1046 |
1047 def spoolstop(self): | |
1048 if self.spoolFile: | |
1049 self.stdout = self.stdoutBeforeSpool | |
1050 print 'Finished spooling to ', self.spoolFile.name | |
1051 self.spoolFile.close() | |
1052 self.spoolFile = None | |
1053 | |
1054 def do_spool(self, arg): | |
1055 """spool [filename] - begins redirecting output to FILENAME.""" | |
1056 self.spoolstop() | |
1057 arg = arg.strip() | |
1058 if not arg: | |
1059 arg = 'output.lst' | |
1060 if arg.lower() != 'off': | |
1061 if '.' not in arg: | |
1062 arg = '%s.lst' % arg | |
1063 print 'Sending output to %s (until SPOOL OFF received)' % (arg) | |
1064 self.spoolFile = open(arg, 'w') | |
1065 self.stdout = self.spoolFile | |
1066 | |
1067 def do_write(self, args): | |
281 | 1068 'Obsolete command. Use (query) > outfilename instead.' |
1069 print self.do_write.__doc__ | |
189 | 1070 return |
1071 | |
1072 def do_compare(self, args): | |
1073 """COMPARE query1 TO query2 - uses external tool to display differences. | |
1074 | |
1075 Sorting is recommended to avoid false hits. | |
1076 Will attempt to use a graphical diff/merge tool like kdiff3, meld, or Araxis Merge, | |
1077 if they are installed.""" | |
193 | 1078 #TODO: Update this to use pyparsing |
189 | 1079 fnames = [] |
1080 args2 = args.split(' to ') | |
1081 if len(args2) < 2: | |
1082 print self.do_compare.__doc__ | |
1083 return | |
1084 for n in range(len(args2)): | |
1085 query = args2[n] | |
1086 fnames.append('compare%s.txt' % n) | |
1087 #TODO: update this terminator-stripping | |
1088 if query.rstrip()[-1] != self.terminator: | |
1089 query = '%s%s' % (query, self.terminator) | |
1090 self.onecmd_plus_hooks('%s > %s' % (query, fnames[n])) | |
1091 diffMergeSearcher.invoke(fnames[0], fnames[1]) | |
1092 | |
1093 bufferPosPattern = re.compile('\d+') | |
1094 rangeIndicators = ('-',':') | |
1095 | |
1096 def do_psql(self, arg): | |
1097 '''Shortcut commands emulating psql's backslash commands. | |
1098 | |
1099 \c connect | |
1100 \d desc | |
1101 \e edit | |
1102 \g run | |
1103 \h help | |
1104 \i load | |
1105 \o spool | |
1106 \p list | |
1107 \q quit | |
1108 \w save | |
1109 \db _dir_tablespaces | |
1110 \dd comments | |
1111 \dn _dir_schemas | |
1112 \dt _dir_tables | |
1113 \dv _dir_views | |
1114 \di _dir_indexes | |
1115 \? help psql''' | |
1116 commands = {} | |
1117 for c in self.do_psql.__doc__.splitlines()[2:]: | |
1118 (abbrev, command) = c.split(None, 1) | |
1119 commands[abbrev[1:]] = command | |
1120 words = arg.split(None,1) | |
1121 try: | |
1122 abbrev = words[0] | |
1123 except IndexError: | |
1124 return | |
1125 try: | |
1126 args = words[1] | |
1127 except IndexError: | |
1128 args = '' | |
1129 try: | |
200 | 1130 return self.onecmd('%s %s%s%s' % (commands[abbrev], args, arg.parsed.terminator, arg.parsed.suffix)) |
189 | 1131 except KeyError: |
1132 print 'psql command \%s not yet supported.' % abbrev | |
1133 | |
194 | 1134 @options([all_users_option]) |
189 | 1135 def do__dir_tables(self, arg, opts): |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1136 ''' |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1137 Lists all tables whose names match argument. |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1138 ''' |
194 | 1139 sql = """SELECT table_name, 'TABLE' as type%s FROM %s_tables WHERE table_name LIKE '%%%s%%';""" % \ |
196 | 1140 (opts.scope['col'], opts.scope['view'], arg.upper()) |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1141 if self.sql_echo: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1142 print sql |
200 | 1143 self.do_select(self.parsed(sql, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
194 | 1144 |
1145 @options([all_users_option]) | |
189 | 1146 def do__dir_views(self, arg, opts): |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1147 ''' |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1148 Lists all views whose names match argument. |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1149 ''' |
194 | 1150 sql = """SELECT view_name, 'VIEW' as type%s FROM %s_views WHERE view_name LIKE '%%%s%%';""" % \ |
196 | 1151 (opts.scope['col'], opts.scope['view'], arg.upper()) |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1152 if self.sql_echo: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1153 print sql |
200 | 1154 self.do_select(self.parsed(sql, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
194 | 1155 |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1156 def do__dir_indexes(self, arg): |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1157 ''' |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1158 Called with an exact table name, lists the indexes of that table. |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1159 Otherwise, acts as shortcut for `ls index/*(arg)*` |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1160 ''' |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1161 try: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1162 table_type, table_owner, table_name = self._resolve(arg) |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1163 except TypeError, IndexError: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1164 return self.onecmd('ls Index/*%s*' % arg) |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1165 sql = """SELECT owner, index_name, index_type FROM all_indexes |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1166 WHERE table_owner = :table_owner |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1167 AND table_name = :table_name; |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1168 ORDER BY owner, index_name""" |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1169 self.do_select(self.parsed(sql, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix), |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1170 bindVarsIn = {'table_owner': table_owner, 'table_name': table_name}) |
189 | 1171 |
1172 def do__dir_tablespaces(self, arg): | |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1173 ''' |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1174 Lists all tablespaces. |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1175 ''' |
194 | 1176 sql = """SELECT tablespace_name, file_name from dba_data_files;""" |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1177 if self.sql_echo: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1178 print sql |
200 | 1179 self.do_select(self.parsed(sql, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
189 | 1180 |
1181 def do__dir_schemas(self, arg): | |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1182 ''' |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1183 Lists all object owners, together with the number of objects they own. |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1184 ''' |
194 | 1185 sql = """SELECT owner, count(*) AS objects FROM all_objects GROUP BY owner ORDER BY owner;""" |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1186 if self.sql_echo: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1187 print sql |
200 | 1188 self.do_select(self.parsed(sql, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
189 | 1189 |
1190 def do_head(self, arg): | |
226 | 1191 '''Shortcut for SELECT * FROM <arg>;10 |
1192 The terminator (\\t, \\g, \\x, etc.) and number of rows can | |
1193 be changed as for any other SELECT statement.''' | |
200 | 1194 sql = self.parsed('SELECT * FROM %s;' % arg, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix) |
195 | 1195 sql.parsed['suffix'] = sql.parsed.suffix or '10' |
196 | 1196 self.do_select(self.parsed(sql)) |
189 | 1197 |
1198 def do_print(self, arg): | |
1199 'print VARNAME: Show current value of bind variable VARNAME.' | |
1200 if arg: | |
1201 if arg[0] == ':': | |
1202 arg = arg[1:] | |
1203 try: | |
1204 self.stdout.write(str(self.binds[arg])+'\n') | |
1205 except KeyError: | |
1206 self.stdout.write('No bind variable %s\n' % arg) | |
1207 else: | |
250 | 1208 for (var, val) in sorted(self.binds.items()): |
189 | 1209 print ':%s = %s' % (var, val) |
1210 | |
285 | 1211 def split_on_parser(self, parser, arg): |
1212 try: | |
1213 assigner, startat, endat = parser.scanner.scanString(arg).next() | |
1214 return (arg[:startat].strip(), arg[endat:].strip()) | |
1215 except StopIteration: | |
1216 return ''.join(arg.split()[:1]), '' | |
1217 | |
189 | 1218 assignmentScanner = Parser(pyparsing.Literal(':=') ^ '=') |
285 | 1219 def interpret_variable_assignment(self, arg): |
1220 ''' | |
1221 Accepts strings like `foo = 'bar'` or `baz := 22`, returning Python | |
1222 variables as appropriate | |
1223 ''' | |
1224 var, val = self.split_on_parser(self.assignmentScanner, arg) | |
1225 if not var: | |
1226 return None, None | |
1227 if (len(val) > 1) and ((val[0] == val[-1] == "'") or (val[0] == val[-1] == '"')): | |
1228 return var, val[1:-1] | |
1229 try: | |
1230 return var, int(val) | |
1231 except ValueError: | |
1232 try: | |
1233 return var, float(val) | |
1234 except ValueError: | |
1235 # use the conversions implicit in cx_Oracle's select to | |
1236 # cast the value into an appropriate type (dates, for instance) | |
1237 try: | |
1238 self.curs.execute('SELECT %s FROM dual' % val) | |
1239 return var, self.curs.fetchone()[0] | |
1240 except cx_Oracle.DatabaseError: | |
1241 return var, val # we give up and assume it's a string | |
1242 | |
189 | 1243 def do_setbind(self, arg): |
285 | 1244 '''Sets or shows values of bind (`:`) variables.''' |
189 | 1245 if not arg: |
1246 return self.do_print(arg) | |
285 | 1247 var, val = self.interpret_variable_assignment(arg) |
1248 if val: | |
1249 self.binds[var] = val | |
1250 else: | |
1251 return self.do_print(var) | |
189 | 1252 |
285 | 1253 def do_define(self, arg): |
1254 '''Sets or shows values of substitution (`&`) variables.''' | |
1255 if not arg: | |
1256 for (substvar, val) in sorted(self.substvars.items()): | |
1257 print 'DEFINE %s = "%s" (%s)' % (substvar, val, type(val)) | |
1258 var, val = self.interpret_variable_assignment(arg) | |
1259 if val: | |
1260 self.substvars[var] = val | |
1261 else: | |
1262 if var in self.substvars: | |
1263 print 'DEFINE %s = "%s" (%s)' % (var, self.substvars[var], type(self.substvars[var])) | |
1264 | |
1265 do_def = do_define | |
1266 | |
189 | 1267 def do_exec(self, arg): |
213 | 1268 if arg.startswith(':'): |
189 | 1269 self.do_setbind(arg[1:]) |
1270 else: | |
1271 varsUsed = findBinds(arg, self.binds, {}) | |
1272 try: | |
1273 self.curs.execute('begin\n%s;end;' % arg, varsUsed) | |
1274 except Exception, e: | |
1275 print e | |
1276 | |
1277 ''' | |
1278 Fails: | |
1279 select n into :n from test;''' | |
1280 | |
1281 def anon_plsql(self, line1): | |
1282 lines = [line1] | |
1283 while True: | |
247 | 1284 line = self.pseudo_raw_input(self.continuation_prompt) |
241 | 1285 if line == 'EOF': |
1286 return | |
189 | 1287 if line.strip() == '/': |
1288 try: | |
1289 self.curs.execute('\n'.join(lines)) | |
1290 except Exception, e: | |
1291 print e | |
1292 return | |
1293 lines.append(line) | |
1294 | |
1295 def do_begin(self, arg): | |
281 | 1296 ''' |
1297 PL/SQL blocks can be used normally in sqlpython, though enclosing statements in | |
1298 REMARK BEGIN... REMARK END statements can help with parsing speed.''' | |
189 | 1299 self.anon_plsql('begin ' + arg) |
1300 | |
1301 def do_declare(self, arg): | |
1302 self.anon_plsql('declare ' + arg) | |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1303 |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1304 def _ls_statement(self, arg, opts): |
189 | 1305 if arg: |
280
8ea39093ddf2
struggling with catching terminator after /*
catherine@dellzilla
parents:
277
diff
changeset
|
1306 target = arg.upper().replace('*','%') |
8ea39093ddf2
struggling with catching terminator after /*
catherine@dellzilla
parents:
277
diff
changeset
|
1307 where = """\nWHERE object_type || '/' || object_name LIKE '%s' |
8ea39093ddf2
struggling with catching terminator after /*
catherine@dellzilla
parents:
277
diff
changeset
|
1308 OR object_name LIKE '%s'""" % (target, target) |
189 | 1309 else: |
1310 where = '' | |
1311 if opts.all: | |
1312 whose = 'all' | |
1313 objname = "owner || '.' || object_name" | |
1314 else: | |
1315 whose = 'user' | |
1316 objname = 'object_name' | |
232
52adb09094b3
fixed bugs in VC introduced by sort-order options
catherine@dellzilla
parents:
230
diff
changeset
|
1317 if hasattr(opts, 'long') and opts.long: |
228 | 1318 moreColumns = ', status, last_ddl_time' |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1319 else: |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1320 moreColumns = '' |
227 | 1321 |
1322 # 'Normal' sort order is DATE DESC (maybe), object type ASC, object name ASC | |
244 | 1323 sortdirection = (hasattr(opts, 'reverse') and opts.reverse and 'DESC') or 'ASC' |
227 | 1324 orderby = 'object_type %s, object_name %s' % (sortdirection, sortdirection) |
232
52adb09094b3
fixed bugs in VC introduced by sort-order options
catherine@dellzilla
parents:
230
diff
changeset
|
1325 if hasattr(opts, 'timesort') and opts.timesort: |
52adb09094b3
fixed bugs in VC introduced by sort-order options
catherine@dellzilla
parents:
230
diff
changeset
|
1326 orderby = 'last_ddl_time %s, %s' % (('ASC' if hasattr(opts, 'reverse') and opts.reverse else 'DESC'), orderby) |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1327 return {'objname': objname, 'moreColumns': moreColumns, |
227 | 1328 'whose': whose, 'where': where, 'orderby': orderby} |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1329 |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1330 def resolve_many(self, arg, opts): |
217
a65b98938596
multi-pull working pretty well
catherine@Elli.myhome.westell.com
parents:
216
diff
changeset
|
1331 opts.long = False |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1332 clauses = self._ls_statement(arg, opts) |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1333 if opts.all: |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1334 clauses['owner'] = 'owner' |
189 | 1335 else: |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1336 clauses['owner'] = 'user' |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1337 statement = '''SELECT %(owner)s, object_type, object_name |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1338 FROM %(whose)s_objects %(where)s |
217
a65b98938596
multi-pull working pretty well
catherine@Elli.myhome.westell.com
parents:
216
diff
changeset
|
1339 ORDER BY object_type, object_name''' % clauses |
249 | 1340 self._execute(statement) |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1341 return self.curs.fetchall() |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1342 |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1343 @options([make_option('-l', '--long', action='store_true', help='long descriptions'), |
217
a65b98938596
multi-pull working pretty well
catherine@Elli.myhome.westell.com
parents:
216
diff
changeset
|
1344 make_option('-a', '--all', action='store_true', help="all schemas' objects"), |
227 | 1345 make_option('-t', '--timesort', action='store_true', help="Sort by last_ddl_time"), |
280
8ea39093ddf2
struggling with catching terminator after /*
catherine@dellzilla
parents:
277
diff
changeset
|
1346 make_option('-r', '--reverse', action='store_true', help="Reverse order while sorting")]) |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1347 def do_ls(self, arg, opts): |
281 | 1348 ''' |
1349 Lists objects as through they were in an {object_type}/{object_name} UNIX | |
1350 directory structure. `*` and `%` may be used as wildcards. | |
1351 ''' | |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1352 statement = '''SELECT object_type || '/' || %(objname)s AS name %(moreColumns)s |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1353 FROM %(whose)s_objects %(where)s |
227 | 1354 ORDER BY %(orderby)s;''' % self._ls_statement(arg, opts) |
200 | 1355 self.do_select(self.parsed(statement, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
189 | 1356 |
1357 @options([make_option('-i', '--ignore-case', dest='ignorecase', action='store_true', help='Case-insensitive search')]) | |
1358 def do_grep(self, arg, opts): | |
281 | 1359 """grep {target} {table} [{table2,...}] - search for {target} in any of {table}'s fields""" |
189 | 1360 |
195 | 1361 targetnames = arg.split() |
189 | 1362 pattern = targetnames.pop(0) |
1363 targets = [] | |
1364 for target in targetnames: | |
1365 if '*' in target: | |
249 | 1366 self._execute("SELECT owner, table_name FROM all_tables WHERE table_name LIKE '%s'%s" % |
189 | 1367 (target.upper().replace('*','%')), arg.terminator) |
1368 for row in self.curs: | |
1369 targets.append('%s.%s' % row) | |
1370 else: | |
1371 targets.append(target) | |
1372 for target in targets: | |
1373 print target | |
1374 target = target.rstrip(';') | |
1375 try: | |
249 | 1376 self._execute('select * from %s where 1=0' % target) # just to fill description |
189 | 1377 if opts.ignorecase: |
1378 sql = ' or '.join("LOWER(%s) LIKE '%%%s%%'" % (d[0], pattern.lower()) for d in self.curs.description) | |
1379 else: | |
1380 sql = ' or '.join("%s LIKE '%%%s%%'" % (d[0], pattern) for d in self.curs.description) | |
200 | 1381 sql = self.parsed('SELECT * FROM %s WHERE %s;' % (target, sql), terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix) |
199
09592342a33d
ugh - parsing stripping command causes real trouble
catherine@dellzilla
parents:
198
diff
changeset
|
1382 self.do_select(sql) |
189 | 1383 except Exception, e: |
1384 print e | |
1385 import traceback | |
1386 traceback.print_exc(file=sys.stdout) | |
1387 | |
249 | 1388 def _execute(self, sql, bindvars={}): |
1389 if self.sql_echo: | |
1390 print sql | |
1391 self.curs.execute(sql, bindvars) | |
1392 | |
189 | 1393 def do_refs(self, arg): |
211 | 1394 '''Lists referential integrity (foreign key constraints) on an object.''' |
1395 | |
1396 if not arg.strip(): | |
1397 print 'Usage: refs (table name)' | |
190 | 1398 result = [] |
205 | 1399 (type, owner, table_name) = self.resolve(arg.upper()) |
249 | 1400 sql = """SELECT constraint_name, r_owner, r_constraint_name |
191 | 1401 FROM all_constraints |
1402 WHERE constraint_type = 'R' | |
1403 AND owner = :owner | |
249 | 1404 AND table_name = :table_name""" |
1405 self._execute(sql, {"owner": owner, "table_name": table_name}) | |
191 | 1406 for (constraint_name, remote_owner, remote_constraint_name) in self.curs.fetchall(): |
1407 result.append('%s on %s.%s:' % (constraint_name, owner, table_name)) | |
249 | 1408 |
1409 self._execute("SELECT column_name FROM all_cons_columns WHERE owner = :owner AND constraint_name = :constraint_name ORDER BY position", | |
1410 {'constraint_name': constraint_name, 'owner': owner}) | |
191 | 1411 result.append(" (%s)" % (",".join(col[0] for col in self.curs.fetchall()))) |
249 | 1412 self._execute("SELECT table_name FROM all_constraints WHERE owner = :remote_owner AND constraint_name = :remote_constraint_name", |
1413 {'remote_owner': remote_owner, 'remote_constraint_name': remote_constraint_name}) | |
191 | 1414 remote_table_name = self.curs.fetchone()[0] |
1415 result.append("must be in %s:" % (remote_table_name)) | |
249 | 1416 self._execute("SELECT column_name FROM all_cons_columns WHERE owner = :remote_owner AND constraint_name = :remote_constraint_name ORDER BY position", |
191 | 1417 {'remote_constraint_name': remote_constraint_name, 'remote_owner': remote_owner}) |
1418 result.append(' (%s)\n' % (",".join(col[0] for col in self.curs.fetchall()))) | |
1419 remote_table_name = table_name | |
1420 remote_owner = owner | |
249 | 1421 self._execute("""SELECT owner, constraint_name, table_name, r_constraint_name |
191 | 1422 FROM all_constraints |
1423 WHERE (r_owner, r_constraint_name) IN | |
1424 ( SELECT owner, constraint_name | |
1425 FROM all_constraints | |
192
6bb8a112af6b
accept special terminators on most anything
catherine@dellzilla
parents:
191
diff
changeset
|
1426 WHERE table_name = :remote_table_name |
191 | 1427 AND owner = :remote_owner )""", |
1428 {'remote_table_name': remote_table_name, 'remote_owner': remote_owner}) | |
1429 for (owner, constraint_name, table_name, remote_constraint_name) in self.curs.fetchall(): | |
1430 result.append('%s on %s.%s:' % (constraint_name, owner, table_name)) | |
249 | 1431 self._execute("SELECT column_name FROM all_cons_columns WHERE owner = :owner AND constraint_name = :constraint_name ORDER BY position", |
191 | 1432 {'constraint_name': constraint_name, 'owner': owner}) |
1433 result.append(" (%s)" % (",".join(col[0] for col in self.curs.fetchall()))) | |
249 | 1434 self._execute("SELECT table_name FROM all_constraints WHERE owner = :remote_owner AND constraint_name = :remote_constraint_name", |
191 | 1435 {'remote_owner': remote_owner, 'remote_constraint_name': remote_constraint_name}) |
1436 remote_table_name = self.curs.fetchone()[0] | |
1437 result.append("must be in %s:" % (remote_table_name)) | |
249 | 1438 self._execute("SELECT column_name FROM all_cons_columns WHERE owner = :remote_owner AND constraint_name = :remote_constraint_name ORDER BY position", |
191 | 1439 {'remote_constraint_name': remote_constraint_name, 'remote_owner': remote_owner}) |
1440 result.append(' (%s)\n' % (",".join(col[0] for col in self.curs.fetchall()))) | |
1441 self.stdout.write('\n'.join(result) + "\n") | |
190 | 1442 |
189 | 1443 def _test(): |
1444 import doctest | |
1445 doctest.testmod() | |
1446 | |
1447 if __name__ == "__main__": | |
1448 "Silent return implies that all unit tests succeeded. Use -v to see details." | |
1449 _test() | |
198
b2d8bf5f89db
merged with changes from work
catherine@Elli.myhome.westell.com
parents:
196
diff
changeset
|
1450 if __name__ == "__main__": |
b2d8bf5f89db
merged with changes from work
catherine@Elli.myhome.westell.com
parents:
196
diff
changeset
|
1451 "Silent return implies that all unit tests succeeded. Use -v to see details." |
b2d8bf5f89db
merged with changes from work
catherine@Elli.myhome.westell.com
parents:
196
diff
changeset
|
1452 _test() |