private ITypeFieldValueList ExtractCurrentRowValues(IReadOnlyEntity entity, Type type, ITransaction tx) { ITypeFieldValueList fieldValueList = null; ITypeFieldValueList keyFieldValueList = OperationUtils.ExtractEntityTypeKeyValues(entity, type); IDbCommand cmd = null; IDataReader reader = null; try { cmd = CreateRetrievalPreparedStatement(keyFieldValueList, tx); reader = cmd.ExecuteReader(); if (reader.Read()) { fieldValueList = ReadValues(type, reader); } } catch (Exception ex) { string message = String.Format("SQL Exception while trying retrieve current data row from {0}" , entity.GetType().FullName); throw new StatementExecutionException(message, ex); } finally { DbMgtUtility.Close(reader); DbMgtUtility.Close(cmd); } return(fieldValueList); }
private Object ExtractCurrentVersionValue(IReadOnlyEntity entity, IColumn versionColumn, Type type , ITransaction tx) { Object versionValue = null; ITypeFieldValueList keyFieldValueList = OperationUtils.ExtractEntityTypeKeyValues(entity, type); IDbCommand cmd = null; IDataReader reader = null; try { cmd = CreateRetrievalPreparedStatement(keyFieldValueList, tx); reader = cmd.ExecuteReader(); if (reader.Read()) { versionValue = DbLayer.DataManipulate().ReadFromResultSet(reader, versionColumn); } } catch (Exception ex) { String message = String.Format("SQL Exception while trying retrieve version information from {0}" , entity.GetType().FullName); throw new StatementExecutionException(message, ex); } finally { DbMgtUtility.Close(reader); DbMgtUtility.Close(cmd); } return(versionValue); }
private void LoadFromDb(IReadOnlyEntity roEntity, IDataReader reader, ITransaction tx) { EntityInfo entityInfo = CacheManager.GetEntityInfo(roEntity); while (entityInfo != null) { string tableName = entityInfo.TableInfo.TableName; if (entityInfo.EntityType == roEntity.GetType() || tableName == null) //if i==0 that means it's base class and can use existing result set { LoadForType(roEntity, entityInfo.EntityType, reader, tx); } else { IDbCommand superCmd = null; IDataReader superReader = null; try { ITypeFieldValueList keyValueList = OperationUtils.ExtractEntityTypeKeyValues(roEntity, entityInfo.EntityType); superCmd = CreateRetrievalPreparedStatement(keyValueList, tx); superReader = superCmd.ExecuteReader(); if (superReader.Read()) { LoadForType(roEntity, entityInfo.EntityType, superReader, tx); } else { string message = String.Format( "Super class {0} does not contains a matching record for the base class {1}", entityInfo.EntityType.FullName, roEntity.GetType().FullName); throw new NoMatchingRecordFoundForSuperClassException(message); } } catch (Exception ex) { String message = String.Format("SQL Exception while trying to read from table {0}", tableName); throw new ReadFromResultSetException(message, ex); } finally { DbMgtUtility.Close(superReader); DbMgtUtility.Close(superCmd); } } entityInfo = entityInfo.SuperEntityInfo; } }
/// <summary> /// Compare equality through ID /// </summary> /// <param name="other">Entity to compare.</param> /// <returns>true if are equals</returns> /// <remarks> /// Two entities are equals if they are of the same hierarchy tree/sub-tree /// and has same id. /// </remarks> public virtual bool Equals(IReadOnlyEntity <IdT> other) { if (null == other || (!GetType().IsInstanceOfType(other) && !other.GetType().IsInstanceOfType(this))) { return(false); } if (ReferenceEquals(this, other)) { return(true); } bool otherIsTransient = Equals(other.ID, default(IdT)); bool thisIsTransient = IsTransient(); if (otherIsTransient && thisIsTransient) { return(ReferenceEquals(other, this)); } return(other.ID.Equals(ID)); }
public void LoadChildrenFromRelation(IReadOnlyEntity parentRoEntity, Type entityType, ITransaction tx , IRelation relation, bool lazy) { EntityInfo entityInfo = CacheManager.GetEntityInfo(entityType); IEntityContext entityContext = parentRoEntity.Context; PropertyInfo property = entityInfo.GetProperty(relation.AttributeName); Object value = ReflectionUtils.GetValue(property, parentRoEntity); if (!lazy && relation.FetchStrategy == FetchStrategy.Lazy) { CreateProxy(parentRoEntity, entityType, tx, relation, value, property); return; } ICollection <IReadOnlyEntity> children = ReadRelationChildrenFromDb(parentRoEntity, entityType, tx, relation); if (entityContext != null && !relation.ReverseRelationship) { foreach (IReadOnlyEntity childEntity in children) { ITypeFieldValueList valueTypeList = OperationUtils.ExtractRelationKeyValues(childEntity, relation); if (valueTypeList != null) { entityContext.ChangeTracker.AddChildEntityKey(valueTypeList); } } } if ((value == null || ProxyUtil.IsProxyType(value.GetType())) && ReflectionUtils.IsImplementInterface(property.PropertyType, typeof(ICollection <>))) { Type propertyType = property.PropertyType; if (propertyType.IsInterface) { Type generic = propertyType.GetGenericArguments()[0]; propertyType = typeof(List <>).MakeGenericType(new Type[] { generic }); } value = Activator.CreateInstance(propertyType); IList genCollection = (IList)value; foreach (IReadOnlyEntity serverRoDbClass in children) { genCollection.Add(serverRoDbClass); } ReflectionUtils.SetValue(property, parentRoEntity, genCollection); } else if (value != null && ReflectionUtils.IsImplementInterface(property.PropertyType, typeof(ICollection <>))) { IList genCollection = (IList)value; foreach (IReadOnlyEntity serverRoDbClass in children) { genCollection.Add(serverRoDbClass); } } else { IEnumerator <IReadOnlyEntity> childEnumarator = children.GetEnumerator(); if (childEnumarator.MoveNext()) { IReadOnlyEntity singleRoDbClass = childEnumarator.Current; if (property.PropertyType.IsAssignableFrom(singleRoDbClass.GetType())) { ReflectionUtils.SetValue(property, parentRoEntity, singleRoDbClass); } else { string message = singleRoDbClass.GetType().FullName + " is not matching the getter " + property.Name; Logger.GetLogger(Config.LoggerName).Fatal(message); throw new NoSetterFoundToSetChildObjectListException(message); } } } }
public EntityFieldValueList(IReadOnlyEntity entity) : base(entity.GetType()) { _entity = entity; }
private static void SetParentRelationFieldsForNonIdentifyingRelations(IEntity parentEntity , IEnumerable <IEntity> childObjects, RelationColumnMapping mapping) { IReadOnlyEntity firstObject = null; IEnumerator <IEntity> childEnumerator = childObjects.GetEnumerator(); if (childEnumerator.MoveNext()) { firstObject = childEnumerator.Current; } if (firstObject == null) { return; } EntityInfo parentInfo = CacheManager.GetEntityInfo(parentEntity); EntityInfo childInfo = CacheManager.GetEntityInfo(firstObject); PropertyInfo setter = null; bool foundOnce = false; while (parentInfo != null) { IColumn parentMatchedColumn = parentInfo.FindColumnByAttribute(mapping.FromField); if (parentMatchedColumn != null) { foundOnce = true; setter = parentInfo.GetProperty(parentMatchedColumn); } parentInfo = parentInfo.SuperEntityInfo; } if (!foundOnce) { string message = String.Format("The field {0} does not have a matching field in the object {1}", mapping.ToField, firstObject.GetType().FullName); throw new NoMatchingColumnFoundException(message); } foundOnce = false; while (childInfo != null) { IColumn childMatchedColumn = childInfo.FindColumnByAttribute(mapping.ToField); if (childMatchedColumn != null) { foundOnce = true; foreach (IReadOnlyEntity dbObject in childObjects) { ITypeFieldValueList fieldValueList = OperationUtils.ExtractEntityTypeFieldValues(dbObject, childInfo.EntityType); EntityFieldValue childFieldValue = fieldValueList.GetFieldValue(childMatchedColumn.AttributeName); setter.SetValue(parentEntity, childFieldValue.Value, null); } } childInfo = childInfo.SuperEntityInfo; } if (!foundOnce) { string message = String.Format("The field {0} does not have a matching field in the object {1}", mapping.ToField, firstObject.GetType().FullName); throw new NoMatchingColumnFoundException(message); } }
protected ICollection <IReadOnlyEntity> ReadRelationChildrenFromDb(IReadOnlyEntity entity, Type entityType , ITransaction tx, IRelation relation) { var retrievedEntities = new List <IReadOnlyEntity>(); ICollection <Type> childTypesToProcess = GetChildTypesToProcess(relation); int index = 0; foreach (var childType in childTypesToProcess) { index++; IRelation effectiveRelation = relation.Clone(); effectiveRelation.RelatedObjectType = childType; effectiveRelation.RelationShipName = relation.RelationShipName + "_" + index; EntityInfo entityInfo = CacheManager.GetEntityInfo(entityType); var logSb = new StringBuilder(); var query = entityInfo.GetRelationObjectLoad(DbLayer, effectiveRelation); IList <string> fields = new List <string>(); foreach (RelationColumnMapping mapping in effectiveRelation.TableColumnMappings) { fields.Add(mapping.FromField); } IDbCommand cmd = null; try { cmd = tx.CreateCommand(); cmd.CommandText = query; } catch (Exception ex) { string message = String.Format("SQL Exception while trying create command for sql {0}", query); throw new CommandCreationException(message, ex); } bool showQuery = Config.ShowQueries; if (showQuery) { logSb.Append(query); } for (int i = 0; i < fields.Count; i++) { string field = fields[i]; object fieldValue = null; IColumn matchColumn = entityInfo.FindColumnByAttribute(field); EntityRelationColumnInfo entityRelationColumnInfo = entityInfo.FindRelationColumnInfo(matchColumn != null ? matchColumn.AttributeName : ""); if (entityRelationColumnInfo != null) { EntityFieldValue entityFieldValue = entity.Context.ChangeTracker.GetFieldValue(matchColumn.AttributeName); fieldValue = entityFieldValue.Value; } else if (matchColumn != null) { PropertyInfo getter = entityInfo.GetProperty(matchColumn.AttributeName); fieldValue = ReflectionUtils.GetValue(getter, entity); } else { string message = String.Format("The field {0} does not have a matching field in the object {1}" , field, entity.GetType().FullName); throw new NoMatchingColumnFoundException(message); } if (showQuery) { logSb.Append(" ,").Append(matchColumn.ColumnName).Append("=").Append(fieldValue); } DbLayer.DataManipulate().SetToPreparedStatement(cmd, fieldValue, i + 1, matchColumn); } if (showQuery) { Logger.GetLogger(Config.LoggerName).Debug(logSb.ToString()); } if (Config.EnableStatistics) { Statistics.RegisterSelect(childType); } ICollection <IReadOnlyEntity> retrievedEntitiesForType = ExecuteAndReadFromPreparedStatement(entity, tx, cmd, childType); retrievedEntities.AddRange(retrievedEntitiesForType); } return(retrievedEntities); }
public static IEntityFieldValueList ExtractEntityKeyValues(IReadOnlyEntity entity) { EntityFieldValueList valueList = null; if (entity is IEntity) { valueList = new EntityFieldValueList(entity); IEntity entityDbClass = (IEntity)entity; ICollection <EntityFieldValue> extractedValues = ExtractValues(entityDbClass, true, entity.GetType()); foreach (EntityFieldValue entityFieldValue in extractedValues) { valueList.FieldValues.Add(entityFieldValue); } } return(valueList); }