public static void Create(Db db, DbAudit a, DbAuditArgs arg) { TableAuditTypes auditType = db.GetTableAuditType(arg.Table.TableName); if(auditType == TableAuditTypes.Default) auditType = Db.DefaultTableAuditType; switch (auditType) { case TableAuditTypes.SharedTable: CreateAuditInShareTable(db, a, arg); break; case TableAuditTypes.StandaloneTable: CreateAuditStandaloneTable(db, a, arg); break; } }
static void CreateAuditInShareTable(Db db, DbAudit a, DbAuditArgs arg) { Dictionary<string, object> dic = (Dictionary<string, object>)arg.Data; DbTable table = db.GetDbTable(typeof(DbAuditDetail)); //assume newvalue and oldvalue are same size int maxValueSize = table.Fields["NEWVALUE"].Length; DataRow oldRow = null; if (arg.Action == ExecTypes.Update) { oldRow = db.SelectRow(arg.Table, dic); } INextIdProvider nextId = db.NextIdProvider; foreach (KeyValuePair<string, object> kvp in dic) { DbAuditDetail ad = new DbAuditDetail(a.Id); ad.Id = nextId.Get(); ad.FieldName = kvp.Key; string newValue = GetValue(db, arg.Table, kvp.Key, kvp.Value, maxValueSize); switch (arg.Action) { case ExecTypes.Insert: ad.NewValue = newValue; ad.Changed = false; break; case ExecTypes.Update: ad.NewValue = newValue; if (oldRow != null) { string oldValue = GetValue(db, arg.Table, kvp.Key, oldRow[kvp.Key], maxValueSize); ad.OldValue = oldValue; ad.Changed = !DbHelper.IsEqual(kvp.Value, oldRow[kvp.Key]); } break; case ExecTypes.Delete: ad.OldValue = newValue; ad.Changed = false; break; } db.Insert<DbAuditDetail>(ad); } }
public static DbAudit Create(Db db, DbAuditArgs arg) { if (!db.IsAuditEnabled(arg.Table.TableName)) return null; DbAudit a = new DbAudit(); INextIdProvider nextId = db.NextIdProvider; if (nextId != null) { a.Id = nextId.Get(); } a.Action = arg.Action.ToString(); a.TableName = arg.Table.TableName; IUserInfoProvider userInfo = db.UserInfoProvider; if (userInfo != null) { a.UserName = userInfo.UserName; a.HostName = userInfo.HostName; a.IPAddress = userInfo.IPAddress; a.OSUser = userInfo.OSUser; } IFunctionInfoProvider funcInfo = db.FunctionInfoProvider; if (funcInfo != null) { a.FunctionId = funcInfo.Id; } if (arg.Data != null && !(arg.Data is Dictionary<string, object>)) { a.Remarks = arg.Data.ToString(); } else if(arg.Action == ExecTypes.Delete) { a.Remarks = arg.Sql; } db.Insert<DbAudit>(a); //details if (arg.Data is Dictionary<string, object>) { DbAuditDetail.Create(db, a, arg); } return a; }
static void CreateAuditStandaloneTable(Db db, DbAudit a, DbAuditArgs arg) { switch (arg.Action) { case ExecTypes.Insert: case ExecTypes.Update: case ExecTypes.Delete: string historyTableName = String.Format("{0}_Audit", arg.Table.TableName); DbTable historyTable = db.GetDbTable(historyTableName); historyTable.DefaultAuditType = TableAuditTypes.None; Dictionary<string, object> dic = (Dictionary<string, object>)arg.Data; dic.Add(DbFieldNames.AuditId, a.Id); db.Insert(historyTable, dic); break; } }