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