mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-27 17:24:26 -05:00
Incremental checkin of connection work
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -203,6 +203,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
new Dictionary<ConnectionSummary, IntellisenseCache>(new ConnectionSummaryComparer());
|
||||
|
||||
private ISqlConnectionFactory factory;
|
||||
private Object factoryLock = new Object();
|
||||
|
||||
/// <summary>
|
||||
/// Internal for testing purposes only
|
||||
@@ -211,16 +212,21 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
{
|
||||
get
|
||||
{
|
||||
// TODO consider protecting against multi-threaded access
|
||||
if(factory == null)
|
||||
lock(factoryLock)
|
||||
{
|
||||
factory = new SqlConnectionFactory();
|
||||
if(factory == null)
|
||||
{
|
||||
factory = new SqlConnectionFactory();
|
||||
}
|
||||
}
|
||||
return factory;
|
||||
}
|
||||
set
|
||||
{
|
||||
factory = value;
|
||||
lock(factoryLock)
|
||||
{
|
||||
factory = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void InitializeService(ServiceHost serviceHost)
|
||||
@@ -265,10 +271,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 info;
|
||||
IntellisenseCache cache;
|
||||
if (ConnectionService.Instance.TryFindConnection(textDocumentPosition.Uri, out connectionSummary)
|
||||
&& caches.TryGetValue(connectionSummary, out cache))
|
||||
if (ConnectionService.Instance.TryFindConnection(textDocumentPosition.Uri, out info)
|
||||
&& caches.TryGetValue((ConnectionSummary)info.ConnectionDetails, out cache))
|
||||
{
|
||||
return cache.GetAutoCompleteItems(textDocumentPosition).ToArray();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user