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