comparison sqlpyPlus.py @ 81:32c868fca272

midway through converting to optparse
author catherine@localhost
date Thu, 15 May 2008 13:58:39 -0400
parents 83de0cb04f12
children 5485b66c3445
comparison
equal deleted inserted replaced
80:83de0cb04f12 81:32c868fca272
185 and c1.owner = :owner 185 and c1.owner = :owner
186 """ 186 """
187 } 187 }
188 188
189 import sys, os, re, sqlpython, cx_Oracle, pyparsing 189 import sys, os, re, sqlpython, cx_Oracle, pyparsing
190 from cmd2 import flagReader, Cmd 190 from cmd2 import Cmd, make_option, options
191 191
192 if float(sys.version[:3]) < 2.3: 192 if float(sys.version[:3]) < 2.3:
193 def enumerate(lst): 193 def enumerate(lst):
194 return zip(range(len(lst)), lst) 194 return zip(range(len(lst)), lst)
195 195
527 print e 527 print e
528 import traceback 528 import traceback
529 traceback.print_exc(file=sys.stdout) 529 traceback.print_exc(file=sys.stdout)
530 self.sqlBuffer.append(self.query) 530 self.sqlBuffer.append(self.query)
531 531
532 pullflags = flagReader.FlagSet([flagReader.Flag('full')]) 532 @options([make_option('-f', '--full', action='store-true', help='get dependent objects as well')])
533 def do_pull(self, arg): 533 def do_pull(self, arg, opts):
534 """Displays source code. 534 """Displays source code."""
535 535
536 --full, -f: get dependent objects as well"""
537
538 options, arg = self.pullflags.parse(arg)
539 object_type, owner, object_name = self.resolve(arg.strip(self.terminator).upper()) 536 object_type, owner, object_name = self.resolve(arg.strip(self.terminator).upper())
540 if not object_type: 537 if not object_type:
541 return 538 return
542 self.stdout.write("%s %s.%s\n" % (object_type, owner, object_name)) 539 self.stdout.write("%s %s.%s\n" % (object_type, owner, object_name))
543 self.stdout.write(str(self.curs.callfunc('DBMS_METADATA.GET_DDL', cx_Oracle.CLOB, 540 self.stdout.write(str(self.curs.callfunc('DBMS_METADATA.GET_DDL', cx_Oracle.CLOB,
544 [object_type, object_name, owner]))) 541 [object_type, object_name, owner])))
545 if options.has_key('full'): 542 if opts.full:
546 for dependent_type in ('OBJECT_GRANT', 'CONSTRAINT', 'TRIGGER'): 543 for dependent_type in ('OBJECT_GRANT', 'CONSTRAINT', 'TRIGGER'):
547 try: 544 try:
548 self.stdout.write(str(self.curs.callfunc('DBMS_METADATA.GET_DEPENDENT_DDL', cx_Oracle.CLOB, 545 self.stdout.write(str(self.curs.callfunc('DBMS_METADATA.GET_DEPENDENT_DDL', cx_Oracle.CLOB,
549 [dependent_type, object_name, owner]))) 546 [dependent_type, object_name, owner])))
550 except cx_Oracle.DatabaseError: 547 except cx_Oracle.DatabaseError:
551 pass 548 pass
552 549
553 findflags = flagReader.FlagSet([flagReader.Flag('insensitive')]) 550 @options([make_option('-i', '--insensitive', action='store-true', help='case-insensitive search')])
554 def do_find(self, arg): 551 def do_find(self, arg, opts):
555 """Finds argument in source code. 552 """Finds argument in source code."""
556 553
557 --insensitive, -i: case-insensitive search""" 554 if opts.insensitive:
558
559 options, arg = self.findflags.parse(arg)
560 if options.has_key('insensitive'):
561 searchfor = "LOWER(text)" 555 searchfor = "LOWER(text)"
562 arg = arg.lower() 556 arg = arg.lower()
563 else: 557 else:
564 searchfor = "text" 558 searchfor = "text"
565 self.do_select("* from all_source where %s like '%%%s%%'" % (searchfor, arg)) 559 self.do_select("* from all_source where %s like '%%%s%%'" % (searchfor, arg))
835 self.anon_plsql('declare ' + arg) 829 self.anon_plsql('declare ' + arg)
836 830
837 def do_create(self, arg): 831 def do_create(self, arg):
838 self.anon_plsql('create ' + arg) 832 self.anon_plsql('create ' + arg)
839 833
840 lsflags = flagReader.FlagSet([flagReader.Flag('long')]) 834 @options([make_option('-l', '--long', action='store-true', help='long descriptions')])
841 def do_ls(self, arg): 835 def do_ls(self, arg, opts):
842 options, arg = self.lsflags.parse(arg)
843 where = '' 836 where = ''
844 if arg: 837 if arg:
845 where = """\nWHERE object_type || '/' || object_name 838 where = """\nWHERE object_type || '/' || object_name
846 LIKE '%%%s%%'""" % (arg.upper().replace('*','%')) 839 LIKE '%%%s%%'""" % (arg.upper().replace('*','%'))
847 else: 840 else:
851 status, last_ddl_time 844 status, last_ddl_time
852 FROM user_objects %s 845 FROM user_objects %s
853 ORDER BY object_type, object_name''' % (where) 846 ORDER BY object_type, object_name''' % (where)
854 self.curs.execute(statement) 847 self.curs.execute(statement)
855 for (object_type, object_name, status, last_ddl_time) in self.curs.fetchall(): 848 for (object_type, object_name, status, last_ddl_time) in self.curs.fetchall():
856 if options.has_key('long'): 849 if opts.long:
857 result.append('%s\t%s\t%s/%s' % (status, last_ddl_time, object_type, object_name)) 850 result.append('%s\t%s\t%s/%s' % (status, last_ddl_time, object_type, object_name))
858 else: 851 else:
859 result.append('%s/%s' % (object_type, object_name)) 852 result.append('%s/%s' % (object_type, object_name))
860 self.stdout.write('\n'.join(result) + '\n') 853 self.stdout.write('\n'.join(result) + '\n')
861 854