comparison sqlpyPlus.py @ 86:ca5d615d8207

hmmm, super-big grep
author catherine@localhost
date Mon, 19 May 2008 17:15:31 -0400
parents b336d049cac7
children 2de82dd6eba2
comparison
equal deleted inserted replaced
85:b336d049cac7 86:ca5d615d8207
885 def do_cat(self, arg): 885 def do_cat(self, arg):
886 targets = arg.split() 886 targets = arg.split()
887 for target in targets: 887 for target in targets:
888 self.do_select('* from %s' % target) 888 self.do_select('* from %s' % target)
889 889
890 def do_grep(self, arg): 890 @options([make_option('-i', '--ignore-case', dest='ignorecase', action='store_true', help='Case-insensitive search')])
891 """grep PATTERN TABLE - search for term in any of TABLE's fields""" 891 def do_grep(self, arg, opts):
892 targets = arg.split() 892 """grep PATTERN TABLE - search for term in any of TABLE's fields"""
893
894 targets = []
895 for target in arg.split():
896 if '*' in target:
897 self.curs.execute("SELECT owner, table_name FROM all_tables WHERE table_name LIKE '%s'" %
898 (target.upper().replace('*','%')))
899 for row in self.curs:
900 targets.append('%s.%s' % row)
901 else:
902 targets.append(target)
893 pattern = targets.pop(0) 903 pattern = targets.pop(0)
894 for target in targets: 904 for target in targets:
905 print target
895 target = target.rstrip(';') 906 target = target.rstrip(';')
896 sql = [] 907 sql = []
897 try: 908 try:
898 self.curs.execute('select * from %s where 1=0' % target) 909 self.curs.execute('select * from %s where 1=0' % target)
899 sql = ' or '.join("%s LIKE '%%%s%%'" % (d[0], pattern) for d in self.curs.description) 910 if opts.ignorecase:
911 sql = ' or '.join("LOWER(%s) LIKE '%%%s%%'" % (d[0], pattern.lower()) for d in self.curs.description)
912 else:
913 sql = ' or '.join("%s LIKE '%%%s%%'" % (d[0], pattern) for d in self.curs.description)
900 sql = '* FROM %s WHERE %s' % (target, sql) 914 sql = '* FROM %s WHERE %s' % (target, sql)
901 self.do_select(sql) 915 self.do_select(sql)
902 except Exception, e: 916 except Exception, e:
903 print e 917 print e
904 import traceback 918 import traceback