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
}
}
}
}
}
}