Add create model request (#1603)

* add create model request
This commit is contained in:
Barbara Valdez
2022-07-29 12:04:16 -07:00
committed by GitHub
parent 8eedac2b82
commit 11dd29d8a0
4 changed files with 222 additions and 1 deletions

View File

@@ -0,0 +1,39 @@
//
// 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.ServiceLayer.Utility;
namespace Microsoft.SqlTools.ServiceLayer.DacFx.Contracts
{
/// <summary>
/// Parameters to generate a SQL model
/// </summary>
public class GenerateTSqlModelParams
{
/// <summary>
/// URI of the project file this model is for
/// </summary>
public string ProjectUri { get; set; }
/// <summary>
/// The version of Sql Server to target
/// </summary>
public string ModelTargetVersion { get; set; }
/// <summary>
/// Gets or sets the Sql script file paths.
/// </summary>
public string[] FilePaths { get; set; }
}
/// <summary>
/// Defines the generate sql model request
/// </summary>
class GenerateTSqlModelRequest
{
public static readonly RequestType<GenerateTSqlModelParams, ResultStatus> Type =
RequestType<GenerateTSqlModelParams, ResultStatus>.Create("dacFx/generateTSqlModel");
}
}

View File

@@ -11,6 +11,8 @@ using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
using Microsoft.SqlTools.ServiceLayer.Hosting;
using Microsoft.SqlTools.ServiceLayer.TaskServices;
using Microsoft.SqlServer.Dac.Model;
using Microsoft.SqlTools.ServiceLayer.Utility;
using DacTableDesigner = Microsoft.Data.Tools.Sql.DesignServices.TableDesigner.TableDesigner;
namespace Microsoft.SqlTools.ServiceLayer.DacFx
@@ -25,6 +27,11 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
private static readonly Lazy<DacFxService> instance = new Lazy<DacFxService>(() => new DacFxService());
private readonly Lazy<ConcurrentDictionary<string, DacFxOperation>> operations =
new Lazy<ConcurrentDictionary<string, DacFxOperation>>(() => new ConcurrentDictionary<string, DacFxOperation>());
/// <summary>
/// <see cref="ConcurrentDictionary{String, TSqlModel}"/> that maps project uri to model
/// </summary>
public Lazy<ConcurrentDictionary<string, TSqlModel>> projectModels =
new Lazy<ConcurrentDictionary<string, TSqlModel>>(() => new ConcurrentDictionary<string, TSqlModel>());
/// <summary>
/// Gets the singleton instance object
@@ -50,6 +57,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
serviceHost.SetRequestHandler(ValidateStreamingJobRequest.Type, this.HandleValidateStreamingJobRequest);
serviceHost.SetRequestHandler(GetDefaultPublishOptionsRequest.Type, this.HandleGetDefaultPublishOptionsRequest);
serviceHost.SetRequestHandler(ParseTSqlScriptRequest.Type, this.HandleParseTSqlScriptRequest);
serviceHost.SetRequestHandler(GenerateTSqlModelRequest.Type, this.HandleGenerateTSqlModelRequest);
}
/// <summary>
@@ -323,6 +331,26 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx
}
}
public async Task HandleGenerateTSqlModelRequest(GenerateTSqlModelParams requestParams, RequestContext<ResultStatus> requestContext)
{
try
{
GenerateTSqlModelOperation operation = new GenerateTSqlModelOperation(requestParams);
TSqlModel model = operation.GenerateTSqlModel();
projectModels.Value[operation.Parameters.ProjectUri] = model;
await requestContext.SendResult(new ResultStatus
{
Success = true,
ErrorMessage = null
});
}
catch (Exception e)
{
await requestContext.SendError(e);
}
}
private void ExecuteOperation(DacFxOperation operation, DacFxParams parameters, string taskName, RequestContext<DacFxResult> requestContext)
{
Task.Run(async () =>

View File

@@ -0,0 +1,53 @@
//
// 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.Diagnostics;
using Microsoft.SqlTools.ServiceLayer.DacFx.Contracts;
using Microsoft.SqlTools.Utility;
using Microsoft.SqlServer.Dac.Model;
namespace Microsoft.SqlTools.ServiceLayer.DacFx
{
/// <summary>
/// Class to represent creating a dacfx model
/// </summary>
class GenerateTSqlModelOperation
{
public GenerateTSqlModelParams Parameters { get; }
public GenerateTSqlModelOperation(GenerateTSqlModelParams parameters)
{
Validate.IsNotNull("parameters", parameters);
this.Parameters = parameters;
}
/// <summary>
/// Generate model from sql files, if no sql files are passed in then it generates an empty model.
/// </summary>
public TSqlModel GenerateTSqlModel()
{
try
{
TSqlModelOptions options = new TSqlModelOptions();
SqlServerVersion version = (SqlServerVersion)Enum.Parse(typeof(SqlServerVersion), Parameters.ModelTargetVersion);
var model = new TSqlModel(version, options);
// read all sql files
foreach (string filePath in Parameters.FilePaths)
{
string fileContent = System.IO.File.ReadAllText(filePath);
model.AddOrUpdateObjects(fileContent, filePath, null);
}
return model;
}
catch (Exception ex)
{
Logger.Write(TraceEventType.Information, $"Failed to generate model. Error: {ex.Message}");
throw;
}
}
}
}