// // 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 System.Collections.Generic; using System.Linq; using Microsoft.SqlServer.Management.Sdk.Sfc; using Microsoft.SqlServer.Management.SqlScriptPublish; using Microsoft.Kusto.ServiceLayer.Scripting.Contracts; using Microsoft.SqlTools.Utility; namespace Microsoft.Kusto.ServiceLayer.Scripting { /// /// Extension methods used by the scripting service. /// internal static class ScriptingExtensionMethods { /// /// Gets the status of a scripting operation for the passed scripting event. /// /// The scripting event. /// The status. public static string GetStatus(this ScriptEventArgs e) { Validate.IsNotNull("e", e); string status = null; if (e.Error != null) { status = "Error"; } else if (e.Completed) { status = "Completed"; } else { status = "Progress"; } return status; } /// /// Returns a list of ScriptingObject instances for the passed SqlScriptPublishModel instance. /// /// The sql script publish model instance. /// The list of scripting objects. public static List GetDatabaseObjects(this SqlScriptPublishModel publishModel) { Validate.IsNotNull("publishModel", publishModel); List databaseObjects = new List(); IEnumerable objectTypes = publishModel.GetDatabaseObjectTypes(); Logger.Verbose( string.Format( "Loaded SMO object type count {0}, types: {1}", objectTypes.Count(), string.Join(", ", objectTypes))); foreach (DatabaseObjectType objectType in objectTypes) { IEnumerable> databaseObjectsOfType = publishModel.EnumChildrenForDatabaseObjectType(objectType); Logger.Verbose( string.Format( "Loaded SMO urn object count {0} for type {1}, urns: {2}", objectType, databaseObjectsOfType.Count(), string.Join(", ", databaseObjectsOfType.Select(p => p.Value)))); databaseObjects.AddRange(databaseObjectsOfType.Select(d => new Urn(d.Value).ToScriptingObject())); } return databaseObjects; } /// /// Creates a SMO Urn instance based on the passed ScriptingObject instance. /// /// The scripting object instance. /// The name of the database referenced by the Urn. /// The Urn instance. public static Urn ToUrn(this ScriptingObject scriptingObject, string server, string database) { Validate.IsNotNull("scriptingObject", scriptingObject); Validate.IsNotNullOrEmptyString("server", server); Validate.IsNotNullOrWhitespaceString("scriptingObject.Name", scriptingObject.Name); Validate.IsNotNullOrWhitespaceString("scriptingObject.Type", scriptingObject.Type); // Leaving the server name blank will automatically match whatever the server SMO is running against. string urn = string.Format( "Server[@Name='{0}']/Database[@Name='{1}']/{2}[@Name='{3}' {4}]", server.ToUpper(System.Globalization.CultureInfo.InvariantCulture), Urn.EscapeString(database), scriptingObject.Type, Urn.EscapeString(scriptingObject.Name), scriptingObject.Schema != null ? string.Format("and @Schema = '{0}'", Urn.EscapeString(scriptingObject.Schema)) : string.Empty); return new Urn(urn); } /// /// Creates a ScriptingObject instance based on the passed SMO Urn instance. /// /// The urn instance. /// The scripting object instance. public static ScriptingObject ToScriptingObject(this Urn urn) { Validate.IsNotNull("urn", urn); return new ScriptingObject { Type = urn.Type, Schema = urn.GetAttribute("Schema"), Name = urn.GetAttribute("Name"), }; } } }