<s> import logging <EOL> from django . utils . translation import ugettext as _ <EOL> from desktop . lib . exceptions_renderable import PopupException <EOL> from desktop . lib . i18n import force_unicode <EOL> from librdbms . jdbc import Jdbc , query_and_fetch <EOL> from notebook . connectors . base import Api , QueryError , AuthenticationRequired <EOL> LOG = logging . getLogger ( __name__ ) <EOL> API_CACHE = { } <EOL> def query_error_handler ( func ) : <EOL> def decorator ( * args , ** kwargs ) : <EOL> try : <EOL> return func ( * args , ** kwargs ) <EOL> except AuthenticationRequired , e : <EOL> raise e <EOL> except Exception , e : <EOL> message = force_unicode ( str ( e ) ) <EOL> if '<STR_LIT>' in message : <EOL> raise QueryError ( _ ( '<STR_LIT>' ) % message ) <EOL> else : <EOL> raise QueryError ( message ) <EOL> return decorator <EOL> class JdbcApi ( Api ) : <EOL> def __init__ ( self , user , interpreter = None ) : <EOL> global API_CACHE <EOL> Api . __init__ ( self , user , interpreter = interpreter ) <EOL> self . db = None <EOL> self . options = interpreter [ '<STR_LIT>' ] <EOL> if self . cache_key in API_CACHE : <EOL> self . db = API_CACHE [ self . cache_key ] <EOL> elif '<STR_LIT:password>' in self . options : <EOL> username = self . options . get ( '<STR_LIT:user>' ) or user . username <EOL> self . db = API_CACHE [ self . cache_key ] = Jdbc ( self . options [ '<STR_LIT>' ] , self . options [ '<STR_LIT:url>' ] , username , self . options [ '<STR_LIT:password>' ] ) <EOL> def create_session ( self , lang = None , properties = None ) : <EOL> global API_CACHE <EOL> props = super ( JdbcApi , self ) . create_session ( lang , properties ) <EOL> properties = dict ( [ ( p [ '<STR_LIT:name>' ] , p [ '<STR_LIT:value>' ] ) for p in properties ] ) if properties is not None else { } <EOL> props [ '<STR_LIT>' ] = { } <EOL> if self . db is None : <EOL> if '<STR_LIT:password>' in properties : <EOL> user = properties . get ( '<STR_LIT:user>' ) or self . options . get ( '<STR_LIT:user>' ) <EOL> props [ '<STR_LIT>' ] = { '<STR_LIT:user>' : user } <EOL> self . db = API_CACHE [ self . cache_key ] = Jdbc ( self . options [ '<STR_LIT>' ] , self . options [ '<STR_LIT:url>' ] , user , properties . pop ( '<STR_LIT:password>' ) ) <EOL> if self . db is None : <EOL> raise AuthenticationRequired ( ) <EOL> return props <EOL> @ query_error_handler <EOL> def execute ( self , notebook , snippet ) : <EOL> if self . db is None : <EOL> raise AuthenticationRequired ( ) <EOL> data , description = query_and_fetch ( self . db , snippet [ '<STR_LIT>' ] , <NUM_LIT:1000> ) <EOL> has_result_set = data is not None <EOL> return { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : has_result_set , <EOL> '<STR_LIT:result>' : { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:data>' : data if has_result_set else [ ] , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:name>' : col [ <NUM_LIT:0> ] , <EOL> '<STR_LIT:type>' : col [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } for col in description ] if has_result_set else [ ] , <EOL> '<STR_LIT:type>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> @ query_error_handler <EOL> def check_status ( self , notebook , snippet ) : <EOL> return { '<STR_LIT:status>' : '<STR_LIT>' } <EOL> def _fetch_result ( self , cursor ) : <EOL> return { } <EOL> @ query_error_handler <EOL> def fetch_result_metadata ( self ) : <EOL> pass <EOL> @ query_error_handler <EOL> def cancel ( self , notebook , snippet ) : <EOL> return { '<STR_LIT:status>' : <NUM_LIT:0> } <EOL> def download ( self , notebook , snippet , format ) : <EOL> raise PopupException ( '<STR_LIT>' ) <EOL> def progress ( self , snippet , logs ) : <EOL> return <NUM_LIT:50> <EOL> @ query_error_handler <EOL> def close_statement ( self , snippet ) : <EOL> return { '<STR_LIT:status>' : - <NUM_LIT:1> } <EOL> @ query_error_handler <EOL> def autocomplete ( self , snippet , database = None , table = None , column = None , nested = None ) : <EOL> if self . db is None : <EOL> raise AuthenticationRequired ( ) <EOL> assist = Assist ( self . db ) <EOL> response = { '<STR_LIT:status>' : - <NUM_LIT:1> } <EOL> if database is None : <EOL> response [ '<STR_LIT>' ] = assist . get_databases ( ) <EOL> elif table is None : <EOL> response [ '<STR_LIT>' ] = assist . get_tables ( database ) <EOL> else : <EOL> columns = assist . get_columns ( database , table ) <EOL> response [ '<STR_LIT>' ] = [ col [ <NUM_LIT:0> ] for col in columns ] <EOL> response [ '<STR_LIT>' ] = [ { <EOL> '<STR_LIT:name>' : col [ <NUM_LIT:0> ] , <EOL> '<STR_LIT:type>' : col [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : col [ <NUM_LIT:5> ] <EOL> } for col in columns ] <EOL> response [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> return response <EOL> @ query_error_handler <EOL> def get_sample_data ( self , snippet , database = None , table = None , column = None ) : <EOL> if self . db is None : <EOL> raise AuthenticationRequired ( ) <EOL> assist = Assist ( self . db ) <EOL> response = { '<STR_LIT:status>' : - <NUM_LIT:1> } <EOL> sample_data , description = assist . get_sample_data ( database , table , column ) <EOL> if sample_data : <EOL> response [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> response [ '<STR_LIT>' ] = [ col [ <NUM_LIT:0> ] for col in description ] if description else [ ] <EOL> response [ '<STR_LIT>' ] = sample_data <EOL> else : <EOL> response [ '<STR_LIT:message>' ] = _ ( '<STR_LIT>' ) <EOL> return response <EOL> @ property <EOL> def cache_key ( self ) : <EOL> return '<STR_LIT>' % ( self . interpreter [ '<STR_LIT:name>' ] , self . user . username ) <EOL> class Assist ( ) : <EOL> def __init__ ( self , db ) : <EOL> self . db = db <EOL> def get_databases ( self ) : <EOL> databases , description = query_and_fetch ( self . db , '<STR_LIT>' ) <EOL> return databases <EOL> def get_tables ( self , database , table_names = [ ] ) : <EOL> tables , description = query_and_fetch ( self . db , '<STR_LIT>' ) <EOL> return tables <EOL> def get_columns ( self , database , table ) : <EOL> columns , description = query_and_fetch ( self . db , '<STR_LIT>' % ( database , table ) ) <EOL> return columns <EOL> def get_sample_data ( self , database , table , column = None ) : <EOL> column = column or '<STR_LIT:*>' <EOL> return query_and_fetch ( self . db , '<STR_LIT>' % ( column , database , table ) ) </s>
