private static void SetupInsertUpdateTrigger(ITable Table) { Contract.Requires <ArgumentNullException>(Table != null, "Table"); Contract.Requires <ArgumentNullException>(Table.Columns != null, "Table.Columns"); var Columns = new StringBuilder(); var Builder = new StringBuilder(); Builder.Append("CREATE TRIGGER dbo.").Append(Table.Name).Append("_Audit_IU ON dbo.") .Append(Table.Name).Append(" FOR INSERT,UPDATE AS IF @@rowcount=0 RETURN declare @AuditType") .Append(" char(1) declare @DeletedCount int SELECT @DeletedCount=count(*) FROM DELETED IF @DeletedCount=0") .Append(" BEGIN SET @AuditType='I' END ELSE BEGIN SET @AuditType='U' END") .Append(" INSERT INTO dbo.").Append(Table.Name).Append("Audit").Append("("); string Splitter = ""; foreach (IColumn Column in Table.Columns) { Columns.Append(Splitter).Append(Column.Name); Splitter = ","; } Builder.Append(Columns.ToString()); Builder.Append(",AuditType) SELECT "); Builder.Append(Columns.ToString()); Builder.Append(",@AuditType FROM inserted"); Table.AddTrigger(Table.Name + "_Audit_IU", Builder.ToString(), TriggerType.Insert); }
/// <summary> /// Setups the columns. /// </summary> /// <param name="table">The table.</param> /// <param name="item">The item.</param> private static void SetupTriggers(ITable table, dynamic item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (table == null) { throw new ArgumentNullException(nameof(table)); } string Name = item.Name; int Type = item.Type; string Definition = item.Definition; table.AddTrigger(Name, Definition, Type.ToString(CultureInfo.InvariantCulture).To <string, TriggerType>()); }
private static void SetupDeleteTrigger(ITable Table) { Contract.Requires <ArgumentNullException>(Table != null, "Table"); Contract.Requires <ArgumentNullException>(Table.Columns != null, "Table.Columns"); var Columns = new StringBuilder(); var Builder = new StringBuilder(); Builder.Append("CREATE TRIGGER dbo.").Append(Table.Name).Append("_Audit_D ON dbo.") .Append(Table.Name).Append(" FOR DELETE AS IF @@rowcount=0 RETURN") .Append(" INSERT INTO dbo.").Append(Table.Name).Append("Audit").Append("("); string Splitter = ""; foreach (IColumn Column in Table.Columns) { Columns.Append(Splitter).Append(Column.Name); Splitter = ","; } Builder.Append(Columns.ToString()); Builder.Append(",AuditType) SELECT "); Builder.Append(Columns.ToString()); Builder.Append(",'D' FROM deleted"); Table.AddTrigger(Table.Name + "_Audit_D", Builder.ToString(), TriggerType.Delete); }
private static void SetupInsertUpdateTrigger(ITable Table) { Contract.Requires<ArgumentNullException>(Table != null, "Table"); Contract.Requires<ArgumentNullException>(Table.Columns != null, "Table.Columns"); var Columns = new StringBuilder(); var Builder = new StringBuilder(); Builder.Append("CREATE TRIGGER dbo.").Append(Table.Name).Append("_Audit_IU ON dbo.") .Append(Table.Name).Append(" FOR INSERT,UPDATE AS IF @@rowcount=0 RETURN declare @AuditType") .Append(" char(1) declare @DeletedCount int SELECT @DeletedCount=count(*) FROM DELETED IF @DeletedCount=0") .Append(" BEGIN SET @AuditType='I' END ELSE BEGIN SET @AuditType='U' END") .Append(" INSERT INTO dbo.").Append(Table.Name).Append("Audit").Append("("); string Splitter = ""; foreach (IColumn Column in Table.Columns) { Columns.Append(Splitter).Append(Column.Name); Splitter = ","; } Builder.Append(Columns.ToString()); Builder.Append(",AuditType) SELECT "); Builder.Append(Columns.ToString()); Builder.Append(",@AuditType FROM inserted"); Table.AddTrigger(Table.Name + "_Audit_IU", Builder.ToString(), TriggerType.Insert); }
private static void SetupDeleteTrigger(ITable Table) { Contract.Requires<ArgumentNullException>(Table != null, "Table"); Contract.Requires<ArgumentNullException>(Table.Columns != null, "Table.Columns"); var Columns = new StringBuilder(); var Builder = new StringBuilder(); Builder.Append("CREATE TRIGGER dbo.").Append(Table.Name).Append("_Audit_D ON dbo.") .Append(Table.Name).Append(" FOR DELETE AS IF @@rowcount=0 RETURN") .Append(" INSERT INTO dbo.").Append(Table.Name).Append("Audit").Append("("); string Splitter = ""; foreach (IColumn Column in Table.Columns) { Columns.Append(Splitter).Append(Column.Name); Splitter = ","; } Builder.Append(Columns.ToString()); Builder.Append(",AuditType) SELECT "); Builder.Append(Columns.ToString()); Builder.Append(",'D' FROM deleted"); Table.AddTrigger(Table.Name + "_Audit_D", Builder.ToString(), TriggerType.Delete); }
/// <summary> /// Setups the columns. /// </summary> /// <param name="table">The table.</param> /// <param name="item">The item.</param> private static void SetupTriggers(ITable table, dynamic item) { if (item == null) throw new ArgumentNullException(nameof(item)); if (table == null) throw new ArgumentNullException(nameof(table)); string Name = item.Name; int Type = item.Type; string Definition = item.Definition; table.AddTrigger(Name, Definition, Type.ToString(CultureInfo.InvariantCulture).To<string, TriggerType>()); }