<s> import os <EOL> from retriever . lib . models import Engine , no_cleanup <EOL> class engine ( Engine ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> abbreviation = "<STR_LIT>" <EOL> datatypes = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:int>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:bool>" : "<STR_LIT>" , <EOL> } <EOL> max_int = <NUM_LIT> <EOL> required_opts = [ ( "<STR_LIT:user>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) , <EOL> ( "<STR_LIT:password>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) , <EOL> ( "<STR_LIT:host>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:localhost>" ) , <EOL> ( "<STR_LIT:port>" , <EOL> "<STR_LIT>" , <EOL> <NUM_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> "<STR_LIT>" ) , <EOL> ] <EOL> def create_db_statement ( self ) : <EOL> """<STR_LIT>""" <EOL> return Engine . create_db_statement ( self ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def create_db ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> Engine . create_db ( self ) <EOL> except : <EOL> self . connection . rollback ( ) <EOL> pass <EOL> def create_table ( self ) : <EOL> """<STR_LIT>""" <EOL> Engine . create_table ( self ) <EOL> self . connection . commit ( ) <EOL> def drop_statement ( self , objecttype , objectname ) : <EOL> """<STR_LIT>""" <EOL> statement = Engine . drop_statement ( self , objecttype , objectname ) <EOL> statement += "<STR_LIT>" <EOL> return statement . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def escape_single_quotes ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) <EOL> def insert_data_from_file ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> self . get_cursor ( ) <EOL> ct = len ( [ True for c in self . table . columns if c [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ : <NUM_LIT:3> ] == "<STR_LIT>" ] ) != <NUM_LIT:0> <EOL> if ( ( [ self . table . cleanup . function , self . table . delimiter , <EOL> self . table . header_rows ] == [ no_cleanup , "<STR_LIT:U+002C>" , <NUM_LIT:1> ] ) <EOL> and not self . table . fixed_width <EOL> and not ct <EOL> and ( not hasattr ( self . table , "<STR_LIT>" ) or not self . table . do_not_bulk_insert ) ) : <EOL> columns = self . table . get_insert_columns ( ) <EOL> filename = os . path . abspath ( filename ) <EOL> statement = """<STR_LIT>""" + self . table_name ( ) + "<STR_LIT>" + columns + """<STR_LIT>""" + filename . replace ( "<STR_LIT:\\>" , "<STR_LIT>" ) + """<STR_LIT>""" <EOL> try : <EOL> self . execute ( "<STR_LIT>" ) <EOL> self . execute ( statement ) <EOL> self . execute ( "<STR_LIT>" ) <EOL> except : <EOL> self . connection . rollback ( ) <EOL> return Engine . insert_data_from_file ( self , filename ) <EOL> else : <EOL> return Engine . insert_data_from_file ( self , filename ) <EOL> def insert_statement ( self , values ) : <EOL> """<STR_LIT>""" <EOL> statement = Engine . insert_statement ( self , values ) <EOL> if isinstance ( statement , basestring ) : <EOL> statement = statement . decode ( "<STR_LIT:utf-8>" , "<STR_LIT:ignore>" ) <EOL> return statement <EOL> def table_exists ( self , dbname , tablename ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . cursor . execute ( <EOL> "<STR_LIT>" ) <EOL> self . existing_table_names = set ( ) <EOL> for schema , table in self . cursor : <EOL> self . existing_table_names . add ( ( schema . lower ( ) , table . lower ( ) ) ) <EOL> return ( dbname . lower ( ) , tablename . lower ( ) ) in self . existing_table_names <EOL> def format_insert_value ( self , value , datatype ) : <EOL> """<STR_LIT>""" <EOL> if datatype == "<STR_LIT:bool>" : <EOL> try : <EOL> if int ( value ) == <NUM_LIT:1> : <EOL> return "<STR_LIT>" <EOL> elif int ( value ) == <NUM_LIT:0> : <EOL> return "<STR_LIT>" <EOL> except : <EOL> pass <EOL> return Engine . format_insert_value ( self , value , datatype ) <EOL> def get_connection ( self ) : <EOL> """<STR_LIT>""" <EOL> import psycopg2 as dbapi <EOL> self . get_input ( ) <EOL> return dbapi . connect ( host = self . opts [ "<STR_LIT:host>" ] , <EOL> port = int ( self . opts [ "<STR_LIT:port>" ] ) , <EOL> user = self . opts [ "<STR_LIT:user>" ] , <EOL> password = self . opts [ "<STR_LIT:password>" ] , <EOL> database = self . opts [ "<STR_LIT>" ] ) </s>
