<s> import dbexts , cmd , sys , os <EOL> if sys . platform . startswith ( "<STR_LIT>" ) : <EOL> import java . lang . String <EOL> """<STR_LIT>""" <EOL> class IsqlExit ( Exception ) : pass <EOL> class Prompt : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , isql ) : <EOL> self . isql = isql <EOL> def __str__ ( self ) : <EOL> prompt = "<STR_LIT>" % ( self . isql . db . dbname ) <EOL> if len ( self . isql . sqlbuffer ) > <NUM_LIT:0> : <EOL> prompt = "<STR_LIT>" <EOL> return prompt <EOL> if sys . platform . startswith ( "<STR_LIT>" ) : <EOL> def __tojava__ ( self , cls ) : <EOL> if cls == java . lang . String : <EOL> return self . __str__ ( ) <EOL> return False <EOL> class IsqlCmd ( cmd . Cmd ) : <EOL> def __init__ ( self , db = None , delimiter = "<STR_LIT:;>" , comment = ( '<STR_LIT:#>' , '<STR_LIT>' ) ) : <EOL> cmd . Cmd . __init__ ( self , completekey = None ) <EOL> if db is None or type ( db ) == type ( "<STR_LIT>" ) : <EOL> self . db = dbexts . dbexts ( db ) <EOL> else : <EOL> self . db = db <EOL> self . kw = { } <EOL> self . sqlbuffer = [ ] <EOL> self . comment = comment <EOL> self . delimiter = delimiter <EOL> self . prompt = Prompt ( self ) <EOL> def parseline ( self , line ) : <EOL> command , arg , line = cmd . Cmd . parseline ( self , line ) <EOL> if command and command < > "<STR_LIT>" : <EOL> command = command . lower ( ) <EOL> return command , arg , line <EOL> def do_which ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> print self . db <EOL> return False <EOL> def do_EOF ( self , arg ) : <EOL> return False <EOL> def do_p ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> exec arg . strip ( ) in globals ( ) <EOL> except : <EOL> print sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> return False <EOL> def do_column ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> def do_use ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> self . db = self . db . __class__ ( arg . strip ( ) ) <EOL> return False <EOL> def do_table ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> if len ( arg . strip ( ) ) : <EOL> self . db . table ( arg , ** self . kw ) <EOL> else : <EOL> self . db . table ( None , ** self . kw ) <EOL> return False <EOL> def do_proc ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> if len ( arg . strip ( ) ) : <EOL> self . db . proc ( arg , ** self . kw ) <EOL> else : <EOL> self . db . proc ( None , ** self . kw ) <EOL> return False <EOL> def do_schema ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> print <EOL> self . db . schema ( arg ) <EOL> print <EOL> return False <EOL> def do_delimiter ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> delimiter = arg . strip ( ) <EOL> if len ( delimiter ) > <NUM_LIT:0> : <EOL> self . delimiter = delimiter <EOL> def do_o ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> if not arg : <EOL> fp = self . db . out <EOL> try : <EOL> if fp : <EOL> fp . close ( ) <EOL> finally : <EOL> self . db . out = None <EOL> else : <EOL> fp = open ( arg , "<STR_LIT:w>" ) <EOL> self . db . out = fp <EOL> def do_q ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if self . db . out : <EOL> self . db . out . close ( ) <EOL> finally : <EOL> return True <EOL> def do_set ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> if len ( arg . strip ( ) ) == <NUM_LIT:0> : <EOL> items = self . kw . items ( ) <EOL> if len ( items ) : <EOL> print <EOL> for a in dbexts . console ( items , ( "<STR_LIT:key>" , "<STR_LIT:value>" ) ) [ : - <NUM_LIT:1> ] : <EOL> print a <EOL> print <EOL> return False <EOL> d = filter ( lambda x : len ( x ) > <NUM_LIT:0> , map ( lambda x : x . strip ( ) , arg . split ( "<STR_LIT:=>" ) ) ) <EOL> if len ( d ) == <NUM_LIT:1> : <EOL> if self . kw . has_key ( d [ <NUM_LIT:0> ] ) : <EOL> del self . kw [ d [ <NUM_LIT:0> ] ] <EOL> else : <EOL> self . kw [ d [ <NUM_LIT:0> ] ] = eval ( d [ <NUM_LIT:1> ] ) <EOL> def do_i ( self , arg ) : <EOL> fp = open ( arg ) <EOL> try : <EOL> print <EOL> for line in fp . readlines ( ) : <EOL> line = self . precmd ( line ) <EOL> stop = self . onecmd ( line ) <EOL> stop = self . postcmd ( stop , line ) <EOL> finally : <EOL> fp . close ( ) <EOL> return False <EOL> def default ( self , arg ) : <EOL> try : <EOL> token = arg . strip ( ) <EOL> if not token : <EOL> return False <EOL> comment = [ token . startswith ( x ) for x in self . comment ] <EOL> if reduce ( lambda x , y : x or y , comment ) : <EOL> return False <EOL> if token [ <NUM_LIT:0> ] == '<STR_LIT:\\>' : <EOL> token = token [ <NUM_LIT:1> : ] <EOL> if len ( token ) >= len ( self . delimiter ) : <EOL> if token [ - <NUM_LIT:1> * len ( self . delimiter ) : ] == self . delimiter : <EOL> self . sqlbuffer . append ( token [ : - <NUM_LIT:1> * len ( self . delimiter ) ] ) <EOL> if self . sqlbuffer : <EOL> q = "<STR_LIT:U+0020>" . join ( self . sqlbuffer ) <EOL> print q <EOL> self . db . isql ( q , ** self . kw ) <EOL> self . sqlbuffer = [ ] <EOL> if self . db . updatecount : <EOL> print <EOL> if self . db . updatecount == <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" % ( self . db . updatecount ) <EOL> print <EOL> return False <EOL> if token : <EOL> self . sqlbuffer . append ( token ) <EOL> except : <EOL> self . sqlbuffer = [ ] <EOL> print <EOL> print sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> print <EOL> return False <EOL> def emptyline ( self ) : <EOL> return False <EOL> def postloop ( self ) : <EOL> raise IsqlExit ( ) <EOL> def cmdloop ( self , intro = None ) : <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> cmd . Cmd . cmdloop ( self , intro ) <EOL> except IsqlExit , e : <EOL> break <EOL> except Exception , e : <EOL> print <EOL> print e <EOL> print <EOL> intro = None <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import getopt <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , "<STR_LIT>" , [ ] ) <EOL> except getopt . error , msg : <EOL> print <EOL> print msg <EOL> print "<STR_LIT>" % ( sys . argv [ <NUM_LIT:0> ] ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> dbname = None <EOL> for opt , arg in opts : <EOL> if opt == '<STR_LIT>' : <EOL> dbname = arg <EOL> intro = "<STR_LIT>" % ( __version__ ) <EOL> isql = IsqlCmd ( dbname ) <EOL> isql . cmdloop ( ) </s>
