<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> from json import JSONEncoder , loads <EOL> try : <EOL> import pymssql <EOL> HAS_ALL_IMPORTS = True <EOL> except ImportError : <EOL> HAS_ALL_IMPORTS = False <EOL> _DEFAULTS = { <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT:port>' : <NUM_LIT> , <EOL> '<STR_LIT:user>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False <EOL> } <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if HAS_ALL_IMPORTS : <EOL> return True <EOL> return ( False , '<STR_LIT>' ) <EOL> def _get_connection ( ** kwargs ) : <EOL> connection_args = { } <EOL> for arg in ( '<STR_LIT>' , '<STR_LIT:port>' , '<STR_LIT:user>' , '<STR_LIT:password>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if arg in kwargs : <EOL> connection_args [ arg ] = kwargs [ arg ] <EOL> else : <EOL> connection_args [ arg ] = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' + arg , _DEFAULTS . get ( arg , None ) ) <EOL> return pymssql . connect ( ** connection_args ) <EOL> class _MssqlEncoder ( JSONEncoder ) : <EOL> def default ( self , o ) : <EOL> return str ( o ) <EOL> def tsql_query ( query , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> cur = _get_connection ( ** kwargs ) . cursor ( ) <EOL> cur . execute ( query ) <EOL> return loads ( _MssqlEncoder ( ) . encode ( { '<STR_LIT>' : cur . fetchall ( ) } ) ) [ '<STR_LIT>' ] <EOL> except Exception as e : <EOL> return ( ( '<STR_LIT>' , ) , ( str ( e ) , ) ) <EOL> def version ( ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> return tsql_query ( '<STR_LIT>' , ** kwargs ) <EOL> def db_list ( ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> return [ row [ <NUM_LIT:0> ] for row in tsql_query ( '<STR_LIT>' , as_dict = False , ** kwargs ) ] <EOL> def db_exists ( database_name , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> return len ( tsql_query ( "<STR_LIT>" . format ( database_name ) , ** kwargs ) ) == <NUM_LIT:1> <EOL> def db_remove ( database_name , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if db_exists ( database_name ) and database_name not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> conn = _get_connection ( ** kwargs ) <EOL> conn . autocommit ( True ) <EOL> cur = conn . cursor ( ) <EOL> cur . execute ( '<STR_LIT>' . format ( database_name ) ) <EOL> cur . execute ( '<STR_LIT>' . format ( database_name ) ) <EOL> conn . autocommit ( False ) <EOL> conn . close ( ) <EOL> return True <EOL> else : <EOL> return False <EOL> except Exception as e : <EOL> return '<STR_LIT>' . format ( e ) <EOL> def role_list ( ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> return tsql_query ( query = '<STR_LIT>' , as_dict = True , ** kwargs ) <EOL> def role_exists ( role , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> return len ( tsql_query ( query = '<STR_LIT>' . format ( role ) , as_dict = True , ** kwargs ) ) == <NUM_LIT:1> <EOL> def role_create ( role , owner = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> conn = _get_connection ( ** kwargs ) <EOL> conn . autocommit ( True ) <EOL> cur = conn . cursor ( ) <EOL> if owner : <EOL> cur . execute ( '<STR_LIT>' . format ( role , owner ) ) <EOL> else : <EOL> cur . execute ( '<STR_LIT>' . format ( role ) ) <EOL> conn . autocommit ( True ) <EOL> conn . close ( ) <EOL> return True <EOL> except Exception as e : <EOL> return '<STR_LIT>' . format ( e ) <EOL> def role_remove ( role , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> conn = _get_connection ( ** kwargs ) <EOL> conn . autocommit ( True ) <EOL> cur = conn . cursor ( ) <EOL> cur . execute ( '<STR_LIT>' . format ( role ) ) <EOL> conn . autocommit ( True ) <EOL> conn . close ( ) <EOL> return True <EOL> except Exception as e : <EOL> return '<STR_LIT>' . format ( e ) <EOL> def login_exists ( login , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return len ( tsql_query ( query = "<STR_LIT>" . format ( login ) , ** kwargs ) ) == <NUM_LIT:1> <EOL> except Exception as e : <EOL> return '<STR_LIT>' . format ( e ) <EOL> def user_exists ( username , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' not in kwargs : <EOL> return False <EOL> return len ( tsql_query ( query = "<STR_LIT>" . format ( username ) , ** kwargs ) ) == <NUM_LIT:1> <EOL> def user_list ( ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> return [ row [ <NUM_LIT:0> ] for row in tsql_query ( "<STR_LIT>" , as_dict = False , ** kwargs ) ] <EOL> def user_create ( username , new_login_password = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' not in kwargs : <EOL> return False <EOL> if user_exists ( username , ** kwargs ) : <EOL> return False <EOL> try : <EOL> conn = _get_connection ( ** kwargs ) <EOL> conn . autocommit ( True ) <EOL> cur = conn . cursor ( ) <EOL> if new_login_password : <EOL> if login_exists ( username , ** kwargs ) : <EOL> conn . close ( ) <EOL> return False <EOL> cur . execute ( "<STR_LIT>" . format ( username , new_login_password ) ) <EOL> cur . execute ( "<STR_LIT>" . format ( username , username ) ) <EOL> else : <EOL> cur . execute ( "<STR_LIT>" . format ( username ) ) <EOL> conn . autocommit ( False ) <EOL> conn . close ( ) <EOL> return True <EOL> except Exception as e : <EOL> return '<STR_LIT>' . format ( e ) <EOL> def user_remove ( username , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' not in kwargs : <EOL> return False <EOL> try : <EOL> conn = _get_connection ( ** kwargs ) <EOL> conn . autocommit ( True ) <EOL> cur = conn . cursor ( ) <EOL> cur . execute ( "<STR_LIT>" . format ( username ) ) <EOL> conn . autocommit ( False ) <EOL> conn . close ( ) <EOL> return True <EOL> except Exception as e : <EOL> return '<STR_LIT>' . format ( e ) </s>
