Example #1
0
        private void AddDatabaseContext(StringCollection output, SmoScriptingContext ctx)
        {
            string batch = String.Format("USE [{0}]", ctx.Connection.DatabaseName);

            output.Add(batch);
            AddLineEnding(output);
        }
Example #2
0
        private SqlSmoObject ViewObj(SmoScriptingContext ctx)
        {
            var view = new View(ctx.Database, ctx.Metadata.Name, ctx.Metadata.Schema);

            view.Refresh();

            return(view);
        }
Example #3
0
        private SqlSmoObject TableObj(SmoScriptingContext ctx)
        {
            var table = new Table(ctx.Database, ctx.Metadata.Name, ctx.Metadata.Schema);

            table.Refresh();

            return(table);
        }
Example #4
0
        private SqlSmoObject FunctionObj(SmoScriptingContext ctx)
        {
            var function = new UserDefinedFunction(ctx.Database, ctx.Metadata.Name, ctx.Metadata.Schema);

            function.Refresh();

            return(function);
        }
Example #5
0
        private SqlSmoObject StoredProcedureObj(SmoScriptingContext ctx)
        {
            var storedProcedure = new StoredProcedure(ctx.Database, ctx.Metadata.Name, ctx.Metadata.Schema);

            storedProcedure.Refresh();

            return(storedProcedure);
        }
Example #6
0
        private void AddAnsiNulls(StringCollection output, SmoScriptingContext ctx)
        {
            bool?value = ctx.Metadata.AnsiNullsStatus;

            if (value.HasValue)
            {
                output.Add(String.Format("SET ANSI_NULLS {0}", value.Value ? "ON" : "OFF"));
                AddLineEnding(output);
            }
        }
Example #7
0
        private void AddQuotedIdentifier(StringCollection output, SmoScriptingContext ctx)
        {
            bool?value = ctx.Metadata.QuotedIdentifierStatus;

            if (value.HasValue)
            {
                output.Add(String.Format("SET QUOTED_IDENTIFIER {0}", value.Value ? "ON" : "OFF"));
                AddLineEnding(output);
            }
        }
Example #8
0
        private SqlSmoObject TriggerObj(SmoScriptingContext ctx)
        {
            var table = new Table(ctx.Database, ctx.Metadata.ParentName, ctx.Metadata.ParentSchema);

            table.Refresh();

            var trigger = new Trigger(table, ctx.Metadata.Name);

            trigger.Refresh();

            return(trigger);
        }
Example #9
0
        public SmoScriptableObject Create(SmoScriptingContext context)
        {
            ObjectFactory <SmoScriptableObject> factory = null;
            SmoObjectType type = context.Metadata.Type;

            if (!_factories.TryGetValue(type, out factory))
            {
                throw new InvalidOperationException("Unknown type of object");
            }

            SmoScriptableObject obj = factory(context);

            return(obj);
        }
Example #10
0
        public override StringCollection Script(SmoScriptingContext context)
        {
            var textObject = ScriptedObject as ITextObject;

            if (textObject == null)
            {
                throw new InvalidOperationException(
                          String.Format("ScriptedObject '{0}' cannot be scripted as alter", ScriptedObject.GetType().Name));
            }

            StringCollection result = Script(textObject, context);

            return(result);
        }
Example #11
0
        private StringCollection Script(ITextObject obj, SmoScriptingContext ctx)
        {
            var output = new StringCollection();

            if (ctx.ScriptDatabaseContext)
            {
                AddDatabaseContext(output, ctx);
                AddBatchSeparator(output, ctx);
            }

            AddAnsiNulls(output, ctx);
            AddBatchSeparator(output, ctx);
            AddQuotedIdentifier(output, ctx);
            AddBatchSeparator(output, ctx);
            AddHeader(output, obj);
            AddBody(output, obj);

            return(output);
        }
Example #12
0
        public override StringCollection Script(SmoScriptingContext context)
        {
            var scripter = new Microsoft.SqlServer.Management.Smo.Scripter(context.Server);

            Microsoft.SqlServer.Management.Smo.ScriptingOptions options = scripter.Options;

            options.IncludeDatabaseContext = context.ScriptDatabaseContext;

            StringCollection scriptingResult = scripter.Script(new[] { ScriptedObject });

            var result = new StringCollection();

            foreach (string scriptedBatch in scriptingResult)
            {
                result.Add(scriptedBatch);
                AddLineEnding(result);
                AddLineEnding(result);
            }

            return(result);
        }
Example #13
0
        private string Script(IDbConnection connection, string schema, string name)
        {
            var metadata = new SmoObjectMetadata(schema, name);

            metadata.Initialize(connection);

            var context = new SmoScriptingContext(connection, metadata);

            SmoScriptableObject obj     = _objectFactory.Create(context);
            StringCollection    batches = obj.Script(context);

            var builder = new StringBuilder();

            foreach (string batch in batches)
            {
                builder.Append(batch);
            }

            string result = builder.ToString();

            return(result);
        }
Example #14
0
 public void AddBatchSeparator(StringCollection output, SmoScriptingContext ctx)
 {
     output.Add(ctx.Connection.BatchSeparator);
     AddLineEnding(output);
 }
Example #15
0
 public abstract StringCollection Script(SmoScriptingContext context);