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

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

            view.Refresh();

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

            table.Refresh();

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

            function.Refresh();

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

            storedProcedure.Refresh();

            return(storedProcedure);
        }
Exemple #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);
            }
        }
Exemple #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);
            }
        }
Exemple #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);
        }
Exemple #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);
        }
Exemple #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);
        }
Exemple #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);
        }
Exemple #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);
        }
Exemple #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);
        }
Exemple #14
0
 public void AddBatchSeparator(StringCollection output, SmoScriptingContext ctx)
 {
     output.Add(ctx.Connection.BatchSeparator);
     AddLineEnding(output);
 }
Exemple #15
0
 public abstract StringCollection Script(SmoScriptingContext context);