Mercurial > sqlpython
annotate sqlpython/sqlpyPlus.py @ 274:0ae3a8227bc0
added binds to help
author | catherine@Elli.myhome.westell.com |
---|---|
date | Wed, 18 Mar 2009 19:03:27 -0400 |
parents | 9d67065ea030 |
children | df78546969c9 |
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] |
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
372 self.resultset.pystate['binds'][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() | |
271 | 390 self.settable += 'autobind commit_on_exit maxfetch maxtselctrows result_history_max_mbytes scan serveroutput sql_echo 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 = [] | |
403 self.result_history_max_mbytes = 10 | |
273
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
404 self.pystate = {'r': [], 'binds': self.binds} |
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 | |
420 | |
421 do__load = Cmd.do_load | |
257
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
422 |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
423 def dbms_output(self): |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
424 "Dumps contents of Oracle's DBMS_OUTPUT buffer (where PUT_LINE goes)" |
261 | 425 try: |
426 line = self.curs.var(cx_Oracle.STRING) | |
427 status = self.curs.var(cx_Oracle.NUMBER) | |
257
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
428 self.curs.callproc('dbms_output.get_line', [line, status]) |
261 | 429 while not status.getvalue(): |
430 self.stdout.write(line.getvalue()) | |
431 self.stdout.write('\n') | |
432 self.curs.callproc('dbms_output.get_line', [line, status]) | |
433 except AttributeError: | |
434 pass | |
257
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
435 |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
436 def postcmd(self, stop, line): |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
437 """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
|
438 if self.serveroutput: |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
439 self.dbms_output() |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
440 return stop |
241 | 441 |
245
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
442 def do_remark(self, line): |
242 | 443 ''' |
245
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
444 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
|
445 |
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
446 Wrapping a *single* SQL or PL/SQL statement in `REMARK BEGIN` and `REMARK END` |
241 | 447 tells sqlpython to submit the enclosed code directly to Oracle as a single |
242 | 448 unit of code. |
241 | 449 |
450 Without these markers, sqlpython fails to properly distinguish the beginning | |
451 and end of all but the simplest PL/SQL blocks, causing errors. sqlpython also | |
452 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
|
453 the statement has ended yet; `REMARK BEGIN` and `REMARK END` allow it to skip this |
241 | 454 parsing. |
455 | |
245
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
456 Standard SQL*Plus interprets REMARK BEGIN and REMARK END as comments, so it is |
242 | 457 safe to include them in SQL*Plus scripts. |
241 | 458 ''' |
245
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
459 if not line.lower().strip().startswith('begin'): |
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
460 return |
241 | 461 statement = [] |
247 | 462 next = self.pseudo_raw_input(self.continuation_prompt) |
245
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
463 while next.lower().split()[:2] != ['remark','end']: |
241 | 464 statement.append(next) |
247 | 465 next = self.pseudo_raw_input(self.continuation_prompt) |
242 | 466 return self.onecmd('\n'.join(statement)) |
272 | 467 |
468 def do_py(self, arg): | |
469 ''' | |
470 py <command>: Executes a Python command. | |
471 py: Enters interactive Python mode (end with `\py`). | |
472 Past SELECT results are stored in list `r`; most recent resultset is `r[-1]`. | |
274 | 473 SQL bind variables can be accessed/changed via `binds`. |
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) |
272 | 476 |
189 | 477 def onecmd_plus_hooks(self, line): |
478 line = self.precmd(line) | |
479 stop = self.onecmd(line) | |
480 stop = self.postcmd(stop, line) | |
481 | |
257
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
482 def _onchange_serveroutput(self, old, new): |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
483 if new: |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
484 self.curs.callproc('dbms_output.enable', []) |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
485 else: |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
486 self.curs.callproc('dbms_output.disable', []) |
6d4d90fb2082
dbms_output.put_line working
catherine@Elli.myhome.westell.com
parents:
254
diff
changeset
|
487 |
189 | 488 def do_shortcuts(self,arg): |
489 """Lists available first-character shortcuts | |
490 (i.e. '!dir' is equivalent to 'shell dir')""" | |
491 for (scchar, scto) in self.shortcuts.items(): | |
492 print '%s: %s' % (scchar, scto) | |
493 | |
254
b61e21386383
oops, restore lines of code after sql_format_item
catherine@Elli.myhome.westell.com
parents:
253
diff
changeset
|
494 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
|
495 inputStatementFormatters = { |
b61e21386383
oops, restore lines of code after sql_format_item
catherine@Elli.myhome.westell.com
parents:
253
diff
changeset
|
496 cx_Oracle.STRING: "'%s'", |
b61e21386383
oops, restore lines of code after sql_format_item
catherine@Elli.myhome.westell.com
parents:
253
diff
changeset
|
497 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
|
498 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
|
499 inputStatementFormatters[cx_Oracle.TIMESTAMP] = inputStatementFormatters[cx_Oracle.DATETIME] |
189 | 500 def output(self, outformat, rowlimit): |
501 self.tblname = self.tableNameFinder.search(self.curs.statement).group(1) | |
502 self.colnames = [d[0] for d in self.curs.description] | |
503 if outformat in output_templates: | |
504 self.colnamelen = max(len(colname) for colname in self.colnames) | |
505 self.coltypes = [d[1] for d in self.curs.description] | |
506 self.formatters = [self.inputStatementFormatters.get(typ, '%s') for typ in self.coltypes] | |
507 result = output_templates[outformat].generate(**self.__dict__) | |
508 elif outformat == '\\t': # transposed | |
509 rows = [self.colnames] | |
510 rows.extend(list(self.rows)) | |
511 transpr = [[rows[y][x] for y in range(len(rows))]for x in range(len(rows[0]))] # matrix transpose | |
512 newdesc = [['ROW N.'+str(y),10] for y in range(len(rows))] | |
513 for x in range(len(self.curs.description)): | |
514 if str(self.curs.description[x][1]) == "<type 'cx_Oracle.BINARY'>": # handles RAW columns | |
515 rname = transpr[x][0] | |
516 transpr[x] = map(binascii.b2a_hex, transpr[x]) | |
517 transpr[x][0] = rname | |
518 newdesc[0][0] = 'COLUMN NAME' | |
519 result = '\n' + sqlpython.pmatrix(transpr,newdesc) | |
520 elif outformat in ('\\l', '\\L', '\\p', '\\b'): | |
521 plot = Plot() | |
522 plot.build(self, outformat) | |
523 plot.shelve() | |
524 plot.draw() | |
525 return '' | |
526 else: | |
229 | 527 result = sqlpython.pmatrix(self.rows, self.curs.description, self.maxfetch, heading=self.heading) |
189 | 528 return result |
529 | |
530 legalOracle = re.compile('[a-zA-Z_$#]') | |
531 | |
532 def select_scalar_list(self, sql, binds={}): | |
249 | 533 self._execute(sql, binds) |
189 | 534 return [r[0] for r in self.curs.fetchall()] |
535 | |
536 columnNameRegex = re.compile( | |
537 r'select\s+(.*)from', | |
538 re.IGNORECASE | re.DOTALL | re.MULTILINE) | |
539 def completedefault(self, text, line, begidx, endidx): | |
540 segment = completion.whichSegment(line) | |
541 text = text.upper() | |
542 completions = [] | |
543 if segment == 'select': | |
544 stmt = "SELECT column_name FROM user_tab_columns WHERE column_name LIKE '%s%%'" | |
545 completions = self.select_scalar_list(stmt % (text)) | |
546 if not completions: | |
547 stmt = "SELECT column_name FROM all_tab_columns WHERE column_name LIKE '%s%%'" | |
548 completions = self.select_scalar_list(stmt % (text)) | |
549 if segment == 'from': | |
550 columnNames = self.columnNameRegex.search(line) | |
551 if columnNames: | |
552 columnNames = columnNames.group(1) | |
553 columnNames = [c.strip().upper() for c in columnNames.split(',')] | |
554 stmt1 = "SELECT table_name FROM all_tab_columns WHERE column_name = '%s' AND table_name LIKE '%s%%'" | |
555 for columnName in columnNames: | |
556 # and if columnName is * ? | |
557 completions.extend(self.select_scalar_list(stmt1 % (columnName, text))) | |
558 if segment in ('from', 'update', 'insert into') and (not completions): | |
559 stmt = "SELECT table_name FROM user_tables WHERE table_name LIKE '%s%%'" | |
560 completions = self.select_scalar_list(stmt % (text)) | |
561 if not completions: | |
562 stmt = """SELECT table_name FROM user_tables WHERE table_name LIKE '%s%%' | |
563 UNION | |
564 SELECT DISTINCT owner FROM all_tables WHERE owner LIKE '%%%s'""" | |
565 completions = self.select_scalar_list(stmt % (text, text)) | |
566 if segment in ('where', 'group by', 'order by', 'having', 'set'): | |
567 tableNames = completion.tableNamesFromFromClause(line) | |
568 if tableNames: | |
569 stmt = """SELECT column_name FROM all_tab_columns | |
570 WHERE table_name IN (%s)""" % \ | |
571 (','.join("'%s'" % (t) for t in tableNames)) | |
572 stmt = "%s AND column_name LIKE '%s%%'" % (stmt, text) | |
573 completions = self.select_scalar_list(stmt) | |
574 if not segment: | |
575 stmt = "SELECT object_name FROM all_objects WHERE object_name LIKE '%s%%'" | |
576 completions = self.select_scalar_list(stmt % (text)) | |
577 return completions | |
233 | 578 |
234
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
579 columnlistPattern = pyparsing.SkipTo(pyparsing.CaselessKeyword('from'))('columns') + \ |
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
580 pyparsing.SkipTo(pyparsing.stringEnd)('remainder') |
233 | 581 |
236 | 582 negator = pyparsing.Literal('!')('exclude') |
242 | 583 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
|
584 colName = negator + pyparsing.Word('$_#' + pyparsing.alphas, '$_#' + pyparsing.alphanums)('column_name') |
241 | 585 wildColName = pyparsing.Optional(negator) + pyparsing.Word('?*%$_#' + pyparsing.alphas, '?*%$_#' + pyparsing.alphanums, min=2)('column_name') |
242 | 586 colNumber.ignore(pyparsing.cStyleComment).ignore(Parser.comment_def). \ |
587 ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString) | |
235 | 588 wildSqlParser = colNumber ^ colName ^ wildColName |
589 wildSqlParser.ignore(pyparsing.cStyleComment).ignore(Parser.comment_def). \ | |
236 | 590 ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString) |
591 emptyCommaRegex = re.compile(',\s*,', re.DOTALL) | |
592 deadStarterCommaRegex = re.compile('^\s*,', re.DOTALL) | |
593 deadEnderCommaRegex = re.compile(',\s*$', re.DOTALL) | |
233 | 594 def expandWildSql(self, arg): |
595 try: | |
234
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
596 columnlist = self.columnlistPattern.parseString(arg) |
233 | 597 except pyparsing.ParseException: |
598 return arg | |
236 | 599 parseresults = list(self.wildSqlParser.scanString(columnlist.columns)) |
239
4c563c2218e6
catching standard names caught
catherine@Elli.myhome.westell.com
parents:
238
diff
changeset
|
600 # 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
|
601 # but can't figure out how |
4c563c2218e6
catching standard names caught
catherine@Elli.myhome.westell.com
parents:
238
diff
changeset
|
602 parseresults = [p for p in parseresults if |
4c563c2218e6
catching standard names caught
catherine@Elli.myhome.westell.com
parents:
238
diff
changeset
|
603 p[0].column_number or |
4c563c2218e6
catching standard names caught
catherine@Elli.myhome.westell.com
parents:
238
diff
changeset
|
604 '*' in p[0].column_name or |
4c563c2218e6
catching standard names caught
catherine@Elli.myhome.westell.com
parents:
238
diff
changeset
|
605 '%' in p[0].column_name or |
241 | 606 '?' in p[0].column_name or |
239
4c563c2218e6
catching standard names caught
catherine@Elli.myhome.westell.com
parents:
238
diff
changeset
|
607 p[0].exclude] |
236 | 608 if not parseresults: |
609 return arg | |
234
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
610 self.curs.execute('select * ' + columnlist.remainder, self.varsUsed) |
236 | 611 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
|
612 replacers = {} |
236 | 613 included = set() |
614 excluded = set() | |
615 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
|
616 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
|
617 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
|
618 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
|
619 finder = finder.replace('%','.*') |
241 | 620 finder = finder.replace('?','.') |
621 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
|
622 elif col.column_number: |
236 | 623 idx = int(col.column_number) |
624 if idx > 0: | |
625 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
|
626 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
|
627 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
|
628 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
|
629 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
|
630 for colname in colnames: |
236 | 631 if col.exclude: |
632 included.discard(colname) | |
633 include_here = columns_available[:] | |
634 include_here.remove(colname) | |
240 | 635 replacers[arg[startpos:endpos]].extend(i for i in include_here if i not in replacers[arg[startpos:endpos]]) |
236 | 636 excluded.add(colname) |
637 else: | |
638 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
|
639 replacers[arg[startpos:endpos]].append(colname) |
236 | 640 |
234
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
641 replacers = sorted(replacers.items(), key=len, reverse=True) |
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
642 result = columnlist.columns |
236 | 643 for (target, replacement) in replacers: |
242 | 644 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
|
645 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
|
646 included.update(cols) |
236 | 647 result = result.replace(target, replacement) |
242 | 648 # some column names could get wiped out completely, so we fix their dangling commas |
236 | 649 result = self.emptyCommaRegex.sub(',', result) |
650 result = self.deadStarterCommaRegex.sub('', result) | |
651 result = self.deadEnderCommaRegex.sub('', result) | |
242 | 652 if not result.strip(): |
653 print 'No columns found matching criteria.' | |
654 return 'null from dual' | |
237 | 655 return result + ' ' + columnlist.remainder |
233 | 656 |
271 | 657 def do_ampersand_substitution(self, raw, regexpr, isglobal): |
658 subst = regexpr.search(raw) | |
659 while subst: | |
660 fullexpr, var = subst.group(1), subst.group(2) | |
661 print 'Substitution variable %s found in:' % fullexpr | |
662 print raw[max(subst.start()-20, 0):subst.end()+20] | |
663 if isglobal and (var in self.substvars): | |
664 val = self.substvars[var] | |
665 else: | |
666 val = raw_input('Substitution for %s (SET SCAN OFF to halt substitution): ' % fullexpr) | |
667 if val.lower().split() == ['set','scan','off']: | |
668 self.scan = False | |
669 return raw | |
670 if isglobal: | |
671 self.substvars[var] = val | |
672 raw = raw.replace(fullexpr, val) | |
673 print 'Substituted %s for %s' % (val, fullexpr) | |
674 subst = regexpr.search(raw) # do not FINDALL b/c we don't want to ask twice | |
675 return raw | |
676 | |
677 doubleampre = re.compile('(&&([a-zA-Z\d_$#]+))') | |
678 singleampre = re.compile( '(&([a-zA-Z\d_$#]+))') | |
679 def preparse(self, raw, **kwargs): | |
680 if self.scan: | |
681 raw = self.do_ampersand_substitution(raw, regexpr=self.doubleampre, isglobal=True) | |
682 if self.scan: | |
683 raw = self.do_ampersand_substitution(raw, regexpr=self.singleampre, isglobal=False) | |
684 return raw | |
685 | |
189 | 686 rowlimitPattern = pyparsing.Word(pyparsing.nums)('rowlimit') |
204 | 687 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
|
688 |
273
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
689 def do_bind(self, arg): |
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
690 self.pystate['r'][-1][-1].bind() |
271 | 691 |
192
6bb8a112af6b
accept special terminators on most anything
catherine@dellzilla
parents:
191
diff
changeset
|
692 def do_select(self, arg, bindVarsIn=None, terminator=None): |
189 | 693 """Fetch rows from a table. |
694 | |
695 Limit the number of rows retrieved by appending | |
696 an integer after the terminator | |
697 (example: SELECT * FROM mytable;10 ) | |
698 | |
699 Output may be formatted by choosing an alternative terminator | |
700 ("help terminators" for details) | |
701 """ | |
702 bindVarsIn = bindVarsIn or {} | |
196 | 703 try: |
704 rowlimit = int(arg.parsed.suffix or 0) | |
705 except ValueError: | |
706 rowlimit = 0 | |
206 | 707 print "Specify desired number of rows after terminator (not '%s')" % arg.parsed.suffix |
194 | 708 self.varsUsed = findBinds(arg, self.binds, bindVarsIn) |
233 | 709 if self.wildsql: |
234
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
710 selecttext = self.expandWildSql(arg) |
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
711 else: |
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
712 selecttext = arg |
a86efbca3da9
ha ha ha - wildcards in selects working now
catherine@dellzilla
parents:
233
diff
changeset
|
713 self.curs.execute('select ' + selecttext, self.varsUsed) |
194 | 714 self.rows = self.curs.fetchmany(min(self.maxfetch, (rowlimit or self.maxfetch))) |
189 | 715 self.rc = self.curs.rowcount |
716 if self.rc > 0: | |
271 | 717 resultset = ResultSet() |
718 resultset.colnames = [d[0].lower() for d in self.curs.description] | |
719 resultset.statement = 'select ' + selecttext | |
273
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
720 resultset.varsUsed = self.varsUsed |
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
721 resultset.pystate = self.pystate |
271 | 722 resultset.extend([Result(r) for r in self.rows]) |
723 for row in resultset: | |
724 row.resultset = resultset | |
194 | 725 self.stdout.write('\n%s\n' % (self.output(arg.parsed.terminator, rowlimit))) |
271 | 726 self.pystate['r'].append(resultset) |
189 | 727 if self.rc == 0: |
728 print '\nNo rows Selected.\n' | |
729 elif self.rc == 1: | |
730 print '\n1 row selected.\n' | |
731 if self.autobind: | |
273
9d67065ea030
data into/out of py via binds
catherine@Elli.myhome.westell.com
parents:
272
diff
changeset
|
732 self.do_bind(None) |
189 | 733 elif self.rc < self.maxfetch: |
734 print '\n%d rows selected.\n' % self.rc | |
735 else: | |
736 print '\nSelected Max Num rows (%d)' % self.rc | |
193 | 737 |
738 def do_cat(self, arg): | |
226 | 739 '''Shortcut for SELECT * FROM''' |
200 | 740 return self.do_select(self.parsed('SELECT * FROM %s;' % arg, |
741 terminator = arg.parsed.terminator or ';', | |
742 suffix = arg.parsed.suffix)) | |
220 | 743 |
744 def _pull(self, arg, opts, vc=None): | |
745 """Displays source code.""" | |
746 if opts.dump: | |
747 statekeeper = Statekeeper(self, ('stdout',)) | |
748 try: | |
749 for (owner, object_type, object_name) in self.resolve_many(arg, opts): | |
750 if object_type in self.supported_ddl_types: | |
751 object_type = {'DATABASE LINK': 'DB_LINK', 'JAVA CLASS': 'JAVA_SOURCE' | |
752 }.get(object_type) or object_type | |
753 object_type = object_type.replace(' ', '_') | |
754 if opts.dump: | |
755 try: | |
756 os.makedirs(os.path.join(owner.lower(), object_type.lower())) | |
757 except OSError: | |
758 pass | |
759 filename = os.path.join(owner.lower(), object_type.lower(), '%s.sql' % object_name.lower()) | |
760 self.stdout = open(filename, 'w') | |
761 if vc: | |
762 subprocess.call(vc + [filename]) | |
248 | 763 if object_type == 'PACKAGE': |
764 ddl = [['PACKAGE_SPEC', object_name, owner],['PACKAGE_BODY', object_name, owner]] | |
765 elif object_type in ['CONTEXT', 'DIRECTORY', 'JOB']: | |
766 ddl = [[object_type, object_name]] | |
767 else: | |
768 ddl = [[object_type, object_name, owner]] | |
769 for ddlargs in ddl: | |
770 try: | |
246 | 771 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 | 772 except cx_Oracle.DatabaseError, errmsg: |
773 if object_type == 'JOB': | |
774 print '%s: DBMS_METADATA.GET_DDL does not support JOBs (MetaLink DocID 567504.1)' % object_name | |
775 elif 'ORA-31603' in str(errmsg): # not found, as in package w/o package body | |
776 pass | |
777 else: | |
778 raise | |
220 | 779 if opts.full: |
780 for dependent_type in ('OBJECT_GRANT', 'CONSTRAINT', 'TRIGGER'): | |
781 try: | |
245
05c90f80815c
trying REMARK BEGIN / REMARK END
catherine@Elli.myhome.westell.com
parents:
244
diff
changeset
|
782 self.stdout.write('REMARK BEGIN\n%s\nREMARK END\n\n' % str(self.curs.callfunc('DBMS_METADATA.GET_DEPENDENT_DDL', cx_Oracle.CLOB, |
220 | 783 [dependent_type, object_name, owner]))) |
784 except cx_Oracle.DatabaseError: | |
785 pass | |
786 if opts.dump: | |
787 self.stdout.close() | |
788 except: | |
789 if opts.dump: | |
790 statekeeper.restore() | |
791 raise | |
792 if opts.dump: | |
793 statekeeper.restore() | |
794 | |
221 | 795 def do_show(self, arg): |
796 ''' | |
797 show - display value of all sqlpython parameters | |
798 show (parameter name) - display value of a sqlpython parameter | |
799 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
|
800 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
|
801 show all err (type/name) - all compilation errors from the user's PL/SQL objects. |
221 | 802 ''' |
803 if arg.startswith('param'): | |
804 try: | |
805 paramname = arg.split()[1].lower() | |
806 except IndexError: | |
807 paramname = '' | |
808 self.onecmd("""SELECT name, | |
809 CASE type WHEN 1 THEN 'BOOLEAN' | |
810 WHEN 2 THEN 'STRING' | |
811 WHEN 3 THEN 'INTEGER' | |
812 WHEN 4 THEN 'PARAMETER FILE' | |
813 WHEN 5 THEN 'RESERVED' | |
814 WHEN 6 THEN 'BIG INTEGER' END type, | |
815 value FROM v$parameter WHERE name LIKE '%%%s%%';""" % paramname) | |
816 else: | |
264
a8deaa38f11e
show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents:
261
diff
changeset
|
817 argpieces = arg.lower().split() |
a8deaa38f11e
show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents:
261
diff
changeset
|
818 try: |
a8deaa38f11e
show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents:
261
diff
changeset
|
819 if argpieces[0][:3] == 'err': |
265
041c656dc8e5
show err working nicely now
catherine@Elli.myhome.westell.com
parents:
264
diff
changeset
|
820 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
|
821 elif (argpieces[0], argpieces[1][:3]) == ('all','err'): |
265
041c656dc8e5
show err working nicely now
catherine@Elli.myhome.westell.com
parents:
264
diff
changeset
|
822 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
|
823 except IndexError: |
a8deaa38f11e
show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents:
261
diff
changeset
|
824 pass |
221 | 825 return Cmd.do_show(self, arg) |
264
a8deaa38f11e
show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents:
261
diff
changeset
|
826 do_sho = do_show |
221 | 827 |
218
397979c7f6d6
dumping working but not for wildcards
catherine@Elli.myhome.westell.com
parents:
217
diff
changeset
|
828 @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
|
829 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
|
830 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
|
831 make_option('-x', '--exact', action='store_true', help="match object name exactly")]) |
189 | 832 def do_pull(self, arg, opts): |
833 """Displays source code.""" | |
220 | 834 self._pull(arg, opts) |
835 | |
247 | 836 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 | 837 do_pull.__doc__ += '\n\nSupported DDL types: ' + supported_ddl_types |
838 supported_ddl_types = supported_ddl_types.split(', ') | |
189 | 839 |
224 | 840 def _vc(self, arg, opts, program): |
248 | 841 if not os.path.exists('.%s' % program): |
842 create = raw_input('%s repository not yet in current directory (%s). Create (y/N)? ' % | |
843 (program, os.getcwd())) | |
844 if not create.strip().lower().startswith('y'): | |
845 return | |
224 | 846 subprocess.call([program, 'init']) |
220 | 847 opts.dump = True |
848 self._pull(arg, opts, vc=[program, 'add']) | |
849 subprocess.call([program, 'commit', '-m', '"%s"' % opts.message or 'committed from sqlpython']) | |
850 | |
851 @options([ | |
852 make_option('-f', '--full', action='store_true', help='get dependent objects as well'), | |
853 make_option('-a', '--all', action='store_true', help="all schemas' objects"), | |
854 make_option('-x', '--exact', action='store_true', help="match object name exactly"), | |
855 make_option('-m', '--message', action='store', type='string', dest='message', help="message to save to hg log during commit")]) | |
856 def do_hg(self, arg, opts): | |
857 '''hg (opts) (objects): | |
226 | 858 Stores DDL on disk and puts files under Mercurial version control. |
859 Args specify which objects to store, same format as `ls`.''' | |
224 | 860 self._vc(arg, opts, 'hg') |
189 | 861 |
220 | 862 @options([ |
863 make_option('-f', '--full', action='store_true', help='get dependent objects as well'), | |
864 make_option('-a', '--all', action='store_true', help="all schemas' objects"), | |
865 make_option('-x', '--exact', action='store_true', help="match object name exactly"), | |
866 make_option('-m', '--message', action='store', type='string', dest='message', help="message to save to hg log during commit")]) | |
867 def do_bzr(self, arg, opts): | |
868 '''bzr (opts) (objects): | |
226 | 869 Stores DDL on disk and puts files under Bazaar version control. |
870 Args specify which objects to store, same format as `ls`.''' | |
224 | 871 self._vc(arg, opts, 'bzr') |
220 | 872 |
873 @options([ | |
874 make_option('-f', '--full', action='store_true', help='get dependent objects as well'), | |
875 make_option('-a', '--all', action='store_true', help="all schemas' objects"), | |
876 make_option('-x', '--exact', action='store_true', help="match object name exactly"), | |
877 make_option('-m', '--message', action='store', type='string', dest='message', help="message to save to hg log during commit")]) | |
224 | 878 def do_git(self, arg, opts): |
879 '''git (opts) (objects): | |
226 | 880 Stores DDL on disk and puts files under git version control. |
881 Args specify which objects to store, same format as `ls`.''' | |
224 | 882 self._vc(arg, opts, 'git') |
220 | 883 |
196 | 884 all_users_option = make_option('-a', action='store_const', dest="scope", |
222 | 885 default={'col':'', 'view':'user', 'schemas':'user', 'firstcol': ''}, |
886 const={'col':', owner', 'view':'all', 'schemas':'all', 'firstcol': 'owner, '}, | |
194 | 887 help='Describe all objects (not just my own)') |
888 @options([all_users_option, | |
189 | 889 make_option('-c', '--col', action='store_true', help='find column'), |
890 make_option('-t', '--table', action='store_true', help='find table')]) | |
891 def do_find(self, arg, opts): | |
892 """Finds argument in source code or (with -c) in column definitions.""" | |
193 | 893 |
894 capArg = arg.upper() | |
189 | 895 |
896 if opts.col: | |
222 | 897 sql = "SELECT table_name, column_name %s FROM %s_tab_columns where column_name like '%%%s%%' ORDER BY %s table_name, column_name;" \ |
898 % (opts.scope['col'], opts.scope['view'], capArg, opts.scope['firstcol']) | |
189 | 899 elif opts.table: |
222 | 900 sql = "SELECT table_name %s from %s_tables where table_name like '%%%s%%' ORDER BY %s table_name;" \ |
901 % (opts.scope['col'], opts.scope['view'], capArg, opts.scope['firstcol']) | |
189 | 902 else: |
196 | 903 sql = "SELECT * from %s_source where UPPER(text) like '%%%s%%';" % (opts.scope['view'], capArg) |
200 | 904 self.do_select(self.parsed(sql, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
193 | 905 |
194 | 906 @options([all_users_option]) |
189 | 907 def do_describe(self, arg, opts): |
908 "emulates SQL*Plus's DESCRIBE" | |
193 | 909 target = arg.upper() |
910 if not target: | |
194 | 911 return self.do_select(self.parsed("""SELECT object_name, object_type%s |
912 FROM %s_objects | |
913 WHERE object_type IN ('TABLE','VIEW','INDEX') | |
196 | 914 ORDER BY object_name;""" % (opts.scope['col'], opts.scope['view']), |
200 | 915 terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
193 | 916 object_type, owner, object_name = self.resolve(target) |
189 | 917 if not object_type: |
194 | 918 return self.do_select(self.parsed("""SELECT object_name, object_type%s FROM %s_objects |
919 WHERE object_type IN ('TABLE','VIEW','INDEX') | |
920 AND object_name LIKE '%%%s%%' | |
921 ORDER BY object_name;""" % | |
200 | 922 (opts.scope['col'], opts.scope['view'], target), |
923 terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) | |
189 | 924 self.stdout.write("%s %s.%s\n" % (object_type, owner, object_name)) |
925 descQ = descQueries.get(object_type) | |
926 if descQ: | |
927 for q in descQ: | |
204 | 928 self.do_select(self.parsed(q, terminator=arg.parsed.terminator or ';' , suffix=arg.parsed.suffix), |
200 | 929 bindVarsIn={'object_name':object_name, 'owner':owner}) |
189 | 930 elif object_type == 'PACKAGE': |
931 packageContents = self.select_scalar_list(descQueries['PackageObjects'][0], {'package_name':object_name, 'owner':owner}) | |
932 for packageObj_name in packageContents: | |
933 self.stdout.write('Arguments to %s\n' % (packageObj_name)) | |
200 | 934 sql = self.parsed(descQueries['PackageObjArgs'][0], terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix) |
194 | 935 self.do_select(sql, bindVarsIn={'package_name':object_name, 'owner':owner, 'object_name':packageObj_name}) |
189 | 936 do_desc = do_describe |
937 | |
938 def do_deps(self, arg): | |
193 | 939 target = arg.upper() |
940 object_type, owner, object_name = self.resolve(target) | |
189 | 941 if object_type == 'PACKAGE BODY': |
942 q = "and (type != 'PACKAGE BODY' or name != :object_name)'" | |
943 object_type = 'PACKAGE' | |
944 else: | |
945 q = "" | |
193 | 946 q = """SELECT name, |
189 | 947 type |
948 from user_dependencies | |
949 where | |
950 referenced_name like :object_name | |
951 and referenced_type like :object_type | |
952 and referenced_owner like :owner | |
193 | 953 %s;""" % (q) |
200 | 954 self.do_select(self.parsed(q, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix), |
955 bindVarsIn={'object_name':object_name, 'object_type':object_type, 'owner':owner}) | |
189 | 956 |
957 def do_comments(self, arg): | |
958 'Prints comments on a table and its columns.' | |
193 | 959 target = arg.upper() |
960 object_type, owner, object_name, colName = self.resolve_with_column(target) | |
189 | 961 if object_type: |
249 | 962 self._execute(queries['tabComments'], {'table_name':object_name, 'owner':owner}) |
189 | 963 self.stdout.write("%s %s.%s: %s\n" % (object_type, owner, object_name, self.curs.fetchone()[0])) |
964 if colName: | |
194 | 965 sql = queries['oneColComments'] |
966 bindVarsIn={'owner':owner, 'object_name': object_name, 'column_name': colName} | |
189 | 967 else: |
194 | 968 sql = queries['colComments'] |
969 bindVarsIn={'owner':owner, 'object_name': object_name} | |
200 | 970 self.do_select(self.parsed(sql, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix), |
971 bindVarsIn=bindVarsIn) | |
189 | 972 |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
973 def _resolve(self, identifier): |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
974 parts = identifier.split('.') |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
975 if len(parts) == 2: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
976 owner, object_name = parts |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
977 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
|
978 {'owner': owner, 'object_name': object_name.upper()} |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
979 )[0] |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
980 elif len(parts) == 1: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
981 object_name = parts[0] |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
982 self._execute(queries['resolve'], {'objName':object_name.upper()}) |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
983 object_type, object_name, owner = self.curs.fetchone() |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
984 return object_type, owner, object_name |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
985 |
189 | 986 def resolve(self, identifier): |
987 """Checks (my objects).name, (my synonyms).name, (public synonyms).name | |
988 to resolve a database object's name. """ | |
989 try: | |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
990 return self._resolve(identifier) |
189 | 991 except (TypeError, IndexError): |
992 print 'Could not resolve object %s.' % identifier | |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
993 return '', '', '' |
189 | 994 |
995 def resolve_with_column(self, identifier): | |
996 colName = None | |
997 object_type, owner, object_name = self.resolve(identifier) | |
998 if not object_type: | |
999 parts = identifier.split('.') | |
1000 if len(parts) > 1: | |
1001 colName = parts[-1] | |
1002 identifier = '.'.join(parts[:-1]) | |
1003 object_type, owner, object_name = self.resolve(identifier) | |
1004 return object_type, owner, object_name, colName | |
1005 | |
1006 def do_resolve(self, arg): | |
195 | 1007 target = arg.upper() |
1008 self.stdout.write(','.join(self.resolve(target))+'\n') | |
189 | 1009 |
1010 def spoolstop(self): | |
1011 if self.spoolFile: | |
1012 self.stdout = self.stdoutBeforeSpool | |
1013 print 'Finished spooling to ', self.spoolFile.name | |
1014 self.spoolFile.close() | |
1015 self.spoolFile = None | |
1016 | |
1017 def do_spool(self, arg): | |
1018 """spool [filename] - begins redirecting output to FILENAME.""" | |
1019 self.spoolstop() | |
1020 arg = arg.strip() | |
1021 if not arg: | |
1022 arg = 'output.lst' | |
1023 if arg.lower() != 'off': | |
1024 if '.' not in arg: | |
1025 arg = '%s.lst' % arg | |
1026 print 'Sending output to %s (until SPOOL OFF received)' % (arg) | |
1027 self.spoolFile = open(arg, 'w') | |
1028 self.stdout = self.spoolFile | |
1029 | |
1030 def do_write(self, args): | |
1031 print 'Use (query) > outfilename instead.' | |
1032 return | |
1033 | |
1034 def do_compare(self, args): | |
1035 """COMPARE query1 TO query2 - uses external tool to display differences. | |
1036 | |
1037 Sorting is recommended to avoid false hits. | |
1038 Will attempt to use a graphical diff/merge tool like kdiff3, meld, or Araxis Merge, | |
1039 if they are installed.""" | |
193 | 1040 #TODO: Update this to use pyparsing |
189 | 1041 fnames = [] |
1042 args2 = args.split(' to ') | |
1043 if len(args2) < 2: | |
1044 print self.do_compare.__doc__ | |
1045 return | |
1046 for n in range(len(args2)): | |
1047 query = args2[n] | |
1048 fnames.append('compare%s.txt' % n) | |
1049 #TODO: update this terminator-stripping | |
1050 if query.rstrip()[-1] != self.terminator: | |
1051 query = '%s%s' % (query, self.terminator) | |
1052 self.onecmd_plus_hooks('%s > %s' % (query, fnames[n])) | |
1053 diffMergeSearcher.invoke(fnames[0], fnames[1]) | |
1054 | |
1055 bufferPosPattern = re.compile('\d+') | |
1056 rangeIndicators = ('-',':') | |
1057 | |
1058 def do_psql(self, arg): | |
1059 '''Shortcut commands emulating psql's backslash commands. | |
1060 | |
1061 \c connect | |
1062 \d desc | |
1063 \e edit | |
1064 \g run | |
1065 \h help | |
1066 \i load | |
1067 \o spool | |
1068 \p list | |
1069 \q quit | |
1070 \w save | |
1071 \db _dir_tablespaces | |
1072 \dd comments | |
1073 \dn _dir_schemas | |
1074 \dt _dir_tables | |
1075 \dv _dir_views | |
1076 \di _dir_indexes | |
1077 \? help psql''' | |
1078 commands = {} | |
1079 for c in self.do_psql.__doc__.splitlines()[2:]: | |
1080 (abbrev, command) = c.split(None, 1) | |
1081 commands[abbrev[1:]] = command | |
1082 words = arg.split(None,1) | |
1083 try: | |
1084 abbrev = words[0] | |
1085 except IndexError: | |
1086 return | |
1087 try: | |
1088 args = words[1] | |
1089 except IndexError: | |
1090 args = '' | |
1091 try: | |
200 | 1092 return self.onecmd('%s %s%s%s' % (commands[abbrev], args, arg.parsed.terminator, arg.parsed.suffix)) |
189 | 1093 except KeyError: |
1094 print 'psql command \%s not yet supported.' % abbrev | |
1095 | |
194 | 1096 @options([all_users_option]) |
189 | 1097 def do__dir_tables(self, arg, opts): |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1098 ''' |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1099 Lists all tables whose names match argument. |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1100 ''' |
194 | 1101 sql = """SELECT table_name, 'TABLE' as type%s FROM %s_tables WHERE table_name LIKE '%%%s%%';""" % \ |
196 | 1102 (opts.scope['col'], opts.scope['view'], arg.upper()) |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1103 if self.sql_echo: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1104 print sql |
200 | 1105 self.do_select(self.parsed(sql, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
194 | 1106 |
1107 @options([all_users_option]) | |
189 | 1108 def do__dir_views(self, arg, opts): |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1109 ''' |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1110 Lists all views whose names match argument. |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1111 ''' |
194 | 1112 sql = """SELECT view_name, 'VIEW' as type%s FROM %s_views WHERE view_name LIKE '%%%s%%';""" % \ |
196 | 1113 (opts.scope['col'], opts.scope['view'], arg.upper()) |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1114 if self.sql_echo: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1115 print sql |
200 | 1116 self.do_select(self.parsed(sql, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
194 | 1117 |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1118 def do__dir_indexes(self, arg): |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1119 ''' |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1120 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
|
1121 Otherwise, acts as shortcut for `ls index/*(arg)*` |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1122 ''' |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1123 try: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1124 table_type, table_owner, table_name = self._resolve(arg) |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1125 except TypeError, IndexError: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1126 return self.onecmd('ls Index/*%s*' % arg) |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1127 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
|
1128 WHERE table_owner = :table_owner |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1129 AND table_name = :table_name; |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1130 ORDER BY owner, index_name""" |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1131 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
|
1132 bindVarsIn = {'table_owner': table_owner, 'table_name': table_name}) |
189 | 1133 |
1134 def do__dir_tablespaces(self, arg): | |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1135 ''' |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1136 Lists all tablespaces. |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1137 ''' |
194 | 1138 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
|
1139 if self.sql_echo: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1140 print sql |
200 | 1141 self.do_select(self.parsed(sql, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
189 | 1142 |
1143 def do__dir_schemas(self, arg): | |
251
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1144 ''' |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1145 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
|
1146 ''' |
194 | 1147 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
|
1148 if self.sql_echo: |
aa33f495a289
reworked \di - not truly better?
catherine@Elli.myhome.westell.com
parents:
250
diff
changeset
|
1149 print sql |
200 | 1150 self.do_select(self.parsed(sql, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
189 | 1151 |
1152 def do_head(self, arg): | |
226 | 1153 '''Shortcut for SELECT * FROM <arg>;10 |
1154 The terminator (\\t, \\g, \\x, etc.) and number of rows can | |
1155 be changed as for any other SELECT statement.''' | |
200 | 1156 sql = self.parsed('SELECT * FROM %s;' % arg, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix) |
195 | 1157 sql.parsed['suffix'] = sql.parsed.suffix or '10' |
196 | 1158 self.do_select(self.parsed(sql)) |
189 | 1159 |
1160 def do_print(self, arg): | |
1161 'print VARNAME: Show current value of bind variable VARNAME.' | |
1162 if arg: | |
1163 if arg[0] == ':': | |
1164 arg = arg[1:] | |
1165 try: | |
1166 self.stdout.write(str(self.binds[arg])+'\n') | |
1167 except KeyError: | |
1168 self.stdout.write('No bind variable %s\n' % arg) | |
1169 else: | |
250 | 1170 for (var, val) in sorted(self.binds.items()): |
189 | 1171 print ':%s = %s' % (var, val) |
1172 | |
1173 assignmentScanner = Parser(pyparsing.Literal(':=') ^ '=') | |
1174 def do_setbind(self, arg): | |
1175 if not arg: | |
1176 return self.do_print(arg) | |
1177 try: | |
1178 assigner, startat, endat = self.assignmentScanner.scanner.scanString(arg).next() | |
1179 except StopIteration: | |
1180 self.do_print(arg) | |
1181 return | |
1182 var, val = arg[:startat].strip(), arg[endat:].strip() | |
1183 if val[0] == val[-1] == "'" and len(val) > 1: | |
1184 self.binds[var] = val[1:-1] | |
1185 return | |
1186 try: | |
1187 self.binds[var] = int(val) | |
1188 return | |
1189 except ValueError: | |
1190 try: | |
1191 self.binds[var] = float(val) | |
1192 return | |
1193 except ValueError: | |
1194 statekeeper = Statekeeper(self, ('autobind',)) | |
1195 self.autobind = True | |
193 | 1196 self.onecmd('SELECT %s AS %s FROM dual;' % (val, var)) |
189 | 1197 statekeeper.restore() |
1198 | |
1199 def do_exec(self, arg): | |
213 | 1200 if arg.startswith(':'): |
189 | 1201 self.do_setbind(arg[1:]) |
1202 else: | |
1203 varsUsed = findBinds(arg, self.binds, {}) | |
1204 try: | |
1205 self.curs.execute('begin\n%s;end;' % arg, varsUsed) | |
1206 except Exception, e: | |
1207 print e | |
1208 | |
1209 ''' | |
1210 Fails: | |
1211 select n into :n from test;''' | |
1212 | |
1213 def anon_plsql(self, line1): | |
1214 lines = [line1] | |
1215 while True: | |
247 | 1216 line = self.pseudo_raw_input(self.continuation_prompt) |
241 | 1217 if line == 'EOF': |
1218 return | |
189 | 1219 if line.strip() == '/': |
1220 try: | |
1221 self.curs.execute('\n'.join(lines)) | |
1222 except Exception, e: | |
1223 print e | |
1224 return | |
1225 lines.append(line) | |
1226 | |
1227 def do_begin(self, arg): | |
1228 self.anon_plsql('begin ' + arg) | |
1229 | |
1230 def do_declare(self, arg): | |
1231 self.anon_plsql('declare ' + arg) | |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1232 |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1233 def _ls_statement(self, arg, opts): |
189 | 1234 if arg: |
217
a65b98938596
multi-pull working pretty well
catherine@Elli.myhome.westell.com
parents:
216
diff
changeset
|
1235 target = arg.upper() |
232
52adb09094b3
fixed bugs in VC introduced by sort-order options
catherine@dellzilla
parents:
230
diff
changeset
|
1236 if hasattr(opts, 'exact') and opts.exact: |
217
a65b98938596
multi-pull working pretty well
catherine@Elli.myhome.westell.com
parents:
216
diff
changeset
|
1237 where = """\nWHERE object_name = '%s' |
a65b98938596
multi-pull working pretty well
catherine@Elli.myhome.westell.com
parents:
216
diff
changeset
|
1238 OR object_type || '/' || object_name = '%s'""" % \ |
a65b98938596
multi-pull working pretty well
catherine@Elli.myhome.westell.com
parents:
216
diff
changeset
|
1239 (target, target) |
a65b98938596
multi-pull working pretty well
catherine@Elli.myhome.westell.com
parents:
216
diff
changeset
|
1240 else: |
264
a8deaa38f11e
show errors works. limiting ls
catherine@Elli.myhome.westell.com
parents:
261
diff
changeset
|
1241 where = "\nWHERE object_type || '/' || object_name LIKE '%s'" % (arg.upper().replace('*','%')) |
189 | 1242 else: |
1243 where = '' | |
1244 if opts.all: | |
1245 whose = 'all' | |
1246 objname = "owner || '.' || object_name" | |
1247 else: | |
1248 whose = 'user' | |
1249 objname = 'object_name' | |
232
52adb09094b3
fixed bugs in VC introduced by sort-order options
catherine@dellzilla
parents:
230
diff
changeset
|
1250 if hasattr(opts, 'long') and opts.long: |
228 | 1251 moreColumns = ', status, last_ddl_time' |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1252 else: |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1253 moreColumns = '' |
227 | 1254 |
1255 # 'Normal' sort order is DATE DESC (maybe), object type ASC, object name ASC | |
244 | 1256 sortdirection = (hasattr(opts, 'reverse') and opts.reverse and 'DESC') or 'ASC' |
227 | 1257 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
|
1258 if hasattr(opts, 'timesort') and opts.timesort: |
52adb09094b3
fixed bugs in VC introduced by sort-order options
catherine@dellzilla
parents:
230
diff
changeset
|
1259 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
|
1260 return {'objname': objname, 'moreColumns': moreColumns, |
227 | 1261 'whose': whose, 'where': where, 'orderby': orderby} |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1262 |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1263 def resolve_many(self, arg, opts): |
217
a65b98938596
multi-pull working pretty well
catherine@Elli.myhome.westell.com
parents:
216
diff
changeset
|
1264 opts.long = False |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1265 clauses = self._ls_statement(arg, opts) |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1266 if opts.all: |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1267 clauses['owner'] = 'owner' |
189 | 1268 else: |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1269 clauses['owner'] = 'user' |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1270 statement = '''SELECT %(owner)s, object_type, object_name |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1271 FROM %(whose)s_objects %(where)s |
217
a65b98938596
multi-pull working pretty well
catherine@Elli.myhome.westell.com
parents:
216
diff
changeset
|
1272 ORDER BY object_type, object_name''' % clauses |
249 | 1273 self._execute(statement) |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1274 return self.curs.fetchall() |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1275 |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1276 @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
|
1277 make_option('-a', '--all', action='store_true', help="all schemas' objects"), |
227 | 1278 make_option('-t', '--timesort', action='store_true', help="Sort by last_ddl_time"), |
1279 make_option('-r', '--reverse', action='store_true', help="Reverse order while sorting"), | |
217
a65b98938596
multi-pull working pretty well
catherine@Elli.myhome.westell.com
parents:
216
diff
changeset
|
1280 make_option('-x', '--exact', action='store_true', default=False, help="match name exactly")]) |
216
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1281 def do_ls(self, arg, opts): |
c5a49947eedc
going to try multiple pull
catherine@Elli.myhome.westell.com
parents:
213
diff
changeset
|
1282 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
|
1283 FROM %(whose)s_objects %(where)s |
227 | 1284 ORDER BY %(orderby)s;''' % self._ls_statement(arg, opts) |
200 | 1285 self.do_select(self.parsed(statement, terminator=arg.parsed.terminator or ';', suffix=arg.parsed.suffix)) |
189 | 1286 |
1287 @options([make_option('-i', '--ignore-case', dest='ignorecase', action='store_true', help='Case-insensitive search')]) | |
1288 def do_grep(self, arg, opts): | |
1289 """grep PATTERN TABLE - search for term in any of TABLE's fields""" | |
1290 | |
195 | 1291 targetnames = arg.split() |
189 | 1292 pattern = targetnames.pop(0) |
1293 targets = [] | |
1294 for target in targetnames: | |
1295 if '*' in target: | |
249 | 1296 self._execute("SELECT owner, table_name FROM all_tables WHERE table_name LIKE '%s'%s" % |
189 | 1297 (target.upper().replace('*','%')), arg.terminator) |
1298 for row in self.curs: | |
1299 targets.append('%s.%s' % row) | |
1300 else: | |
1301 targets.append(target) | |
1302 for target in targets: | |
1303 print target | |
1304 target = target.rstrip(';') | |
1305 try: | |
249 | 1306 self._execute('select * from %s where 1=0' % target) # just to fill description |
189 | 1307 if opts.ignorecase: |
1308 sql = ' or '.join("LOWER(%s) LIKE '%%%s%%'" % (d[0], pattern.lower()) for d in self.curs.description) | |
1309 else: | |
1310 sql = ' or '.join("%s LIKE '%%%s%%'" % (d[0], pattern) for d in self.curs.description) | |
200 | 1311 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
|
1312 self.do_select(sql) |
189 | 1313 except Exception, e: |
1314 print e | |
1315 import traceback | |
1316 traceback.print_exc(file=sys.stdout) | |
1317 | |
249 | 1318 def _execute(self, sql, bindvars={}): |
1319 if self.sql_echo: | |
1320 print sql | |
1321 self.curs.execute(sql, bindvars) | |
1322 | |
189 | 1323 def do_refs(self, arg): |
211 | 1324 '''Lists referential integrity (foreign key constraints) on an object.''' |
1325 | |
1326 if not arg.strip(): | |
1327 print 'Usage: refs (table name)' | |
190 | 1328 result = [] |
205 | 1329 (type, owner, table_name) = self.resolve(arg.upper()) |
249 | 1330 sql = """SELECT constraint_name, r_owner, r_constraint_name |
191 | 1331 FROM all_constraints |
1332 WHERE constraint_type = 'R' | |
1333 AND owner = :owner | |
249 | 1334 AND table_name = :table_name""" |
1335 self._execute(sql, {"owner": owner, "table_name": table_name}) | |
191 | 1336 for (constraint_name, remote_owner, remote_constraint_name) in self.curs.fetchall(): |
1337 result.append('%s on %s.%s:' % (constraint_name, owner, table_name)) | |
249 | 1338 |
1339 self._execute("SELECT column_name FROM all_cons_columns WHERE owner = :owner AND constraint_name = :constraint_name ORDER BY position", | |
1340 {'constraint_name': constraint_name, 'owner': owner}) | |
191 | 1341 result.append(" (%s)" % (",".join(col[0] for col in self.curs.fetchall()))) |
249 | 1342 self._execute("SELECT table_name FROM all_constraints WHERE owner = :remote_owner AND constraint_name = :remote_constraint_name", |
1343 {'remote_owner': remote_owner, 'remote_constraint_name': remote_constraint_name}) | |
191 | 1344 remote_table_name = self.curs.fetchone()[0] |
1345 result.append("must be in %s:" % (remote_table_name)) | |
249 | 1346 self._execute("SELECT column_name FROM all_cons_columns WHERE owner = :remote_owner AND constraint_name = :remote_constraint_name ORDER BY position", |
191 | 1347 {'remote_constraint_name': remote_constraint_name, 'remote_owner': remote_owner}) |
1348 result.append(' (%s)\n' % (",".join(col[0] for col in self.curs.fetchall()))) | |
1349 remote_table_name = table_name | |
1350 remote_owner = owner | |
249 | 1351 self._execute("""SELECT owner, constraint_name, table_name, r_constraint_name |
191 | 1352 FROM all_constraints |
1353 WHERE (r_owner, r_constraint_name) IN | |
1354 ( SELECT owner, constraint_name | |
1355 FROM all_constraints | |
192
6bb8a112af6b
accept special terminators on most anything
catherine@dellzilla
parents:
191
diff
changeset
|
1356 WHERE table_name = :remote_table_name |
191 | 1357 AND owner = :remote_owner )""", |
1358 {'remote_table_name': remote_table_name, 'remote_owner': remote_owner}) | |
1359 for (owner, constraint_name, table_name, remote_constraint_name) in self.curs.fetchall(): | |
1360 result.append('%s on %s.%s:' % (constraint_name, owner, table_name)) | |
249 | 1361 self._execute("SELECT column_name FROM all_cons_columns WHERE owner = :owner AND constraint_name = :constraint_name ORDER BY position", |
191 | 1362 {'constraint_name': constraint_name, 'owner': owner}) |
1363 result.append(" (%s)" % (",".join(col[0] for col in self.curs.fetchall()))) | |
249 | 1364 self._execute("SELECT table_name FROM all_constraints WHERE owner = :remote_owner AND constraint_name = :remote_constraint_name", |
191 | 1365 {'remote_owner': remote_owner, 'remote_constraint_name': remote_constraint_name}) |
1366 remote_table_name = self.curs.fetchone()[0] | |
1367 result.append("must be in %s:" % (remote_table_name)) | |
249 | 1368 self._execute("SELECT column_name FROM all_cons_columns WHERE owner = :remote_owner AND constraint_name = :remote_constraint_name ORDER BY position", |
191 | 1369 {'remote_constraint_name': remote_constraint_name, 'remote_owner': remote_owner}) |
1370 result.append(' (%s)\n' % (",".join(col[0] for col in self.curs.fetchall()))) | |
1371 self.stdout.write('\n'.join(result) + "\n") | |
190 | 1372 |
189 | 1373 def _test(): |
1374 import doctest | |
1375 doctest.testmod() | |
1376 | |
1377 if __name__ == "__main__": | |
1378 "Silent return implies that all unit tests succeeded. Use -v to see details." | |
1379 _test() | |
198
b2d8bf5f89db
merged with changes from work
catherine@Elli.myhome.westell.com
parents:
196
diff
changeset
|
1380 if __name__ == "__main__": |
b2d8bf5f89db
merged with changes from work
catherine@Elli.myhome.westell.com
parents:
196
diff
changeset
|
1381 "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
|
1382 _test() |