Mercurial > sqlpython
comparison sqlpyPlus.py @ 16:2776755a3a7e
beginning separation of cmd2
author | devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil |
---|---|
date | Wed, 19 Dec 2007 10:28:01 -0500 |
parents | 9c7df9f825a1 |
children | d6d64c2e3b98 |
comparison
equal
deleted
inserted
replaced
15:9c7df9f825a1 | 16:2776755a3a7e |
---|---|
338 result[varname] = existingBinds[varname] | 338 result[varname] = existingBinds[varname] |
339 except KeyError: | 339 except KeyError: |
340 if not givenBindVars.has_key(varname): | 340 if not givenBindVars.has_key(varname): |
341 print 'Bind variable %s not defined.' % (varname) | 341 print 'Bind variable %s not defined.' % (varname) |
342 return result | 342 return result |
343 | |
344 class HistoryItem(str): | |
345 def __init__(self, instr): | |
346 str.__init__(self, instr) | |
347 self.lowercase = self.lower() | |
348 self.idx = None | |
349 def pr(self): | |
350 print '-------------------------[%d]' % (self.idx) | |
351 print self | |
352 | |
353 class History(list): | |
354 rangeFrom = re.compile(r'^([\d])+\s*\-$') | |
355 def append(self, new): | |
356 new = HistoryItem(new) | |
357 list.append(self, new) | |
358 new.idx = len(self) | |
359 def extend(self, new): | |
360 for n in new: | |
361 self.append(n) | |
362 def get(self, getme): | |
363 try: | |
364 getme = int(getme) | |
365 if getme < 0: | |
366 return self[:(-1 * getme)] | |
367 else: | |
368 return [self[getme-1]] | |
369 except IndexError: | |
370 return [] | |
371 except (ValueError, TypeError): | |
372 getme = getme.strip() | |
373 mtch = self.rangeFrom.search(getme) | |
374 if mtch: | |
375 return self[(int(mtch.group(1))-1):] | |
376 if getme.startswith(r'/') and getme.endswith(r'/'): | |
377 finder = re.compile(getme[1:-1], re.DOTALL | re.MULTILINE | re.IGNORECASE) | |
378 def isin(hi): | |
379 return finder.search(hi) | |
380 else: | |
381 def isin(hi): | |
382 return (getme.lower() in hi.lowercase) | |
383 return [itm for itm in self if isin(itm)] | |
384 | 343 |
385 class sqlpyPlus(sqlpython.sqlpython): | 344 class sqlpyPlus(sqlpython.sqlpython): |
386 def __init__(self): | 345 def __init__(self): |
387 sqlpython.sqlpython.__init__(self) | 346 sqlpython.sqlpython.__init__(self) |
388 self.binds = CaselessDict() | 347 self.binds = CaselessDict() |
389 self.sqlBuffer = [] | 348 self.sqlBuffer = [] |
390 self.history = History() | |
391 self.settable = ['maxtselctrows', 'maxfetch', 'autobind', 'failover', 'timeout'] # settables must be lowercase | 349 self.settable = ['maxtselctrows', 'maxfetch', 'autobind', 'failover', 'timeout'] # settables must be lowercase |
392 self.stdoutBeforeSpool = sys.stdout | 350 self.stdoutBeforeSpool = sys.stdout |
393 self.spoolFile = None | 351 self.spoolFile = None |
394 self.autobind = False | 352 self.autobind = False |
395 self.failover = False | 353 self.failover = False |
396 self.multiline = '''select insert update delete tselect | 354 self.multiline = '''select insert update delete tselect |
397 create drop alter'''.split() | 355 create drop alter'''.split() |
398 self.excludeFromHistory = '''run r list l history hi ed li'''.split() | |
399 | 356 |
400 def default(self, arg, do_everywhere=False): | 357 def default(self, arg, do_everywhere=False): |
401 sqlpython.sqlpython.default(self, arg, do_everywhere) | 358 sqlpython.sqlpython.default(self, arg, do_everywhere) |
402 self.sqlBuffer.append(self.query) | 359 self.sqlBuffer.append(self.query) |
403 | 360 |
455 if args[0] in self.multiline: | 412 if args[0] in self.multiline: |
456 statement = sqlpython.finishStatement(statement) | 413 statement = sqlpython.finishStatement(statement) |
457 return statement | 414 return statement |
458 except Exception: | 415 except Exception: |
459 return line | 416 return line |
460 | |
461 def postcmd(self, stop, line): | |
462 """Hook method executed just after a command dispatch is finished.""" | |
463 try: | |
464 command = line.split(None,1)[0].lower() | |
465 if command not in self.excludeFromHistory: | |
466 self.history.append(line) | |
467 finally: | |
468 return stop | |
469 | 417 |
470 def onecmd_plus_hooks(self, line): | 418 def onecmd_plus_hooks(self, line): |
471 line = self.precmd(line) | 419 line = self.precmd(line) |
472 stop = self.onecmd(line) | 420 stop = self.onecmd(line) |
473 stop = self.postcmd(stop, line) | 421 stop = self.postcmd(stop, line) |
811 'run [N]: runs the SQL that was run N commands ago' | 759 'run [N]: runs the SQL that was run N commands ago' |
812 runme = self.last_matching(arg) | 760 runme = self.last_matching(arg) |
813 print runme | 761 print runme |
814 self.onecmd_plus_hooks(runme) | 762 self.onecmd_plus_hooks(runme) |
815 do_r = do_run | 763 do_r = do_run |
816 def do_history(self, arg): | 764 |
817 """history [arg]: lists past commands issued | |
818 | |
819 no arg -> list all | |
820 arg is integer -> list one history item, by index | |
821 arg is string -> string search | |
822 arg is /enclosed in forward-slashes/ -> regular expression search | |
823 """ | |
824 if arg: | |
825 history = self.history.get(arg) | |
826 else: | |
827 history = self.history | |
828 for hi in history: | |
829 hi.pr() | |
830 def last_matching(self, arg): | |
831 try: | |
832 if arg: | |
833 return self.history.get(arg)[-1] | |
834 else: | |
835 return self.history[-1] | |
836 except: | |
837 return None | |
838 def do_list(self, arg): | |
839 """list [arg]: lists last command issued | |
840 | |
841 no arg -> list absolute last | |
842 arg is integer -> list one history item, by index | |
843 - arg, arg - (integer) -> list up to or after #arg | |
844 arg is string -> list last command matching string search | |
845 arg is /enclosed in forward-slashes/ -> regular expression search | |
846 """ | |
847 try: | |
848 self.last_matching(arg).pr() | |
849 except: | |
850 pass | |
851 do_hi = do_history | |
852 do_l = do_list | |
853 do_li = do_list | |
854 def load(self, fname): | 765 def load(self, fname): |
855 """Pulls command(s) into sql buffer. Returns number of commands loaded.""" | 766 """Pulls command(s) into sql buffer. Returns number of commands loaded.""" |
856 try: | 767 try: |
857 f = open(fname, 'r') | 768 f = open(fname, 'r') |
858 except IOError, e: | 769 except IOError, e: |