Enable scripting for subobjects such as triggers (#1237)

* WIP 1

* Cleanups

* Change how schema is appended in to urn

* Add comment and fix incorrect condition

* Add parent type name to support Views, etc.
This commit is contained in:
Karl Burtram
2021-08-26 13:06:15 -07:00
committed by GitHub
parent 0421d3ae7c
commit 5d5debbad6
4 changed files with 75 additions and 19 deletions

View File

@@ -4,7 +4,6 @@
//
using System;
using System.Collections.Generic;
namespace Microsoft.SqlTools.ServiceLayer.Scripting.Contracts
{
@@ -52,13 +51,28 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting.Contracts
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the parent object name
/// </summary>
public string ParentName { get; set; }
/// <summary>
/// Gets or sets the parent object type name, such as Table, View, etc.
/// </summary>
public string ParentTypeName { get; set; }
public override string ToString()
{
string objectName = string.IsNullOrEmpty(this.Schema)
? this.Name
: this.Schema + "." + this.Name;
return objectName;
string objectName = string.Empty;
if (!string.IsNullOrEmpty(this.Schema))
{
objectName += this.Schema + ".";
}
if (!string.IsNullOrEmpty(this.ParentName))
{
objectName += this.ParentName + ".";
}
return objectName + this.Name;
}
public override int GetHashCode()
@@ -66,6 +80,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting.Contracts
return
StringComparer.OrdinalIgnoreCase.GetHashCode(this.Type ?? string.Empty) ^
StringComparer.OrdinalIgnoreCase.GetHashCode(this.Schema ?? string.Empty) ^
StringComparer.OrdinalIgnoreCase.GetHashCode(this.ParentName ?? string.Empty) ^
StringComparer.OrdinalIgnoreCase.GetHashCode(this.ParentTypeName ?? string.Empty) ^
StringComparer.OrdinalIgnoreCase.GetHashCode(this.Name ?? string.Empty);
}
@@ -87,6 +103,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting.Contracts
return
string.Equals(this.Type, other.Type, StringComparison.OrdinalIgnoreCase) &&
string.Equals(this.Schema, other.Schema, StringComparison.OrdinalIgnoreCase) &&
string.Equals(this.ParentName, other.ParentName, StringComparison.OrdinalIgnoreCase) &&
string.Equals(this.ParentTypeName, other.ParentTypeName, StringComparison.OrdinalIgnoreCase) &&
string.Equals(this.ParentTypeName, other.ParentTypeName, StringComparison.OrdinalIgnoreCase) &&
string.Equals(this.Name, other.Name, StringComparison.OrdinalIgnoreCase);
}
}

View File

@@ -7,6 +7,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.SqlScriptPublish;
@@ -100,15 +101,35 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
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(),
Urn.EscapeString(database),
scriptingObject.Type,
Urn.EscapeString(scriptingObject.Name),
scriptingObject.Schema != null ? string.Format("and @Schema = '{0}'", Urn.EscapeString(scriptingObject.Schema)) : string.Empty);
StringBuilder urnBuilder = new StringBuilder();
urnBuilder.AppendFormat("Server[@Name='{0}']/", server.ToUpper());
urnBuilder.AppendFormat("Database[@Name='{0}']/", Urn.EscapeString(database));
return new Urn(urn);
bool hasParentObject = !string.IsNullOrWhiteSpace(scriptingObject.ParentName)
&& !string.IsNullOrWhiteSpace(scriptingObject.ParentTypeName);
if (hasParentObject)
{
urnBuilder.AppendFormat("{0}[@Name='{1}'", scriptingObject.ParentTypeName, Urn.EscapeString(scriptingObject.ParentName));
if (!string.IsNullOrWhiteSpace(scriptingObject.Schema))
{
urnBuilder.AppendFormat(" and @Schema = '{0}'", Urn.EscapeString(scriptingObject.Schema));
}
urnBuilder.Append("]/");
}
urnBuilder.AppendFormat("{0}[@Name='{1}'", scriptingObject.Type, Urn.EscapeString(scriptingObject.Name));
// add schema to object only if there is no parent object specified
// the parent object field is only set for objects that don't have schema themselves
// so if parent is not null then the schema filter will already be set that part of the urn above
if (!string.IsNullOrWhiteSpace(scriptingObject.Schema) && !hasParentObject)
{
urnBuilder.AppendFormat(" and @Schema = '{0}'", Urn.EscapeString(scriptingObject.Schema));
}
urnBuilder.Append("]");
return new Urn(urnBuilder.ToString());
}
/// <summary>