mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-08 01:28:29 -05:00
Merge pull request #14 from Microsoft/feature/connectUsingService
Feature/connect using service
This commit is contained in:
@@ -9,7 +9,6 @@
|
||||
|
||||
<!-- Add the SSMS repo for private requirements -->
|
||||
<add key="SQLDS - SSMS" value="http://SQLISNuget/DS-SSMS/nuget/" />
|
||||
|
||||
</packageSources>
|
||||
|
||||
</configuration>
|
||||
|
||||
@@ -17,13 +17,35 @@ using Microsoft.SqlTools.ServiceLayer.Workspace;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
{
|
||||
public class ConnectionInfo
|
||||
{
|
||||
public ConnectionInfo(ISqlConnectionFactory factory, string ownerUri, ConnectionDetails details)
|
||||
{
|
||||
Factory = factory;
|
||||
OwnerUri = ownerUri;
|
||||
ConnectionDetails = details;
|
||||
ConnectionId = Guid.NewGuid();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unique Id, helpful to identify a connection info object
|
||||
/// </summary>
|
||||
public Guid ConnectionId { get; private set; }
|
||||
|
||||
public string OwnerUri { get; private set; }
|
||||
|
||||
public ISqlConnectionFactory Factory {get; private set;}
|
||||
|
||||
public ConnectionDetails ConnectionDetails { get; private set; }
|
||||
|
||||
public DbConnection SqlConnection { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Main class for the Connection Management services
|
||||
/// </summary>
|
||||
public class ConnectionService
|
||||
{
|
||||
#region Singleton Instance Implementation
|
||||
|
||||
/// <summary>
|
||||
/// Singleton service instance
|
||||
/// </summary>
|
||||
@@ -40,6 +62,13 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
return instance.Value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The SQL connection factory object
|
||||
/// </summary>
|
||||
private ISqlConnectionFactory connectionFactory;
|
||||
|
||||
private Dictionary<string, ConnectionInfo> ownerToConnectionMap = new Dictionary<string, ConnectionInfo>();
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor is private since it's a singleton class
|
||||
@@ -48,48 +77,26 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// The SQL connection factory object
|
||||
/// </summary>
|
||||
private ISqlConnectionFactory connectionFactory;
|
||||
|
||||
/// <summary>
|
||||
/// The current connection id that was previously used
|
||||
/// </summary>
|
||||
private int maxConnectionId = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Active connections lazy dictionary instance
|
||||
/// </summary>
|
||||
private readonly Lazy<Dictionary<int, DbConnection>> activeConnections
|
||||
= new Lazy<Dictionary<int, DbConnection>>(()
|
||||
=> new Dictionary<int, DbConnection>());
|
||||
|
||||
/// <summary>
|
||||
/// Callback for onconnection handler
|
||||
/// </summary>
|
||||
/// <param name="sqlConnection"></param>
|
||||
public delegate Task OnConnectionHandler(DbConnection sqlConnection);
|
||||
public delegate Task OnConnectionHandler(ConnectionInfo info);
|
||||
|
||||
/// <summary>
|
||||
// Callback for ondisconnect handler
|
||||
/// </summary>
|
||||
public delegate Task OnDisconnectHandler(ConnectionSummary summary);
|
||||
|
||||
/// <summary>
|
||||
/// List of onconnection handlers
|
||||
/// </summary>
|
||||
private readonly List<OnConnectionHandler> onConnectionActivities = new List<OnConnectionHandler>();
|
||||
private readonly List<OnConnectionHandler> onConnectionActivities = new List<OnConnectionHandler>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the active connection map
|
||||
/// List of ondisconnect handlers
|
||||
/// </summary>
|
||||
public Dictionary<int, DbConnection> ActiveConnections
|
||||
{
|
||||
get
|
||||
{
|
||||
return activeConnections.Value;
|
||||
}
|
||||
}
|
||||
private readonly List<OnDisconnectHandler> onDisconnectActivities = new List<OnDisconnectHandler>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the SQL connection factory instance
|
||||
@@ -105,9 +112,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
return this.connectionFactory;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test constructor that injects dependency interfaces
|
||||
/// </summary>
|
||||
@@ -117,43 +122,109 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
this.connectionFactory = testFactory;
|
||||
}
|
||||
|
||||
#region Public Methods
|
||||
// Attempts to link a URI to an actively used connection for this URI
|
||||
public bool TryFindConnection(string ownerUri, out ConnectionInfo connectionInfo)
|
||||
{
|
||||
return this.ownerToConnectionMap.TryGetValue(ownerUri, out connectionInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Open a connection with the specified connection details
|
||||
/// </summary>
|
||||
/// <param name="connectionDetails"></param>
|
||||
public ConnectionResult Connect(ConnectionDetails connectionDetails)
|
||||
/// <param name="connectionParams"></param>
|
||||
public ConnectResponse Connect(ConnectParams connectionParams)
|
||||
{
|
||||
// build the connection string from the input parameters
|
||||
string connectionString = BuildConnectionString(connectionDetails);
|
||||
// Validate parameters
|
||||
if(connectionParams == null || !connectionParams.IsValid())
|
||||
{
|
||||
return new ConnectResponse()
|
||||
{
|
||||
Messages = "Error: Invalid connection parameters provided."
|
||||
};
|
||||
}
|
||||
|
||||
// create a sql connection instance
|
||||
DbConnection connection = this.ConnectionFactory.CreateSqlConnection(connectionString);
|
||||
// Resolve if it is an existing connection
|
||||
// Disconnect active connection if the URI is already connected
|
||||
ConnectionInfo connectionInfo;
|
||||
if (ownerToConnectionMap.TryGetValue(connectionParams.OwnerUri, out connectionInfo) )
|
||||
{
|
||||
var disconnectParams = new DisconnectParams()
|
||||
{
|
||||
OwnerUri = connectionParams.OwnerUri
|
||||
};
|
||||
Disconnect(disconnectParams);
|
||||
}
|
||||
connectionInfo = new ConnectionInfo(ConnectionFactory, connectionParams.OwnerUri, connectionParams.Connection);
|
||||
|
||||
// open the database
|
||||
connection.Open();
|
||||
// try to connect
|
||||
var response = new ConnectResponse();
|
||||
try
|
||||
{
|
||||
// build the connection string from the input parameters
|
||||
string connectionString = ConnectionService.BuildConnectionString(connectionInfo.ConnectionDetails);
|
||||
|
||||
// map the connection id to the connection object for future lookups
|
||||
this.ActiveConnections.Add(++maxConnectionId, connection);
|
||||
// create a sql connection instance
|
||||
connectionInfo.SqlConnection = connectionInfo.Factory.CreateSqlConnection(connectionString);
|
||||
connectionInfo.SqlConnection.Open();
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
response.Messages = ex.Message;
|
||||
return response;
|
||||
}
|
||||
|
||||
ownerToConnectionMap[connectionParams.OwnerUri] = connectionInfo;
|
||||
|
||||
// invoke callback notifications
|
||||
foreach (var activity in this.onConnectionActivities)
|
||||
{
|
||||
activity(connection);
|
||||
activity(connectionInfo);
|
||||
}
|
||||
|
||||
// return the connection result
|
||||
return new ConnectionResult()
|
||||
{
|
||||
ConnectionId = maxConnectionId
|
||||
};
|
||||
response.ConnectionId = connectionInfo.ConnectionId.ToString();
|
||||
return response;
|
||||
}
|
||||
|
||||
public void InitializeService(ServiceHost serviceHost)
|
||||
/// <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)
|
||||
{
|
||||
// Register request and event handlers with the Service Host
|
||||
serviceHost.SetRequestHandler(ConnectionRequest.Type, HandleConnectRequest);
|
||||
serviceHost.SetRequestHandler(DisconnectRequest.Type, HandleDisconnectRequest);
|
||||
|
||||
// Register the configuration update handler
|
||||
WorkspaceService<SqlToolsSettings>.Instance.RegisterConfigChangeCallback(HandleDidChangeConfigurationNotification);
|
||||
@@ -168,10 +239,14 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
onConnectionActivities.Add(activity);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Request Handlers
|
||||
|
||||
/// <summary>
|
||||
/// Add a new method to be called when the ondisconnect request is submitted
|
||||
/// </summary>
|
||||
public void RegisterOnDisconnectTask(OnDisconnectHandler activity)
|
||||
{
|
||||
onDisconnectActivities.Add(activity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle new connection requests
|
||||
/// </summary>
|
||||
@@ -179,15 +254,15 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
/// <param name="requestContext"></param>
|
||||
/// <returns></returns>
|
||||
protected async Task HandleConnectRequest(
|
||||
ConnectionDetails connectionDetails,
|
||||
RequestContext<ConnectionResult> requestContext)
|
||||
ConnectParams connectParams,
|
||||
RequestContext<ConnectResponse> requestContext)
|
||||
{
|
||||
Logger.Write(LogLevel.Verbose, "HandleConnectRequest");
|
||||
|
||||
try
|
||||
{
|
||||
// open connection base on request details
|
||||
ConnectionResult result = ConnectionService.Instance.Connect(connectionDetails);
|
||||
ConnectResponse result = ConnectionService.Instance.Connect(connectParams);
|
||||
await requestContext.SendResult(result);
|
||||
}
|
||||
catch(Exception ex)
|
||||
@@ -196,10 +271,27 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// Handle disconnect requests
|
||||
/// </summary>
|
||||
protected async Task HandleDisconnectRequest(
|
||||
DisconnectParams disconnectParams,
|
||||
RequestContext<bool> requestContext)
|
||||
{
|
||||
Logger.Write(LogLevel.Verbose, "HandleDisconnectRequest");
|
||||
|
||||
#region Handlers for Events from Other Services
|
||||
try
|
||||
{
|
||||
bool result = ConnectionService.Instance.Disconnect(disconnectParams);
|
||||
await requestContext.SendResult(result);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
await requestContext.SendError(ex.Message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Task HandleDidChangeConfigurationNotification(
|
||||
SqlToolsSettings newSettings,
|
||||
SqlToolsSettings oldSettings,
|
||||
@@ -207,16 +299,12 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
{
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Helpers
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Build a connection string from a connection details instance
|
||||
/// </summary>
|
||||
/// <param name="connectionDetails"></param>
|
||||
private string BuildConnectionString(ConnectionDetails connectionDetails)
|
||||
public static string BuildConnectionString(ConnectionDetails connectionDetails)
|
||||
{
|
||||
SqlConnectionStringBuilder connectionBuilder = new SqlConnectionStringBuilder();
|
||||
connectionBuilder["Data Source"] = connectionDetails.ServerName;
|
||||
@@ -226,7 +314,5 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
connectionBuilder["Initial Catalog"] = connectionDetails.DatabaseName;
|
||||
return connectionBuilder.ToString();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,44 @@ using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||
{
|
||||
/// <summary>
|
||||
/// Message format for the initial connection request
|
||||
/// Parameters for the Connect Request.
|
||||
/// </summary>
|
||||
public class ConnectionDetails
|
||||
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>
|
||||
/// Message format for the connection result response
|
||||
/// </summary>
|
||||
public class ConnectResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// A GUID representing a unique connection ID
|
||||
/// </summary>
|
||||
public string ConnectionId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets any connection error messages
|
||||
/// </summary>
|
||||
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
|
||||
@@ -26,28 +61,20 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||
/// 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; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Message format for the connection result response
|
||||
/// </summary>
|
||||
public class ConnectionResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the connection id
|
||||
/// </summary>
|
||||
public int ConnectionId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets any connection error messages
|
||||
/// </summary>
|
||||
public string Messages { get; set; }
|
||||
// TODO Handle full set of properties
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -56,8 +83,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||
public class ConnectionRequest
|
||||
{
|
||||
public static readonly
|
||||
RequestType<ConnectionDetails, ConnectionResult> Type =
|
||||
RequestType<ConnectionDetails, ConnectionResult>.Create("connection/connect");
|
||||
RequestType<ConnectParams, ConnectResponse> Type =
|
||||
RequestType<ConnectParams, ConnectResponse>.Create("connection/connect");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Hosting.Protocol
|
||||
{
|
||||
internal interface IMessageSender
|
||||
public interface IMessageSender
|
||||
{
|
||||
Task SendEvent<TParams>(
|
||||
EventType<TParams> eventType,
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Hosting.Protocol
|
||||
{
|
||||
/// <summary>
|
||||
/// A ProtocolEndpoint is used for inter-process communication. Services can register to
|
||||
/// respond to requests and events, send their own requests, and listen for notifications
|
||||
/// sent by the other side of the endpoint
|
||||
/// </summary>
|
||||
public interface IProtocolEndpoint : IMessageSender
|
||||
{
|
||||
void SetRequestHandler<TParams, TResult>(
|
||||
RequestType<TParams, TResult> requestType,
|
||||
Func<TParams, RequestContext<TResult>, Task> requestHandler);
|
||||
|
||||
void SetEventHandler<TParams>(
|
||||
EventType<TParams> eventType,
|
||||
Func<TParams, EventContext, Task> eventHandler);
|
||||
|
||||
void SetEventHandler<TParams>(
|
||||
EventType<TParams> eventType,
|
||||
Func<TParams, EventContext, Task> eventHandler,
|
||||
bool overrideExisting);
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting.Protocol
|
||||
/// Provides behavior for a client or server endpoint that
|
||||
/// communicates using the specified protocol.
|
||||
/// </summary>
|
||||
public class ProtocolEndpoint : IMessageSender
|
||||
public class ProtocolEndpoint : IMessageSender, IProtocolEndpoint
|
||||
{
|
||||
private bool isStarted;
|
||||
private int currentMessageId;
|
||||
|
||||
@@ -9,63 +9,46 @@ using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting;
|
||||
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
{
|
||||
/// <summary>
|
||||
/// Main class for Autocomplete functionality
|
||||
/// </summary>
|
||||
public class AutoCompleteService
|
||||
internal class IntellisenseCache
|
||||
{
|
||||
#region Singleton Instance Implementation
|
||||
// connection used to query for intellisense info
|
||||
private DbConnection connection;
|
||||
|
||||
/// <summary>
|
||||
/// Singleton service instance
|
||||
/// </summary>
|
||||
private static Lazy<AutoCompleteService> instance
|
||||
= new Lazy<AutoCompleteService>(() => new AutoCompleteService());
|
||||
// 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; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the singleton service instance
|
||||
/// </summary>
|
||||
public static AutoCompleteService Instance
|
||||
public IntellisenseCache(ISqlConnectionFactory connectionFactory, ConnectionDetails connectionDetails)
|
||||
{
|
||||
get
|
||||
{
|
||||
return instance.Value;
|
||||
}
|
||||
ReferenceCount = 0;
|
||||
DatabaseInfo = CopySummary(connectionDetails);
|
||||
|
||||
// TODO error handling on this. Intellisense should catch or else the service should handle
|
||||
connection = connectionFactory.CreateSqlConnection(ConnectionService.BuildConnectionString(connectionDetails));
|
||||
connection.Open();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Default, parameterless constructor.
|
||||
/// TODO: Figure out how to make this truely singleton even with dependency injection for tests
|
||||
/// Used to identify a database for which this cache is used
|
||||
/// </summary>
|
||||
public AutoCompleteService()
|
||||
{
|
||||
public ConnectionSummary DatabaseInfo
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current autocomplete candidate list
|
||||
/// </summary>
|
||||
public IEnumerable<string> AutoCompleteList { get; private set; }
|
||||
|
||||
public void InitializeService(ServiceHost serviceHost)
|
||||
{
|
||||
// Register a callback for when a connection is created
|
||||
ConnectionService.Instance.RegisterOnConnectionTask(UpdateAutoCompleteCache);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the cached autocomplete candidate list when the user connects to a database
|
||||
/// TODO: Update with refactoring/async
|
||||
/// </summary>
|
||||
/// <param name="connection"></param>
|
||||
public async Task UpdateAutoCompleteCache(DbConnection connection)
|
||||
public async Task UpdateCache()
|
||||
{
|
||||
DbCommand command = connection.CreateCommand();
|
||||
command.CommandText = "SELECT name FROM sys.tables";
|
||||
@@ -83,20 +66,19 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
await Task.FromResult(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the completion item list for the current text position
|
||||
/// </summary>
|
||||
/// <param name="textDocumentPosition"></param>
|
||||
public CompletionItem[] GetCompletionItems(TextDocumentPosition textDocumentPosition)
|
||||
public List<CompletionItem> GetAutoCompleteItems(TextDocumentPosition textDocumentPosition)
|
||||
{
|
||||
var completions = new List<CompletionItem>();
|
||||
List<CompletionItem> completions = new List<CompletionItem>();
|
||||
|
||||
int i = 0;
|
||||
|
||||
// Take a reference to the list at a point in time in case we update and replace the list
|
||||
var suggestions = AutoCompleteList;
|
||||
// the completion list will be null is user not connected to server
|
||||
if (this.AutoCompleteList != null)
|
||||
{
|
||||
foreach (var autoCompleteItem in this.AutoCompleteList)
|
||||
|
||||
foreach (var autoCompleteItem in suggestions)
|
||||
{
|
||||
// convert the completion item candidates into CompletionItems
|
||||
completions.Add(new CompletionItem()
|
||||
@@ -131,7 +113,212 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
}
|
||||
}
|
||||
}
|
||||
return completions.ToArray();
|
||||
|
||||
return completions;
|
||||
}
|
||||
|
||||
private static ConnectionSummary CopySummary(ConnectionSummary summary)
|
||||
{
|
||||
return new ConnectionSummary()
|
||||
{
|
||||
ServerName = summary.ServerName,
|
||||
DatabaseName = summary.DatabaseName,
|
||||
UserName = summary.UserName
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Treats connections as the same if their server, db and usernames all match
|
||||
/// </summary>
|
||||
public class ConnectionSummaryComparer : IEqualityComparer<ConnectionSummary>
|
||||
{
|
||||
public bool Equals(ConnectionSummary x, ConnectionSummary y)
|
||||
{
|
||||
if(x == y) { return true; }
|
||||
else if(x != null)
|
||||
{
|
||||
if(y == null) { return false; }
|
||||
|
||||
// Compare server, db, username. Note: server is case-insensitive in the driver
|
||||
return string.Compare(x.ServerName, y.ServerName, StringComparison.OrdinalIgnoreCase) == 0
|
||||
&& string.Compare(x.DatabaseName, y.DatabaseName, StringComparison.Ordinal) == 0
|
||||
&& string.Compare(x.UserName, y.UserName, StringComparison.Ordinal) == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int GetHashCode(ConnectionSummary obj)
|
||||
{
|
||||
int hashcode = 31;
|
||||
if(obj != null)
|
||||
{
|
||||
if(obj.ServerName != null)
|
||||
{
|
||||
hashcode ^= obj.ServerName.GetHashCode();
|
||||
}
|
||||
if (obj.DatabaseName != null)
|
||||
{
|
||||
hashcode ^= obj.DatabaseName.GetHashCode();
|
||||
}
|
||||
if (obj.UserName != null)
|
||||
{
|
||||
hashcode ^= obj.UserName.GetHashCode();
|
||||
}
|
||||
}
|
||||
return hashcode;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Main class for Autocomplete functionality
|
||||
/// </summary>
|
||||
public class AutoCompleteService
|
||||
{
|
||||
#region Singleton Instance Implementation
|
||||
|
||||
/// <summary>
|
||||
/// Singleton service instance
|
||||
/// </summary>
|
||||
private static Lazy<AutoCompleteService> instance
|
||||
= new Lazy<AutoCompleteService>(() => new AutoCompleteService());
|
||||
|
||||
/// <summary>
|
||||
/// Gets the singleton service instance
|
||||
/// </summary>
|
||||
public static AutoCompleteService Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return instance.Value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Default, parameterless constructor.
|
||||
/// TODO: Figure out how to make this truely singleton even with dependency injection for tests
|
||||
/// </summary>
|
||||
public AutoCompleteService()
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// Dictionary of unique intellisense caches for each Connection
|
||||
private Dictionary<ConnectionSummary, IntellisenseCache> caches =
|
||||
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 Object factoryLock = new Object();
|
||||
|
||||
/// <summary>
|
||||
/// Internal for testing purposes only
|
||||
/// </summary>
|
||||
internal ISqlConnectionFactory ConnectionFactory
|
||||
{
|
||||
get
|
||||
{
|
||||
lock(factoryLock)
|
||||
{
|
||||
if(factory == null)
|
||||
{
|
||||
factory = new SqlConnectionFactory();
|
||||
}
|
||||
}
|
||||
return factory;
|
||||
}
|
||||
set
|
||||
{
|
||||
lock(factoryLock)
|
||||
{
|
||||
factory = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void InitializeService(ServiceHost serviceHost)
|
||||
{
|
||||
// Register a callback for when a connection is created
|
||||
ConnectionService.Instance.RegisterOnConnectionTask(UpdateAutoCompleteCache);
|
||||
|
||||
// Register a callback for when a connection is closed
|
||||
ConnectionService.Instance.RegisterOnDisconnectTask(RemoveAutoCompleteCacheUriReference);
|
||||
}
|
||||
|
||||
private async Task UpdateAutoCompleteCache(ConnectionInfo connectionInfo)
|
||||
{
|
||||
if (connectionInfo != null)
|
||||
{
|
||||
await UpdateAutoCompleteCache(connectionInfo.ConnectionDetails);
|
||||
}
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// Update the cached autocomplete candidate list when the user connects to a database
|
||||
/// </summary>
|
||||
/// <param name="details"></param>
|
||||
public async Task UpdateAutoCompleteCache(ConnectionDetails details)
|
||||
{
|
||||
IntellisenseCache cache;
|
||||
lock(cachesLock)
|
||||
{
|
||||
if(!caches.TryGetValue(details, out cache))
|
||||
{
|
||||
cache = new IntellisenseCache(ConnectionFactory, details);
|
||||
caches[cache.DatabaseInfo] = cache;
|
||||
}
|
||||
cache.ReferenceCount++;
|
||||
}
|
||||
|
||||
await cache.UpdateCache();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the completion item list for the current text position.
|
||||
/// This method does not await cache builds since it expects to return quickly
|
||||
/// </summary>
|
||||
/// <param name="textDocumentPosition"></param>
|
||||
public CompletionItem[] GetCompletionItems(TextDocumentPosition textDocumentPosition)
|
||||
{
|
||||
// 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
|
||||
// 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
|
||||
ConnectionInfo info;
|
||||
IntellisenseCache cache;
|
||||
if (ConnectionService.Instance.TryFindConnection(textDocumentPosition.Uri, out info)
|
||||
&& caches.TryGetValue((ConnectionSummary)info.ConnectionDetails, out cache))
|
||||
{
|
||||
return cache.GetAutoCompleteItems(textDocumentPosition).ToArray();
|
||||
}
|
||||
|
||||
return new CompletionItem[0];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -103,10 +103,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
WorkspaceService<SqlToolsSettings>.Instance.RegisterTextDocChangeCallback(HandleDidChangeTextDocumentNotification);
|
||||
|
||||
// Register the file open update handler
|
||||
WorkspaceService<SqlToolsSettings>.Instance.RegisterTextDocOpenCallback(HandleDidOpenTextDocumentNotification);
|
||||
|
||||
// register an OnConnection callback
|
||||
ConnectionService.Instance.RegisterOnConnectionTask(OnConnection);
|
||||
WorkspaceService<SqlToolsSettings>.Instance.RegisterTextDocOpenCallback(HandleDidOpenTextDocumentNotification);
|
||||
|
||||
// Store the SqlToolsContext for future use
|
||||
Context = context;
|
||||
@@ -305,16 +302,6 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
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(DbConnection sqlConnection)
|
||||
{
|
||||
await AutoCompleteService.Instance.UpdateAutoCompleteCache(sqlConnection);
|
||||
await Task.FromResult(true);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Helpers
|
||||
|
||||
@@ -3,8 +3,13 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.Test.Utility;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
|
||||
@@ -14,7 +19,130 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
|
||||
/// </summary>
|
||||
public class ConnectionServiceTests
|
||||
{
|
||||
#region "Connection tests"
|
||||
/// <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>
|
||||
/// Verify that when connecting with invalid credentials, an error is thrown.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ConnectingWithInvalidCredentialsYieldsErrorMessage()
|
||||
{
|
||||
var testConnectionDetails = TestObjects.GetTestConnectionDetails();
|
||||
var invalidConnectionDetails = new ConnectionDetails();
|
||||
invalidConnectionDetails.ServerName = testConnectionDetails.ServerName;
|
||||
invalidConnectionDetails.DatabaseName = testConnectionDetails.DatabaseName;
|
||||
invalidConnectionDetails.UserName = "invalidUsername"; // triggers exception when opening mock connection
|
||||
invalidConnectionDetails.Password = "invalidPassword";
|
||||
|
||||
// Connect to test db with invalid credentials
|
||||
var connectionResult =
|
||||
TestObjects.GetTestConnectionService()
|
||||
.Connect(new ConnectParams()
|
||||
{
|
||||
OwnerUri = "file://my/sample/file.sql",
|
||||
Connection = invalidConnectionDetails
|
||||
});
|
||||
|
||||
// check that an error was caught
|
||||
Assert.NotNull(connectionResult.Messages);
|
||||
Assert.NotEqual(String.Empty, connectionResult.Messages);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that when connecting with invalid parameters, an error is thrown.
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineDataAttribute(null, "my-server", "test", "sa", "123456")]
|
||||
[InlineDataAttribute("file://my/sample/file.sql", null, "test", "sa", "123456")]
|
||||
[InlineDataAttribute("file://my/sample/file.sql", "my-server", null, "sa", "123456")]
|
||||
[InlineDataAttribute("file://my/sample/file.sql", "my-server", "test", null, "123456")]
|
||||
[InlineDataAttribute("file://my/sample/file.sql", "my-server", "test", "sa", null)]
|
||||
[InlineDataAttribute("", "my-server", "test", "sa", "123456")]
|
||||
[InlineDataAttribute("file://my/sample/file.sql", "", "test", "sa", "123456")]
|
||||
[InlineDataAttribute("file://my/sample/file.sql", "my-server", "", "sa", "123456")]
|
||||
[InlineDataAttribute("file://my/sample/file.sql", "my-server", "test", "", "123456")]
|
||||
[InlineDataAttribute("file://my/sample/file.sql", "my-server", "test", "sa", "")]
|
||||
public void ConnectingWithInvalidParametersYieldsErrorMessage(string ownerUri, string server, string database, string userName, string password)
|
||||
{
|
||||
// Connect with invalid parameters
|
||||
var connectionResult =
|
||||
TestObjects.GetTestConnectionService()
|
||||
.Connect(new ConnectParams()
|
||||
{
|
||||
OwnerUri = ownerUri,
|
||||
Connection = new ConnectionDetails() {
|
||||
ServerName = server,
|
||||
DatabaseName = database,
|
||||
UserName = userName,
|
||||
Password = password
|
||||
}
|
||||
});
|
||||
|
||||
// check that an error was caught
|
||||
Assert.NotNull(connectionResult.Messages);
|
||||
Assert.NotEqual(String.Empty, connectionResult.Messages);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that when connecting with a null parameters object, an error is thrown.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ConnectingWithNullParametersObjectYieldsErrorMessage()
|
||||
{
|
||||
// Connect with null parameters
|
||||
var connectionResult =
|
||||
TestObjects.GetTestConnectionService()
|
||||
.Connect(null);
|
||||
|
||||
// check that an error was caught
|
||||
Assert.NotNull(connectionResult.Messages);
|
||||
Assert.NotEqual(String.Empty, connectionResult.Messages);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that the SQL parser correctly detects errors in text
|
||||
@@ -23,12 +151,162 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
|
||||
public void ConnectToDatabaseTest()
|
||||
{
|
||||
// connect to a database instance
|
||||
var connectionResult =
|
||||
string ownerUri = "file://my/sample/file.sql";
|
||||
var connectionResult =
|
||||
TestObjects.GetTestConnectionService()
|
||||
.Connect(TestObjects.GetTestConnectionDetails());
|
||||
.Connect(new ConnectParams()
|
||||
{
|
||||
OwnerUri = ownerUri,
|
||||
Connection = TestObjects.GetTestConnectionDetails()
|
||||
});
|
||||
|
||||
// verify that a valid connection id was returned
|
||||
Assert.True(connectionResult.ConnectionId > 0);
|
||||
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>
|
||||
@@ -49,12 +327,33 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection
|
||||
);
|
||||
|
||||
// connect to a database instance
|
||||
var connectionResult = connectionService.Connect(TestObjects.GetTestConnectionDetails());
|
||||
var connectionResult = connectionService.Connect(TestObjects.GetTestConnectionParams());
|
||||
|
||||
// verify that a valid connection id was returned
|
||||
Assert.True(callbackInvoked);
|
||||
}
|
||||
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// Verify when a connection is created that the URI -> Connection mapping is created in the connection service.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestConnectRequestRegistersOwner()
|
||||
{
|
||||
// Given a request to connect to a database
|
||||
var service = TestObjects.GetTestConnectionService();
|
||||
var connectParams = TestObjects.GetTestConnectionParams();
|
||||
|
||||
// connect to a database instance
|
||||
var connectionResult = service.Connect(connectParams);
|
||||
|
||||
// verify that a valid connection id was returned
|
||||
Assert.NotNull(connectionResult.ConnectionId);
|
||||
Assert.NotEqual(String.Empty, connectionResult.ConnectionId);
|
||||
Assert.NotNull(new Guid(connectionResult.ConnectionId));
|
||||
|
||||
// verify that the (URI -> connection) mapping was created
|
||||
ConnectionInfo info;
|
||||
Assert.True(service.TryFindConnection(connectParams.OwnerUri, out info));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
|
||||
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||
using Microsoft.SqlTools.Test.Utility;
|
||||
@@ -109,13 +111,21 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
||||
/// Verify that the SQL parser correctly detects errors in text
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void AutocompleteTest()
|
||||
public async Task AutocompleteTest()
|
||||
{
|
||||
var autocompleteService = TestObjects.GetAutoCompleteService();
|
||||
var connectionService = TestObjects.GetTestConnectionService();
|
||||
var connectionResult = connectionService.Connect(TestObjects.GetTestConnectionDetails());
|
||||
var sqlConnection = connectionService.ActiveConnections[connectionResult.ConnectionId];
|
||||
autocompleteService.UpdateAutoCompleteCache(sqlConnection).Wait();
|
||||
// TODO Re-enable this test once we have a way to hook up the right auto-complete and connection services.
|
||||
// Probably need a service provider channel so that we can mock service access. Otherwise everything accesses
|
||||
// static instances and cannot be properly tested.
|
||||
|
||||
//var autocompleteService = TestObjects.GetAutoCompleteService();
|
||||
//var connectionService = TestObjects.GetTestConnectionService();
|
||||
|
||||
//ConnectParams connectionRequest = TestObjects.GetTestConnectionParams();
|
||||
//var connectionResult = connectionService.Connect(connectionRequest);
|
||||
|
||||
//var sqlConnection = connectionService.ActiveConnections[connectionResult.ConnectionId];
|
||||
//await autocompleteService.UpdateAutoCompleteCache(sqlConnection);
|
||||
await Task.Run(() => { return; });
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -41,6 +41,15 @@ namespace Microsoft.SqlTools.Test.Utility
|
||||
#endif
|
||||
}
|
||||
|
||||
public static ConnectParams GetTestConnectionParams()
|
||||
{
|
||||
return new ConnectParams()
|
||||
{
|
||||
OwnerUri = "file://some/file.sql",
|
||||
Connection = GetTestConnectionDetails()
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a test connection details object
|
||||
/// </summary>
|
||||
@@ -313,12 +322,16 @@ namespace Microsoft.SqlTools.Test.Utility
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
// No Op
|
||||
}
|
||||
|
||||
public override void Open()
|
||||
{
|
||||
// No Op
|
||||
// No Op, unless credentials are bad
|
||||
if(ConnectionString.Contains("invalidUsername"))
|
||||
{
|
||||
throw new Exception("Invalid credentials provided");
|
||||
}
|
||||
}
|
||||
|
||||
public override string ConnectionString { get; set; }
|
||||
|
||||
@@ -11,8 +11,9 @@
|
||||
"System.Data.SqlClient": "4.1.0",
|
||||
"xunit": "2.1.0",
|
||||
"dotnet-test-xunit": "1.0.0-rc2-192208-24",
|
||||
"moq.netcore": "4.4.0-beta8",
|
||||
"Microsoft.SqlTools.ServiceLayer": {
|
||||
"target": "project"
|
||||
"target": "project"
|
||||
}
|
||||
},
|
||||
"testRunner": "xunit",
|
||||
|
||||
78
test/ServiceHost.Test/Workspace/WorkspaceServiceTests.cs
Normal file
78
test/ServiceHost.Test/Workspace/WorkspaceServiceTests.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
// //
|
||||
// // 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.LanguageServices;
|
||||
// using Microsoft.SqlTools.ServiceLayer.WorkspaceServices.Contracts;
|
||||
// using Microsoft.SqlTools.Test.Utility;
|
||||
// using Xunit;
|
||||
|
||||
// namespace Microsoft.SqlTools.ServiceLayer.Test.Workspace
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// Tests for the ServiceHost Language Service tests
|
||||
// /// </summary>
|
||||
// public class WorkspaceServiceTests
|
||||
// {
|
||||
|
||||
// [Fact]
|
||||
// public async Task ServiceLoadsProfilesOnDemand()
|
||||
// {
|
||||
// // Given an event detailing
|
||||
|
||||
// // when
|
||||
// // Send the configuration change to cause profiles to be loaded
|
||||
// await this.languageServiceClient.SendEvent(
|
||||
// DidChangeConfigurationNotification<LanguageServerSettingsWrapper>.Type,
|
||||
// new DidChangeConfigurationParams<LanguageServerSettingsWrapper>
|
||||
// {
|
||||
// Settings = new LanguageServerSettingsWrapper
|
||||
// {
|
||||
// Powershell = new LanguageServerSettings
|
||||
// {
|
||||
// EnableProfileLoading = true,
|
||||
// ScriptAnalysis = new ScriptAnalysisSettings
|
||||
// {
|
||||
// Enable = false
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
|
||||
// OutputReader outputReader = new OutputReader(this.protocolClient);
|
||||
|
||||
// Task<EvaluateResponseBody> evaluateTask =
|
||||
// this.SendRequest(
|
||||
// EvaluateRequest.Type,
|
||||
// new EvaluateRequestArguments
|
||||
// {
|
||||
// Expression = "\"PROFILE: $(Assert-ProfileLoaded)\"",
|
||||
// Context = "repl"
|
||||
// });
|
||||
|
||||
// // Try reading up to 10 lines to find the expected output line
|
||||
// string outputString = null;
|
||||
// for (int i = 0; i < 10; i++)
|
||||
// {
|
||||
// outputString = await outputReader.ReadLine();
|
||||
|
||||
// if (outputString.StartsWith("PROFILE"))
|
||||
// {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Delete the test profile before any assert failures
|
||||
// // cause the function to exit
|
||||
// File.Delete(currentUserCurrentHostPath);
|
||||
|
||||
// // Wait for the selection to appear as output
|
||||
// await evaluateTask;
|
||||
// Assert.Equal("PROFILE: True", outputString);
|
||||
// }
|
||||
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user