mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-09 01:32:38 -05:00
Merge branch 'dev' into feature/queryExecutionV1
Also adding a fancy new mocked out reader for mocking db calls.
This commit is contained in:
@@ -5,14 +5,14 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.EditorServices.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.ConnectionServices.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.ConnectionServices.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
||||
using Microsoft.SqlTools.ServiceLayer.WorkspaceServices;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.ConnectionServices
|
||||
{
|
||||
@@ -56,12 +56,17 @@ namespace Microsoft.SqlTools.ServiceLayer.ConnectionServices
|
||||
/// </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 Lazy<Dictionary<Guid, ISqlConnection>> activeConnections
|
||||
= new Lazy<Dictionary<Guid, ISqlConnection>>(()
|
||||
=> new Dictionary<Guid, ISqlConnection>());
|
||||
private Lazy<Dictionary<int, ISqlConnection>> activeConnections
|
||||
= new Lazy<Dictionary<int, ISqlConnection>>(()
|
||||
=> new Dictionary<int, ISqlConnection>());
|
||||
|
||||
/// <summary>
|
||||
/// Callback for onconnection handler
|
||||
@@ -77,7 +82,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ConnectionServices
|
||||
/// <summary>
|
||||
/// Gets the active connection map
|
||||
/// </summary>
|
||||
public Dictionary<Guid, ISqlConnection> ActiveConnections
|
||||
public Dictionary<int, ISqlConnection> ActiveConnections
|
||||
{
|
||||
get
|
||||
{
|
||||
@@ -117,56 +122,40 @@ namespace Microsoft.SqlTools.ServiceLayer.ConnectionServices
|
||||
/// Open a connection with the specified connection details
|
||||
/// </summary>
|
||||
/// <param name="connectionDetails"></param>
|
||||
public async Task<ConnectionResult> Connect(ConnectionDetails connectionDetails)
|
||||
public ConnectionResult Connect(ConnectionDetails connectionDetails)
|
||||
{
|
||||
// build the connection string from the input parameters
|
||||
string connectionString = BuildConnectionString(connectionDetails);
|
||||
|
||||
// create a sql connection instance
|
||||
ISqlConnection connection = ConnectionFactory.CreateSqlConnection(connectionString);
|
||||
ISqlConnection connection = this.ConnectionFactory.CreateSqlConnection(connectionString);
|
||||
|
||||
// open the database
|
||||
await connection.OpenAsync();
|
||||
connection.Open();
|
||||
|
||||
// map the connection id to the connection object for future lookups
|
||||
Guid connectionId = Guid.NewGuid();
|
||||
ActiveConnections.Add(connectionId, connection);
|
||||
this.ActiveConnections.Add(++maxConnectionId, connection);
|
||||
|
||||
// invoke callback notifications
|
||||
var onConnectionCallbackTasks = onConnectionActivities.Select(t => t(connection));
|
||||
await Task.WhenAll(onConnectionCallbackTasks);
|
||||
// TODO: Evaulate if we want to avoid waiting here. We'll need error handling on the other side if we don't wait
|
||||
foreach (var activity in this.onConnectionActivities)
|
||||
{
|
||||
activity(connection);
|
||||
}
|
||||
|
||||
// return the connection result
|
||||
return new ConnectionResult
|
||||
return new ConnectionResult()
|
||||
{
|
||||
ConnectionId = connectionId
|
||||
ConnectionId = maxConnectionId
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes an active connection and removes it from the active connections list
|
||||
/// </summary>
|
||||
/// <param name="connectionId">ID of the connection to close</param>
|
||||
public void Disconnect(Guid connectionId)
|
||||
{
|
||||
if (!ActiveConnections.ContainsKey(connectionId))
|
||||
{
|
||||
// TODO: Should this possibly be a throw condition?
|
||||
return;
|
||||
}
|
||||
|
||||
ActiveConnections[connectionId].Close();
|
||||
ActiveConnections.Remove(connectionId);
|
||||
}
|
||||
|
||||
public void Initialize(ServiceHost serviceHost)
|
||||
public void InitializeService(ServiceHost serviceHost)
|
||||
{
|
||||
// Register request and event handlers with the Service Host
|
||||
serviceHost.SetRequestHandler(ConnectionRequest.Type, HandleConnectRequest);
|
||||
|
||||
// Register the shutdown handler
|
||||
serviceHost.RegisterShutdownTask(HandleShutdownRequest);
|
||||
|
||||
// Register the configuration update handler
|
||||
WorkspaceService<SqlToolsSettings>.Instance.RegisterConfigChangeCallback(HandleDidChangeConfigurationNotification);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -178,15 +167,6 @@ namespace Microsoft.SqlTools.ServiceLayer.ConnectionServices
|
||||
onConnectionActivities.Add(activity);
|
||||
}
|
||||
|
||||
public ISqlConnection GetConnection(Guid connectionId)
|
||||
{
|
||||
if (!ActiveConnections.ContainsKey(connectionId))
|
||||
{
|
||||
throw new ArgumentException("Connection with provided ID could not be found");
|
||||
}
|
||||
return ActiveConnections[connectionId];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Request Handlers
|
||||
@@ -203,24 +183,28 @@ namespace Microsoft.SqlTools.ServiceLayer.ConnectionServices
|
||||
{
|
||||
Logger.Write(LogLevel.Verbose, "HandleConnectRequest");
|
||||
|
||||
// open connection base on request details
|
||||
ConnectionResult result = await Connect(connectionDetails);
|
||||
|
||||
await requestContext.SendResult(result);
|
||||
try
|
||||
{
|
||||
// open connection base on request details
|
||||
ConnectionResult result = ConnectionService.Instance.Connect(connectionDetails);
|
||||
await requestContext.SendResult(result);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
await requestContext.SendError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles shutdown event by closing out any active connections
|
||||
/// </summary>
|
||||
protected async Task HandleShutdownRequest(object shutdownObj, RequestContext<object> shutdownContext)
|
||||
{
|
||||
// Go through all the existing connections and close them out
|
||||
foreach (ISqlConnection conn in ActiveConnections.Values)
|
||||
{
|
||||
conn.Close();
|
||||
}
|
||||
#endregion
|
||||
|
||||
await Task.FromResult(0);
|
||||
#region Handlers for Events from Other Services
|
||||
|
||||
public Task HandleDidChangeConfigurationNotification(
|
||||
SqlToolsSettings newSettings,
|
||||
SqlToolsSettings oldSettings,
|
||||
EventContext eventContext)
|
||||
{
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -3,10 +3,9 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.ConnectionServices.Contracts
|
||||
namespace Microsoft.SqlTools.ServiceLayer.ConnectionServices
|
||||
{
|
||||
/// <summary>
|
||||
/// Message format for the initial connection request
|
||||
@@ -43,7 +42,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ConnectionServices.Contracts
|
||||
/// <summary>
|
||||
/// Gets or sets the connection id
|
||||
/// </summary>
|
||||
public Guid ConnectionId { get; set; }
|
||||
public int ConnectionId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets any connection error messages
|
||||
@@ -45,7 +45,7 @@ namespace Microsoft.SqlTools.ServiceLayer
|
||||
WorkspaceService<SqlToolsSettings>.Instance.InitializeService(serviceHost);
|
||||
AutoCompleteService.Instance.InitializeService(serviceHost);
|
||||
LanguageService.Instance.InitializeService(serviceHost, sqlToolsContext);
|
||||
ConnectionService.Instance.Initialize(serviceHost);
|
||||
ConnectionService.Instance.InitializeService(serviceHost);
|
||||
|
||||
serviceHost.Initialize();
|
||||
serviceHost.WaitForExit();
|
||||
@@ -41,4 +41,4 @@ using System.Runtime.InteropServices;
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
[assembly: AssemblyInformationalVersion("1.0.0.0")]
|
||||
|
||||
[assembly: InternalsVisibleTo("Microsoft.SqlTools.ServiceHost.Test")]
|
||||
[assembly: InternalsVisibleTo("Microsoft.SqlTools.ServiceLayer.Test")]
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "Microsoft.SqlTools.ServiceHost",
|
||||
"name": "Microsoft.SqlTools.ServiceLayer",
|
||||
"version": "1.0.0-*",
|
||||
"buildOptions": {
|
||||
"debugType": "portable",
|
||||
Reference in New Issue
Block a user