Merge branch 'feature/connectUsingService' into feature/queryExecutionV1

This commit is contained in:
Benjamin Russell
2016-08-05 11:18:09 -07:00
5 changed files with 177 additions and 55 deletions

View File

@@ -34,7 +34,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
public string OwnerUri { get; private set; }
private ISqlConnectionFactory Factory {get; set;}
public ISqlConnectionFactory Factory {get; private set;}
public ConnectionDetails ConnectionDetails { get; private set; }
@@ -123,16 +123,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
}
// Attempts to link a URI to an actively used connection for this URI
public bool TryFindConnection(string ownerUri, out ConnectionSummary connectionSummary)
public bool TryFindConnection(string ownerUri, out ConnectionInfo connectionInfo)
{
connectionSummary = null;
ConnectionInfo connectionInfo;
if (this.ownerToConnectionMap.TryGetValue(ownerUri, out connectionInfo))
{
connectionSummary = CopySummary(connectionInfo.ConnectionDetails);
return true;
}
return false;
return this.ownerToConnectionMap.TryGetValue(ownerUri, out connectionInfo);
}
private static ConnectionSummary CopySummary(ConnectionSummary summary)
@@ -151,16 +144,33 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
/// <param name="connectionParams"></param>
public ConnectResponse Connect(ConnectParams connectionParams)
{
// Validate parameters
if(connectionParams == null || !connectionParams.IsValid())
{
return new ConnectResponse()
{
Messages = "Error: Invalid connection parameters provided."
};
}
ConnectionInfo connectionInfo;
if (ownerToConnectionMap.TryGetValue(connectionParams.OwnerUri, out connectionInfo) )
{
// TODO disconnect
}
connectionInfo = new ConnectionInfo(this.connectionFactory, connectionParams.OwnerUri, connectionParams.Connection);
connectionInfo = new ConnectionInfo(ConnectionFactory, connectionParams.OwnerUri, connectionParams.Connection);
// try to connect
connectionInfo.OpenConnection();
// TODO: check that connection worked
var response = new ConnectResponse();
try
{
connectionInfo.OpenConnection();
}
catch(Exception ex)
{
response.Messages = ex.Message;
return response;
}
ownerToConnectionMap[connectionParams.OwnerUri] = connectionInfo;
@@ -171,10 +181,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
}
// return the connection result
return new ConnectResponse()
{
ConnectionId = connectionInfo.ConnectionId.ToString()
};
response.ConnectionId = connectionInfo.ConnectionId.ToString();
return response;
}
public void InitializeService(IProtocolEndpoint serviceHost)

View File

@@ -0,0 +1,30 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
{
/// <summary>
/// Extension methods to ConnectParams
/// </summary>
public static class ConnectParamsExtensions
{
/// <summary>
/// Check that the fields in ConnectParams are all valid
/// </summary>
public static bool IsValid(this ConnectParams parameters)
{
return !(
String.IsNullOrEmpty(parameters.OwnerUri) ||
parameters.Connection == null ||
String.IsNullOrEmpty(parameters.Connection.DatabaseName) ||
String.IsNullOrEmpty(parameters.Connection.Password) ||
String.IsNullOrEmpty(parameters.Connection.ServerName) ||
String.IsNullOrEmpty(parameters.Connection.UserName)
);
}
}
}

View File

@@ -265,10 +265,10 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
// If we have a connection but no cache, we don't care - assuming the OnConnect and OnDisconnect listeners
// behave well, there should be a cache for any actively connected document. This also helps skip documents
// that are not backed by a SQL connection
ConnectionSummary connectionSummary;
ConnectionInfo connectionInfo;
IntellisenseCache cache;
if (ConnectionService.Instance.TryFindConnection(textDocumentPosition.Uri, out connectionSummary)
&& caches.TryGetValue(connectionSummary, out cache))
if (ConnectionService.Instance.TryFindConnection(textDocumentPosition.Uri, out connectionInfo)
&& caches.TryGetValue(connectionInfo.ConnectionDetails, out cache))
{
return cache.GetAutoCompleteItems(textDocumentPosition).ToArray();
}
@@ -278,3 +278,4 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
}
}