simplify drop object request handler (#1953)

* simplify drop object request handler

* fix test cases

* fix issues

* update strings

* fix error

* fix error
This commit is contained in:
Alan Ren
2023-03-20 21:54:34 -07:00
committed by GitHub
parent b4781cf267
commit 692f444ccb
14 changed files with 187 additions and 271 deletions

View File

@@ -0,0 +1,32 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#nullable disable
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement.Contracts
{
public class DropRequestParams : GeneralRequestDetails
{
/// <summary>
/// SFC (SMO) URN identifying the object
/// </summary>
public string ObjectUrn { get; set; }
/// <summary>
/// Connection uri
/// </summary>
public string ConnectionUri { get; set; }
/// <summary>
/// Whether to throw an error if the object does not exist. The default value is false.
/// </summary>
public bool ThrowIfNotExist { get; set; } = false;
}
public class DropRequest
{
public static readonly RequestType<DropRequestParams, bool> Type = RequestType<DropRequestParams, bool>.Create("objectManagement/drop");
}
}

View File

@@ -11,7 +11,9 @@ using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlTools.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Management;
using Microsoft.SqlTools.ServiceLayer.ObjectManagement.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
@@ -48,6 +50,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
{
this.serviceHost = serviceHost;
this.serviceHost.SetRequestHandler(RenameRequest.Type, HandleRenameRequest, true);
this.serviceHost.SetRequestHandler(DropRequest.Type, HandleDropRequest, true);
}
/// <summary>
@@ -58,40 +61,74 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
/// <returns></returns>
internal async Task HandleRenameRequest(RenameRequestParams requestParams, RequestContext<bool> requestContext)
{
Logger.Verbose("Handle Request in HandleProcessRenameEditRequest()");
ConnectionInfo connInfo;
if (ConnectionServiceInstance.TryFindConnection(
requestParams.ConnectionUri,
out connInfo))
Logger.Verbose("Handle Request in HandleRenameRequest()");
ExecuteActionOnObject(requestParams.ConnectionUri, requestParams.ObjectUrn, (dbObject) =>
{
ServerConnection serverConnection = ConnectionService.OpenServerConnection(connInfo, ObjectManagementServiceApplicationName);
using (serverConnection.SqlConnectionObject)
var renamable = dbObject as IRenamable;
if (renamable != null)
{
IRenamable renameObject = this.GetRenamable(requestParams, serverConnection);
renameObject.Rename(requestParams.NewName);
renamable.Rename(requestParams.NewName);
}
}
else
{
Logger.Error($"The connection {requestParams.ConnectionUri} could not be found.");
throw new Exception(SR.ErrorConnectionNotFound);
}
else
{
throw new Exception(SR.ObjectNotRenamable(requestParams.ObjectUrn));
}
});
await requestContext.SendResult(true);
}
/// <summary>
/// Method to get the sql object, which should be renamed
/// Method to handle the delete object request
/// </summary>
/// <param name="requestParams">parameters which are required for the rename operation</param>
/// <param name="connection">the server connection on the server to search for the sqlobject</param>
/// <returns>the sql object if implements the interface IRenamable, so they can be renamed</returns>
private IRenamable GetRenamable(RenameRequestParams requestParams, ServerConnection connection)
/// <param name="requestParams">parameters which are needed to execute deletion operation</param>
/// <param name="requestContext">Request Context</param>
/// <returns></returns>
internal async Task HandleDropRequest(DropRequestParams requestParams, RequestContext<bool> requestContext)
{
Server server = new Server(connection);
SqlSmoObject dbObject = server.GetSmoObject(new Urn(requestParams.ObjectUrn));
return (IRenamable)dbObject;
Logger.Verbose("Handle Request in HandleDeleteRequest()");
ConnectionInfo connectionInfo = this.GetConnectionInfo(requestParams.ConnectionUri);
using (CDataContainer dataContainer = CDataContainer.CreateDataContainer(connectionInfo, databaseExists: true))
{
try
{
dataContainer.SqlDialogSubject = dataContainer.Server?.GetSmoObject(requestParams.ObjectUrn);
DatabaseUtils.DoDropObject(dataContainer);
}
catch (FailedOperationException ex)
{
if (ex.InnerException is MissingObjectException && requestParams.ThrowIfNotExist)
{
throw;
}
}
}
await requestContext.SendResult(true);
}
private ConnectionInfo GetConnectionInfo(string connectionUri)
{
ConnectionInfo connInfo;
if (ConnectionServiceInstance.TryFindConnection(connectionUri, out connInfo))
{
return connInfo;
}
else
{
Logger.Error($"The connection with URI '{connectionUri}' could not be found.");
throw new Exception(SR.ErrorConnectionNotFound);
}
}
private void ExecuteActionOnObject(string connectionUri, string objectUrn, Action<SqlSmoObject> action)
{
ConnectionInfo connInfo = this.GetConnectionInfo(connectionUri);
ServerConnection serverConnection = ConnectionService.OpenServerConnection(connInfo, ObjectManagementServiceApplicationName);
using (serverConnection.SqlConnectionObject)
{
Server server = new Server(serverConnection);
SqlSmoObject dbObject = server.GetSmoObject(new Urn(objectUrn));
action(dbObject);
}
}
}
}