mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-20 01:25:41 -05:00
Add Attach Database functionality to Object Management Service (#2193)
This commit is contained in:
@@ -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.
|
||||
//
|
||||
|
||||
#nullable disable
|
||||
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement.Contracts
|
||||
{
|
||||
public class DatabaseFileData
|
||||
{
|
||||
public string DatabaseName { get; set; }
|
||||
public string[] DatabaseFilePaths { get; set; }
|
||||
public string Owner { get; set; }
|
||||
}
|
||||
|
||||
public class AttachDatabaseRequestParams
|
||||
{
|
||||
public string ConnectionUri { get; set; }
|
||||
public DatabaseFileData[] Databases { get; set; }
|
||||
public bool GenerateScript { get; set; }
|
||||
}
|
||||
|
||||
public class AttachDatabaseRequest
|
||||
{
|
||||
public static readonly RequestType<AttachDatabaseRequestParams, string> Type = RequestType<AttachDatabaseRequestParams, string>.Create("objectManagement/attachDatabase");
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
|
||||
this.serviceHost.SetRequestHandler(DisposeViewRequest.Type, HandleDisposeViewRequest, true);
|
||||
this.serviceHost.SetRequestHandler(SearchRequest.Type, HandleSearchRequest, true);
|
||||
this.serviceHost.SetRequestHandler(DetachDatabaseRequest.Type, HandleDetachDatabaseRequest, true);
|
||||
this.serviceHost.SetRequestHandler(AttachDatabaseRequest.Type, HandleAttachDatabaseRequest, true);
|
||||
this.serviceHost.SetRequestHandler(DropDatabaseRequest.Type, HandleDropDatabaseRequest, true);
|
||||
}
|
||||
|
||||
@@ -155,7 +156,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
|
||||
}
|
||||
|
||||
SearchableObjectTypeDescription desc = SearchableObjectTypeDescription.GetDescription(searchableObjectType);
|
||||
|
||||
|
||||
if (desc.IsDatabaseObject)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(requestParams.Schema))
|
||||
@@ -207,6 +208,13 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
|
||||
await requestContext.SendResult(sqlScript);
|
||||
}
|
||||
|
||||
internal async Task HandleAttachDatabaseRequest(AttachDatabaseRequestParams requestParams, RequestContext<string> requestContext)
|
||||
{
|
||||
var handler = this.GetObjectTypeHandler(SqlObjectType.Database) as DatabaseHandler;
|
||||
var sqlScript = handler.Attach(requestParams);
|
||||
await requestContext.SendResult(sqlScript);
|
||||
}
|
||||
|
||||
internal async Task HandleDropDatabaseRequest(DropDatabaseRequestParams requestParams, RequestContext<string> requestContext)
|
||||
{
|
||||
var handler = this.GetObjectTypeHandler(SqlObjectType.Database) as DatabaseHandler;
|
||||
|
||||
@@ -20,6 +20,7 @@ using Microsoft.SqlTools.Utility;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility.SqlScriptFormatters;
|
||||
using System.Collections.Specialized;
|
||||
using Microsoft.SqlTools.SqlCore.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
|
||||
@@ -393,6 +394,60 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
public string Attach(AttachDatabaseRequestParams attachParams)
|
||||
{
|
||||
var sqlScript = string.Empty;
|
||||
ConnectionInfo connectionInfo = this.GetConnectionInfo(attachParams.ConnectionUri);
|
||||
using (var dataContainer = CreateDatabaseDataContainer(attachParams.ConnectionUri, null, true, null))
|
||||
{
|
||||
var server = dataContainer.Server!;
|
||||
var originalExecuteMode = server.ConnectionContext.SqlExecutionModes;
|
||||
if (attachParams.GenerateScript)
|
||||
{
|
||||
server.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql;
|
||||
server.ConnectionContext.CapturedSql.Clear();
|
||||
}
|
||||
try
|
||||
{
|
||||
foreach (var database in attachParams.Databases)
|
||||
{
|
||||
var fileCollection = new StringCollection();
|
||||
fileCollection.AddRange(database.DatabaseFilePaths);
|
||||
if (database.Owner != SR.general_default)
|
||||
{
|
||||
server.AttachDatabase(database.DatabaseName, fileCollection, database.Owner);
|
||||
}
|
||||
else
|
||||
{
|
||||
server.AttachDatabase(database.DatabaseName, fileCollection);
|
||||
}
|
||||
}
|
||||
if (attachParams.GenerateScript)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
var capturedText = server.ConnectionContext.CapturedSql.Text;
|
||||
foreach (var entry in capturedText)
|
||||
{
|
||||
if (entry != null)
|
||||
{
|
||||
builder.AppendLine(entry);
|
||||
}
|
||||
}
|
||||
sqlScript = builder.ToString();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (attachParams.GenerateScript)
|
||||
{
|
||||
server.ConnectionContext.SqlExecutionModes = originalExecuteMode;
|
||||
}
|
||||
dataContainer.ServerConnection.Disconnect();
|
||||
}
|
||||
}
|
||||
return sqlScript;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used to drop the specified database
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user