Feature rename sql objects (#1686)

This commit is contained in:
M-Patrone
2022-09-22 23:42:23 +02:00
committed by GitHub
parent ec1bb0e9ec
commit 88eaa64755
6 changed files with 294 additions and 1 deletions

View File

@@ -40,6 +40,7 @@ using Microsoft.SqlTools.ServiceLayer.ModelManagement;
using Microsoft.SqlTools.ServiceLayer.TableDesigner;
using Microsoft.SqlTools.ServiceLayer.AzureBlob;
using Microsoft.SqlTools.ServiceLayer.ExecutionPlan;
using Microsoft.SqlTools.ServiceLayer.ObjectManagement;
using System.IO;
namespace Microsoft.SqlTools.ServiceLayer
@@ -179,6 +180,9 @@ namespace Microsoft.SqlTools.ServiceLayer
ExecutionPlanService.Instance.InitializeService(serviceHost);
serviceProvider.RegisterSingleService(ExecutionPlanService.Instance);
ObjectManagementService.Instance.InitializeService(serviceHost);
serviceProvider.RegisterSingleService(ObjectManagementService.Instance);
serviceHost.InitializeRequestHandlers();
}

View File

@@ -9613,6 +9613,14 @@ namespace Microsoft.SqlTools.ServiceLayer
}
}
public static string ErrorConnectionNotFound
{
get
{
return Keys.GetString(Keys.ErrorConnectionNotFound);
}
}
public static string ConnectionServiceListDbErrorNotConnected(string uri)
{
return Keys.GetString(Keys.ConnectionServiceListDbErrorNotConnected, uri);
@@ -13878,6 +13886,8 @@ namespace Microsoft.SqlTools.ServiceLayer
public const string GetUserDefinedObjectsFromModelFailed = "GetUserDefinedObjectsFromModelFailed";
public const string ErrorConnectionNotFound = "ErrorConnectionNotFound";
private Keys()
{ }

View File

@@ -2415,4 +2415,7 @@ TableDesignerConfirmationText = I have read the summary and understand the poten
SqlProjectModelNotFound(string projectUri) = Could not find SQL model from project: {0}.
UnsupportedModelType(string type) = Unsupported model type: {0}.
GetUserDefinedObjectsFromModelFailed = Failed to get user defined objects from model.
GetUserDefinedObjectsFromModelFailed = Failed to get user defined objects from model.
#ObjectManagement Service
ErrorConnectionNotFound = The connection could not be found

View File

@@ -0,0 +1,29 @@
//
// 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;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement.Contracts
{
public class RenameRequestParams : GeneralRequestDetails
{
/// <summary>
/// SFC (SMO) URN identifying the object
/// </summary>
public string ObjectUrn { get; set; }
/// <summary>
/// the new name of the object
/// </summary>
public string NewName { get; set; }
/// <summary>
/// Connection uri
/// </summary>
public string ConnectionUri { get; set; }
}
public class RenameRequest
{
public static readonly RequestType<RenameRequestParams, bool> Type = RequestType<RenameRequestParams, bool>.Create("objectmanagement/rename");
}
}

View File

@@ -0,0 +1,98 @@
//
// 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.Threading.Tasks;
using Microsoft.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
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.ObjectManagement.Contracts;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
{
/// <summary>
/// Main class for ObjectManagement Service functionality
/// </summary>
public class ObjectManagementService
{
private const string ObjectManagementServiceApplicationName = "azdata-object-management";
private static Lazy<ObjectManagementService> objectManagementServiceInstance = new Lazy<ObjectManagementService>(() => new ObjectManagementService());
public static ObjectManagementService Instance => objectManagementServiceInstance.Value;
public static ConnectionService connectionService;
private IProtocolEndpoint serviceHost;
public ObjectManagementService() { }
/// <summary>
/// Internal for testing purposes only
/// </summary>
internal static ConnectionService ConnectionServiceInstance
{
get
{
connectionService ??= ConnectionService.Instance;
return connectionService;
}
set
{
connectionService = value;
}
}
public void InitializeService(IProtocolEndpoint serviceHost)
{
this.serviceHost = serviceHost;
this.serviceHost.SetRequestHandler(RenameRequest.Type, HandleRenameRequest);
}
/// <summary>
/// Method to handle the renaming operation
/// </summary>
/// <param name="requestParams">parameters which are needed to execute renaming operation</param>
/// <param name="requestContext">Request Context</param>
/// <returns></returns>
internal async Task HandleRenameRequest(RenameRequestParams requestParams, RequestContext<bool> requestContext)
{
Logger.Verbose("Handle Request in HandleProcessRenameEditRequest()");
ConnectionInfo connInfo;
if (connectionService.TryFindConnection(
requestParams.ConnectionUri,
out connInfo))
{
using (SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo, ObjectManagementServiceApplicationName))
{
IRenamable renameObject = this.GetRenamable(requestParams, sqlConn);
renameObject.Rename(requestParams.NewName);
}
}
else
{
Logger.Error($"The connection {requestParams.ConnectionUri} could not be found.");
throw new Exception(SR.ErrorConnectionNotFound);
}
await requestContext.SendResult(true);
}
/// <summary>
/// Method to get the sql object, which should be renamed
/// </summary>
/// <param name="requestParams">parameters which are required for the rename operation</param>
/// <param name="connection">the sqlconnection 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, SqlConnection connection)
{
ServerConnection serverConnection = new ServerConnection(connection);
Server server = new Server(serverConnection);
SqlSmoObject dbObject = server.GetSmoObject(new Urn(requestParams.ObjectUrn));
return (IRenamable)dbObject;
}
}
}