mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 10:58:30 -05:00
Get Connection String request handler (#665)
This commit is contained in:
@@ -14,11 +14,11 @@ using System.Linq;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.SqlTools.Hosting.Protocol;
|
using Microsoft.SqlTools.Hosting.Protocol;
|
||||||
|
using Microsoft.SqlServer.Management.Common;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
|
using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
|
||||||
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
|
||||||
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
||||||
using Microsoft.SqlServer.Management.Common;
|
|
||||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||||
using Microsoft.SqlTools.Utility;
|
using Microsoft.SqlTools.Utility;
|
||||||
|
|
||||||
@@ -949,7 +949,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
serviceHost.SetRequestHandler(CancelConnectRequest.Type, HandleCancelConnectRequest);
|
serviceHost.SetRequestHandler(CancelConnectRequest.Type, HandleCancelConnectRequest);
|
||||||
serviceHost.SetRequestHandler(DisconnectRequest.Type, HandleDisconnectRequest);
|
serviceHost.SetRequestHandler(DisconnectRequest.Type, HandleDisconnectRequest);
|
||||||
serviceHost.SetRequestHandler(ListDatabasesRequest.Type, HandleListDatabasesRequest);
|
serviceHost.SetRequestHandler(ListDatabasesRequest.Type, HandleListDatabasesRequest);
|
||||||
serviceHost.SetRequestHandler(ChangeDatabaseRequest.Type, HandleChangeDatabase);
|
serviceHost.SetRequestHandler(ChangeDatabaseRequest.Type, HandleChangeDatabaseRequest);
|
||||||
|
serviceHost.SetRequestHandler(GetConnectionStringRequest.Type, HandleGetConnectionStringRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -1252,10 +1253,37 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
|||||||
return connectionBuilder;
|
return connectionBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handles a request to get a connection string for the provided connection
|
||||||
|
/// </summary>
|
||||||
|
public async Task HandleGetConnectionStringRequest(
|
||||||
|
GetConnectionStringParams connStringParams,
|
||||||
|
RequestContext<string> requestContext)
|
||||||
|
{
|
||||||
|
await Task.Run(async () =>
|
||||||
|
{
|
||||||
|
string connectionString = string.Empty;
|
||||||
|
ConnectionInfo info;
|
||||||
|
if (TryFindConnection(connStringParams.OwnerUri, out info))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
connectionString = BuildConnectionString(info.ConnectionDetails);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
await requestContext.SendError(e.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await requestContext.SendResult(connectionString);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handles a request to change the database for a connection
|
/// Handles a request to change the database for a connection
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task HandleChangeDatabase(
|
public async Task HandleChangeDatabaseRequest(
|
||||||
ChangeDatabaseParams changeDatabaseParams,
|
ChangeDatabaseParams changeDatabaseParams,
|
||||||
RequestContext<bool> requestContext)
|
RequestContext<bool> requestContext)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Parameters for the Get Connection String Request.
|
||||||
|
/// </summary>
|
||||||
|
public class GetConnectionStringParams
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// URI of the owner of the connection
|
||||||
|
/// </summary>
|
||||||
|
public string OwnerUri { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// 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.Hosting.Protocol.Contracts;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Get Connection String request
|
||||||
|
/// </summary>
|
||||||
|
public class GetConnectionStringRequest
|
||||||
|
{
|
||||||
|
public static readonly
|
||||||
|
RequestType<GetConnectionStringParams, string> Type =
|
||||||
|
RequestType<GetConnectionStringParams, string>.Create("connection/getconnectionstring");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,10 +7,11 @@ using System.Data.Common;
|
|||||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
|
||||||
using Xunit;
|
|
||||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
||||||
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||||
|
using Moq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Connection
|
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Connection
|
||||||
{
|
{
|
||||||
@@ -103,5 +104,24 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Connection
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test HandleGetConnectionStringRequest
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public async void GetCurrentConnectionStringTest()
|
||||||
|
{
|
||||||
|
// If we make a connection to a live database
|
||||||
|
ConnectionService service = ConnectionService.Instance;
|
||||||
|
var result = LiveConnectionHelper.InitLiveConnectionInfo();
|
||||||
|
var requestContext = new Mock<SqlTools.Hosting.Protocol.RequestContext<string>>();
|
||||||
|
var requestParams = new GetConnectionStringParams()
|
||||||
|
{
|
||||||
|
OwnerUri = result.ConnectionInfo.OwnerUri
|
||||||
|
};
|
||||||
|
|
||||||
|
await service.HandleGetConnectionStringRequest(requestParams, requestContext.Object);
|
||||||
|
requestContext.VerifyAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user