changeset 86:ca5d615d8207

hmmm, super-big grep
author catherine@localhost
date Mon, 19 May 2008 17:15:31 -0400
parents b336d049cac7
children 2de82dd6eba2
files sqlpyPlus.py
diffstat 1 files changed, 18 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/sqlpyPlus.py	Mon May 19 16:45:24 2008 -0400
+++ b/sqlpyPlus.py	Mon May 19 17:15:31 2008 -0400
@@ -887,16 +887,30 @@
         for target in targets:
             self.do_select('* from %s' % target)
 
-    def do_grep(self, arg):
-        """grep PATTERN TABLE - search for term in any of TABLE's fields"""
-        targets = arg.split()
+    @options([make_option('-i', '--ignore-case', dest='ignorecase', action='store_true', help='Case-insensitive search')])        
+    def do_grep(self, arg, opts):
+        """grep PATTERN TABLE - search for term in any of TABLE's fields"""    
+                
+        targets = []
+        for target in arg.split():
+            if '*' in target:
+                self.curs.execute("SELECT owner, table_name FROM all_tables WHERE table_name LIKE '%s'" %
+                                  (target.upper().replace('*','%')))
+                for row in self.curs:
+                    targets.append('%s.%s' % row)
+            else:
+                targets.append(target)
         pattern = targets.pop(0)
         for target in targets:
+            print target
             target = target.rstrip(';')
             sql = []
             try:
                 self.curs.execute('select * from %s where 1=0' % target)
-                sql = ' or '.join("%s LIKE '%%%s%%'" % (d[0], pattern) for d in self.curs.description)
+                if opts.ignorecase:
+                    sql = ' or '.join("LOWER(%s) LIKE '%%%s%%'" % (d[0], pattern.lower()) for d in self.curs.description)                                        
+                else:
+                    sql = ' or '.join("%s LIKE '%%%s%%'" % (d[0], pattern) for d in self.curs.description)
                 sql = '* FROM %s WHERE %s' % (target, sql)
                 self.do_select(sql)
             except Exception, e: