//copies in another audit list to this one public void Add(String KeyPrefix, AuditFields source) { string prefix = KeyPrefix + "."; foreach (string k in source.mUpdateList.Keys) { mUpdateList[prefix + k] = source.mUpdateList[k]; } }
/// <summary> /// protected so classes that dont want to allow application to use CreateRecord can set /// the virtual property AllowDirectCreate to false /// While they still can do /// custom processing in their ovrrides of the protected method /// </summary> /// <param name="force"></param> /// <returns></returns> protected virtual long ProtectedCreateRecord(bool force) { if (mID < 1 && !mHasIdentity) { Exception x = new Exception("must set ID"); throw x; } this.PreWrite(); SqlParameterCollection oParams = CreateSqlParams(true); if (mChangeList.Count == 0 && !force) return 0; if (mWriteStmt == null) { mWriteStmt = "insert into " + mTableName + " ("; string InsertFields = ""; string InsertValues = ""; foreach (string fname in mTableDef.Keys) { if (!((GenericField)mTableDef[fname]).mReadOnly) { InsertFields += "[" + fname + "],"; InsertValues += "@" + ValidVar(fname) + ","; } } mWriteStmt += InsertFields; if (!mHasIdentity) mWriteStmt += "[" + mIdColumn + "]"; else mWriteStmt = mWriteStmt.TrimEnd(",".ToCharArray()); mWriteStmt += ") Values (" + InsertValues; if (!mHasIdentity) mWriteStmt += "@ID"; else mWriteStmt = mWriteStmt.TrimEnd(",".ToCharArray()); mWriteStmt += ")"; //has SQL return the identity if (mHasIdentity) { mWriteStmt += "; select @@IDENTITY as ID"; } } bool Recovered = false; try { if (mHasIdentity) { DataTable oTable = mSqlUtil.ExecuteSingleResultSetSQLQuery(mWriteStmt, oParams); if (oTable.Rows.Count == 0) return -1; mID = DataUtils.LongZeroIfNull(oTable.Rows[0]["ID"]); } else { oParams.AddWithValue("@ID", mID); mSqlUtil.ExecuteNoResultSetSQLQuery(mWriteStmt, oParams); } } catch (SqlException e) { if (SqlUtil.isTimeRelatedError(e.Message) && this.AutoRecover != null) { SqlRecover.SaveForRecovery(this, this.AutoRecover); Recovered = true; mID = 0; } else throw new Exception("Can't AutoRecover - " + e.Message, e); } if (AutomaticAuditHistory) { AuditFields af = new AuditFields(); af.Add("create", null, null); CreateAudit(-1, af); } mNewRecord = false; mChangeList.Clear(); if (!Recovered) RaiseSavedEvent(); return mID; }
public virtual void UpdateRecord(bool IncludeUnchangedFields) { if (mID < 0) { Exception x = new Exception("must set ID"); throw x; } this.PreWrite(); //even if its in the chnage list - it could be the app just did a few assigment stmts //and in the end the data value is still the same string[] keys = new string[mChangeList.Count]; mChangeList.Keys.CopyTo(keys, 0); //cant modify an enumeration in middle of the loop - so we copy out the keys foreach (string k in keys) { if (CompareVals(mChangeList[k], mData[k]) == 0) mChangeList.Remove(k); } if (mChangeList.Count == 0) return; ConcurrencyViolationCheck(true); //for an archive update we delete the archive and assume the data will be in the primary table. //that will only happen if the fields changed - which they may have not. One solution is //when we merge in the archived columsn to treat that as a change. But then that would mean a save //would write even when nothing really chnaged. Or we just turn on IncludeUnchnagedFIleds //on an update that had an archive. // just found out (5/2008) that you cant update a clustered index and a text field in the same stmt //so we will need to break this down into two updates. One for the archived fields and one for normail update. //this only solves the problem for archived data. if (mArchivedColumn != null) { //create a temp chnage list that only has the archived colunms //do that update and the com back and do the other update Dictionary<string, object> ArchivedChangedList = new Dictionary<string, object>(); foreach (string k in mArchivedColumn.Data.Keys) { ArchivedChangedList[k] = mData[k]; mChangeList.Remove(k); } ApplyUpdate(IncludeUnchangedFields, ArchivedChangedList); } //if we applid the update to the arcive, it could be there is no more work to do if (mChangeList.Count > 0) { ApplyUpdate(IncludeUnchangedFields, mChangeList); } DateTimeUtility.ServerDate(true); // insert an audit record if necessary if (AutomaticAuditHistory) { AuditFields af = new AuditFields(); foreach (string fname in mChangeList.Keys) { if (mTableDef.ContainsKey(fname)) { af.Add(fname, mChangeList[fname], mData[fname]); } } CreateAudit(-2, af); } //concurency time get updated mLoadTime = DateTimeUtility.ServerDate(); //if there was a column archive on this - delete the archive record. This allows // the app to update the record and not have //the update lost by a merge of archived ata that takes place on load. //this gets done only after a successful update took place if (mArchivedColumn != null) mArchivedColumn.Delete(); mChangeList.Clear(); RaiseSavedEvent(); }
public virtual void DeleteRecord() { if (mID < 0) { Exception x = new Exception("must set ID"); throw x; } if (mDeleteStmt == null) mDeleteStmt = "delete from " + mTableName + " where [" + mIdColumn + "]= @ID"; SqlParameterCollection oParams = (new SqlCommand()).Parameters; oParams.AddWithValue("@ID", mID); mSqlUtil.ExecuteNoResultSetSQLQuery(mDeleteStmt, oParams); //remove from archve also if (mArchivedColumn != null) mArchivedColumn.Delete(); if (mArchivedRecord != null) mArchivedRecord.Delete(); mArchivedRecord = null; mArchivedColumn = null; mNewRecord = true; if (AutomaticAuditHistory) { AuditFields af = new AuditFields(); af.Add("delete", null, null); CreateAudit(-3, af); } }
private void CreateAudit(int type, AuditFields af) { if (AdditionalAuditData != null) { string oldVal; string newVal; foreach (KeyValuePair<string, string[]> AuditEntry in AdditionalAuditData.UpdateList) { AuditFields.ExtractAuditValue(AuditEntry.Value, out oldVal, out newVal); af.Add((string)AuditEntry.Key, oldVal, newVal); } AdditionalAuditData = null; } string EventSrc = null; long EventID = 0; try { AuditLog al = new AuditLog((long)type, TableName + ":Audit", ID, EventSrc, EventID, af, AuditHistoryDatabase); //mLastAuditID is used for concurrency checking if (AuditHistoryDatabase == TDatabase.Unknown) mLastAuditID = al.Save(AuditLog.CompletionStatus.Success, Process.GetCurrentProcess().ProcessName, this.sqlUtil); else mLastAuditID = al.Save(AuditLog.CompletionStatus.Success, Process.GetCurrentProcess().ProcessName); } catch (Exception) { // don't crash the overall delete if the audit record failed to insert // TODO - log this error somewhere else } }
private static void AddExceptionInfo(Exception x, MethodBase ErrorOriginatingMethod, AuditFields changelist) { Process oProcess = Process.GetCurrentProcess(); changelist.Add("Exception details", x.Message, x.ToString()); changelist.Add("Program details", oProcess.MainModule.FileName, DBLocator.ActiveClientName); changelist.Add("caller details", ErrorOriginatingMethod == null ? null : ErrorOriginatingMethod.ReflectedType.FullName, ErrorOriginatingMethod == null ? null : ErrorOriginatingMethod.Name); if (x.InnerException != null) changelist.Add("Inner Exception details", x.InnerException.Message, x.InnerException.ToString()); }
public void FieldUpdate(string f, string o, string n) { if (mFieldChanges == null) mFieldChanges = new AuditFields(); mFieldChanges.Add(f, o, n); }
public void AddException(Exception x) { if (mFieldChanges == null) mFieldChanges = new AuditFields(); AuditLog.AddExceptionInfo(x, null, mFieldChanges); }
public static void AuditException(TDatabase db, Exception x, string msg, MethodBase ErrorOriginatingMethod) { try { if (bRecursiveCall) return; bRecursiveCall = true; AuditFields changelist = new AuditFields(); AddExceptionInfo(x, ErrorOriginatingMethod, changelist); string EventSrc = Environment.MachineName; long EventID = 0; AuditLog l = new AuditLog(0, "Exception", 0, EventSrc, EventID, changelist, db); l.Recover = true; l.Save(AuditLog.CompletionStatus.Exception, msg, true); } catch { } finally { bRecursiveCall = false; } }
public AuditLog(long correspondence_id, string datatype, long dataid, string EventDescript, long eid, AuditFields changeList, TDatabase Database) { mDataType = datatype; mCorrespondenceID = correspondence_id; mDataID = dataid; mFieldChanges = changeList; mEventID = eid; mEventDescription = EventDescript; mDatabase = Database; }
public AuditLog(long correspondence_id, string datatype, long dataid, string EventDescript, long eid, AuditFields changeList) : this(correspondence_id, datatype, dataid, EventDescript, eid, changeList, TDatabase.Client) { }
public AuditLog(DataRow r, bool FillDetails, TDatabase Database) { mID = DataUtils.LongZeroIfNull(r["ID"]); mDataType = (string)DataUtils.BlankIfNull(r["DataType"]); mEventDescription = (string)DataUtils.BlankIfNull(r["EventDescription"]); mCompletionMsg = (string)DataUtils.BlankIfNull(r["CompletionMsg"]); mCorrespondenceID = DataUtils.LongZeroIfNull(r["CorrespondenceID"]); mDataID = DataUtils.LongZeroIfNull(r["DataID"]); mCompletionStatus = (AuditLog.CompletionStatus)DataUtils.LongZeroIfNull(r["CompletionStatus"]); mLogTime = (DateTime)r["LogTime"]; mDatabase = Database; mFieldChanges = new AuditFields(); if (FillDetails) { SqlParameterCollection oParameters = new SqlCommand().Parameters; oParameters.AddWithValue("@ID", mID); string query = "select * from AuditDetail where AuditID = @ID"; DataTable oTable = SqlUtil.Get(mDatabase).ExecuteSingleResultSetSQLQuery(query, oParameters); if (oTable.Rows.Count == 0) return; foreach (DataRow r2 in oTable.Rows) { mFieldChanges.Add(r2); } } }