comparison sqlpyPlus.py @ 121:3dd852ab45c0

fixing terminator stripping
author catherine@Elli.myhome.westell.com
date Wed, 23 Jul 2008 14:29:15 -0400
parents e11bbeb3f34c
children 61e2a824b66b
comparison
equal deleted inserted replaced
120:e11bbeb3f34c 121:3dd852ab45c0
122 WHERE object_name = :object_name 122 WHERE object_name = :object_name
123 AND owner = :owner 123 AND owner = :owner
124 AND package_name IS NULL 124 AND package_name IS NULL
125 AND argument_name IS NOT NULL 125 AND argument_name IS NOT NULL
126 ORDER BY sequence;""", 126 ORDER BY sequence;""",
127 descQueries['PROCEDURE']) 127 descQueries['PROCEDURE'][0])
128 128
129 queries = { 129 queries = {
130 'resolve': """ 130 'resolve': """
131 SELECT object_type, object_name, owner FROM ( 131 SELECT object_type, object_name, owner FROM (
132 SELECT object_type, object_name, user owner, 1 priority 132 SELECT object_type, object_name, user owner, 1 priority
565 565
566 @options([make_option('-f', '--full', action='store_true', help='get dependent objects as well')]) 566 @options([make_option('-f', '--full', action='store_true', help='get dependent objects as well')])
567 def do_pull(self, arg, opts): 567 def do_pull(self, arg, opts):
568 """Displays source code.""" 568 """Displays source code."""
569 569
570 object_type, owner, object_name = self.resolve(arg.strip(self.terminator).upper()) 570 arg = self.parsed(arg).unterminated
571 object_type, owner, object_name = self.resolve(arg.upper())
571 if not object_type: 572 if not object_type:
572 return 573 return
573 self.stdout.write("%s %s.%s\n" % (object_type, owner, object_name)) 574 self.stdout.write("%s %s.%s\n" % (object_type, owner, object_name))
574 self.stdout.write(str(self.curs.callfunc('DBMS_METADATA.GET_DDL', cx_Oracle.CLOB, 575 self.stdout.write(str(self.curs.callfunc('DBMS_METADATA.GET_DDL', cx_Oracle.CLOB,
575 [object_type, object_name, owner]))) 576 [object_type, object_name, owner])))
579 self.stdout.write(str(self.curs.callfunc('DBMS_METADATA.GET_DEPENDENT_DDL', cx_Oracle.CLOB, 580 self.stdout.write(str(self.curs.callfunc('DBMS_METADATA.GET_DEPENDENT_DDL', cx_Oracle.CLOB,
580 [dependent_type, object_name, owner]))) 581 [dependent_type, object_name, owner])))
581 except cx_Oracle.DatabaseError: 582 except cx_Oracle.DatabaseError:
582 pass 583 pass
583 584
584 @options([make_option('-i', '--insensitive', action='store_true', help='case-insensitive search')]) 585 @options([make_option('-i', '--insensitive', action='store_true', help='case-insensitive search'),
586 make_option('-c', '--col', action='store_true', help='find column')])
585 def do_find(self, arg, opts): 587 def do_find(self, arg, opts):
586 """Finds argument in source code.""" 588 """Finds argument in source code or (with -c) in column definitions."""
587 589
588 if opts.insensitive: 590 arg = self.parsed(arg).unterminated.upper()
589 searchfor = "LOWER(text)" 591 if opts.col:
590 arg = arg.lower() 592 self.do_select("table_name, column_name from all_tab_columns where column_name like '%%%s%%'" % (arg))
591 else: 593 else:
592 searchfor = "text" 594 if opts.insensitive:
593 self.do_select("* from all_source where %s like '%%%s%%'" % (searchfor, arg)) 595 searchfor = "LOWER(text)"
596 arg = arg.lower()
597 else:
598 searchfor = "text"
599 self.do_select("* from all_source where %s like '%%%s%%'" % (searchfor, arg))
594 600
595 @options([make_option('-a','--all',action='store_true', 601 @options([make_option('-a','--all',action='store_true',
596 help='Describe all objects (not just my own)')]) 602 help='Describe all objects (not just my own)')])
597 def do_describe(self, arg, opts): 603 def do_describe(self, arg, opts):
598 "emulates SQL*Plus's DESCRIBE" 604 "emulates SQL*Plus's DESCRIBE"
599 605
606 arg = self.parsed(arg).unterminated.upper()
600 if opts.all: 607 if opts.all:
601 which_view = (', owner', 'all') 608 which_view = (', owner', 'all')
602 else: 609 else:
603 which_view = ('', 'user') 610 which_view = ('', 'user')
604 611
605 if not arg: 612 if not arg:
606 self.do_select("""object_name, object_type%s FROM %s_objects WHERE object_type IN ('TABLE','VIEW','INDEX') ORDER BY object_name""" % which_view) 613 self.do_select("""object_name, object_type%s FROM %s_objects WHERE object_type IN ('TABLE','VIEW','INDEX') ORDER BY object_name""" % which_view)
607 return 614 return
608 object_type, owner, object_name = self.resolve(arg.strip(self.terminator).upper()) 615 object_type, owner, object_name = self.resolve(arg)
609 if not object_type: 616 if not object_type:
610 self.do_select("""object_name, object_type%s FROM %s_objects 617 self.do_select("""object_name, object_type%s FROM %s_objects
611 WHERE object_type IN ('TABLE','VIEW','INDEX') 618 WHERE object_type IN ('TABLE','VIEW','INDEX')
612 AND object_name LIKE '%%%s%%' 619 AND object_name LIKE '%%%s%%'
613 ORDER BY object_name""" % 620 ORDER BY object_name""" %
625 self.stdout.write(packageObj_name + '\n') 632 self.stdout.write(packageObj_name + '\n')
626 self.do_select(descQueries['PackageObjArgs'][0],bindVarsIn={'package_name':object_name, 'owner':owner, 'object_name':packageObj_name}) 633 self.do_select(descQueries['PackageObjArgs'][0],bindVarsIn={'package_name':object_name, 'owner':owner, 'object_name':packageObj_name})
627 do_desc = do_describe 634 do_desc = do_describe
628 635
629 def do_deps(self, arg): 636 def do_deps(self, arg):
630 object_type, owner, object_name = self.resolve(arg.strip(self.terminator).upper()) 637 arg = self.parsed(arg).unterminated.upper()
638 object_type, owner, object_name = self.resolve(arg)
631 if object_type == 'PACKAGE BODY': 639 if object_type == 'PACKAGE BODY':
632 q = "and (type != 'PACKAGE BODY' or name != :object_name)'" 640 q = "and (type != 'PACKAGE BODY' or name != :object_name)'"
633 object_type = 'PACKAGE' 641 object_type = 'PACKAGE'
634 else: 642 else:
635 q = "" 643 q = ""
643 %s""" % (q) 651 %s""" % (q)
644 self.do_select(q, {'object_name':object_name, 'object_type':object_type, 'owner':owner}) 652 self.do_select(q, {'object_name':object_name, 'object_type':object_type, 'owner':owner})
645 653
646 def do_comments(self, arg): 654 def do_comments(self, arg):
647 'Prints comments on a table and its columns.' 655 'Prints comments on a table and its columns.'
648 object_type, owner, object_name = self.resolve(arg.strip(self.terminator).upper()) 656 arg = self.parsed(arg).unterminated.upper()
657 object_type, owner, object_name = self.resolve(arg)
649 if object_type: 658 if object_type:
650 self.curs.execute(queries['tabComments'],{'table_name':object_name, 'owner':owner}) 659 self.curs.execute(queries['tabComments'],{'table_name':object_name, 'owner':owner})
651 self.stdout.write("%s %s.%s: %s\n" % (object_type, owner, object_name, self.curs.fetchone()[0])) 660 self.stdout.write("%s %s.%s: %s\n" % (object_type, owner, object_name, self.curs.fetchone()[0]))
652 self.do_select(queries['colComments'],bindVarsIn={'owner':owner, 'object_name': object_name}) 661 self.do_select(queries['colComments'],bindVarsIn={'owner':owner, 'object_name': object_name})
653 662
709 print self.do_compare.__doc__ 718 print self.do_compare.__doc__
710 return 719 return
711 for n in range(len(args2)): 720 for n in range(len(args2)):
712 query = args2[n] 721 query = args2[n]
713 fnames.append('compare%s.txt' % n) 722 fnames.append('compare%s.txt' % n)
723 #TODO: update this terminator-stripping
714 if query.rstrip()[-1] != self.terminator: 724 if query.rstrip()[-1] != self.terminator:
715 query = '%s%s' % (query, self.terminator) 725 query = '%s%s' % (query, self.terminator)
716 self.onecmd_plus_hooks('%s > %s' % (query, fnames[n])) 726 self.onecmd_plus_hooks('%s > %s' % (query, fnames[n]))
717 diffMergeSearcher.invoke(fnames[0], fnames[1]) 727 diffMergeSearcher.invoke(fnames[0], fnames[1])
718 728
819 else: 829 else:
820 for (var, val) in self.binds.items(): 830 for (var, val) in self.binds.items():
821 print ':%s = %s' % (var, val) 831 print ':%s = %s' % (var, val)
822 832
823 def do_setbind(self, arg): 833 def do_setbind(self, arg):
824 arg = self.parsed(arg).statement # removes terminators? 834 arg = self.parsed(arg).unterminated
825 args = arg.split(None, 2) 835 args = arg.split(None, 2)
826 if len(args) < 2: 836 if len(args) < 2:
827 self.do_print(arg) 837 self.do_print(arg)
828 return 838 return
829 elif len(args) > 2 and args[1] in ('=',':='): 839 elif len(args) > 2 and args[1] in ('=',':='):
838 try: 848 try:
839 self.binds[var] = float(val) 849 self.binds[var] = float(val)
840 return 850 return
841 except ValueError: 851 except ValueError:
842 try: 852 try:
843 self.binds[var] = self.curs.callfunc(val, []) 853 varsUsed = findBinds(arg, self.binds, {})
854 self.binds[var] = self.curs.callfunc(val, varsUsed) #TODO: wrong args
855 # NotSupportedError: Variable_TypeByPythonType(): unhandled data type
856 # need to warn that it's a date?
844 return 857 return
845 except: 858 except:
846 pass 859 pass
847 # TODO: when setting
848 860
849 print 'Could not parse ', args 861 print 'Could not parse ', args
850 862
851 def do_exec(self, arg): 863 def do_exec(self, arg):
852 if arg[0] == ':': 864 if arg[0] == ':':
964 print e 976 print e
965 import traceback 977 import traceback
966 traceback.print_exc(file=sys.stdout) 978 traceback.print_exc(file=sys.stdout)
967 979
968 def do_refs(self, arg): 980 def do_refs(self, arg):
969 object_type, owner, object_name = self.resolve(arg.strip(self.terminator).upper()) 981 arg = self.parsed(arg).unterminated.upper()
982 object_type, owner, object_name = self.resolve(arg)
970 if object_type == 'TABLE': 983 if object_type == 'TABLE':
971 self.do_select(queries['refs'],bindVarsIn={'object_name':object_name, 'owner':owner}) 984 self.do_select(queries['refs'],bindVarsIn={'object_name':object_name, 'owner':owner})
972 985
973 def _test(): 986 def _test():
974 import doctest 987 import doctest