mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 10:58:30 -05:00
Merge branch 'dev' into feature/queryExecutionV1
This commit is contained in:
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
<!-- Add the SSMS repo for private requirements -->
|
<!-- Add the SSMS repo for private requirements -->
|
||||||
<add key="SQLDS - SSMS" value="http://SQLISNuget/DS-SSMS/nuget/" />
|
<add key="SQLDS - SSMS" value="http://SQLISNuget/DS-SSMS/nuget/" />
|
||||||
<add key="CrossPlat" value="W:/" />
|
|
||||||
</packageSources>
|
</packageSources>
|
||||||
|
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|||||||
@@ -38,17 +38,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
|
|
||||||
public ConnectionDetails ConnectionDetails { get; private set; }
|
public ConnectionDetails ConnectionDetails { get; private set; }
|
||||||
|
|
||||||
public DbConnection SqlConnection { get; private set; }
|
public DbConnection SqlConnection { get; set; }
|
||||||
|
|
||||||
public void OpenConnection()
|
|
||||||
{
|
|
||||||
// build the connection string from the input parameters
|
|
||||||
string connectionString = ConnectionService.BuildConnectionString(ConnectionDetails);
|
|
||||||
|
|
||||||
// create a sql connection instance
|
|
||||||
SqlConnection = Factory.CreateSqlConnection(connectionString);
|
|
||||||
SqlConnection.Open();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -93,11 +83,21 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
/// <param name="sqlConnection"></param>
|
/// <param name="sqlConnection"></param>
|
||||||
public delegate Task OnConnectionHandler(ConnectionInfo info);
|
public delegate Task OnConnectionHandler(ConnectionInfo info);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
// Callback for ondisconnect handler
|
||||||
|
/// </summary>
|
||||||
|
public delegate Task OnDisconnectHandler(ConnectionSummary summary);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// List of onconnection handlers
|
/// List of onconnection handlers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly List<OnConnectionHandler> onConnectionActivities = new List<OnConnectionHandler>();
|
private readonly List<OnConnectionHandler> onConnectionActivities = new List<OnConnectionHandler>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// List of ondisconnect handlers
|
||||||
|
/// </summary>
|
||||||
|
private readonly List<OnDisconnectHandler> onDisconnectActivities = new List<OnDisconnectHandler>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the SQL connection factory instance
|
/// Gets the SQL connection factory instance
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -127,16 +127,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
{
|
{
|
||||||
return this.ownerToConnectionMap.TryGetValue(ownerUri, out connectionInfo);
|
return this.ownerToConnectionMap.TryGetValue(ownerUri, out connectionInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ConnectionSummary CopySummary(ConnectionSummary summary)
|
|
||||||
{
|
|
||||||
return new ConnectionSummary()
|
|
||||||
{
|
|
||||||
ServerName = summary.ServerName,
|
|
||||||
DatabaseName = summary.DatabaseName,
|
|
||||||
UserName = summary.UserName
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Open a connection with the specified connection details
|
/// Open a connection with the specified connection details
|
||||||
@@ -153,10 +143,16 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Resolve if it is an existing connection
|
||||||
|
// Disconnect active connection if the URI is already connected
|
||||||
ConnectionInfo connectionInfo;
|
ConnectionInfo connectionInfo;
|
||||||
if (ownerToConnectionMap.TryGetValue(connectionParams.OwnerUri, out connectionInfo) )
|
if (ownerToConnectionMap.TryGetValue(connectionParams.OwnerUri, out connectionInfo) )
|
||||||
{
|
{
|
||||||
// TODO disconnect
|
var disconnectParams = new DisconnectParams()
|
||||||
|
{
|
||||||
|
OwnerUri = connectionParams.OwnerUri
|
||||||
|
};
|
||||||
|
Disconnect(disconnectParams);
|
||||||
}
|
}
|
||||||
connectionInfo = new ConnectionInfo(ConnectionFactory, connectionParams.OwnerUri, connectionParams.Connection);
|
connectionInfo = new ConnectionInfo(ConnectionFactory, connectionParams.OwnerUri, connectionParams.Connection);
|
||||||
|
|
||||||
@@ -164,7 +160,12 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
var response = new ConnectResponse();
|
var response = new ConnectResponse();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
connectionInfo.OpenConnection();
|
// build the connection string from the input parameters
|
||||||
|
string connectionString = ConnectionService.BuildConnectionString(connectionInfo.ConnectionDetails);
|
||||||
|
|
||||||
|
// create a sql connection instance
|
||||||
|
connectionInfo.SqlConnection = connectionInfo.Factory.CreateSqlConnection(connectionString);
|
||||||
|
connectionInfo.SqlConnection.Open();
|
||||||
}
|
}
|
||||||
catch(Exception ex)
|
catch(Exception ex)
|
||||||
{
|
{
|
||||||
@@ -185,10 +186,45 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Close a connection with the specified connection details.
|
||||||
|
/// </summary>
|
||||||
|
public bool Disconnect(DisconnectParams disconnectParams)
|
||||||
|
{
|
||||||
|
// Validate parameters
|
||||||
|
if (disconnectParams == null || String.IsNullOrEmpty(disconnectParams.OwnerUri))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lookup the connection owned by the URI
|
||||||
|
ConnectionInfo info;
|
||||||
|
if (!ownerToConnectionMap.TryGetValue(disconnectParams.OwnerUri, out info))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the connection
|
||||||
|
info.SqlConnection.Close();
|
||||||
|
|
||||||
|
// Remove URI mapping
|
||||||
|
ownerToConnectionMap.Remove(disconnectParams.OwnerUri);
|
||||||
|
|
||||||
|
// Invoke callback notifications
|
||||||
|
foreach (var activity in this.onDisconnectActivities)
|
||||||
|
{
|
||||||
|
activity(info.ConnectionDetails);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Success
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public void InitializeService(IProtocolEndpoint serviceHost)
|
public void InitializeService(IProtocolEndpoint serviceHost)
|
||||||
{
|
{
|
||||||
// Register request and event handlers with the Service Host
|
// Register request and event handlers with the Service Host
|
||||||
serviceHost.SetRequestHandler(ConnectionRequest.Type, HandleConnectRequest);
|
serviceHost.SetRequestHandler(ConnectionRequest.Type, HandleConnectRequest);
|
||||||
|
serviceHost.SetRequestHandler(DisconnectRequest.Type, HandleDisconnectRequest);
|
||||||
|
|
||||||
// Register the configuration update handler
|
// Register the configuration update handler
|
||||||
WorkspaceService<SqlToolsSettings>.Instance.RegisterConfigChangeCallback(HandleDidChangeConfigurationNotification);
|
WorkspaceService<SqlToolsSettings>.Instance.RegisterConfigChangeCallback(HandleDidChangeConfigurationNotification);
|
||||||
@@ -202,6 +238,14 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
{
|
{
|
||||||
onConnectionActivities.Add(activity);
|
onConnectionActivities.Add(activity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add a new method to be called when the ondisconnect request is submitted
|
||||||
|
/// </summary>
|
||||||
|
public void RegisterOnDisconnectTask(OnDisconnectHandler activity)
|
||||||
|
{
|
||||||
|
onDisconnectActivities.Add(activity);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handle new connection requests
|
/// Handle new connection requests
|
||||||
@@ -226,6 +270,27 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
await requestContext.SendError(ex.Message);
|
await requestContext.SendError(ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handle disconnect requests
|
||||||
|
/// </summary>
|
||||||
|
protected async Task HandleDisconnectRequest(
|
||||||
|
DisconnectParams disconnectParams,
|
||||||
|
RequestContext<bool> requestContext)
|
||||||
|
{
|
||||||
|
Logger.Write(LogLevel.Verbose, "HandleDisconnectRequest");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
bool result = ConnectionService.Instance.Disconnect(disconnectParams);
|
||||||
|
await requestContext.SendResult(result);
|
||||||
|
}
|
||||||
|
catch(Exception ex)
|
||||||
|
{
|
||||||
|
await requestContext.SendError(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public Task HandleDidChangeConfigurationNotification(
|
public Task HandleDidChangeConfigurationNotification(
|
||||||
SqlToolsSettings newSettings,
|
SqlToolsSettings newSettings,
|
||||||
|
|||||||
@@ -7,85 +7,23 @@ using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
|||||||
|
|
||||||
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Parameters for the Connect Request.
|
|
||||||
/// </summary>
|
|
||||||
public class ConnectParams
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// A URI identifying the owner of the connection. This will most commonly be a file in the workspace
|
|
||||||
/// or a virtual file representing an object in a database.
|
|
||||||
/// </summary>
|
|
||||||
public string OwnerUri { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// Contains the required parameters to initialize a connection to a database.
|
|
||||||
/// A connection will identified by its server name, database name and user name.
|
|
||||||
/// This may be changed in the future to support multiple connections with different
|
|
||||||
/// connection properties to the same database.
|
|
||||||
/// </summary>
|
|
||||||
public ConnectionDetails Connection { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Parameters for the Disconnect Request.
|
|
||||||
/// </summary>
|
|
||||||
public class DisconnectParams
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// A URI identifying the owner of the connection. This will most commonly be a file in the workspace
|
|
||||||
/// or a virtual file representing an object in a database.
|
|
||||||
/// </summary>
|
|
||||||
public string ownerUri { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Parameters for the ConnectionChanged Notification.
|
|
||||||
/// </summary>
|
|
||||||
public class ConnectionChangedParams
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// A URI identifying the owner of the connection. This will most commonly be a file in the workspace
|
|
||||||
/// or a virtual file representing an object in a database.
|
|
||||||
/// </summary>
|
|
||||||
public string ownerUri { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// Contains the high-level properties about the connection, for display to the user.
|
|
||||||
/// </summary>
|
|
||||||
public ConnectionSummary Connection { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Provides high level information about a connection.
|
|
||||||
/// </summary>
|
|
||||||
public class ConnectionSummary
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the connection server name
|
|
||||||
/// </summary>
|
|
||||||
public string ServerName { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the connection database name
|
|
||||||
/// </summary>
|
|
||||||
public string DatabaseName { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the connection user name
|
|
||||||
/// </summary>
|
|
||||||
public string UserName { get; set; }
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Message format for the initial connection request
|
/// Parameters for the Connect Request.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ConnectionDetails : ConnectionSummary
|
public class ConnectParams
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the connection password
|
/// A URI identifying the owner of the connection. This will most commonly be a file in the workspace
|
||||||
|
/// or a virtual file representing an object in a database.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
public string OwnerUri { get; set; }
|
||||||
public string Password { get; set; }
|
/// <summary>
|
||||||
|
/// Contains the required parameters to initialize a connection to a database.
|
||||||
// TODO Handle full set of properties
|
/// A connection will identified by its server name, database name and user name.
|
||||||
|
/// This may be changed in the future to support multiple connections with different
|
||||||
|
/// connection properties to the same database.
|
||||||
|
/// </summary>
|
||||||
|
public ConnectionDetails Connection { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -102,8 +40,43 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
|||||||
/// Gets or sets any connection error messages
|
/// Gets or sets any connection error messages
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Messages { get; set; }
|
public string Messages { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Provides high level information about a connection.
|
||||||
|
/// </summary>
|
||||||
|
public class ConnectionSummary
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the connection server name
|
||||||
|
/// </summary>
|
||||||
|
public string ServerName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the connection database name
|
||||||
|
/// </summary>
|
||||||
|
public string DatabaseName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the connection user name
|
||||||
|
/// </summary>
|
||||||
|
public string UserName { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Message format for the initial connection request
|
||||||
|
/// </summary>
|
||||||
|
public class ConnectionDetails : ConnectionSummary
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the connection password
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public string Password { get; set; }
|
||||||
|
|
||||||
|
// TODO Handle full set of properties
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Connect request mapping entry
|
/// Connect request mapping entry
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -113,25 +86,4 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
|||||||
RequestType<ConnectParams, ConnectResponse> Type =
|
RequestType<ConnectParams, ConnectResponse> Type =
|
||||||
RequestType<ConnectParams, ConnectResponse>.Create("connection/connect");
|
RequestType<ConnectParams, ConnectResponse>.Create("connection/connect");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Disconnect request mapping entry
|
|
||||||
/// </summary>
|
|
||||||
public class DisconnectRequest
|
|
||||||
{
|
|
||||||
public static readonly
|
|
||||||
RequestType<DisconnectParams, bool> Type =
|
|
||||||
RequestType<DisconnectParams, bool>.Create("connection/disconnect");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// ConnectionChanged notification mapping entry
|
|
||||||
/// </summary>
|
|
||||||
public class ConnectionChangedNotification
|
|
||||||
{
|
|
||||||
public static readonly
|
|
||||||
EventType<ConnectionChangedParams> Type =
|
|
||||||
EventType<ConnectionChangedParams>.Create("connection/connectionchanged");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Parameters for the ConnectionChanged Notification.
|
||||||
|
/// </summary>
|
||||||
|
public class ConnectionChangedParams
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A URI identifying the owner of the connection. This will most commonly be a file in the workspace
|
||||||
|
/// or a virtual file representing an object in a database.
|
||||||
|
/// </summary>
|
||||||
|
public string OwnerUri { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Contains the high-level properties about the connection, for display to the user.
|
||||||
|
/// </summary>
|
||||||
|
public ConnectionSummary Connection { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// ConnectionChanged notification mapping entry
|
||||||
|
/// </summary>
|
||||||
|
public class ConnectionChangedNotification
|
||||||
|
{
|
||||||
|
public static readonly
|
||||||
|
EventType<ConnectionChangedParams> Type =
|
||||||
|
EventType<ConnectionChangedParams>.Create("connection/connectionchanged");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Parameters for the Disconnect Request.
|
||||||
|
/// </summary>
|
||||||
|
public class DisconnectParams
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A URI identifying the owner of the connection. This will most commonly be a file in the workspace
|
||||||
|
/// or a virtual file representing an object in a database.
|
||||||
|
/// </summary>
|
||||||
|
public string OwnerUri { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Disconnect request mapping entry
|
||||||
|
/// </summary>
|
||||||
|
public class DisconnectRequest
|
||||||
|
{
|
||||||
|
public static readonly
|
||||||
|
RequestType<DisconnectParams, bool> Type =
|
||||||
|
RequestType<DisconnectParams, bool>.Create("connection/disconnect");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,8 +21,13 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
// connection used to query for intellisense info
|
// connection used to query for intellisense info
|
||||||
private DbConnection connection;
|
private DbConnection connection;
|
||||||
|
|
||||||
|
// number of documents (URI's) that are using the cache for the same database
|
||||||
|
// the autocomplete service uses this to remove unreferenced caches
|
||||||
|
public int ReferenceCount { get; set; }
|
||||||
|
|
||||||
public IntellisenseCache(ISqlConnectionFactory connectionFactory, ConnectionDetails connectionDetails)
|
public IntellisenseCache(ISqlConnectionFactory connectionFactory, ConnectionDetails connectionDetails)
|
||||||
{
|
{
|
||||||
|
ReferenceCount = 0;
|
||||||
DatabaseInfo = CopySummary(connectionDetails);
|
DatabaseInfo = CopySummary(connectionDetails);
|
||||||
|
|
||||||
// TODO error handling on this. Intellisense should catch or else the service should handle
|
// TODO error handling on this. Intellisense should catch or else the service should handle
|
||||||
@@ -130,10 +135,10 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
{
|
{
|
||||||
public bool Equals(ConnectionSummary x, ConnectionSummary y)
|
public bool Equals(ConnectionSummary x, ConnectionSummary y)
|
||||||
{
|
{
|
||||||
if (x == y) { return true; }
|
if(x == y) { return true; }
|
||||||
else if (x != null)
|
else if(x != null)
|
||||||
{
|
{
|
||||||
if (y == null) { return false; }
|
if(y == null) { return false; }
|
||||||
|
|
||||||
// Compare server, db, username. Note: server is case-insensitive in the driver
|
// Compare server, db, username. Note: server is case-insensitive in the driver
|
||||||
return string.Compare(x.ServerName, y.ServerName, StringComparison.OrdinalIgnoreCase) == 0
|
return string.Compare(x.ServerName, y.ServerName, StringComparison.OrdinalIgnoreCase) == 0
|
||||||
@@ -146,9 +151,9 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
public int GetHashCode(ConnectionSummary obj)
|
public int GetHashCode(ConnectionSummary obj)
|
||||||
{
|
{
|
||||||
int hashcode = 31;
|
int hashcode = 31;
|
||||||
if (obj != null)
|
if(obj != null)
|
||||||
{
|
{
|
||||||
if (obj.ServerName != null)
|
if(obj.ServerName != null)
|
||||||
{
|
{
|
||||||
hashcode ^= obj.ServerName.GetHashCode();
|
hashcode ^= obj.ServerName.GetHashCode();
|
||||||
}
|
}
|
||||||
@@ -174,13 +179,13 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Singleton service instance
|
/// Singleton service instance
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static Lazy<AutoCompleteService> instance
|
private static Lazy<AutoCompleteService> instance
|
||||||
= new Lazy<AutoCompleteService>(() => new AutoCompleteService());
|
= new Lazy<AutoCompleteService>(() => new AutoCompleteService());
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the singleton service instance
|
/// Gets the singleton service instance
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static AutoCompleteService Instance
|
public static AutoCompleteService Instance
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
@@ -193,16 +198,18 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
/// TODO: Figure out how to make this truely singleton even with dependency injection for tests
|
/// TODO: Figure out how to make this truely singleton even with dependency injection for tests
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public AutoCompleteService()
|
public AutoCompleteService()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
// Dictionary of unique intellisense caches for each Connection
|
// Dictionary of unique intellisense caches for each Connection
|
||||||
private Dictionary<ConnectionSummary, IntellisenseCache> caches =
|
private Dictionary<ConnectionSummary, IntellisenseCache> caches =
|
||||||
new Dictionary<ConnectionSummary, IntellisenseCache>(new ConnectionSummaryComparer());
|
new Dictionary<ConnectionSummary, IntellisenseCache>(new ConnectionSummaryComparer());
|
||||||
|
private Object cachesLock = new Object(); // Used when we insert/remove something from the cache dictionary
|
||||||
|
|
||||||
private ISqlConnectionFactory factory;
|
private ISqlConnectionFactory factory;
|
||||||
|
private Object factoryLock = new Object();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Internal for testing purposes only
|
/// Internal for testing purposes only
|
||||||
@@ -211,22 +218,30 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
// TODO consider protecting against multi-threaded access
|
lock(factoryLock)
|
||||||
if (factory == null)
|
|
||||||
{
|
{
|
||||||
factory = new SqlConnectionFactory();
|
if(factory == null)
|
||||||
|
{
|
||||||
|
factory = new SqlConnectionFactory();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return factory;
|
return factory;
|
||||||
}
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
factory = value;
|
lock(factoryLock)
|
||||||
|
{
|
||||||
|
factory = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void InitializeService(ServiceHost serviceHost)
|
public void InitializeService(ServiceHost serviceHost)
|
||||||
{
|
{
|
||||||
// Register a callback for when a connection is created
|
// Register a callback for when a connection is created
|
||||||
ConnectionService.Instance.RegisterOnConnectionTask(UpdateAutoCompleteCache);
|
ConnectionService.Instance.RegisterOnConnectionTask(UpdateAutoCompleteCache);
|
||||||
|
|
||||||
|
// Register a callback for when a connection is closed
|
||||||
|
ConnectionService.Instance.RegisterOnDisconnectTask(RemoveAutoCompleteCacheUriReference);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task UpdateAutoCompleteCache(ConnectionInfo connectionInfo)
|
private async Task UpdateAutoCompleteCache(ConnectionInfo connectionInfo)
|
||||||
@@ -237,20 +252,50 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove a reference to an autocomplete cache from a URI. If
|
||||||
|
/// it is the last URI connected to a particular connection,
|
||||||
|
/// then remove the cache.
|
||||||
|
/// </summary>
|
||||||
|
public async Task RemoveAutoCompleteCacheUriReference(ConnectionSummary summary)
|
||||||
|
{
|
||||||
|
await Task.Run( () =>
|
||||||
|
{
|
||||||
|
lock(cachesLock)
|
||||||
|
{
|
||||||
|
IntellisenseCache cache;
|
||||||
|
if( caches.TryGetValue(summary, out cache) )
|
||||||
|
{
|
||||||
|
cache.ReferenceCount--;
|
||||||
|
|
||||||
|
// Remove unused caches
|
||||||
|
if( cache.ReferenceCount == 0 )
|
||||||
|
{
|
||||||
|
caches.Remove(summary);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Update the cached autocomplete candidate list when the user connects to a database
|
/// Update the cached autocomplete candidate list when the user connects to a database
|
||||||
/// TODO: Update with refactoring/async
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="details"></param>
|
/// <param name="details"></param>
|
||||||
public async Task UpdateAutoCompleteCache(ConnectionDetails details)
|
public async Task UpdateAutoCompleteCache(ConnectionDetails details)
|
||||||
{
|
{
|
||||||
IntellisenseCache cache;
|
IntellisenseCache cache;
|
||||||
if (!caches.TryGetValue(details, out cache))
|
lock(cachesLock)
|
||||||
{
|
{
|
||||||
cache = new IntellisenseCache(ConnectionFactory, details);
|
if(!caches.TryGetValue(details, out cache))
|
||||||
caches[cache.DatabaseInfo] = cache;
|
{
|
||||||
|
cache = new IntellisenseCache(ConnectionFactory, details);
|
||||||
|
caches[cache.DatabaseInfo] = cache;
|
||||||
|
}
|
||||||
|
cache.ReferenceCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
await cache.UpdateCache();
|
await cache.UpdateCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,19 +308,18 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
{
|
{
|
||||||
// Try to find a cache for the document's backing connection (if available)
|
// Try to find a cache for the document's backing connection (if available)
|
||||||
// If we have a connection but no cache, we don't care - assuming the OnConnect and OnDisconnect listeners
|
// 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
|
// 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
|
// that are not backed by a SQL connection
|
||||||
ConnectionInfo connectionInfo;
|
ConnectionInfo info;
|
||||||
IntellisenseCache cache;
|
IntellisenseCache cache;
|
||||||
if (ConnectionService.Instance.TryFindConnection(textDocumentPosition.Uri, out connectionInfo)
|
if (ConnectionService.Instance.TryFindConnection(textDocumentPosition.Uri, out info)
|
||||||
&& caches.TryGetValue(connectionInfo.ConnectionDetails, out cache))
|
&& caches.TryGetValue((ConnectionSummary)info.ConnectionDetails, out cache))
|
||||||
{
|
{
|
||||||
return cache.GetAutoCompleteItems(textDocumentPosition).ToArray();
|
return cache.GetAutoCompleteItems(textDocumentPosition).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new CompletionItem[0];
|
return new CompletionItem[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -103,10 +103,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
WorkspaceService<SqlToolsSettings>.Instance.RegisterTextDocChangeCallback(HandleDidChangeTextDocumentNotification);
|
WorkspaceService<SqlToolsSettings>.Instance.RegisterTextDocChangeCallback(HandleDidChangeTextDocumentNotification);
|
||||||
|
|
||||||
// Register the file open update handler
|
// Register the file open update handler
|
||||||
WorkspaceService<SqlToolsSettings>.Instance.RegisterTextDocOpenCallback(HandleDidOpenTextDocumentNotification);
|
WorkspaceService<SqlToolsSettings>.Instance.RegisterTextDocOpenCallback(HandleDidOpenTextDocumentNotification);
|
||||||
|
|
||||||
// register an OnConnection callback
|
|
||||||
ConnectionService.Instance.RegisterOnConnectionTask(OnConnection);
|
|
||||||
|
|
||||||
// Store the SqlToolsContext for future use
|
// Store the SqlToolsContext for future use
|
||||||
Context = context;
|
Context = context;
|
||||||
@@ -305,16 +302,6 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
CurrentSettings.ScriptAnalysis.Update(newSettings.ScriptAnalysis, CurrentWorkspace.WorkspacePath);
|
CurrentSettings.ScriptAnalysis.Update(newSettings.ScriptAnalysis, CurrentWorkspace.WorkspacePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Callback for when a user connection is done processing
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sqlConnection"></param>
|
|
||||||
public async Task OnConnection(ConnectionInfo connectionInfo)
|
|
||||||
{
|
|
||||||
// TODO consider whether this is needed at all - currently AutoComplete service handles its own updating
|
|
||||||
await Task.FromResult(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Private Helpers
|
#region Private Helpers
|
||||||
|
|||||||
@@ -19,6 +19,53 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ConnectionServiceTests
|
public class ConnectionServiceTests
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Verify that when a connection is started for a URI with an already existing
|
||||||
|
/// connection, we disconnect first before connecting.
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void ConnectingWhenConnectionExistCausesDisconnectThenConnect()
|
||||||
|
{
|
||||||
|
bool callbackInvoked = false;
|
||||||
|
|
||||||
|
// first connect
|
||||||
|
string ownerUri = "file://my/sample/file.sql";
|
||||||
|
var connectionService = TestObjects.GetTestConnectionService();
|
||||||
|
var connectionResult =
|
||||||
|
connectionService
|
||||||
|
.Connect(new ConnectParams()
|
||||||
|
{
|
||||||
|
OwnerUri = ownerUri,
|
||||||
|
Connection = TestObjects.GetTestConnectionDetails()
|
||||||
|
});
|
||||||
|
|
||||||
|
// verify that we are connected
|
||||||
|
Assert.NotEmpty(connectionResult.ConnectionId);
|
||||||
|
|
||||||
|
// register disconnect callback
|
||||||
|
connectionService.RegisterOnDisconnectTask(
|
||||||
|
(result) => {
|
||||||
|
callbackInvoked = true;
|
||||||
|
return Task.FromResult(true);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// send annother connect request
|
||||||
|
connectionResult =
|
||||||
|
connectionService
|
||||||
|
.Connect(new ConnectParams()
|
||||||
|
{
|
||||||
|
OwnerUri = ownerUri,
|
||||||
|
Connection = TestObjects.GetTestConnectionDetails()
|
||||||
|
});
|
||||||
|
|
||||||
|
// verify that the event was fired (we disconnected first before connecting)
|
||||||
|
Assert.True(callbackInvoked);
|
||||||
|
|
||||||
|
// verify that we connected again
|
||||||
|
Assert.NotEmpty(connectionResult.ConnectionId);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Verify that when connecting with invalid credentials, an error is thrown.
|
/// Verify that when connecting with invalid credentials, an error is thrown.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -117,6 +164,151 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
|
|||||||
Assert.NotEmpty(connectionResult.ConnectionId);
|
Assert.NotEmpty(connectionResult.ConnectionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verify that we can disconnect from an active connection succesfully
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void DisconnectFromDatabaseTest()
|
||||||
|
{
|
||||||
|
// first connect
|
||||||
|
string ownerUri = "file://my/sample/file.sql";
|
||||||
|
var connectionService = TestObjects.GetTestConnectionService();
|
||||||
|
var connectionResult =
|
||||||
|
connectionService
|
||||||
|
.Connect(new ConnectParams()
|
||||||
|
{
|
||||||
|
OwnerUri = ownerUri,
|
||||||
|
Connection = TestObjects.GetTestConnectionDetails()
|
||||||
|
});
|
||||||
|
|
||||||
|
// verify that we are connected
|
||||||
|
Assert.NotEmpty(connectionResult.ConnectionId);
|
||||||
|
|
||||||
|
// send disconnect request
|
||||||
|
var disconnectResult =
|
||||||
|
connectionService
|
||||||
|
.Disconnect(new DisconnectParams()
|
||||||
|
{
|
||||||
|
OwnerUri = ownerUri
|
||||||
|
});
|
||||||
|
Assert.True(disconnectResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test that when a disconnect is performed, the callback event is fired
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void DisconnectFiresCallbackEvent()
|
||||||
|
{
|
||||||
|
bool callbackInvoked = false;
|
||||||
|
|
||||||
|
// first connect
|
||||||
|
string ownerUri = "file://my/sample/file.sql";
|
||||||
|
var connectionService = TestObjects.GetTestConnectionService();
|
||||||
|
var connectionResult =
|
||||||
|
connectionService
|
||||||
|
.Connect(new ConnectParams()
|
||||||
|
{
|
||||||
|
OwnerUri = ownerUri,
|
||||||
|
Connection = TestObjects.GetTestConnectionDetails()
|
||||||
|
});
|
||||||
|
|
||||||
|
// verify that we are connected
|
||||||
|
Assert.NotEmpty(connectionResult.ConnectionId);
|
||||||
|
|
||||||
|
// register disconnect callback
|
||||||
|
connectionService.RegisterOnDisconnectTask(
|
||||||
|
(result) => {
|
||||||
|
callbackInvoked = true;
|
||||||
|
return Task.FromResult(true);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// send disconnect request
|
||||||
|
var disconnectResult =
|
||||||
|
connectionService
|
||||||
|
.Disconnect(new DisconnectParams()
|
||||||
|
{
|
||||||
|
OwnerUri = ownerUri
|
||||||
|
});
|
||||||
|
Assert.True(disconnectResult);
|
||||||
|
|
||||||
|
// verify that the event was fired
|
||||||
|
Assert.True(callbackInvoked);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test that disconnecting an active connection removes the Owner URI -> ConnectionInfo mapping
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void DisconnectRemovesOwnerMapping()
|
||||||
|
{
|
||||||
|
// first connect
|
||||||
|
string ownerUri = "file://my/sample/file.sql";
|
||||||
|
var connectionService = TestObjects.GetTestConnectionService();
|
||||||
|
var connectionResult =
|
||||||
|
connectionService
|
||||||
|
.Connect(new ConnectParams()
|
||||||
|
{
|
||||||
|
OwnerUri = ownerUri,
|
||||||
|
Connection = TestObjects.GetTestConnectionDetails()
|
||||||
|
});
|
||||||
|
|
||||||
|
// verify that we are connected
|
||||||
|
Assert.NotEmpty(connectionResult.ConnectionId);
|
||||||
|
|
||||||
|
// check that the owner mapping exists
|
||||||
|
ConnectionInfo info;
|
||||||
|
Assert.True(connectionService.TryFindConnection(ownerUri, out info));
|
||||||
|
|
||||||
|
// send disconnect request
|
||||||
|
var disconnectResult =
|
||||||
|
connectionService
|
||||||
|
.Disconnect(new DisconnectParams()
|
||||||
|
{
|
||||||
|
OwnerUri = ownerUri
|
||||||
|
});
|
||||||
|
Assert.True(disconnectResult);
|
||||||
|
|
||||||
|
// check that the owner mapping no longer exists
|
||||||
|
Assert.False(connectionService.TryFindConnection(ownerUri, out info));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test that disconnecting validates parameters and doesn't succeed when they are invalid
|
||||||
|
/// </summary>
|
||||||
|
[Theory]
|
||||||
|
[InlineDataAttribute(null)]
|
||||||
|
[InlineDataAttribute("")]
|
||||||
|
|
||||||
|
public void DisconnectValidatesParameters(string disconnectUri)
|
||||||
|
{
|
||||||
|
// first connect
|
||||||
|
string ownerUri = "file://my/sample/file.sql";
|
||||||
|
var connectionService = TestObjects.GetTestConnectionService();
|
||||||
|
var connectionResult =
|
||||||
|
connectionService
|
||||||
|
.Connect(new ConnectParams()
|
||||||
|
{
|
||||||
|
OwnerUri = ownerUri,
|
||||||
|
Connection = TestObjects.GetTestConnectionDetails()
|
||||||
|
});
|
||||||
|
|
||||||
|
// verify that we are connected
|
||||||
|
Assert.NotEmpty(connectionResult.ConnectionId);
|
||||||
|
|
||||||
|
// send disconnect request
|
||||||
|
var disconnectResult =
|
||||||
|
connectionService
|
||||||
|
.Disconnect(new DisconnectParams()
|
||||||
|
{
|
||||||
|
OwnerUri = disconnectUri
|
||||||
|
});
|
||||||
|
|
||||||
|
// verify that disconnect failed
|
||||||
|
Assert.False(disconnectResult);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Verify that the SQL parser correctly detects errors in text
|
/// Verify that the SQL parser correctly detects errors in text
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -151,24 +343,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
|
|||||||
var service = TestObjects.GetTestConnectionService();
|
var service = TestObjects.GetTestConnectionService();
|
||||||
var connectParams = TestObjects.GetTestConnectionParams();
|
var connectParams = TestObjects.GetTestConnectionParams();
|
||||||
|
|
||||||
//var endpoint = new Mock<IProtocolEndpoint>();
|
|
||||||
//Func<ConnectParams, RequestContext<ConnectResponse>, Task> connectRequestHandler = null;
|
|
||||||
//endpoint.Setup(e => e.SetRequestHandler(ConnectionRequest.Type, It.IsAny<Func<ConnectParams, RequestContext<ConnectResponse>, Task>>()))
|
|
||||||
// .Callback<Func<ConnectParams, RequestContext<ConnectResponse>, Task>>(handler => connectRequestHandler = handler);
|
|
||||||
|
|
||||||
// when I initialize the service
|
|
||||||
//service.InitializeService(endpoint.Object);
|
|
||||||
|
|
||||||
// then I expect the handler to be captured
|
|
||||||
//Assert.NotNull(connectRequestHandler);
|
|
||||||
|
|
||||||
// when I call the service
|
|
||||||
//var requestContext = new Mock<RequestContext<ConnectResponse>>();
|
|
||||||
|
|
||||||
//connectRequestHandler(connectParams, requestContext.Object);
|
|
||||||
// then I should get a live connection
|
|
||||||
|
|
||||||
// and then I should have
|
|
||||||
// connect to a database instance
|
// connect to a database instance
|
||||||
var connectionResult = service.Connect(connectParams);
|
var connectionResult = service.Connect(connectParams);
|
||||||
|
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ namespace Microsoft.SqlTools.Test.Utility
|
|||||||
|
|
||||||
public override void Close()
|
public override void Close()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
// No Op
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Open()
|
public override void Open()
|
||||||
|
|||||||
Reference in New Issue
Block a user