Create sql tools service requests.
| 14 | |
| 15 | |
| 16 | class SqlToolsClient: |
| 17 | """ |
| 18 | Create sql tools service requests. |
| 19 | """ |
| 20 | CONNECTION_REQUEST = u'connection_request' |
| 21 | QUERY_EXECUTE_STRING_REQUEST = u'query_execute_string_request' |
| 22 | QUERY_SUBSET_REQUEST = u'query_subset_request' |
| 23 | |
| 24 | def __init__(self, input_stream=None, output_stream=None, enable_logging=False): |
| 25 | """ |
| 26 | Initializes the sql tools client. |
| 27 | Input and output streams for JsonRpcClient are taken as optional params, |
| 28 | Else a SqlToolsService process is started and its stdin and stdout is used. |
| 29 | """ |
| 30 | self.current_id = uuid.uuid4().int |
| 31 | self.tools_service_process = None |
| 32 | |
| 33 | sqltoolsservice_args = [mssqltoolsservice.get_executable_path()] |
| 34 | |
| 35 | if enable_logging: |
| 36 | sqltoolsservice_args.append('--enable-logging') |
| 37 | sqltoolsservice_args.append('--log-dir') |
| 38 | sqltoolsservice_args.append(config_location()) |
| 39 | |
| 40 | if input_stream and output_stream: |
| 41 | self.json_rpc_client = json_rpc_client.JsonRpcClient( |
| 42 | input_stream, output_stream) |
| 43 | else: |
| 44 | self.tools_service_process = subprocess.Popen( |
| 45 | sqltoolsservice_args, |
| 46 | bufsize=0, |
| 47 | stdin=subprocess.PIPE, |
| 48 | stdout=subprocess.PIPE) |
| 49 | |
| 50 | self.json_rpc_client = json_rpc_client.JsonRpcClient( |
| 51 | self.tools_service_process.stdin, |
| 52 | io.open( |
| 53 | self.tools_service_process.stdout.fileno(), |
| 54 | u'rb', |
| 55 | buffering=0, |
| 56 | closefd=False)) |
| 57 | |
| 58 | logger.info(u'SqlToolsService process id: %s', self.tools_service_process.pid) |
| 59 | |
| 60 | self.json_rpc_client.start() |
| 61 | logger.info(u'Sql Tools Client Initialized') |
| 62 | |
| 63 | def create_request(self, request_type, parameters, owner_uri): |
| 64 | """ |
| 65 | Create request of request type passed in. |
| 66 | """ |
| 67 | request = None |
| 68 | self.current_id = str(uuid.uuid4().int) |
| 69 | |
| 70 | if request_type == u'connection_request': |
| 71 | logger.info(u'SqlToolsClient connection request Id %s and owner Uri %s', |
| 72 | self.current_id, owner_uri) |
| 73 | request = connection.ConnectionRequest(self.current_id, owner_uri, self.json_rpc_client, |