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); }
public void CopyReferenceStoreFrom(IReadOnlyEntity entity) { if (entity.Context != null) { _referenceStore = entity.Context.ReferenceStore; } }
private ICollection <EntityFieldValue> GetModifiedFieldValues(IReadOnlyEntity entity, Type type) { EntityInfo entityInfo = CacheManager.GetEntityInfo(type); ICollection <IColumn> typeColumns = entityInfo.Columns; ITypeFieldValueList currentValues = OperationUtils.ExtractEntityTypeFieldValues(entity, type); ICollection <EntityFieldValue> modifiedColumns = new List <EntityFieldValue>(); foreach (IColumn typeColumn in typeColumns) { if (typeColumn.Key) { continue; } EntityFieldValue classFieldValue = currentValues.GetFieldValue(typeColumn.AttributeName); EntityFieldValue originalFieldValue = entity.Context != null?entity.Context.ChangeTracker.GetFieldValue(typeColumn.AttributeName) : null; bool matches = originalFieldValue != null && classFieldValue != null && classFieldValue.Value == originalFieldValue.Value || (originalFieldValue != null && classFieldValue != null && classFieldValue.Value != null && classFieldValue.Value.Equals(originalFieldValue.Value)); if (!matches) { modifiedColumns.Add(classFieldValue); } } return(modifiedColumns); }
public static ICollection <IEntity> GetRelationEntities(IReadOnlyEntity rootEntity, IRelation relation) { EntityInfo entityInfo = CacheManager.GetEntityInfo(rootEntity); PropertyInfo property = entityInfo.GetProperty(relation.AttributeName); object value = ReflectionUtils.GetValue(property, rootEntity); ICollection <IEntity> treeEntities = new List <IEntity>(); if (value is ICollection) { ICollection collection = (ICollection)value; foreach (object o in collection) { if (o is IEntity && ReflectionUtils.IsSubClassOf(o.GetType(), relation.RelatedObjectType)) { treeEntities.Add((IEntity)o); } } } else if (value is IEntity && ReflectionUtils.IsSubClassOf(value.GetType(), relation.RelatedObjectType)) { treeEntities.Add((IEntity)value); } return(treeEntities); }
public override bool Filter(IReadOnlyEntity entity, QueryParameter param) { var fileInfo = entity.GetComponent(FileInfo.TypeCode); if (fileInfo == null) { return(true); } if (param.Template == Parameters.FileName) { return(param.FilterOperatorString(fileInfo.FileName)); } else if (param.Template == Parameters.HashMD5) { return(param.FilterOperatorString(fileInfo.HashMD5)); } else if (param.Template == Parameters.HashSHA1OfMD5) { return(param.FilterOperatorString(fileInfo.HashSHA1OfMD5)); } else { return(true); } }
private void CreateProxy(IReadOnlyEntity parentRoEntity, Type type, ITransaction tx, IRelation relation, object value, PropertyInfo property) { Type proxyType = value == null ? property.PropertyType : value.GetType(); if (proxyType.IsGenericType) { Type generic = proxyType.GetGenericArguments()[0]; if (proxyType.IsInterface) { proxyType = typeof(List <>).MakeGenericType(new Type[] { generic }); } } if (value == null) { value = Activator.CreateInstance(proxyType); } Object proxy = null; if (ReflectionUtils.IsImplementInterface(property.PropertyType, typeof(ICollection <>))) { Type generic = proxyType.GetGenericArguments()[0]; Type genericType = typeof(ICollection <>).MakeGenericType(new Type[] { generic }); proxy = _proxyGenerator.CreateInterfaceProxyWithTarget(genericType, value, new ChildLoadInterceptor(this, parentRoEntity, type, tx, relation)); } else { proxy = _proxyGenerator.CreateClassProxy(proxyType, new object[] {}, new ChildLoadInterceptor(this, parentRoEntity, type, tx, relation)); } ReflectionUtils.SetValue(property, parentRoEntity, proxy); }
private void RemoveEntity(IReadOnlyEntity entity) { if (_contents.Remove(entity)) { Removed?.Invoke(this, entity); } }
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 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); } }
public bool Filter(IReadOnlyEntity entity, QueryHandler queryHandler) { if (queryHandler == null) { throw new ArgumentNullException(nameof(queryHandler)); } return(Parameters.All(parameter => parameter.Filter(entity, queryHandler))); }
public static IReadOnlyEntity AsReadOnly(this IReadOnlyEntity self) { if (self == null) { throw new ArgumentNullException(nameof(self)); } return(new ReadOnlyEntity(self)); }
public ReadOnlyEntity(IReadOnlyEntity source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } Source = source; }
public void AddToCurrentObjectGraphIndex(IReadOnlyEntity refEntity) { IEntityFieldValueList refKeyList = OperationUtils.ExtractEntityKeyValues(refEntity); if (!AlreadyInCurrentObjectGraph(refKeyList)) { _entityFieldValueList.Add(refKeyList); } }
public ChildLoadInterceptor(RetrievalOperationLayer dataRetrievalOperationLayer, IReadOnlyEntity parentRoEntity , Type applicableParentType, ITransaction transaction, IRelation relation) { _dataRetrievalOperationLayer = dataRetrievalOperationLayer; _parentRoEntity = parentRoEntity; _applicableParentType = applicableParentType; _transaction = transaction; _transactionFactory = transaction.Factory; _relation = relation; }
public static IReadOnlyEntity AsReadOnly(this IReadOnlyEntity self, IReadOnlyComponentFilter componentFilter) { if (self == null) { throw new ArgumentNullException(nameof(self)); } if (componentFilter == null) { throw new ArgumentNullException(nameof(componentFilter)); } return(new FilteredEntity(self, componentFilter)); }
public FilteredEntity(IReadOnlyEntity entity, IReadOnlyComponentFilter componentFilter) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } if (componentFilter == null) { throw new ArgumentNullException(nameof(componentFilter)); } Entity = entity; ComponentFilter = componentFilter; }
protected static void SetValues(IReadOnlyEntity roEntity, ITypeFieldValueList values) { EntityInfo entityInfo = CacheManager.GetEntityInfo(roEntity); foreach (EntityFieldValue fieldValue in values.FieldValues) { if (entityInfo.FindRelationColumnInfo(fieldValue.Column.AttributeName) == null) { PropertyInfo setter = entityInfo.GetProperty(fieldValue.Column.AttributeName); ReflectionUtils.SetValue(setter, roEntity, fieldValue.Value); } } }
public override void GetQueryFormat(IReadOnlyEntity entity, QueryFormat format) { var fileInfo = entity.GetComponent(FileInfo.TypeCode); if (fileInfo == null) { return; } format.AddParameter(Parameters.FileName); format.AddParameter(Parameters.HashMD5); format.AddParameter(Parameters.HashSHA1OfMD5); }
private ICollection <IReadOnlyEntity> ExecuteAndReadFromPreparedStatement(IReadOnlyEntity entity, ITransaction tx, IDbCommand cmd , Type childType) { EntityInfo entityInfo = CacheManager.GetEntityInfo(childType); ICollection <IColumn> childKeys = null; ICollection <IReadOnlyEntity> data = new List <IReadOnlyEntity>(); IDataReader reader = null; try { reader = cmd.ExecuteReader(); while (reader.Read()) { if (childKeys == null) { childKeys = entityInfo.GetKeys(); } ITypeFieldValueList childTypeKeyList = new EntityTypeFieldValueList(childType); foreach (IColumn childKey in childKeys) { Object value = DbLayer.DataManipulate().ReadFromResultSet(reader, childKey); childTypeKeyList.FieldValues.Add(new EntityFieldValue(value, childKey)); } if (entity.Context.AlreadyInCurrentObjectGraph(childTypeKeyList)) { data.Add(entity.Context.GetFromCurrentObjectGraph(childTypeKeyList)); continue; } IReadOnlyEntity rodbClass = (IReadOnlyEntity)Activator.CreateInstance(childType); rodbClass.Context.CopyReferenceStoreFrom(entity); rodbClass.Retrieve(reader, tx); data.Add(rodbClass); entity.Context.AddToCurrentObjectGraphIndex(rodbClass); } } catch (Exception ex) { string message = String.Format("SQL Exception while trying to read type {0} from result set", childType.FullName); throw new ReadFromResultSetException(message, ex); } finally { DbMgtUtility.Close(reader); DbMgtUtility.Close(cmd); } return(data); }
public static ITypeFieldValueList ExtractEntityTypeKeyValues(IReadOnlyEntity entity, Type type) { EntityTypeFieldValueList valueList = null; if (entity is IEntity) { valueList = new EntityTypeFieldValueList(type); IEntity entityDbClass = (IEntity)entity; ICollection <EntityFieldValue> extractedValues = ExtractValues(entityDbClass, true, type); foreach (EntityFieldValue entityFieldValue in extractedValues) { valueList.FieldValues.Add(entityFieldValue); } } return(valueList); }
public static ITypeFieldValueList ExtractRelationKeyValues(IReadOnlyEntity child, IRelation relation) { EntityRelationFieldValueList valueList = null; if (child is IEntity) { valueList = new EntityRelationFieldValueList(relation); IEntity childDbClass = (IEntity)child; ICollection <EntityFieldValue> extractedValues = ExtractValues(childDbClass, true, null); foreach (EntityFieldValue entityFieldValue in extractedValues) { valueList.FieldValues.Add(entityFieldValue); } } return(valueList); }
public override void GetQueryFormat(IReadOnlyEntity entity, QueryFormat format) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } if (format == null) { throw new ArgumentNullException(nameof(format)); } foreach (var handler in Handlers) { handler.GetQueryFormat(entity, format); } }
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; } }
private bool VersionValidated(IReadOnlyEntity entity, Type type, ITransaction tx) { EntityInfo entityInfo = CacheManager.GetEntityInfo(type); ICollection <IColumn> typeColumns = entityInfo.Columns; foreach (IColumn typeColumn in typeColumns) { if (typeColumn.ColumnType == ColumnType.Version) { Object classValue = ExtractCurrentVersionValue(entity, typeColumn, type, tx); EntityFieldValue originalFieldValue = entity.Context.ChangeTracker.GetFieldValue(typeColumn.AttributeName); return(originalFieldValue != null && classValue == originalFieldValue.Value || (originalFieldValue != null && classValue != null && classValue.Equals(originalFieldValue.Value))); } } if (entityInfo.TableInfo.UpdateStrategy == UpdateStrategy.ChangedColumns) { ICollection <EntityFieldValue> modified = GetModifiedFieldValues(entity, type); typeColumns = new List <IColumn>(); foreach (EntityFieldValue fieldValue in modified) { typeColumns.Add(fieldValue.Column); } } ITypeFieldValueList fieldValueList = ExtractCurrentRowValues(entity, type, tx); if (fieldValueList == null) { return(false); } foreach (IColumn typeColumn in typeColumns) { EntityFieldValue classFieldValue = fieldValueList.GetFieldValue(typeColumn.AttributeName); EntityFieldValue originalFieldValue = entity.Context != null?entity.Context.ChangeTracker.GetFieldValue(typeColumn.AttributeName) : null; bool matches = originalFieldValue != null && classFieldValue != null && classFieldValue.Value == originalFieldValue.Value || (originalFieldValue != null && classFieldValue != null && classFieldValue.Value != null && classFieldValue.Value.Equals(originalFieldValue.Value)); if (!matches) { return(false); } } return(true); }
public void Load(IReadOnlyEntity roEntity, IDataReader reader, ITransaction tx) { if (roEntity is IEntity) { IEntity entity = (IEntity)roEntity; entity.Status = EntityStatus.Unmodified; } try { LoadFromDb(roEntity, reader, tx); roEntity.Context.DestroyReferenceStore(); } catch (Exception e) { Logger.GetLogger(Config.LoggerName).Fatal(e.Message, e); throw new RetrievalException(e.Message, e); } }
private void LoadForType(IReadOnlyEntity entity, Type type, IDataReader reader, ITransaction tx) { EntityInfo entityInfo = CacheManager.GetEntityInfo(type); IEntityContext entityContext = entity.Context; ITypeFieldValueList valueTypeList = ReadValues(type, reader); SetValues(entity, valueTypeList); entity.Context.AddToCurrentObjectGraphIndex(entity); if (entityContext != null) { entityContext.ChangeTracker.AddFields(valueTypeList.FieldValues); } ICollection <IRelation> dbRelations = entityInfo.Relations; foreach (IRelation relation in dbRelations) { LoadChildrenFromRelation(entity, type, tx, relation, false); } }
/// <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 override void GetQueryFormat(IReadOnlyEntity entity, QueryFormat format) { var card = entity.GetComponent(KoikatuCharacterCard.TypeCode); if (card == null) { return; } format.AddParameter(Parameters.Name); format.AddSelectableValue(Parameters.Sex, card.Sex); format.AddSelectableValue(Parameters.Personality, card.Personality); format.AddSelectableValue(Parameters.ClubActivities, card.ClubActivity); format.AddSelectableValue(Parameters.BloodType, card.BloodType); format.AddSelectableValue(Parameters.TeethType, card.TeethType); format.AddSelectableValue(Parameters.HeightType, card.HeightType); format.AddSelectableValue(Parameters.BustSizeType, card.BustSizeType); format.AddSelectableValue(Parameters.SkinType, card.SkinType); foreach (var adjective in Palettes.Skin.GetAdjectives(card.SkinColor)) { format.AddSelectableValue(Parameters.SkinColorType, adjective); #if DEBUG format.AddSelectableValue(Parameters.InverseSkinColorType, adjective); #endif } format.AddSelectableValue(Parameters.HairStyle, card.HairStyle); foreach (var adjective in card.HairColors.Select(i => Palettes.Hair.GetAdjectives(i)).SelectMany(i => i)) { format.AddSelectableValue(Parameters.HairColorType, adjective); #if DEBUG format.AddSelectableValue(Parameters.InverseHairColorType, adjective); #endif } }
public void Load(IReadOnlyEntity readOnlyEntity, IDataReader reader, ITransaction tx) { _persistRetrievalLayer.Load(readOnlyEntity, reader, tx); }
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); } } } }