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