mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 09:59:48 -05:00
Adding new handler for script as requests (#552)
* added new scripting handler for script as request *change the language service to get the peek definition using scripting as operatin
This commit is contained in:
@@ -6,23 +6,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Microsoft.SqlServer.Management.SqlScriptPublish;
|
||||
using Microsoft.SqlTools.ServiceLayer.Scripting.Contracts;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
using static Microsoft.SqlServer.Management.SqlScriptPublish.SqlScriptOptions;
|
||||
using Microsoft.SqlServer.Management.Common;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Scripting
|
||||
{
|
||||
/// <summary>
|
||||
/// Class to represent an in-progress script operation.
|
||||
/// </summary>
|
||||
public sealed class ScriptingScriptOperation : ScriptingOperation
|
||||
public sealed class ScriptingScriptOperation : SmoScriptingOperation
|
||||
{
|
||||
private bool disposed = false;
|
||||
|
||||
private int scriptedObjectCount = 0;
|
||||
|
||||
@@ -30,35 +25,15 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
|
||||
|
||||
private int eventSequenceNumber = 1;
|
||||
|
||||
public ScriptingScriptOperation(ScriptingParams parameters)
|
||||
public ScriptingScriptOperation(ScriptingParams parameters): base(parameters)
|
||||
{
|
||||
Validate.IsNotNull("parameters", parameters);
|
||||
|
||||
this.Parameters = parameters;
|
||||
}
|
||||
|
||||
private ScriptingParams Parameters { get; set; }
|
||||
|
||||
public string ScriptText { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Event raised when a scripting operation has resolved which database objects will be scripted.
|
||||
/// </summary>
|
||||
public event EventHandler<ScriptingPlanNotificationParams> PlanNotification;
|
||||
|
||||
/// <summary>
|
||||
/// Event raised when a scripting operation has made forward progress.
|
||||
/// </summary>
|
||||
public event EventHandler<ScriptingProgressNotificationParams> ProgressNotification;
|
||||
|
||||
/// <summary>
|
||||
/// Event raised when a scripting operation is complete.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// An event can be completed by the following conditions: success, cancel, error.
|
||||
/// </remarks>
|
||||
public event EventHandler<ScriptingCompleteParams> CompleteNotification;
|
||||
|
||||
public override void Execute()
|
||||
{
|
||||
SqlScriptPublishModel publishModel = null;
|
||||
@@ -141,10 +116,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
|
||||
}
|
||||
}
|
||||
|
||||
private void SendCompletionNotificationEvent(ScriptingCompleteParams parameters)
|
||||
protected override void SendCompletionNotificationEvent(ScriptingCompleteParams parameters)
|
||||
{
|
||||
this.SetCommonEventProperties(parameters);
|
||||
this.CompleteNotification?.Invoke(this, parameters);
|
||||
base.SendCompletionNotificationEvent(parameters);
|
||||
}
|
||||
|
||||
private void SendPlanNotificationEvent(ScriptingPlanNotificationParams parameters)
|
||||
@@ -153,10 +128,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
|
||||
this.PlanNotification?.Invoke(this, parameters);
|
||||
}
|
||||
|
||||
private void SendProgressNotificationEvent(ScriptingProgressNotificationParams parameters)
|
||||
protected override void SendProgressNotificationEvent(ScriptingProgressNotificationParams parameters)
|
||||
{
|
||||
this.SetCommonEventProperties(parameters);
|
||||
this.ProgressNotification?.Invoke(this, parameters);
|
||||
base.SendProgressNotificationEvent(parameters);
|
||||
}
|
||||
|
||||
private void SetCommonEventProperties(ScriptingEventParams parameters)
|
||||
@@ -242,108 +217,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
|
||||
return publishModel;
|
||||
}
|
||||
|
||||
private string GetServerNameFromLiveInstance(string connectionString)
|
||||
{
|
||||
string serverName = null;
|
||||
using(SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
ServerConnection serverConnection = new ServerConnection(connection);
|
||||
serverName = serverConnection.TrueName;
|
||||
}
|
||||
catch (SqlException e)
|
||||
{
|
||||
Logger.Write(
|
||||
LogLevel.Verbose,
|
||||
string.Format("Exception getting server name", e));
|
||||
}
|
||||
}
|
||||
|
||||
Logger.Write(LogLevel.Verbose, string.Format("Resolved server name '{0}'", serverName));
|
||||
return serverName;
|
||||
}
|
||||
|
||||
private static void PopulateAdvancedScriptOptions(ScriptOptions scriptOptionsParameters, SqlScriptOptions advancedOptions)
|
||||
{
|
||||
if (scriptOptionsParameters == null)
|
||||
{
|
||||
Logger.Write(LogLevel.Verbose, "No advanced options set, the ScriptOptions object is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (PropertyInfo optionPropInfo in scriptOptionsParameters.GetType().GetProperties())
|
||||
{
|
||||
PropertyInfo advancedOptionPropInfo = advancedOptions.GetType().GetProperty(optionPropInfo.Name);
|
||||
if (advancedOptionPropInfo == null)
|
||||
{
|
||||
Logger.Write(LogLevel.Warning, string.Format("Invalid property info name {0} could not be mapped to a property on SqlScriptOptions.", optionPropInfo.Name));
|
||||
continue;
|
||||
}
|
||||
|
||||
object optionValue = optionPropInfo.GetValue(scriptOptionsParameters, index: null);
|
||||
if (optionValue == null)
|
||||
{
|
||||
Logger.Write(LogLevel.Verbose, string.Format("Skipping ScriptOptions.{0} since value is null", optionPropInfo.Name));
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
// The ScriptOptions property types from the request will be either a string or a bool?.
|
||||
// The SqlScriptOptions property types from SMO will all be an Enum. Using reflection, we
|
||||
// map the request ScriptOptions values to the SMO SqlScriptOptions values.
|
||||
//
|
||||
|
||||
try
|
||||
{
|
||||
object smoValue = null;
|
||||
if (optionPropInfo.PropertyType == typeof(bool?))
|
||||
{
|
||||
smoValue = (bool)optionValue ? BooleanTypeOptions.True : BooleanTypeOptions.False;
|
||||
}
|
||||
else
|
||||
{
|
||||
smoValue = Enum.Parse(advancedOptionPropInfo.PropertyType, (string)optionValue, ignoreCase: true);
|
||||
}
|
||||
|
||||
Logger.Write(LogLevel.Verbose, string.Format("Setting ScriptOptions.{0} to value {1}", optionPropInfo.Name, smoValue));
|
||||
advancedOptionPropInfo.SetValue(advancedOptions, smoValue);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Write(
|
||||
LogLevel.Warning,
|
||||
string.Format("An exception occurred setting option {0} to value {1}: {2}", optionPropInfo.Name, optionValue, e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateScriptDatabaseParams()
|
||||
{
|
||||
try
|
||||
{
|
||||
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(this.Parameters.ConnectionString);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new ArgumentException(SR.ScriptingParams_ConnectionString_Property_Invalid, e);
|
||||
}
|
||||
if (this.Parameters.FilePath == null && this.Parameters.ScriptDestination != "ToEditor")
|
||||
{
|
||||
throw new ArgumentException(SR.ScriptingParams_FilePath_Property_Invalid);
|
||||
}
|
||||
else if (this.Parameters.FilePath != null && this.Parameters.ScriptDestination != "ToEditor")
|
||||
{
|
||||
if (!Directory.Exists(Path.GetDirectoryName(this.Parameters.FilePath)))
|
||||
{
|
||||
throw new ArgumentException(SR.ScriptingParams_FilePath_Property_Invalid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPublishModelScriptError(object sender, ScriptEventArgs e)
|
||||
{
|
||||
this.CancellationToken.ThrowIfCancellationRequested();
|
||||
@@ -425,16 +298,5 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the scripting operation.
|
||||
/// </summary>
|
||||
public override void Dispose()
|
||||
{
|
||||
if (!disposed)
|
||||
{
|
||||
this.Cancel();
|
||||
disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user