<s> import logging <EOL> import pymysql <EOL> import sqlparse <EOL> from . packages import connection , special <EOL> _logger = logging . getLogger ( __name__ ) <EOL> class SQLExecute ( object ) : <EOL> databases_query = '''<STR_LIT>''' <EOL> tables_query = '''<STR_LIT>''' <EOL> version_query = '''<STR_LIT>''' <EOL> version_comment_query = '''<STR_LIT>''' <EOL> show_candidates_query = '''<STR_LIT>''' <EOL> users_query = '''<STR_LIT>''' <EOL> functions_query = '''<STR_LIT>''' <EOL> table_columns_query = '''<STR_LIT>''' <EOL> def __init__ ( self , database , user , password , host , port , socket , charset , <EOL> local_infile , ssl = False ) : <EOL> self . dbname = database <EOL> self . user = user <EOL> self . password = password <EOL> self . host = host <EOL> self . port = port <EOL> self . socket = socket <EOL> self . charset = charset <EOL> self . local_infile = local_infile <EOL> self . ssl = ssl <EOL> self . _server_type = None <EOL> self . connect ( ) <EOL> def connect ( self , database = None , user = None , password = None , host = None , <EOL> port = None , socket = None , charset = None , local_infile = None , ssl = None ) : <EOL> db = ( database or self . dbname ) <EOL> user = ( user or self . user ) <EOL> password = ( password or self . password ) <EOL> host = ( host or self . host ) <EOL> port = ( port or self . port ) <EOL> socket = ( socket or self . socket ) <EOL> charset = ( charset or self . charset ) <EOL> local_infile = ( local_infile or self . local_infile ) <EOL> ssl = ( ssl or self . ssl ) <EOL> _logger . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> database , user , host , port , socket , charset , local_infile , ssl ) <EOL> conn = connection . connect ( database = db , user = user , password = password , <EOL> host = host , port = port , unix_socket = socket , <EOL> use_unicode = True , charset = charset , autocommit = True , <EOL> client_flag = pymysql . constants . CLIENT . INTERACTIVE , <EOL> cursorclass = connection . Cursor , local_infile = local_infile , <EOL> ssl = ssl ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . conn . close ( ) <EOL> self . conn = conn <EOL> self . dbname = db <EOL> self . user = user <EOL> self . password = password <EOL> self . host = host <EOL> self . port = port <EOL> self . socket = socket <EOL> self . charset = charset <EOL> self . ssl = ssl <EOL> def run ( self , statement ) : <EOL> """<STR_LIT>""" <EOL> statement = statement . strip ( ) <EOL> if not statement : <EOL> yield ( None , None , None , None ) <EOL> if statement . startswith ( '<STR_LIT>' ) : <EOL> components = [ statement ] <EOL> else : <EOL> components = sqlparse . split ( statement ) <EOL> for sql in components : <EOL> sql = sql . rstrip ( '<STR_LIT:;>' ) <EOL> if sql . endswith ( '<STR_LIT>' ) : <EOL> special . set_expanded_output ( True ) <EOL> sql = sql [ : - <NUM_LIT:2> ] . strip ( ) <EOL> try : <EOL> _logger . debug ( '<STR_LIT>' , sql ) <EOL> cur = self . conn . cursor ( ) <EOL> for result in special . execute ( cur , sql ) : <EOL> yield result <EOL> except special . CommandNotFound : <EOL> yield self . execute_normal_sql ( sql ) <EOL> def execute_normal_sql ( self , split_sql ) : <EOL> _logger . debug ( '<STR_LIT>' , split_sql ) <EOL> cur = self . conn . cursor ( ) <EOL> num_rows = cur . execute ( split_sql ) <EOL> title = headers = None <EOL> if cur . description is not None : <EOL> headers = [ x [ <NUM_LIT:0> ] for x in cur . description ] <EOL> status = '<STR_LIT>' <EOL> else : <EOL> _logger . debug ( '<STR_LIT>' ) <EOL> status = '<STR_LIT>' <EOL> status = status . format ( num_rows , '<STR_LIT>' if num_rows == <NUM_LIT:1> else '<STR_LIT:s>' ) <EOL> return ( title , cur if cur . description else None , headers , status ) <EOL> def tables ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . conn . cursor ( ) as cur : <EOL> _logger . debug ( '<STR_LIT>' , self . tables_query ) <EOL> cur . execute ( self . tables_query ) <EOL> for row in cur : <EOL> yield row <EOL> def table_columns ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . conn . cursor ( ) as cur : <EOL> _logger . debug ( '<STR_LIT>' , self . table_columns_query ) <EOL> cur . execute ( self . table_columns_query % self . dbname ) <EOL> for row in cur : <EOL> yield row <EOL> def databases ( self ) : <EOL> with self . conn . cursor ( ) as cur : <EOL> _logger . debug ( '<STR_LIT>' , self . databases_query ) <EOL> cur . execute ( self . databases_query ) <EOL> return [ x [ <NUM_LIT:0> ] for x in cur . fetchall ( ) ] <EOL> def functions ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . conn . cursor ( ) as cur : <EOL> _logger . debug ( '<STR_LIT>' , self . functions_query ) <EOL> cur . execute ( self . functions_query % self . dbname ) <EOL> for row in cur : <EOL> yield row <EOL> def show_candidates ( self ) : <EOL> with self . conn . cursor ( ) as cur : <EOL> _logger . debug ( '<STR_LIT>' , self . show_candidates_query ) <EOL> try : <EOL> cur . execute ( self . show_candidates_query ) <EOL> except pymysql . OperationalError as e : <EOL> _logger . error ( '<STR_LIT>' , e ) <EOL> yield '<STR_LIT>' <EOL> else : <EOL> for row in cur : <EOL> yield ( row [ <NUM_LIT:0> ] . split ( None , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] , ) <EOL> def users ( self ) : <EOL> with self . conn . cursor ( ) as cur : <EOL> _logger . debug ( '<STR_LIT>' , self . users_query ) <EOL> try : <EOL> cur . execute ( self . users_query ) <EOL> except pymysql . OperationalError as e : <EOL> _logger . error ( '<STR_LIT>' , e ) <EOL> yield '<STR_LIT>' <EOL> else : <EOL> for row in cur : <EOL> yield row <EOL> def server_type ( self ) : <EOL> if self . _server_type : <EOL> return self . _server_type <EOL> with self . conn . cursor ( ) as cur : <EOL> _logger . debug ( '<STR_LIT>' , self . version_query ) <EOL> cur . execute ( self . version_query ) <EOL> version = cur . fetchone ( ) [ <NUM_LIT:0> ] <EOL> _logger . debug ( '<STR_LIT>' , self . version_comment_query ) <EOL> cur . execute ( self . version_comment_query ) <EOL> version_comment = cur . fetchone ( ) [ <NUM_LIT:0> ] . lower ( ) <EOL> if '<STR_LIT>' in version_comment : <EOL> product_type = '<STR_LIT>' <EOL> elif '<STR_LIT>' in version_comment : <EOL> product_type = '<STR_LIT>' <EOL> else : <EOL> product_type = '<STR_LIT>' <EOL> self . _server_type = ( product_type , version ) <EOL> return self . _server_type </s>
