public void Insert(object obj) { string insertSql = ClassConfigContainer.FindInsertSql(obj.GetType()); try { var classConfig = ClassConfigContainer.FindClassConfig2(obj); if (classConfig.NextIdOption == NextIdOption.Sequence) { if (string.IsNullOrEmpty(classConfig.SequenceName)) { throw new EasylinkException( "Class {0} mapping NextId option is Sequence, but no sequence name is found.", obj.GetType().Name); } SetObjectIdBeforeInsert(obj); } Command.Start(); var propertyParameters = ParametersCreator.CreatePropertyParameters(ParameterPrefix, obj, ref insertSql); var sqlParameters = ConvertPropertyParametersToSqlParameters(propertyParameters); insertSql = BeforeInsertingRecord(insertSql); var result = ExecuteScalar(insertSql, sqlParameters); if (classConfig.NextIdOption == NextIdOption.AutoIncrement) { string idPropertyName; var id = SetObjectIdAfterInsert(obj, result, out idPropertyName); propertyParameters.Add(idPropertyName, id); } if (Shared.IsAuditable(obj)) { string auditText = Auditor.AuditInsert(obj, propertyParameters); InsertAuditRecord(obj, DbOperation.Insert, auditText); } } catch (Exception ex) { throw CreateEasylinkException(insertSql, ex); } }
private void DeleteAll <T>(CriteriaNode criteriaNode) where T : new() { var deleteSql = ClassConfigContainer.FindDeleteSql(typeof(T)); try { Command.Start(); var criterias = criteriaNode.SearchAll(); if (criterias.Count > 0) { UpdateCriteria <T>(criterias); } if (Shared.IsAuditable <T>()) { var originalObjects = RetrieveAll <T>(criteriaNode); var dict = new Dictionary <T, string>(); foreach (var original in originalObjects) { string updateSql = ClassConfigContainer.FindUpdateSql1(typeof(T)); var propertyParameters = ParametersCreator.CreatePropertyParameters(ParameterPrefix, original, ref updateSql); string auditText = Auditor.AuditDelete(original, propertyParameters); dict[original] = auditText; } ExecuteNonQuery(deleteSql, criteriaNode); foreach (var original in originalObjects) { InsertAuditRecord(original, DbOperation.Delete, dict[original]); } } else { ExecuteNonQuery(deleteSql, criteriaNode); } } catch (Exception ex) { throw CreateEasylinkException(deleteSql, ex); } }
public void Delete(object obj) { if (obj == null) { return; } var deleteSql = ClassConfigContainer.FindDeleteSql1(obj.GetType()); try { Command.Start(); var propertyParameters = ParametersCreator.CreatePropertyParameters(ParameterPrefix, obj, ref deleteSql); var sqlParameters = ConvertPropertyParametersToSqlParameters(propertyParameters); var original = GetOriginal(obj); ExecuteNonQuery(deleteSql, sqlParameters); var businessBase = obj as BusinessBase; if (businessBase != null) { businessBase.MarkAsDeleted(); } if (Shared.IsAuditable(obj)) { string updateSql = ClassConfigContainer.FindUpdateSql1(obj.GetType()); propertyParameters = ParametersCreator.CreatePropertyParameters(ParameterPrefix, original, ref updateSql); string auditText = Auditor.AuditDelete(original, propertyParameters); InsertAuditRecord(original, DbOperation.Delete, auditText); } NullifyIdProperty(obj); } catch (Exception ex) { throw CreateEasylinkException(deleteSql, ex); } }
public void Update(object obj) { var id = GetObjectId(obj); if (id == null || id.ToString() == "0") { return; } var updateSql = ClassConfigContainer.FindUpdateSql1(obj.GetType()); try { Command.Start(); var propertyParameters = ParametersCreator.CreatePropertyParameters(ParameterPrefix, obj, ref updateSql); var sqlParameters = ConvertPropertyParametersToSqlParameters(propertyParameters); var changes = CompareToOriginal(obj, propertyParameters); if (changes.Keys.Count > 0) { ExecuteNonQuery(updateSql, sqlParameters); if (Shared.IsAuditable(obj)) { var auditText = Auditor.AuditUpdate(obj, changes); InsertAuditRecord(obj, DbOperation.Update, auditText); } } } catch (Exception ex) { throw CreateEasylinkException(updateSql, ex); } }