def run_cli_with(options): if create_config_dir_for_first_use(): display_telemetry_message() display_version_message(options) display_integrated_auth_message_for_non_windows(options) configure_and_update_options(options) mssqlcli = MssqlCli(options) mssqlcli.connect_to_database() telemetry_session.set_server_information(mssqlcli.mssqlcliclient_main) mssqlcli.run()
def run_cli_with(options): if create_config_dir_for_first_use(): display_telemetry_message() display_version_message(options) configure_and_update_options(options) # Importing MssqlCli creates a config dir by default. # Moved import here so we can create the config dir for first use prior. from mssqlcli.mssql_cli import MssqlCli mssqlcli = MssqlCli(options) mssqlcli.connect_to_database() telemetry_session.set_server_information(mssqlcli.mssqlcliclient_main) mssqlcli.run()
def run_cli_with(options): if create_config_dir_for_first_use(): display_telemetry_message() display_version_message(options) configure_and_update_options(options) # Importing MssqlCli creates a config dir by default. # Moved import here so we can create the config dir for first use prior. # pylint: disable=import-outside-toplevel from mssqlcli.mssql_cli import MssqlCli # set interactive mode to false if -Q or -i is specified if options.query or options.input_file: options.interactive_mode = False mssqlcli = MssqlCli(options) try: mssqlcli.connect_to_database() telemetry_session.set_server_information(mssqlcli.mssqlcliclient_main) if mssqlcli.interactive_mode: mssqlcli.run() else: text = options.query if options.input_file: # get query text from input file try: if six.PY2: with io.open(options.input_file, 'r', encoding='utf-8') as f: text = f.read() else: with open(options.input_file, 'r', encoding='utf-8') as f: text = f.read() except OSError as e: click.secho(str(e), err=True, fg='red') sys.exit(1) mssqlcli.execute_query(text) finally: mssqlcli.shutdown()
def connect(self, database='', server='', user='', port='', passwd='', dsn='', encrypt=None, trust_server_certificate=None, connection_timeout=None, application_intent=None, multi_subnet_failover=None, packet_size=None, **kwargs): # Connect to the database. if not user and not self.integrated_auth: user = click.prompt('Username (press enter for sa)', default=u'sa', show_default=False) if not server: server = u'localhost' if not database: database = u'master' # If password prompt is not forced but no password is provided, try # getting it from environment variable. if not self.force_passwd_prompt and not passwd: passwd = os.environ.get('MSSQL_CLI_PASSWORD', '') if not self.integrated_auth: # Prompt for a password immediately if requested via the -W flag. This # avoids wasting time trying to connect to the database and catching a # no-password exception. # If we successfully parsed a password from a URI, there's no need to # prompt for it, even with the -W flag if self.force_passwd_prompt and not passwd: passwd = click.prompt('Password', hide_input=True, show_default=False, type=str) if not passwd: passwd = click.prompt('Password', hide_input=True, show_default=False, type=str) # Attempt to connect to the database. # Note that passwd may be empty on the first attempt. In this case try # integrated auth. try: # mssql-cli authentication_type = u'SqlLogin' if self.integrated_auth: authentication_type = u'Integrated' self.mssqlcliclient_query_execution = MssqlCliClient( self.sqltoolsclient, server, user, passwd, database=database, authentication_type=authentication_type, encrypt=encrypt, trust_server_certificate=trust_server_certificate, connection_timeout=connection_timeout, application_intent=application_intent, multi_subnet_failover=multi_subnet_failover, packet_size=packet_size, **kwargs) if not self.mssqlcliclient_query_execution.connect(): click.secho('\nUnable to connect. Please try again', err=True, fg='red') exit(1) telemetry_session.set_server_information( self.mssqlcliclient_query_execution) except Exception as e: # Connecting to a database could fail. self.logger.debug('Database connection failed: %r.', e) self.logger.error("traceback: %r", traceback.format_exc()) click.secho(str(e), err=True, fg='red') exit(1)