private object RetrieveOriginal(object obj, Criteria criteria) { var selectSql = ClassConfigContainer.FindSelectSql(obj.GetType()); criteria = UpdateCriteria(obj.GetType(), criteria); var table = RetrieveTable(selectSql, new CriteriaNode(null) { Criteria = criteria }); var list = ObjectMapper.MapTableToList(obj.GetType(), table); if (list.Count == 0) { return(null); } if (list.Count > 1) { throw new ServerException("More than one objects matched with criteria are retrieved."); } return(list[0]); }
internal List <PropertyConfig> GetDerivedPropertyConfigs() { if (_derivedPropertyConfigs == null) { if (_linkType == LinkType.Property) { return(new List <PropertyConfig>()); } _derivedPropertyConfigs = new List <PropertyConfig>(); var rightClassConfig = ClassConfigContainer.FindClassConfig(RightType); foreach (var property in rightClassConfig.Properties) { if (!property.IsDerived) { var derived = CreateDerivedProperty(property); _derivedPropertyConfigs.Add(derived); } } } return(_derivedPropertyConfigs); }
private static string CreateUpdateAuditText(object obj, Dictionary <string, string> changes) { var temp = new List <string>(); foreach (var key in changes.Keys) { var classConfig = ClassConfigContainer.FindClassConfig2(obj); PropertyConfig propertyConfig = classConfig.GetPropertyConfig(key); if (propertyConfig == null) { throw new EasylinkException( "Error occurred when create update audit text. property name {0} is not defined in the {1} mapping.", key, obj.GetType().Name); } string item = string.Format("{0} {1}", propertyConfig.ColumnName, changes[key]); temp.Add(item); } return("Updated " + string.Join(", ", temp) + "."); }
private static List <string> CreateColumnTexts(object obj, Dictionary <string, object> parameters) { var temp = new List <string>(); foreach (var key in parameters.Keys) { var classConfig = ClassConfigContainer.FindClassConfig2(obj); PropertyConfig propertyConfig = classConfig.GetPropertyConfig(key); if (propertyConfig == null) { throw new EasylinkException( "Error occurred when create create column texts. property name {0} is not defined in the {1} mapping.", key, obj.GetType().Name); } string item = string.Format("{0}={1}", propertyConfig.ColumnName, parameters[key]); temp.Add(item); } return(temp); }
private static string GetRecordId(object obj) { var isAuditable = Shared.IsAuditable(obj); if (isAuditable) { var classsConfig = ClassConfigContainer.FindClassConfig2(obj); if (classsConfig == null) { throw new EasylinkException("class {0} mapping file is not found!", obj.GetType().Name); } var key = classsConfig.IdPropertyName; if (string.IsNullOrEmpty(key)) { throw new EasylinkException("class {0} mapping Id property is not set.", obj.GetType().Name); } var property = obj.GetType().GetProperty(key); var propertyValue = property.GetValue(obj, null); return(propertyValue.ToString()); } throw new EasylinkException("Record key is not set."); }
private static void CheckIfAuditTableExists(Type auditRecordType, DatabaseType databaseType) { var classConfig = ClassConfigContainer.FindClassConfig(auditRecordType); var table = classConfig.TableName; var sql = string.Empty; if (databaseType != DatabaseType.Oracle) { sql = string.Format("SELECT COUNT(*) " + "FROM INFORMATION_SCHEMA.TABLES " + "WHERE TABLE_SCHEMA = '[Schema]' AND TABLE_NAME = '{0}' OR TABLE_NAME = '{1}' ", table.ToLower(), table); } else { sql = string.Format("SELECT COUNT(*) FROM ALL_TABLES WHERE OWNER ='[Schema]' AND TABLE_NAME ='{0}'", table); } var database = DatabaseCreator.CreateDatabase(); var result = database.ExecuteScalar(sql); if (result.ToString() == "0") { throw new EasylinkException("Audit table {0} does not exist in the database.", table); } }
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 object GetObjectId(object obj) { var classConfig = ClassConfigContainer.FindClassConfig2(obj); var idPropertyName = classConfig.IdPropertyName; var property = obj.GetType().GetProperty(idPropertyName); return(property.GetValue(obj, null)); }
private string GetTableName() { if (IsDerived) { return(_link.Alias); } var myClassConfig = ClassConfigContainer.FindClassConfig(_type); return(myClassConfig.TableName); }
private void SetObjectIdBeforeInsert(object obj) { var objectType = obj.GetType(); var id = RetrieveNextId(objectType); var idPropertyName = ClassConfigContainer.FindIdPropertyNamer(objectType); SetObjectId(obj, idPropertyName, id); }
private object GetOriginal(object obj) { var classConfig = ClassConfigContainer.FindClassConfig(obj.GetType()); string idPropertyName = classConfig.IdPropertyName; var propertyInfo = obj.GetType().GetProperty(idPropertyName); object idPropertyValue = propertyInfo.GetValue(obj, null); return(RetrieveOriginal(obj, new Criteria(idPropertyName, "=", idPropertyValue))); }
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); } }
private List <Criteria> UpdateCriteria <T>(List <Criteria> criterias) { var found = ClassConfigContainer.FindClassConfig1 <T>(); foreach (var criteria in criterias) { PropertyConfig propertyConfig = found.GetPropertyConfig(criteria.PropertyName); if (propertyConfig == null) { var temp = criteria.PropertyName.Split(new[] { '.' }).ToList(); var propertyName = temp[temp.Count - 1]; temp.RemoveAt(temp.Count - 1); var linkName = string.Join(".", temp); var link = found.FindMatchedLink(linkName); if (link != null) { var classConfig = ClassConfigContainer.FindClassConfig(link.RightType); var propertyConfig1 = classConfig.GetPropertyConfig(propertyName); if (propertyConfig1 == null) { throw new EasylinkException( "Error occurred when updating criteria. property name {0} is not defined in the {1} mapping.", criteria.PropertyName, link.RightType.Name); } criteria.SetColumnName(string.Format("{0}.{1}", link.Alias, propertyConfig1.ColumnName)); } else { throw new EasylinkException( "Error occurred when updating criteria. link name {0} is not defined in the {1} mapping.", linkName, typeof(T).Name); } } else { criteria.SetColumnName(propertyConfig.SelectColumn.Trim()); } } return(criterias); }
internal string GetRightColumnName() { var classConfig = ClassConfigContainer.FindClassConfig(RightType); var propertyConfig = classConfig.GetPropertyConfig(RightPropertyName); if (propertyConfig == null) { throw new EasylinkException("Property name {0} is not defined in {1} mapping class.", RightPropertyName, RightType.Name); } return(propertyConfig.ColumnName); }
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); } }
private List <T> RetrieveAll <T>(CriteriaNode criteriaNode) where T : new() { var selectSql = ClassConfigContainer.FindSelectSql <T>(); var criterias = criteriaNode.SearchAll(); if (criterias.Count > 0) { UpdateCriteria <T>(criterias); } var table = RetrieveTable(selectSql, criteriaNode); return(ObjectMapper.MapTableToList <T>(table)); }
private static AuditBase CreateAuditRecordBase(object obj, DbOperation operation, string auditText) { var auditRecord = new AuditBase { RecordId = GetRecordId(obj), TableName = ClassConfigContainer.FindClassConfig2(obj).TableName, Description = auditText, TimeStamp = DateTime.Now, Operation = operation.ToString(), ObjectToAudit = obj }; return(auditRecord); }
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); } }
private string GetColumnName() { var getFromClassConfig = ClassConfigContainer.FindClassConfig(_getFromType); var getFromPropertyConfig = getFromClassConfig.GetPropertyConfig(_getFromProperty); if (getFromPropertyConfig == null) { throw new EasylinkException("Property [{0}] in class [{1}] column mapping is not set.", _getFromProperty, _getFromType.Name); } var columnName = getFromPropertyConfig.ColumnName; return(columnName); }
private static long SetObjectIdAfterInsert(object obj, object result, out string idPropertyName) { long id = 0; var succeeded = Int64.TryParse(result.ToString(), out id); if (succeeded == false) { throw new EasylinkException( "Error occured when setting object id for {0}, database does not return number!", obj.GetType().Name); } idPropertyName = ClassConfigContainer.FindIdPropertyNamer(obj.GetType()); SetObjectId(obj, idPropertyName, id); return(id); }
private Criteria UpdateCriteria(Type objectType, Criteria criteria) { var found = ClassConfigContainer.FindClassConfig(objectType); PropertyConfig propertyConfig = found.GetPropertyConfig(criteria.PropertyName); if (propertyConfig == null) { var temp = criteria.PropertyName.Split(new[] { '.' }).ToList(); var propertyName = temp[temp.Count - 1]; temp.RemoveAt(temp.Count - 1); var linkName = string.Join(".", temp); var link = found.FindMatchedLink(linkName); if (link != null) { var classConfig = ClassConfigContainer.FindClassConfig(link.RightType); var propertyConfig1 = classConfig.GetPropertyConfig(propertyName); criteria.SetColumnName(string.Format("{0}.{1}", link.Alias, propertyConfig1.ColumnName)); } else { throw new EasylinkException( "Error occurred when updating criteria. property name {0} or link name{1} is not found in the {2} mapping.", criteria.PropertyName, linkName, objectType.Name); } } else { criteria.SetColumnName(propertyConfig.SelectColumn.Trim()); } return(criteria); }
private void NullifyIdProperty(object obj) { var classConfig = ClassConfigContainer.FindClassConfig2(obj); var idPropertyName = classConfig.IdPropertyName; var property = obj.GetType().GetProperty(idPropertyName); if (property.PropertyType == typeof(string) || property.PropertyType == typeof(int?) || property.PropertyType == typeof(Int64?) || property.PropertyType == typeof(DateTime?)) { property.SetValue(obj, null, null); } if (property.PropertyType == typeof(int) || property.PropertyType == typeof(long)) { property.SetValue(obj, -1, null); } if (property.PropertyType == typeof(DateTime)) { property.SetValue(obj, DateTime.MinValue, null); } }
internal static bool IsAuditable <T>( ) where T : new() { var classConfig = ClassConfigContainer.FindClassConfig(typeof(T)); return(classConfig.Auditable); }
internal static bool IsAuditable(object obj) { var classConfig = ClassConfigContainer.FindClassConfig2(obj); return(classConfig.Auditable); }
internal override string FindNextIdSql(Type objectType) { string sequenceName = ClassConfigContainer.FindClassConfig(objectType).SequenceName; return(string.Format("SELECT nextval('{0}') as NextId", sequenceName)); }
internal override string FindNextIdSql(Type objectType) { string sequenceName = ClassConfigContainer.FindClassConfig(objectType).SequenceName; return(string.Format("Select [Schema].{0}.NEXTVAL As NextId From DUAL", sequenceName)); }
internal override string FindNextIdSql(Type objectType) { string sequenceName = ClassConfigContainer.FindClassConfig(objectType).SequenceName; return(string.Format("SELECT Next VALUE FOR [Schema].{0} as NextId", sequenceName)); }
internal string GetRightTableName() { return(ClassConfigContainer.FindClassConfig(RightType).TableName); }