<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import atexit <EOL> import csv <EOL> import os <EOL> import re <EOL> import readline <EOL> import sys <EOL> import gflags <EOL> from pylib import app <EOL> from pylib import db <EOL> FLAGS = gflags . FLAGS <EOL> gflags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT:utf8>' , '<STR_LIT>' ) <EOL> gflags . DEFINE_string ( '<STR_LIT>' , None , '<STR_LIT>' ) <EOL> gflags . DEFINE_boolean ( '<STR_LIT>' , True , <EOL> '<STR_LIT>' ) <EOL> _CSV_RE = re . compile ( r'<STR_LIT>' , <EOL> re . IGNORECASE | re . DOTALL | re . MULTILINE ) <EOL> def _Encode ( value ) : <EOL> if isinstance ( value , unicode ) : <EOL> return value . encode ( FLAGS . charset ) <EOL> else : <EOL> return value <EOL> def _ShowTable ( result ) : <EOL> if result is None : <EOL> return <EOL> if result : <EOL> for row in result . GetTable ( FLAGS . print_field_names ) : <EOL> print _Encode ( row ) <EOL> rowcount = result . GetRowsAffected ( ) <EOL> if rowcount is not None and rowcount != len ( result ) : <EOL> print '<STR_LIT>' % rowcount <EOL> class CancellableExecutor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dbh ) : <EOL> """<STR_LIT>""" <EOL> self . dbh = dbh <EOL> self . ongoing_operation = None <EOL> def CancelOngoingOperation ( self ) : <EOL> if self . ongoing_operation : <EOL> self . dbh . Cancel ( self . ongoing_operation ) <EOL> self . ongoing_operation = None <EOL> def Execute ( self , query ) : <EOL> """<STR_LIT>""" <EOL> csvh = None <EOL> csv_match = _CSV_RE . match ( query ) <EOL> if csv_match : <EOL> csvh = csv . writer ( sys . stdout ) <EOL> query = csv_match . group ( '<STR_LIT>' ) <EOL> self . ongoing_operation = self . dbh . Submit ( query ) <EOL> results = self . dbh . Wait ( self . ongoing_operation ) <EOL> for key , values in results . items ( ) : <EOL> for value in values : <EOL> results [ key ] = value <EOL> break <EOL> self . ongoing_operation = None <EOL> if csvh : <EOL> if FLAGS . print_field_names : <EOL> csvh . writerow ( results . values ( ) [ <NUM_LIT:0> ] . GetFields ( ) ) <EOL> for result in results . itervalues ( ) : <EOL> for row in result . GetRows ( ) : <EOL> fields = [ ] <EOL> for field in row : <EOL> if isinstance ( field , unicode ) : <EOL> fields . append ( field . encode ( FLAGS . charset ) ) <EOL> else : <EOL> fields . append ( field ) <EOL> csvh . writerow ( fields ) <EOL> return <EOL> by_result = { } <EOL> result = None <EOL> for name , result in results . iteritems ( ) : <EOL> by_result . setdefault ( result , [ ] ) . append ( name ) <EOL> if len ( by_result ) > <NUM_LIT:1> : <EOL> for result , names in by_result . iteritems ( ) : <EOL> if not result : <EOL> continue <EOL> names . sort ( ) <EOL> print '<STR_LIT>' % names <EOL> _ShowTable ( result ) <EOL> else : <EOL> _ShowTable ( result ) <EOL> def Execute ( dbh , query ) : <EOL> executor = CancellableExecutor ( dbh ) <EOL> executor . Execute ( query ) <EOL> def main ( argv ) : <EOL> if len ( argv ) < <NUM_LIT:2> : <EOL> raise app . UsageError ( '<STR_LIT>' ) <EOL> try : <EOL> histfile = os . path . join ( os . environ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> readline . read_history_file ( histfile ) <EOL> atexit . register ( readline . write_history_file , histfile ) <EOL> except ( KeyError , IOError ) : <EOL> pass <EOL> if sys . stdin . isatty ( ) : <EOL> if FLAGS . prompt : <EOL> prompt = '<STR_LIT>' % FLAGS . prompt <EOL> else : <EOL> prompt = '<STR_LIT>' % argv [ <NUM_LIT:1> ] <EOL> continued_prompt = '<STR_LIT:U+0020>' * ( len ( prompt ) - <NUM_LIT:3> ) + '<STR_LIT>' <EOL> else : <EOL> prompt = continued_prompt = '<STR_LIT>' <EOL> continued = [ False ] <EOL> def GetLines ( ) : <EOL> while True : <EOL> try : <EOL> display_prompt = continued_prompt if continued [ <NUM_LIT:0> ] else prompt <EOL> continued [ <NUM_LIT:0> ] = True <EOL> yield raw_input ( display_prompt ) . decode ( FLAGS . charset ) <EOL> except EOFError : <EOL> print <EOL> return <EOL> with db . Connect ( argv [ <NUM_LIT:1> ] , charset = FLAGS . charset ) as dbh : <EOL> for statement in db . XCombineSQL ( GetLines ( ) ) : <EOL> continued [ <NUM_LIT:0> ] = False <EOL> executor = CancellableExecutor ( dbh ) <EOL> try : <EOL> executor . Execute ( statement ) <EOL> except KeyboardInterrupt : <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> executor . CancelOngoingOperation ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app . run ( ) </s>
