added support for more object scripting (#435)

* added support for more object scripting

* allow using script options in scripting

* added tests for scripting all objects
This commit is contained in:
Aditya Bist
2017-08-16 17:11:41 -07:00
committed by GitHub
parent 4e9ff42dfc
commit 3915688332
8 changed files with 247 additions and 86 deletions

View File

@@ -1,29 +1,31 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// THIS FILE IS GENERATED BY A CODEGEN TOOL. DO NOT EDIT!!!!
// IF YOU NEED TO MAKE CHANGES, EDIT THE .TT FILE!!!
// To regenerate either (1) save the .TT file from Visual Studio or (2) run the build script codegen task.
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.Scripting
{
internal partial class Scripter
{
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// THIS FILE IS GENERATED BY A CODEGEN TOOL. DO NOT EDIT!!!!
// IF YOU NEED TO MAKE CHANGES, EDIT THE .TT FILE!!!
// To regenerate either (1) save the .TT file from Visual Studio or (2) run the build script codegen task.
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.Scripting
{
internal partial class Scripter
{
private void Initialize()
{
AddSupportedType(DeclarationType.Table, GetTableScripts, "Table", "table");
AddSupportedType(DeclarationType.View, GetViewScripts, "View", "view");
AddSupportedType(DeclarationType.StoredProcedure, GetStoredProcedureScripts, "Procedure", "stored procedure");
AddSupportedType(DeclarationType.Schema, GetSchemaScripts, "Schema", "schema");
AddSupportedType(DeclarationType.Database, GetDatabaseScripts, "Database", "database");
AddSupportedType(DeclarationType.UserDefinedDataType, GetUserDefinedDataTypeScripts, "Type", "user-defined data type");
AddSupportedType(DeclarationType.UserDefinedTableType, GetUserDefinedTableTypeScripts, "Type", "user-defined table type");
AddSupportedType(DeclarationType.Synonym, GetSynonymScripts, "Synonym", "");
@@ -43,7 +45,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
{
Table smoObject = string.IsNullOrEmpty(schemaName) ? new Table(this.Database, objectName) : new Table(this.Database, objectName, schemaName);
smoObject.Refresh();
return smoObject.Script();
return (scriptingOptions == null) ? smoObject.Script() : smoObject.Script(scriptingOptions);
}
catch (Exception ex)
{
@@ -64,7 +66,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
{
View smoObject = string.IsNullOrEmpty(schemaName) ? new View(this.Database, objectName) : new View(this.Database, objectName, schemaName);
smoObject.Refresh();
return smoObject.Script();
return (scriptingOptions == null) ? smoObject.Script() : smoObject.Script(scriptingOptions);
}
catch (Exception ex)
{
@@ -85,7 +87,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
{
StoredProcedure smoObject = string.IsNullOrEmpty(schemaName) ? new StoredProcedure(this.Database, objectName) : new StoredProcedure(this.Database, objectName, schemaName);
smoObject.Refresh();
return smoObject.Script();
return (scriptingOptions == null) ? smoObject.Script() : smoObject.Script(scriptingOptions);
}
catch (Exception ex)
{
@@ -94,6 +96,48 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
}
}
/// <summary>
/// Script a Schema using SMO
/// </summary>
/// <param name="objectName">Schema name</param>
/// <param name="schemaName">Schema name</param>
/// <returns>String collection of scripts</returns>
internal StringCollection GetSchemaScripts(string objectName, string schemaName, ScriptingOptions scriptingOptions = null)
{
try
{
Schema smoObject = new Schema(this.Database, objectName);
smoObject.Refresh();
return (scriptingOptions == null) ? smoObject.Script() : smoObject.Script(scriptingOptions);
}
catch (Exception ex)
{
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetSchemaScripts : " + ex.Message);
return null;
}
}
/// <summary>
/// Script a Database using SMO
/// </summary>
/// <param name="objectName">Database name</param>
/// <param name="schemaName">Schema name</param>
/// <returns>String collection of scripts</returns>
internal StringCollection GetDatabaseScripts(string objectName, string schemaName, ScriptingOptions scriptingOptions = null)
{
try
{
Database smoObject = new Database(new Server(this.serverConnection), objectName);
smoObject.Refresh();
return (scriptingOptions == null) ? smoObject.Script() : smoObject.Script(scriptingOptions);
}
catch (Exception ex)
{
Logger.Write(LogLevel.Error,"Exception at PeekDefinition GetDatabaseScripts : " + ex.Message);
return null;
}
}
/// <summary>
/// Script a UserDefinedDataType using SMO
/// </summary>
@@ -106,7 +150,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
{
UserDefinedDataType smoObject = string.IsNullOrEmpty(schemaName) ? new UserDefinedDataType(this.Database, objectName) : new UserDefinedDataType(this.Database, objectName, schemaName);
smoObject.Refresh();
return smoObject.Script();
return (scriptingOptions == null) ? smoObject.Script() : smoObject.Script(scriptingOptions);
}
catch (Exception ex)
{
@@ -127,7 +171,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
{
UserDefinedTableType smoObject = string.IsNullOrEmpty(schemaName) ? new UserDefinedTableType(this.Database, objectName) : new UserDefinedTableType(this.Database, objectName, schemaName);
smoObject.Refresh();
return smoObject.Script();
return (scriptingOptions == null) ? smoObject.Script() : smoObject.Script(scriptingOptions);
}
catch (Exception ex)
{
@@ -148,7 +192,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
{
Synonym smoObject = string.IsNullOrEmpty(schemaName) ? new Synonym(this.Database, objectName) : new Synonym(this.Database, objectName, schemaName);
smoObject.Refresh();
return smoObject.Script();
return (scriptingOptions == null) ? smoObject.Script() : smoObject.Script(scriptingOptions);
}
catch (Exception ex)
{
@@ -169,7 +213,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
{
UserDefinedFunction smoObject = string.IsNullOrEmpty(schemaName) ? new UserDefinedFunction(this.Database, objectName) : new UserDefinedFunction(this.Database, objectName, schemaName);
smoObject.Refresh();
return smoObject.Script();
return (scriptingOptions == null) ? smoObject.Script() : smoObject.Script(scriptingOptions);
}
catch (Exception ex)
{
@@ -190,7 +234,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
{
UserDefinedFunction smoObject = string.IsNullOrEmpty(schemaName) ? new UserDefinedFunction(this.Database, objectName) : new UserDefinedFunction(this.Database, objectName, schemaName);
smoObject.Refresh();
return smoObject.Script();
return (scriptingOptions == null) ? smoObject.Script() : smoObject.Script(scriptingOptions);
}
catch (Exception ex)
{
@@ -199,6 +243,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
}
}
}
}
}
}

View File

@@ -79,12 +79,20 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
}
else
{
statement = string.Format("{0} smoObject = new {0}(this.Database, objectName);", typeProperty["Name"]);
// If it's a database
if (typeProperty["AccessClass"] == "Database")
{
statement = string.Format("{0} smoObject = new {0}(new Server(this.serverConnection), objectName);", typeProperty["Name"]);
}
else
{
statement = string.Format("{0} smoObject = new {0}(this.Database, objectName);", typeProperty["Name"]);
}
}
PushIndent(indent);
WriteLine(statement);
WriteLine("smoObject.Refresh();");
WriteLine("return smoObject.Script();");
WriteLine("return (scriptingOptions == null) ? smoObject.Script() : smoObject.Script(scriptingOptions);");
PopIndent();
WriteLine("}");

View File

@@ -28,6 +28,20 @@ SupportsSchemaQuery - Boolean denoting if the object can be queried/accessed usi
<QuickInfoType>stored procedure</QuickInfoType>
<SupportsSchemaQuery>true</SupportsSchemaQuery>
</Type>
<Type>
<Name>Schema</Name>
<CreateSyntax>Schema</CreateSyntax>
<AccessClass>Schema</AccessClass>
<QuickInfoType>schema</QuickInfoType>
<SupportsSchemaQuery>false</SupportsSchemaQuery>
</Type>
<Type>
<Name>Database</Name>
<CreateSyntax>Database</CreateSyntax>
<AccessClass>Database</AccessClass>
<QuickInfoType>database</QuickInfoType>
<SupportsSchemaQuery>false</SupportsSchemaQuery>
</Type>
<Type>
<Name>UserDefinedDataType</Name>
<CreateSyntax>Type</CreateSyntax>

View File

@@ -18,6 +18,7 @@ using Microsoft.SqlTools.ServiceLayer.LanguageServices;
using Microsoft.SqlTools.ServiceLayer.Metadata.Contracts;
using Microsoft.SqlTools.ServiceLayer.Scripting.Contracts;
using Microsoft.SqlTools.Utility;
using Microsoft.SqlServer.Management.Smo;
namespace Microsoft.SqlTools.ServiceLayer.Scripting
{
@@ -231,19 +232,27 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
{
Scripter scripter = new Scripter(bindingContext.ServerConnection, connInfo);
StringCollection results = null;
if (metadata.MetadataType == MetadataType.Table)
switch(metadata.MetadataTypeName)
{
results = scripter.GetTableScripts(metadata.Name, metadata.Schema);
case ("Table"):
results = scripter.GetTableScripts(metadata.Name, metadata.Schema);
break;
case ("View"):
results = scripter.GetViewScripts(metadata.Name, metadata.Schema);
break;
case("StoredProcedure"):
results = scripter.GetStoredProcedureScripts(metadata.Name, metadata.Schema);
break;
case("Schema"):
results = scripter.GetSchemaScripts(metadata.Name, metadata.Schema);
break;
case("Database"):
results = scripter.GetDatabaseScripts(metadata.Name, metadata.Schema);
break;
default:
results = null;
break;
}
else if (metadata.MetadataType == MetadataType.SProc)
{
results = scripter.GetStoredProcedureScripts(metadata.Name, metadata.Schema);
}
else if (metadata.MetadataType == MetadataType.View)
{
results = scripter.GetViewScripts(metadata.Name, metadata.Schema);
}
StringBuilder builder = null;
if (results != null)
{
@@ -287,7 +296,42 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
ConnectionInfo connInfo,
ObjectMetadata metadata)
{
return null;
Scripter scripter = new Scripter(bindingContext.ServerConnection, connInfo);
StringCollection results = null;
ScriptingOptions options = new ScriptingOptions();
options.ScriptDrops = true;
switch(metadata.MetadataTypeName)
{
case ("Table"):
results = scripter.GetTableScripts(metadata.Name, metadata.Schema, options);
break;
case ("View"):
results = scripter.GetViewScripts(metadata.Name, metadata.Schema, options);
break;
case("StoredProcedure"):
results = scripter.GetStoredProcedureScripts(metadata.Name, metadata.Schema, options);
break;
case("Schema"):
results = scripter.GetSchemaScripts(metadata.Name, metadata.Schema, options);
break;
case("Database"):
results = scripter.GetDatabaseScripts(metadata.Name, metadata.Schema, options);
break;
default:
results = null;
break;
}
StringBuilder builder = null;
if (results != null)
{
builder = new StringBuilder();
foreach (var result in results)
{
builder.AppendLine(result);
builder.AppendLine();
}
}
return builder != null ? builder.ToString() : null;
}
/// <summary>