Skip to content

Commit

Permalink
Removing redundant method in connection service. (#169)
Browse files Browse the repository at this point in the history
* Removing redundant method in connection service.

* Updating mssqlcliclient to surface error messages to it's callers.

* Removed copyright statement in test and flake8 format.

* Converting output of stack trace to be logged instead.
  • Loading branch information
MrMeemus authored Feb 15, 2018
1 parent 6374354 commit 0c1063d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 57 deletions.
6 changes: 4 additions & 2 deletions mssqlcli/completion_refresher.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ def _bg_refresh(self, mssqlcliclient, callbacks, history=None,
completer = MssqlCompleter(smart_completion=True, settings=settings)

executor = mssqlcliclient
if not executor.connect():
owner_uri, error_messages = executor.connect()

if not owner_uri:
# If we were unable to connect, do not break the experience for the user.
# Return nothing, smart completion can maintain the keywords and functions completions.
logger.error(u'Completion Refresher failed to connect to the target server.')
logger.error(u'Completion refresher connection failure.'.join(error_messages))
return
# If callbacks is a single function then push it into a list.
if callable(callbacks):
Expand Down
38 changes: 1 addition & 37 deletions mssqlcli/jsonrpc/contracts/connectionservice.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from mssqlcli.jsonrpc.contracts import Request

import sys
import logging

logger = logging.getLogger(u'mssqlcli.connectionservice')
Expand Down Expand Up @@ -42,7 +41,7 @@ def get_response(self):
return decoded_response

except Exception as error:
logger.debug(str(error))
logger.info(str(error))
self.finished = True
self.json_rpc_client.request_finished(self.id)
self.json_rpc_client.request_finished(self.owner_uri)
Expand Down Expand Up @@ -144,38 +143,3 @@ class ConnectionResponse(object):
def __init__(self, params):
self.result = params[u'result']
self.id = params[u'id']


#
# Handle Connection Events.
#

def handle_connection_response(response):
# Handles complete notification and return ConnectionCompleteEvent object
# if connection successful.
def handle_connection_complete_notification(response):
if not response.connection_id:
sys.stderr.write(u'\nConnection did not succeed.')
if response.error_message:
sys.stderr.write(u'\nError message: ' + response.error_message)
logger.error(response.error_message)
if response.messages:
logger.error(response.messages)

return response

def handle_connection_response_notification(response):
if not response.result:
sys.stderr.write(
u'\nIncorrect json rpc request. Connection not successful.')
return None

response_handlers = {
u'ConnectionResponse': handle_connection_response_notification,
u'ConnectionCompleteEvent': handle_connection_complete_notification
}

response_name = type(response).__name__

if response_name in response_handlers:
return response_handlers[response_name](response)
2 changes: 0 additions & 2 deletions mssqlcli/jsonrpc/contracts/tests/test_json_rpc_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ def verify_connection_service_response(self,
elif isinstance(response, connectionservice.ConnectionCompleteEvent):
if response.connection_id:
complete_event += 1
self.assertTrue(connectionservice.handle_connection_response(response).connection_id,
response.connection_id)

self.assertEqual(response_event, expected_response_event)
self.assertEqual(complete_event, expected_complete_event)
Expand Down
7 changes: 4 additions & 3 deletions mssqlcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,12 @@ def connect(self, database='', server='', user='', port='', passwd='',
multi_subnet_failover=multi_subnet_failover,
packet_size=packet_size, **kwargs)

if not self.mssqlcliclient_query_execution.connect():
owner_uri, error_messages = self.mssqlcliclient_query_execution.connect()
if not owner_uri:
click.secho(
'\nUnable to connect. Please try again',
'\n'.join(error_messages),
err=True,
fg='red')
fg='yellow')
exit(1)

telemetry_session.set_server_information(
Expand Down
24 changes: 15 additions & 9 deletions mssqlcli/mssqlcliclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ def __init__(self, sql_tools_client, server_name, user_name, password,
def connect(self):
"""
Connects to the SQL Server instance using specified credentials
:return: OwnerUri if connection established else None
:return: Tuple (OwnerURI, list of error messages)
"""
if self.is_connected:
return self.owner_uri
return self.owner_uri, []

# Required params
connection_params = {u'ServerName': self.server_name,
Expand Down Expand Up @@ -99,11 +99,15 @@ def connect(self):
u'connection_request', connection_params, self.owner_uri)
connection_request.execute()

error_messages = []
while not connection_request.completed():
response = connection_request.get_response()
if response:
response = connectionservice.handle_connection_response(
response)

if isinstance(response, connectionservice.ConnectionCompleteEvent):
if response.error_message:
error_messages.append(u'Error message: {}'.format(response.error_message))
if response.messages:
logger.error(response.messages)
else:
time.sleep(time_wait_if_no_response)

Expand All @@ -119,7 +123,10 @@ def connect(self):
logger.info(
u'Connection Successful. Connection Id {0}'.format(
response.connection_id))
return self.owner_uri

return self.owner_uri, []

return None, error_messages

def execute_multi_statement_single_batch(self, query):
# Try to run first as special command
Expand Down Expand Up @@ -158,9 +165,8 @@ def execute_single_batch_query(self, query):
query_messages = []
while not query_request.completed():
query_response = query_request.get_response()
if query_response:
if isinstance(query_response, queryservice.QueryMessageEvent):
query_messages.append(query_response)
if isinstance(query_response, queryservice.QueryMessageEvent):
query_messages.append(query_response)
else:
sleep(time_wait_if_no_response)

Expand Down
5 changes: 1 addition & 4 deletions tests/test_mssqlcliclient.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# coding=utf-8
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
import os
import io
import unittest
Expand Down Expand Up @@ -205,5 +201,6 @@ def test_stored_proc_multiple_result_sets(self):
finally:
shutdown(client)


if __name__ == u'__main__':
unittest.main()

0 comments on commit 0c1063d

Please sign in to comment.