Exemple #1
0
    def reset(self):
        """
        Reset mssqlcli client with a new sql tools service and connection.
        """
        try:
            self.sqltoolsclient.shutdown()
            self.sqltoolsclient = SqlToolsClient()

            self.mssqlcliclient_main = self.mssqlcliclient_main.clone(
                self.sqltoolsclient)

            if not self.mssqlcliclient_main.connect_to_database():
                click.secho(
                    'Unable reconnect to server {0}; database {1}.'.format(
                        self.mssqlcliclient_main.server_name,
                        self.mssqlcliclient_main.connected_database),
                    err=True,
                    fg='yellow')

                self.logger.info(
                    u'Unable to reset connection to server {0}; database {1}'.
                    format(self.mssqlcliclient_main.server_name,
                           self.mssqlcliclient_main.connected_database))
                exit(1)
        except Exception as e:
            self.logger.error(u'Error in reset : {0}'.format(e.message))
            raise e
Exemple #2
0
def reset_connection_and_clients(sql_tools_client, *mssqlcliclients):
    """
    Restarts the sql_tools_client and establishes new connections for each of the
    mssqlcliclients passed
    """
    try:
        sql_tools_client.shutdown()
        new_tools_client = SqlToolsClient()
        for mssqlcliclient in mssqlcliclients:
            mssqlcliclient.sql_tools_client = new_tools_client
            mssqlcliclient.is_connected = False
            mssqlcliclient.owner_uri = generate_owner_uri()
            if not mssqlcliclient.connect():
                click.secho(
                    'Unable reconnect to server {0}; database {1}.'.format(
                        mssqlcliclient.server_name, mssqlcliclient.database),
                    err=True,
                    fg='red')
                logger.info(
                    u'Unable to reset connection to server {0}; database {1}'.
                    format(mssqlcliclient.server_name,
                           mssqlcliclient.database))
                exit(1)

    except Exception as e:
        logger.error(u'Error in reset_connection : {0}'.format(e.message))
        raise e
Exemple #3
0
    def reset(self):
        """
        Reset mssqlcli client with a new sql tools service and connection.
        """
        self.sqltoolsclient.shutdown()
        self.sqltoolsclient = SqlToolsClient()

        self.mssqlcliclient_main = self.mssqlcliclient_main.clone(
            self.sqltoolsclient)

        database_response = self.mssqlcliclient_main.connect_to_database()
        if not database_response:
            click.secho('Unable reconnect to server %s; database %s.' %
                        (self.mssqlcliclient_main.server_name,
                         self.mssqlcliclient_main.connected_database),
                        err=True,
                        fg='yellow')

            self.logger.info(
                u'Unable to reset connection to server %s; database %s',
                self.mssqlcliclient_main.server_name,
                self.mssqlcliclient_main.connected_database)

            sys.exit(1)
        else:
            owner_uri, error_messages = database_response
            if not owner_uri and error_messages:
                # can occur if database credentials change during reset
                self.logger.error(u'Error in reset : %s', error_messages)
                raise ConnectionResetError(error_messages)
Exemple #4
0
    def __init__(self, options):

        # Load config.
        c = self.config = get_config(options.mssqlclirc_file)

        self.initialize_logging()
        self.logger = logging.getLogger(u'mssqlcli.main')

        self.set_default_pager(c)
        self.output_file = None

        self.multi_line = c['main'].as_bool('multi_line')
        self.multiline_mode = c['main'].get('multi_line_mode', 'tsql')
        self.vi_mode = c['main'].as_bool('vi')
        self.auto_expand = options.auto_vertical_output or c['main']['expand'] == 'auto'
        self.expanded_output = c['main']['expand'] == 'always'
        self.prompt_format = options.prompt or c['main'].get('prompt', self.default_prompt)
        if options.row_limit is not None:
            self.row_limit = options.row_limit
        else:
            self.row_limit = c['main'].as_int('row_limit')

        self.min_num_menu_lines = c['main'].as_int('min_num_menu_lines')
        self.multiline_continuation_char = c['main']['multiline_continuation_char']
        self.table_format = c['main']['table_format']
        self.syntax_style = c['main']['syntax_style']
        self.cli_style = c['colors']
        self.wider_completion_menu = c['main'].as_bool('wider_completion_menu')
        self.less_chatty = bool(
            options.less_chatty) or c['main'].as_bool('less_chatty')
        self.null_string = c['main'].get('null_string', '<null>')
        self.on_error = c['main']['on_error'].upper()
        self.decimal_format = c['data_formats']['decimal']
        self.float_format = c['data_formats']['float']

        self.now = dt.datetime.today()

        self.completion_refresher = CompletionRefresher()

        self.query_history = []

        # Initialize completer
        smart_completion = True if c['main'].get('smart_completion', 'True') == 'True' else False
        keyword_casing = c['main']['keyword_casing']
        self.settings = {
            'casing_file': get_casing_file(c),
            'generate_casing_file': c['main'].as_bool('generate_casing_file'),
            'generate_aliases': c['main'].as_bool('generate_aliases'),
            'asterisk_column_order': c['main']['asterisk_column_order'],
            'qualify_columns': c['main']['qualify_columns'],
            'case_column_headers': c['main'].as_bool('case_column_headers'),
            'search_path_filter': c['main'].as_bool('search_path_filter'),
            'single_connection': False,
            'less_chatty': options.less_chatty,
            'keyword_casing': keyword_casing,
        }

        self.completer = MssqlCompleter(smart_completion=smart_completion, settings=self.settings)
        self._completer_lock = threading.Lock()

        self.eventloop = create_eventloop()
        self.cli = None
        self.integrated_auth = options.integrated_auth

        self.sqltoolsclient = SqlToolsClient(enable_logging=options.enable_sqltoolsservice_logging)
        self.mssqlcliclient_main = MssqlCliClient(options, self.sqltoolsclient)
Exemple #5
0
    def __init__(self, options):

        # Load config.
        c = self.config = get_config(options.mssqlclirc_file)

        self.initialize_logging()
        self.logger = logging.getLogger(u'mssqlcli.main')

        self.interactive_mode = options.interactive_mode

        self.table_format = c['main']['table_format']
        self.decimal_format = c['data_formats']['decimal']
        self.float_format = c['data_formats']['float']
        self.null_string = c['main'].get('null_string', '<null>')
        self.expanded_output = c['main']['expand'] == 'always'
        self.integrated_auth = options.integrated_auth
        self.less_chatty = bool(options.less_chatty) or c['main'].as_bool(
            'less_chatty') or self.interactive_mode

        keyword_casing = c['main']['keyword_casing']
        self.settings = {
            'casing_file': get_casing_file(c),
            'generate_casing_file': c['main'].as_bool('generate_casing_file'),
            'generate_aliases': c['main'].as_bool('generate_aliases'),
            'asterisk_column_order': c['main']['asterisk_column_order'],
            'qualify_columns': c['main']['qualify_columns'],
            'case_column_headers': c['main'].as_bool('case_column_headers'),
            'search_path_filter': c['main'].as_bool('search_path_filter'),
            'single_connection': False,
            'less_chatty': self.less_chatty,
            'keyword_casing': keyword_casing,
        }

        if self.interactive_mode:
            pager = self.set_default_pager(c)
            self.prompt_session = None

            # set auto_expand to false if less is detected with auto expand
            self.auto_expand = options.auto_vertical_output \
                or (c['main']['expand'] == 'auto' and pager != 'less -SRXF')
            self.multiline = c['main'].as_bool('multi_line')
            self.multiline_mode = c['main'].get('multi_line_mode', 'tsql')
            self.vi_mode = c['main'].as_bool('vi')
            self.prompt_format = options.prompt or c['main'].get(
                'prompt', self.default_prompt)
            if options.row_limit is not None:
                self.row_limit = options.row_limit
            else:
                self.row_limit = c['main'].as_int('row_limit')

            self.min_num_menu_lines = c['main'].as_int('min_num_menu_lines')
            self.multiline_continuation_char = c['main'][
                'multiline_continuation_char']
            self.syntax_style = c['main']['syntax_style']
            self.cli_style = c['colors']
            self.output_style = style_factory_output(self.syntax_style,
                                                     self.cli_style)
            self.wider_completion_menu = c['main'].as_bool(
                'wider_completion_menu')
            self.on_error = c['main']['on_error'].upper()

            self.now = dt.datetime.today()

            self.completion_refresher = CompletionRefresher()

            self.query_history = []

            # Initialize completer
            smart_completion = c['main'].get('smart_completion',
                                             'True').lower() == 'true'
            self.completer = MssqlCompleter(smart_completion=smart_completion,
                                            settings=self.settings)
            self._completer_lock = threading.Lock()

        # input and output file are for non-interactive mode
        self.input_file = options.input_file
        self.output_file = options.output_file

        self.query = options.query

        self.sqltoolsclient = SqlToolsClient(
            enable_logging=options.enable_sqltoolsservice_logging)
        self.mssqlcliclient_main = MssqlCliClient(options, self.sqltoolsclient)

        # exit and return error if user enters interactive mode with -i or -o arguments enabled
        if self.interactive_mode and (self.input_file or self.output_file):
            raise ValueError(
                "Invalid arguments: -i and -o can only be used in non-interactive "
                "mode.")

        # exit and return error if both query text and an input file are specified
        if self.query and self.input_file:
            raise ValueError(
                "Invalid arguments: either query [-Q] or input file [-i] may be "
                "specified.")
Exemple #6
0
    def __init__(self,
                 force_passwd_prompt=False,
                 mssqlclirc_file=None,
                 row_limit=None,
                 single_connection=False,
                 less_chatty=None,
                 auto_vertical_output=False,
                 sql_tools_client=None,
                 integrated_auth=False,
                 enable_sqltoolsservice_logging=False):

        self.force_passwd_prompt = force_passwd_prompt

        # Load config.
        c = self.config = get_config(mssqlclirc_file)

        self.logger = logging.getLogger(u'mssqlcli.main')
        self.initialize_logging()

        self.set_default_pager(c)
        self.output_file = None

        self.multi_line = c['main'].as_bool('multi_line')
        self.multiline_mode = c['main'].get('multi_line_mode', 'tsql')
        self.vi_mode = c['main'].as_bool('vi')
        self.auto_expand = auto_vertical_output or c['main']['expand'] == 'auto'
        self.expanded_output = c['main']['expand'] == 'always'
        if row_limit is not None:
            self.row_limit = row_limit
        else:
            self.row_limit = c['main'].as_int('row_limit')

        self.min_num_menu_lines = c['main'].as_int('min_num_menu_lines')
        self.multiline_continuation_char = c['main'][
            'multiline_continuation_char']
        self.table_format = c['main']['table_format']
        self.syntax_style = c['main']['syntax_style']
        self.cli_style = c['colors']
        self.wider_completion_menu = c['main'].as_bool('wider_completion_menu')
        self.less_chatty = bool(less_chatty) or c['main'].as_bool(
            'less_chatty')
        self.null_string = c['main'].get('null_string', '<null>')
        self.on_error = c['main']['on_error'].upper()
        self.decimal_format = c['data_formats']['decimal']
        self.float_format = c['data_formats']['float']

        self.now = dt.datetime.today()

        self.completion_refresher = CompletionRefresher()

        self.query_history = []

        # Initialize completer
        # Smart completion is not-supported in Public Preview. Tracked by
        # GitHub issue number 47.
        smart_completion = False
        keyword_casing = c['main']['keyword_casing']
        self.settings = {
            'casing_file': get_casing_file(c),
            'generate_casing_file': c['main'].as_bool('generate_casing_file'),
            'generate_aliases': c['main'].as_bool('generate_aliases'),
            'asterisk_column_order': c['main']['asterisk_column_order'],
            'qualify_columns': c['main']['qualify_columns'],
            'case_column_headers': c['main'].as_bool('case_column_headers'),
            'search_path_filter': c['main'].as_bool('search_path_filter'),
            'single_connection': single_connection,
            'less_chatty': less_chatty,
            'keyword_casing': keyword_casing,
        }

        completer = MssqlCompleter(smart_completion, settings=self.settings)

        self.completer = completer
        self._completer_lock = threading.Lock()

        self.eventloop = create_eventloop()
        self.cli = None

        # mssql-cli
        self.sqltoolsclient = sql_tools_client if sql_tools_client else SqlToolsClient(
            enable_logging=enable_sqltoolsservice_logging)
        self.mssqlcliclient_query_execution = None
        self.integrated_auth = integrated_auth