using System; using OpenDBDiff.Schema.Attributes; using OpenDBDiff.Schema.Model; using OpenDBDiff.Schema.SQLServer.Generates.Model.Util; namespace OpenDBDiff.Schema.SQLServer.Generates.Model { public class View : Code { public View(ISchemaBase parent) : base(parent, ObjectType.View, ScriptAction.AddView, ScriptAction.DropView) { Indexes = new SchemaList(this, ((Database)parent).AllObjects); Triggers = new SchemaList(this, ((Database)parent).AllObjects); CLRTriggers = new SchemaList(this, ((Database)parent).AllObjects); } /// /// Clona el objeto en una nueva instancia. /// public override ISchemaBase Clone(ISchemaBase parent) { View item = new View(parent); item.Text = this.Text; item.Status = this.Status; item.Name = this.Name; item.Id = this.Id; item.Owner = this.Owner; item.IsSchemaBinding = this.IsSchemaBinding; item.DependenciesIn = this.DependenciesIn; item.DependenciesOut = this.DependenciesOut; item.Indexes = this.Indexes.Clone(item); item.Triggers = this.Triggers.Clone(item); return item; } [SchemaNode("CLR Triggers")] public SchemaList CLRTriggers { get; set; } [SchemaNode("Triggers")] public SchemaList Triggers { get; set; } [SchemaNode("Indexes", "Index")] public SchemaList Indexes { get; set; } public override Boolean IsCodeType { get { return true; } } public override string ToSqlAdd() { string sql = ToSql(); this.Indexes.ForEach(item => { if (item.Status != ObjectStatus.Drop) { item.SetWasInsertInDiffList(ScriptAction.AddIndex); sql += item.ToSql(); } } ); this.Triggers.ForEach(item => { if (item.Status != ObjectStatus.Drop) { item.SetWasInsertInDiffList(ScriptAction.AddTrigger); sql += item.ToSql(); } } ); sql += this.ExtendedProperties.ToSql(); return sql; } public string ToSQLAlter() { return ToSQLAlter(false); } public string ToSQLAlter(Boolean quitSchemaBinding) { return FormatCode.FormatAlter("VIEW", ToSql(), this, quitSchemaBinding); } /// /// Devuelve el schema de diferencias del Schema en formato SQL. /// public override SQLScriptList ToSqlDiff(System.Collections.Generic.ICollection schemas) { SQLScriptList list = new SQLScriptList(); if (this.Status != ObjectStatus.Original) RootParent.ActionMessage.Add(this); if (this.HasState(ObjectStatus.Drop)) list.Add(Drop()); if (this.HasState(ObjectStatus.Create)) list.Add(Create()); if (this.HasState(ObjectStatus.Alter)) { if (this.HasState(ObjectStatus.RebuildDependencies)) list.AddRange(RebuildDependencies()); if (this.HasState(ObjectStatus.Rebuild)) { list.Add(Drop()); list.Add(Create()); } if (this.HasState(ObjectStatus.AlterBody)) { int iCount = DependenciesCount; list.Add(ToSQLAlter(), iCount, ScriptAction.AlterView); } if (!this.GetWasInsertInDiffList(ScriptAction.DropFunction) && (!this.GetWasInsertInDiffList(ScriptAction.AddFunction))) list.AddRange(Indexes.ToSqlDiff()); list.AddRange(Triggers.ToSqlDiff()); } return list; } } }