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