mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-24 09:35:39 -05:00
Merge from Master
This commit is contained in:
@@ -1,199 +1,199 @@
|
||||
//
|
||||
// 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.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using Microsoft.SqlServer.Management.SqlParser.Intellisense;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
{
|
||||
internal partial class PeekDefinition
|
||||
{
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
AddSupportedType(DeclarationType.Table, GetTableScripts, "Table", "table");
|
||||
AddSupportedType(DeclarationType.View, GetViewScripts, "View", "view");
|
||||
AddSupportedType(DeclarationType.StoredProcedure, GetStoredProcedureScripts, "Procedure", "stored procedure");
|
||||
AddSupportedType(DeclarationType.UserDefinedDataType, GetUserDefinedDataTypeScripts, "Type", "user-defined data type");
|
||||
AddSupportedType(DeclarationType.UserDefinedTableType, GetUserDefinedTableTypeScripts, "Type", "user-defined table type");
|
||||
AddSupportedType(DeclarationType.Synonym, GetSynonymScripts, "Synonym", "");
|
||||
AddSupportedType(DeclarationType.ScalarValuedFunction, GetScalarValuedFunctionScripts, "Function", "scalar-valued function");
|
||||
AddSupportedType(DeclarationType.TableValuedFunction, GetTableValuedFunctionScripts, "Function", "table-valued function");
|
||||
//
|
||||
// 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.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using Microsoft.SqlServer.Management.SqlParser.Intellisense;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
{
|
||||
internal partial class PeekDefinition
|
||||
{
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
AddSupportedType(DeclarationType.Table, GetTableScripts, "Table", "table");
|
||||
AddSupportedType(DeclarationType.View, GetViewScripts, "View", "view");
|
||||
AddSupportedType(DeclarationType.StoredProcedure, GetStoredProcedureScripts, "Procedure", "stored procedure");
|
||||
AddSupportedType(DeclarationType.UserDefinedDataType, GetUserDefinedDataTypeScripts, "Type", "user-defined data type");
|
||||
AddSupportedType(DeclarationType.UserDefinedTableType, GetUserDefinedTableTypeScripts, "Type", "user-defined table type");
|
||||
AddSupportedType(DeclarationType.Synonym, GetSynonymScripts, "Synonym", "");
|
||||
AddSupportedType(DeclarationType.ScalarValuedFunction, GetScalarValuedFunctionScripts, "Function", "scalar-valued function");
|
||||
AddSupportedType(DeclarationType.TableValuedFunction, GetTableValuedFunctionScripts, "Function", "table-valued function");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a Table using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">Table name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetTableScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
Table smoObject = string.IsNullOrEmpty(schemaName) ? new Table(this.Database, objectName) : new Table(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetTableScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a Table using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">Table name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetTableScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
Table smoObject = string.IsNullOrEmpty(schemaName) ? new Table(this.Database, objectName) : new Table(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetTableScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a View using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">View name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetViewScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
View smoObject = string.IsNullOrEmpty(schemaName) ? new View(this.Database, objectName) : new View(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetViewScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a View using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">View name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetViewScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
View smoObject = string.IsNullOrEmpty(schemaName) ? new View(this.Database, objectName) : new View(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetViewScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a StoredProcedure using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">StoredProcedure name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetStoredProcedureScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
StoredProcedure smoObject = string.IsNullOrEmpty(schemaName) ? new StoredProcedure(this.Database, objectName) : new StoredProcedure(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetStoredProcedureScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a StoredProcedure using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">StoredProcedure name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetStoredProcedureScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
StoredProcedure smoObject = string.IsNullOrEmpty(schemaName) ? new StoredProcedure(this.Database, objectName) : new StoredProcedure(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetStoredProcedureScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a UserDefinedDataType using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">UserDefinedDataType name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetUserDefinedDataTypeScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
UserDefinedDataType smoObject = string.IsNullOrEmpty(schemaName) ? new UserDefinedDataType(this.Database, objectName) : new UserDefinedDataType(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetUserDefinedDataTypeScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a UserDefinedDataType using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">UserDefinedDataType name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetUserDefinedDataTypeScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
UserDefinedDataType smoObject = string.IsNullOrEmpty(schemaName) ? new UserDefinedDataType(this.Database, objectName) : new UserDefinedDataType(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetUserDefinedDataTypeScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a UserDefinedTableType using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">UserDefinedTableType name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetUserDefinedTableTypeScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
UserDefinedTableType smoObject = string.IsNullOrEmpty(schemaName) ? new UserDefinedTableType(this.Database, objectName) : new UserDefinedTableType(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetUserDefinedTableTypeScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a UserDefinedTableType using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">UserDefinedTableType name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetUserDefinedTableTypeScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
UserDefinedTableType smoObject = string.IsNullOrEmpty(schemaName) ? new UserDefinedTableType(this.Database, objectName) : new UserDefinedTableType(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetUserDefinedTableTypeScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a Synonym using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">Synonym name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetSynonymScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
Synonym smoObject = string.IsNullOrEmpty(schemaName) ? new Synonym(this.Database, objectName) : new Synonym(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetSynonymScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a Synonym using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">Synonym name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetSynonymScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
Synonym smoObject = string.IsNullOrEmpty(schemaName) ? new Synonym(this.Database, objectName) : new Synonym(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetSynonymScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a ScalarValuedFunction using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">ScalarValuedFunction name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetScalarValuedFunctionScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
UserDefinedFunction smoObject = string.IsNullOrEmpty(schemaName) ? new UserDefinedFunction(this.Database, objectName) : new UserDefinedFunction(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetScalarValuedFunctionScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a ScalarValuedFunction using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">ScalarValuedFunction name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetScalarValuedFunctionScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
UserDefinedFunction smoObject = string.IsNullOrEmpty(schemaName) ? new UserDefinedFunction(this.Database, objectName) : new UserDefinedFunction(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetScalarValuedFunctionScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a TableValuedFunction using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">TableValuedFunction name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetTableValuedFunctionScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
UserDefinedFunction smoObject = string.IsNullOrEmpty(schemaName) ? new UserDefinedFunction(this.Database, objectName) : new UserDefinedFunction(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetTableValuedFunctionScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Script a TableValuedFunction using SMO
|
||||
/// </summary>
|
||||
/// <param name="objectName">TableValuedFunction name</param>
|
||||
/// <param name="schemaName">Schema name</param>
|
||||
/// <returns>String collection of scripts</returns>
|
||||
internal StringCollection GetTableValuedFunctionScripts(string objectName, string schemaName)
|
||||
{
|
||||
try
|
||||
{
|
||||
UserDefinedFunction smoObject = string.IsNullOrEmpty(schemaName) ? new UserDefinedFunction(this.Database, objectName) : new UserDefinedFunction(this.Database, objectName, schemaName);
|
||||
smoObject.Refresh();
|
||||
return smoObject.Script();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetTableValuedFunctionScripts : " + ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,144 +1,144 @@
|
||||
<#@ template debug="false" hostspecific="true" language="C#" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Xml.dll" #>
|
||||
<#@ import namespace="System" #>
|
||||
<#@ import namespace="System.Globalization" #>
|
||||
<#@ import namespace="System.Text" #>
|
||||
<#@ import namespace="System.Xml" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ import namespace="System.IO" #>
|
||||
//
|
||||
// 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.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using Microsoft.SqlServer.Management.SqlParser.Intellisense;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
{
|
||||
internal partial class PeekDefinition
|
||||
{
|
||||
|
||||
<#
|
||||
///
|
||||
/// Generate Initialize method
|
||||
///
|
||||
var indent = " ";
|
||||
var directory = Path.GetDirectoryName(Host.TemplateFile);
|
||||
string xmlFile = Path.Combine(directory, "PeekDefinitionSupportedTypes.xml");
|
||||
var supportedTypes = GetSupportedTypes(xmlFile);
|
||||
if (supportedTypes != null && supportedTypes.Count > 0)
|
||||
{
|
||||
WriteLine("private void Initialize()");
|
||||
PushIndent(indent);
|
||||
PushIndent(indent);
|
||||
WriteLine("{");
|
||||
PushIndent(indent);
|
||||
foreach(var typeProperty in supportedTypes)
|
||||
{
|
||||
string functionCall = string.Format("AddSupportedType(DeclarationType.{0}, Get{0}Scripts, \"{1}\", \"{2}\");", typeProperty["Name"], typeProperty["CreateSyntax"], typeProperty["QuickInfoType"]);
|
||||
WriteLine(functionCall);
|
||||
}
|
||||
PopIndent();
|
||||
WriteLine("}\n");
|
||||
|
||||
///
|
||||
/// Generate scriptGetters for each type
|
||||
///
|
||||
|
||||
foreach(var typeProperty in supportedTypes)
|
||||
{
|
||||
string statement;
|
||||
// Write comments
|
||||
WriteLine("/// <summary>");
|
||||
WriteLine(string.Format("/// Script a {0} using SMO", typeProperty["Name"]));
|
||||
WriteLine("/// </summary>");
|
||||
WriteLine(string.Format("/// <param name=\"objectName\">{0} name</param>", typeProperty["Name"]));
|
||||
WriteLine(string.Format("/// <param name=\"schemaName\">Schema name</param>"));
|
||||
WriteLine("/// <returns>String collection of scripts</returns>");
|
||||
|
||||
WriteLine(string.Format("internal StringCollection Get{0}Scripts(string objectName, string schemaName)", typeProperty["Name"]));
|
||||
WriteLine("{");
|
||||
PushIndent(indent);
|
||||
|
||||
// Write try block to retrieve object and return script
|
||||
WriteLine("try");
|
||||
WriteLine("{");
|
||||
if(typeProperty["SupportsSchemaQuery"].IndexOf("true", StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
{
|
||||
statement = string.Format("{0} smoObject = string.IsNullOrEmpty(schemaName) ? new {0}(this.Database, objectName) : new {0}(this.Database, objectName, schemaName);", typeProperty["AccessClass"]);
|
||||
}
|
||||
else
|
||||
{
|
||||
statement = string.Format("{0} smoObject = new {0}(this.Database, objectName);", typeProperty["Name"]);
|
||||
}
|
||||
PushIndent(indent);
|
||||
WriteLine(statement);
|
||||
WriteLine("smoObject.Refresh();");
|
||||
WriteLine("return smoObject.Script();");
|
||||
PopIndent();
|
||||
WriteLine("}");
|
||||
|
||||
// Write catch block to catch and log exceptions
|
||||
WriteLine("catch (Exception ex)");
|
||||
WriteLine("{");
|
||||
PushIndent(indent);
|
||||
statement = string.Format("LogLevel.Error,\"Exception at PeekDefinition Get{0}Scripts : \" + ex.Message", typeProperty["Name"]);
|
||||
WriteLine("Logger.Write(" + statement + ");");
|
||||
WriteLine("return null;");
|
||||
PopIndent();
|
||||
WriteLine("}");
|
||||
PopIndent();
|
||||
WriteLine("}\n");
|
||||
}
|
||||
}
|
||||
PopIndent();
|
||||
PopIndent();
|
||||
#>
|
||||
}
|
||||
}
|
||||
<#+
|
||||
///
|
||||
/// Get the supported types from the xml file
|
||||
///
|
||||
public static List<Dictionary<string, string>> GetSupportedTypes(string xmlFile)
|
||||
{
|
||||
List<Dictionary<string, string>> typeList = null;
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.Load(xmlFile);
|
||||
XmlNodeList supportedTypes = doc.SelectNodes("/SupportedTypes/Type");
|
||||
if (supportedTypes != null)
|
||||
{
|
||||
typeList = new List<Dictionary<string, string>>();
|
||||
foreach (var type in supportedTypes)
|
||||
{
|
||||
XmlElement node = type as XmlElement;
|
||||
if (node != null)
|
||||
{
|
||||
string typeName = (node["Name"] != null) ? node["Name"].InnerText : null;
|
||||
string createSyntax = (node["CreateSyntax"] != null) ? node["CreateSyntax"].InnerText : null;
|
||||
string accessClass = (node["AccessClass"] != null) ? node["AccessClass"].InnerText : null;
|
||||
string supportsSchemaQuery = (node["SupportsSchemaQuery"] != null) ? node["SupportsSchemaQuery"].InnerText : null;
|
||||
string quickInfoType = (node["QuickInfoType"] != null) ? node["QuickInfoType"].InnerText : null;
|
||||
if (typeName != null && createSyntax != null && accessClass != null && supportsSchemaQuery!= null)
|
||||
{
|
||||
Dictionary<string, string> typeProperties = new Dictionary<string, string>();
|
||||
typeProperties.Add("Name", typeName);
|
||||
typeProperties.Add("CreateSyntax", createSyntax);
|
||||
typeProperties.Add("AccessClass", accessClass);
|
||||
typeProperties.Add("SupportsSchemaQuery", supportsSchemaQuery);
|
||||
typeProperties.Add("QuickInfoType", quickInfoType);
|
||||
typeList.Add(typeProperties);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return typeList;
|
||||
}
|
||||
|
||||
<#@ template debug="false" hostspecific="true" language="C#" #>
|
||||
<#@ output extension=".cs" #>
|
||||
<#@ assembly name="System.Xml.dll" #>
|
||||
<#@ import namespace="System" #>
|
||||
<#@ import namespace="System.Globalization" #>
|
||||
<#@ import namespace="System.Text" #>
|
||||
<#@ import namespace="System.Xml" #>
|
||||
<#@ import namespace="System.Collections.Generic" #>
|
||||
<#@ import namespace="System.IO" #>
|
||||
//
|
||||
// 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.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using Microsoft.SqlServer.Management.SqlParser.Intellisense;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
{
|
||||
internal partial class PeekDefinition
|
||||
{
|
||||
|
||||
<#
|
||||
///
|
||||
/// Generate Initialize method
|
||||
///
|
||||
var indent = " ";
|
||||
var directory = Path.GetDirectoryName(Host.TemplateFile);
|
||||
string xmlFile = Path.Combine(directory, "PeekDefinitionSupportedTypes.xml");
|
||||
var supportedTypes = GetSupportedTypes(xmlFile);
|
||||
if (supportedTypes != null && supportedTypes.Count > 0)
|
||||
{
|
||||
WriteLine("private void Initialize()");
|
||||
PushIndent(indent);
|
||||
PushIndent(indent);
|
||||
WriteLine("{");
|
||||
PushIndent(indent);
|
||||
foreach(var typeProperty in supportedTypes)
|
||||
{
|
||||
string functionCall = string.Format("AddSupportedType(DeclarationType.{0}, Get{0}Scripts, \"{1}\", \"{2}\");", typeProperty["Name"], typeProperty["CreateSyntax"], typeProperty["QuickInfoType"]);
|
||||
WriteLine(functionCall);
|
||||
}
|
||||
PopIndent();
|
||||
WriteLine("}\n");
|
||||
|
||||
///
|
||||
/// Generate scriptGetters for each type
|
||||
///
|
||||
|
||||
foreach(var typeProperty in supportedTypes)
|
||||
{
|
||||
string statement;
|
||||
// Write comments
|
||||
WriteLine("/// <summary>");
|
||||
WriteLine(string.Format("/// Script a {0} using SMO", typeProperty["Name"]));
|
||||
WriteLine("/// </summary>");
|
||||
WriteLine(string.Format("/// <param name=\"objectName\">{0} name</param>", typeProperty["Name"]));
|
||||
WriteLine(string.Format("/// <param name=\"schemaName\">Schema name</param>"));
|
||||
WriteLine("/// <returns>String collection of scripts</returns>");
|
||||
|
||||
WriteLine(string.Format("internal StringCollection Get{0}Scripts(string objectName, string schemaName)", typeProperty["Name"]));
|
||||
WriteLine("{");
|
||||
PushIndent(indent);
|
||||
|
||||
// Write try block to retrieve object and return script
|
||||
WriteLine("try");
|
||||
WriteLine("{");
|
||||
if(typeProperty["SupportsSchemaQuery"].IndexOf("true", StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
{
|
||||
statement = string.Format("{0} smoObject = string.IsNullOrEmpty(schemaName) ? new {0}(this.Database, objectName) : new {0}(this.Database, objectName, schemaName);", typeProperty["AccessClass"]);
|
||||
}
|
||||
else
|
||||
{
|
||||
statement = string.Format("{0} smoObject = new {0}(this.Database, objectName);", typeProperty["Name"]);
|
||||
}
|
||||
PushIndent(indent);
|
||||
WriteLine(statement);
|
||||
WriteLine("smoObject.Refresh();");
|
||||
WriteLine("return smoObject.Script();");
|
||||
PopIndent();
|
||||
WriteLine("}");
|
||||
|
||||
// Write catch block to catch and log exceptions
|
||||
WriteLine("catch (Exception ex)");
|
||||
WriteLine("{");
|
||||
PushIndent(indent);
|
||||
statement = string.Format("LogLevel.Error,\"Exception at PeekDefinition Get{0}Scripts : \" + ex.Message", typeProperty["Name"]);
|
||||
WriteLine("Logger.Write(" + statement + ");");
|
||||
WriteLine("return null;");
|
||||
PopIndent();
|
||||
WriteLine("}");
|
||||
PopIndent();
|
||||
WriteLine("}\n");
|
||||
}
|
||||
}
|
||||
PopIndent();
|
||||
PopIndent();
|
||||
#>
|
||||
}
|
||||
}
|
||||
<#+
|
||||
///
|
||||
/// Get the supported types from the xml file
|
||||
///
|
||||
public static List<Dictionary<string, string>> GetSupportedTypes(string xmlFile)
|
||||
{
|
||||
List<Dictionary<string, string>> typeList = null;
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.Load(xmlFile);
|
||||
XmlNodeList supportedTypes = doc.SelectNodes("/SupportedTypes/Type");
|
||||
if (supportedTypes != null)
|
||||
{
|
||||
typeList = new List<Dictionary<string, string>>();
|
||||
foreach (var type in supportedTypes)
|
||||
{
|
||||
XmlElement node = type as XmlElement;
|
||||
if (node != null)
|
||||
{
|
||||
string typeName = (node["Name"] != null) ? node["Name"].InnerText : null;
|
||||
string createSyntax = (node["CreateSyntax"] != null) ? node["CreateSyntax"].InnerText : null;
|
||||
string accessClass = (node["AccessClass"] != null) ? node["AccessClass"].InnerText : null;
|
||||
string supportsSchemaQuery = (node["SupportsSchemaQuery"] != null) ? node["SupportsSchemaQuery"].InnerText : null;
|
||||
string quickInfoType = (node["QuickInfoType"] != null) ? node["QuickInfoType"].InnerText : null;
|
||||
if (typeName != null && createSyntax != null && accessClass != null && supportsSchemaQuery!= null)
|
||||
{
|
||||
Dictionary<string, string> typeProperties = new Dictionary<string, string>();
|
||||
typeProperties.Add("Name", typeName);
|
||||
typeProperties.Add("CreateSyntax", createSyntax);
|
||||
typeProperties.Add("AccessClass", accessClass);
|
||||
typeProperties.Add("SupportsSchemaQuery", supportsSchemaQuery);
|
||||
typeProperties.Add("QuickInfoType", quickInfoType);
|
||||
typeList.Add(typeProperties);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return typeList;
|
||||
}
|
||||
|
||||
#>
|
||||
Reference in New Issue
Block a user