comparison sqlpyPlus.py @ 128:c5e6e475cdbe

allow terminators in grep
author catherine@localhost
date Tue, 12 Aug 2008 13:58:13 -0400
parents 5d3f0b9c01df
children d8661065fb77
comparison
equal deleted inserted replaced
127:5d3f0b9c01df 128:c5e6e475cdbe
669 except TypeError: 669 except TypeError:
670 print 'Could not resolve object %s.' % identifier 670 print 'Could not resolve object %s.' % identifier
671 object_type, owner, object_name = '', '', '' 671 object_type, owner, object_name = '', '', ''
672 return object_type, owner, object_name 672 return object_type, owner, object_name
673 #todo: resolve not finding cwm$ table 673 #todo: resolve not finding cwm$ table
674 #todo: names can overlap: view/trigger; index/index partition; table/table partition;
675 #package/package body; table/index; type/type body;
676 674
677 def do_resolve(self, arg): 675 def do_resolve(self, arg):
678 self.stdout.write(self.resolve(arg)+'\n') 676 self.stdout.write(self.resolve(arg)+'\n')
679 677
680 def spoolstop(self): 678 def spoolstop(self):
917 else: 915 else:
918 result.append('%s/%s' % (object_type, qualified_name)) 916 result.append('%s/%s' % (object_type, qualified_name))
919 self.stdout.write('\n'.join(result) + '\n') 917 self.stdout.write('\n'.join(result) + '\n')
920 918
921 def do_cat(self, arg): 919 def do_cat(self, arg):
920 '''cat TABLENAME --> SELECT * FROM equivalent'''
922 targets = arg.split() 921 targets = arg.split()
923 for target in targets: 922 for target in targets:
924 self.do_select('* from %s' % target) 923 self.do_select('* from %s' % target)
925 924
926 @options([make_option('-i', '--ignore-case', dest='ignorecase', action='store_true', help='Case-insensitive search')]) 925 @options([make_option('-i', '--ignore-case', dest='ignorecase', action='store_true', help='Case-insensitive search')])
927 def do_grep(self, arg, opts): 926 def do_grep(self, arg, opts):
928 """grep PATTERN TABLE - search for term in any of TABLE's fields""" 927 """grep PATTERN TABLE - search for term in any of TABLE's fields"""
929 928
930 targets = [] 929 arg = self.parsed(arg)
931 for target in arg.split(): 930 targetnames = arg.unterminated.split()
931 pattern = targetnames.pop(0)
932 targets = []
933 for target in targetnames:
932 if '*' in target: 934 if '*' in target:
933 self.curs.execute("SELECT owner, table_name FROM all_tables WHERE table_name LIKE '%s'" % 935 self.curs.execute("SELECT owner, table_name FROM all_tables WHERE table_name LIKE '%s'%s" %
934 (target.upper().replace('*','%'))) 936 (target.upper().replace('*','%')), arg.terminator)
935 for row in self.curs: 937 for row in self.curs:
936 targets.append('%s.%s' % row) 938 targets.append('%s.%s' % row)
937 else: 939 else:
938 targets.append(target) 940 targets.append(target)
939 pattern = targets.pop(0)
940 for target in targets: 941 for target in targets:
941 print target 942 print target
942 target = target.rstrip(';') 943 target = target.rstrip(';')
943 sql = [] 944 sql = []
944 try: 945 try:
945 self.curs.execute('select * from %s where 1=0' % target) 946 self.curs.execute('select * from %s where 1=0' % target) # just to fill description
946 if opts.ignorecase: 947 if opts.ignorecase:
947 sql = ' or '.join("LOWER(%s) LIKE '%%%s%%'" % (d[0], pattern.lower()) for d in self.curs.description) 948 sql = ' or '.join("LOWER(%s) LIKE '%%%s%%'" % (d[0], pattern.lower()) for d in self.curs.description)
948 else: 949 else:
949 sql = ' or '.join("%s LIKE '%%%s%%'" % (d[0], pattern) for d in self.curs.description) 950 sql = ' or '.join("%s LIKE '%%%s%%'" % (d[0], pattern) for d in self.curs.description)
950 sql = '* FROM %s WHERE %s' % (target, sql) 951 sql = '* FROM %s WHERE %s' % (target, sql)
951 self.do_select(sql) 952 self.do_select('%s%s%s' % (sql, arg.terminator, arg.rowlimit))
952 except Exception, e: 953 except Exception, e:
953 print e 954 print e
954 import traceback 955 import traceback
955 traceback.print_exc(file=sys.stdout) 956 traceback.print_exc(file=sys.stdout)
956 957