<s> import json <EOL> import logging <EOL> import sys <EOL> from redash . query_runner import * <EOL> from redash . utils import JSONEncoder <EOL> logger = logging . getLogger ( __name__ ) <EOL> try : <EOL> import pymssql <EOL> enabled = True <EOL> except ImportError : <EOL> enabled = False <EOL> types_map = { <EOL> <NUM_LIT:1> : TYPE_STRING , <EOL> <NUM_LIT:2> : TYPE_BOOLEAN , <EOL> <NUM_LIT:3> : TYPE_INTEGER , <EOL> <NUM_LIT:4> : TYPE_DATETIME , <EOL> <NUM_LIT:5> : TYPE_FLOAT , <EOL> } <EOL> class SqlServer ( BaseSQLQueryRunner ) : <EOL> @ classmethod <EOL> def configuration_schema ( cls ) : <EOL> return { <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:user>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT:password>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" , <EOL> "<STR_LIT:default>" : "<STR_LIT:127.0.0.1>" <EOL> } , <EOL> "<STR_LIT:port>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:default>" : <NUM_LIT> <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" , <EOL> "<STR_LIT:title>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT:password>" ] <EOL> } <EOL> @ classmethod <EOL> def enabled ( cls ) : <EOL> return enabled <EOL> @ classmethod <EOL> def name ( cls ) : <EOL> return "<STR_LIT>" <EOL> @ classmethod <EOL> def type ( cls ) : <EOL> return "<STR_LIT>" <EOL> def __init__ ( self , configuration ) : <EOL> super ( SqlServer , self ) . __init__ ( configuration ) <EOL> def _get_tables ( self , schema ) : <EOL> query = """<STR_LIT>""" <EOL> results , error = self . run_query ( query ) <EOL> if error is not None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> results = json . loads ( results ) <EOL> for row in results [ '<STR_LIT>' ] : <EOL> if row [ '<STR_LIT>' ] != self . configuration [ '<STR_LIT>' ] : <EOL> table_name = '<STR_LIT>' . format ( row [ '<STR_LIT>' ] , row [ '<STR_LIT>' ] ) <EOL> else : <EOL> table_name = row [ '<STR_LIT>' ] <EOL> if table_name not in schema : <EOL> schema [ table_name ] = { '<STR_LIT:name>' : table_name , '<STR_LIT>' : [ ] } <EOL> schema [ table_name ] [ '<STR_LIT>' ] . append ( row [ '<STR_LIT>' ] ) <EOL> return schema . values ( ) <EOL> def run_query ( self , query ) : <EOL> connection = None <EOL> try : <EOL> server = self . configuration . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> user = self . configuration . get ( '<STR_LIT:user>' , '<STR_LIT>' ) <EOL> password = self . configuration . get ( '<STR_LIT:password>' , '<STR_LIT>' ) <EOL> db = self . configuration [ '<STR_LIT>' ] <EOL> port = self . configuration . get ( '<STR_LIT:port>' , <NUM_LIT> ) <EOL> if port != <NUM_LIT> : <EOL> server = server + '<STR_LIT::>' + str ( port ) <EOL> connection = pymssql . connect ( server , user , password , db ) <EOL> cursor = connection . cursor ( ) <EOL> logger . debug ( "<STR_LIT>" , query ) <EOL> cursor . execute ( query ) <EOL> data = cursor . fetchall ( ) <EOL> if cursor . description is not None : <EOL> columns = self . fetch_columns ( [ ( i [ <NUM_LIT:0> ] , types_map . get ( i [ <NUM_LIT:1> ] , None ) ) for i in cursor . description ] ) <EOL> rows = [ dict ( zip ( ( c [ '<STR_LIT:name>' ] for c in columns ) , row ) ) for row in data ] <EOL> data = { '<STR_LIT>' : columns , '<STR_LIT>' : rows } <EOL> json_data = json . dumps ( data , cls = JSONEncoder ) <EOL> error = None <EOL> else : <EOL> error = "<STR_LIT>" <EOL> json_data = None <EOL> cursor . close ( ) <EOL> except pymssql . Error as e : <EOL> logging . exception ( e ) <EOL> try : <EOL> error = e . args [ <NUM_LIT:1> ] <EOL> except IndexError : <EOL> error = e . args [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> json_data = None <EOL> except KeyboardInterrupt : <EOL> connection . cancel ( ) <EOL> error = "<STR_LIT>" <EOL> json_data = None <EOL> except Exception as e : <EOL> raise sys . exc_info ( ) [ <NUM_LIT:1> ] , None , sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> finally : <EOL> if connection : <EOL> connection . close ( ) <EOL> return json_data , error <EOL> register ( SqlServer ) </s>
