Files
sqltoolsservice/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs
Kevin Cunnane 402e25f77d Per editor Connect support v0.1
- Basic plumbing to support connections for a URI rather than global connections. Typical use case is editor requests to connect, but this isn't the only possible use
- Tests pass but need updating to cover new functionality, and re-enable AutoCompleteService test once there is a ServiceDiscovery component that registers and returns services. This is necessary as .Instance won't allow for dependency injection and proper testing.
2016-08-04 14:01:17 -07:00

246 lines
8.5 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Threading.Tasks;
using Microsoft.SqlTools.EditorServices.Utility;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Microsoft.SqlTools.ServiceLayer.Hosting;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.SqlContext;
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; }
private ISqlConnectionFactory Factory {get; set;}
public ConnectionDetails ConnectionDetails { get; private set; }
public DbConnection SqlConnection { get; private 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>
/// Main class for the Connection Management services
/// </summary>
public class ConnectionService
{
/// <summary>
/// Singleton service instance
/// </summary>
private static Lazy<ConnectionService> instance
= new Lazy<ConnectionService>(() => new ConnectionService());
/// <summary>
/// Gets the singleton service instance
/// </summary>
public static ConnectionService Instance
{
get
{
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
/// </summary>
private ConnectionService()
{
}
/// <summary>
/// Callback for onconnection handler
/// </summary>
/// <param name="sqlConnection"></param>
public delegate Task OnConnectionHandler(ConnectionInfo info);
/// <summary>
/// List of onconnection handlers
/// </summary>
private readonly List<OnConnectionHandler> onConnectionActivities = new List<OnConnectionHandler>();
/// <summary>
/// Gets the SQL connection factory instance
/// </summary>
public ISqlConnectionFactory ConnectionFactory
{
get
{
if (this.connectionFactory == null)
{
this.connectionFactory = new SqlConnectionFactory();
}
return this.connectionFactory;
}
}
/// <summary>
/// Test constructor that injects dependency interfaces
/// </summary>
/// <param name="testFactory"></param>
public ConnectionService(ISqlConnectionFactory testFactory)
{
this.connectionFactory = testFactory;
}
// Attempts to link a URI to an actively used connection for this URI
public bool TryFindConnection(string ownerUri, out ConnectionSummary connectionSummary)
{
connectionSummary = null;
ConnectionInfo connectionInfo;
if (this.ownerToConnectionMap.TryGetValue(ownerUri, out connectionInfo))
{
connectionSummary = CopySummary(connectionInfo.ConnectionDetails);
return true;
}
return false;
}
private static ConnectionSummary CopySummary(ConnectionSummary summary)
{
return new ConnectionSummary()
{
ServerName = summary.ServerName,
DatabaseName = summary.DatabaseName,
UserName = summary.UserName
};
}
/// <summary>
/// Open a connection with the specified connection details
/// </summary>
/// <param name="connectionParams"></param>
public ConnectResponse Connect(ConnectParams connectionParams)
{
ConnectionInfo connectionInfo;
if (ownerToConnectionMap.TryGetValue(connectionParams.OwnerUri, out connectionInfo) )
{
// TODO disconnect
}
connectionInfo = new ConnectionInfo(this.connectionFactory, connectionParams.OwnerUri, connectionParams.Connection);
// try to connect
connectionInfo.OpenConnection();
// TODO: check that connection worked
ownerToConnectionMap[connectionParams.OwnerUri] = connectionInfo;
// invoke callback notifications
foreach (var activity in this.onConnectionActivities)
{
activity(connectionInfo);
}
// return the connection result
return new ConnectResponse()
{
ConnectionId = connectionInfo.ConnectionId.ToString()
};
}
public void InitializeService(IProtocolEndpoint serviceHost)
{
// Register request and event handlers with the Service Host
serviceHost.SetRequestHandler(ConnectionRequest.Type, HandleConnectRequest);
// Register the configuration update handler
WorkspaceService<SqlToolsSettings>.Instance.RegisterConfigChangeCallback(HandleDidChangeConfigurationNotification);
}
/// <summary>
/// Add a new method to be called when the onconnection request is submitted
/// </summary>
/// <param name="activity"></param>
public void RegisterOnConnectionTask(OnConnectionHandler activity)
{
onConnectionActivities.Add(activity);
}
/// <summary>
/// Handle new connection requests
/// </summary>
/// <param name="connectionDetails"></param>
/// <param name="requestContext"></param>
/// <returns></returns>
protected async Task HandleConnectRequest(
ConnectParams connectParams,
RequestContext<ConnectResponse> requestContext)
{
Logger.Write(LogLevel.Verbose, "HandleConnectRequest");
try
{
// open connection base on request details
ConnectResponse result = ConnectionService.Instance.Connect(connectParams);
await requestContext.SendResult(result);
}
catch(Exception ex)
{
await requestContext.SendError(ex.Message);
}
}
public Task HandleDidChangeConfigurationNotification(
SqlToolsSettings newSettings,
SqlToolsSettings oldSettings,
EventContext eventContext)
{
return Task.FromResult(true);
}
/// <summary>
/// Build a connection string from a connection details instance
/// </summary>
/// <param name="connectionDetails"></param>
public static string BuildConnectionString(ConnectionDetails connectionDetails)
{
SqlConnectionStringBuilder connectionBuilder = new SqlConnectionStringBuilder();
connectionBuilder["Data Source"] = connectionDetails.ServerName;
connectionBuilder["Integrated Security"] = false;
connectionBuilder["User Id"] = connectionDetails.UserName;
connectionBuilder["Password"] = connectionDetails.Password;
connectionBuilder["Initial Catalog"] = connectionDetails.DatabaseName;
return connectionBuilder.ToString();
}
}
}