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