def generate_query_baseline(file_name): sqltoolsservice_args = [mssqltoolsservice.get_executable_path()] with io.open(file_name, 'wb') as baseline: tools_service_process = subprocess.Popen(sqltoolsservice_args, bufsize=0, stdin=subprocess.PIPE, stdout=baseline) # Before running this script, enter a real server name. parameters = { u'OwnerUri': u'connectionservicetest', u'Connection': { u'ServerName': u'*', u'DatabaseName': u'AdventureWorks2014', u'UserName': u'*', u'Password': u'*', u'AuthenticationType': u'Integrated' } } writer = json_rpc_client.JsonRpcWriter(tools_service_process.stdin) writer.send_request(u'connection/connect', parameters, id=1) time.sleep(2) parameters = { u'OwnerUri': u'connectionservicetest', u'Query': "select * from HumanResources.Department" } writer.send_request(u'query/executeString', parameters, id=2) time.sleep(5) parameters = { u'OwnerUri': u'connectionservicetest', u'BatchIndex': 0, u'ResultSetIndex': 0, u'RowsStartIndex': 0, u'RowsCount': 16 } writer.send_request(u'query/subset', parameters, id=3) # submit raw request. time.sleep(5) tools_service_process.kill()
def __init__(self, input_stream=None, output_stream=None, enable_logging=False): """ Initializes the sql tools client. Input and output streams for JsonRpcClient are taken as optional params, Else a SqlToolsService process is started and its stdin and stdout is used. """ self.current_id = uuid.uuid4().int self.tools_service_process = None sqltoolsservice_args = [mssqltoolsservice.get_executable_path()] if enable_logging: sqltoolsservice_args.append('--enable-logging') sqltoolsservice_args.append('--log-dir') sqltoolsservice_args.append(config_location()) if input_stream and output_stream: self.json_rpc_client = json_rpc_client.JsonRpcClient( input_stream, output_stream) else: self.tools_service_process = subprocess.Popen( sqltoolsservice_args, bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE) self.json_rpc_client = json_rpc_client.JsonRpcClient( self.tools_service_process.stdin, io.open(self.tools_service_process.stdout.fileno(), u'rb', buffering=0, closefd=False)) logger.info(u'SqlToolsService process id: {0}'.format( self.tools_service_process.pid)) self.json_rpc_client.start() logger.info(u'Sql Tools Client Initialized')
# Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- import logging import subprocess import io import time import sys import mssqlcli.mssqltoolsservice as mssqltoolsservice import mssqlcli.jsonrpc.jsonrpcclient as json_rpc_client import mssqlcli.jsonrpc.contracts.connectionservice as connection import mssqlcli.jsonrpc.contracts.queryexecutestringservice as query logger = logging.getLogger(u'mssqlcli.sqltoolsclient') sqltoolsservice_args = [mssqltoolsservice.get_executable_path()] class SqlToolsClient(object): """ Create sql tools service requests. """ def __init__(self, input_stream=None, output_stream=None): """ Initializes the sql tools client. Input and output streams for JsonRpcClient are taken as optional params, Else a SqlToolsService process is started and its stdin and stdout is used. """ self.current_id = 1 self.tools_service_process = None