/// <summary> /// Hydrates the specified instance to hydrate. /// </summary> /// <param name="instanceToHydrate">The instance to hydrate.</param> /// <param name="typeOfInstance">The type of instance.</param> /// <param name="entityMap">The entity map.</param> /// <param name="queryMap">The query map.</param> /// <param name="dataReader">The data reader.</param> public void Hydrate(object instanceToHydrate, Type typeOfInstance, EntityMap entityMap, TableQueryMap queryMap, DbDataReader dataReader) { if (dataReader.HasRows) { // Dictionary<string, int> columns = GetColumnNames(dataReader, entityMap.TableAlias); var entityAccessor = entityAccessorStore.GetEntityAccessor(typeOfInstance, entityMap); dataReader.Read(); SerializeSingle(instanceToHydrate, typeOfInstance, entityMap, entityAccessor, queryMap, dataReader); } }
void AddColumnAndParameterToUpdateInfo(UpdateSqlExecutionList execList, UpdateSqlBodyInfo updateBodyInfo, EntityMap entityMap, Property prop, PropertyAccessor propInfo, EntityAccessor accessor) { object val = propInfo.GetMethod(entity); bool isRel = false; if (prop is Relation) { var rel = (Relation)prop; if (updateBodyInfo.Columns.ContainsKey(prop.ColumnName)) { return; } isRel = true; if (val != null) { if (rel.RelationType == RelationshipType.ManyToOne) { var store = new EntityAccessorStore(); var relMap = session.SessionFactory.DbSettings.Map.GetEntityMap(rel.ReferenceEntityName); var relAccessor = store.GetEntityAccessor(val.GetType(), relMap); var relPinfo = relAccessor.Properties[rel.ReferenceProperty]; if (relPinfo == null) { throw new MappingException(string.Format("could not find property {0} in mapped entity {1}", rel.ReferenceProperty, relMap.FullName)); } val = relPinfo.GetMethod(val); } else if (rel.RelationType == RelationshipType.ManyToMany) { var trackableCollection = val as ITrackableCollection; if (trackableCollection != null) { AddInsertManyToManyOperation(execList, trackableCollection.InsertedItems, rel, accessor, true); AddInsertManyToManyOperation(execList, trackableCollection.DeletedItems, rel, accessor, false); } return; //NOTE: if not trackable collection used mapped statement to add or remove many to many associations } else { return; } } } else { Tuple <QueryParam, bool> etuple; if (updateBodyInfo.Columns.TryGetValue(prop.ColumnName, out etuple)) { if (etuple.Item2) { updateBodyInfo.Columns.Remove(prop.ColumnName); } else { return; } } } //Tuple<QueryParam, bool> tuple = Tuple.Create(new QueryParam(string.Format("{0}_{1}",entityMap.TableAlias, prop.ColumnName)) { Value = val }, isRel); Tuple <QueryParam, bool> tuple = Tuple.Create(QueryParam.CreateParameter(prop, string.Format("{0}_{1}", TableQueryMap.CreatePrefix(2, 2), prop.ColumnName), val), isRel); updateBodyInfo.Columns.Add(prop.ColumnName, tuple); }
/// <summary> /// Serializes all. /// </summary> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <param name="dataReader">The data reader.</param> /// <param name="entityMap">The entity map.</param> /// <param name="queryMap">The query map.</param> /// <returns></returns> /// <exception cref="GoliathDataException">unknown factory method</exception> public IList <TEntity> SerializeAll <TEntity>(DbDataReader dataReader, IEntityMap entityMap, TableQueryMap queryMap = null) { Delegate dlgMethod; Type type = typeof(TEntity); Func <DbDataReader, IEntityMap, TableQueryMap, IList <TEntity> > factoryMethod = null; if (factoryList.TryGetValue(type, out dlgMethod)) { factoryMethod = dlgMethod as Func <DbDataReader, IEntityMap, TableQueryMap, IList <TEntity> >; if (factoryMethod == null) { throw new GoliathDataException("unknown factory method"); } } else { factoryMethod = CreateSerializerMethod <TEntity>(entityMap); factoryList.TryAdd(type, factoryMethod); } IList <TEntity> entityList = factoryMethod(dataReader, entityMap, queryMap); return(entityList); }
public override void Serialize(IDatabaseSettings settings, EntitySerializer serializer, Relation rel, object instanceEntity, PropertyAccessor pInfo, EntityMap entityMap, EntityAccessor entityAccessor, DbDataReader dbReader) { var propType = pInfo.PropertyType; var relEntMap = entityMap.Parent.GetEntityMap(rel.ReferenceEntityName); var refEntityType = propType.GetGenericArguments().FirstOrDefault(); if (refEntityType == null) { throw new MappingException(string.Format("property type mismatch: {0} should be IList<T>", rel.PropertyName)); } var prop = entityMap.FirstOrDefault(c => c.ColumnName == rel.ColumnName); if (prop == null) { throw new GoliathDataException(string.Format("{0}: Reference {1} does not have matching property.", entityMap.FullName, rel.PropertyName)); } if (propType == typeof(IList <>).MakeGenericType(new Type[] { refEntityType })) { var valAccessor = entityAccessor.GetPropertyAccessor(prop.PropertyName); var val = valAccessor.GetMethod(instanceEntity); if (val != null) { var relCols = new List <string>(); var session = serializer.SessionCreator(); int iteration = 0; int recursion = 0; var relQueryMap = new TableQueryMap(relEntMap.FullName, ref recursion, ref iteration); QueryBuilder q = new QueryBuilder(session, relCols); relQueryMap.LoadColumns(relEntMap, session, q, relCols); q.QueryMap = relQueryMap; var queryBuilder = q.From(relEntMap.TableName, relQueryMap.Prefix) .InnerJoin(rel.MapTableName, "m_t1") .On(relQueryMap.Prefix, rel.MapReferenceColumn) .EqualTo(rel.MapPropertyName) .InnerJoin(entityMap.TableName, "e_t1") .On("m_t1", "m_t1." + rel.MapColumn) .EqualTo("e_t1." + rel.MapPropertyName) .Where("m_t1", rel.MapColumn).EqualToValue(val) as QueryBuilder; var collectionType = typeof(Collections.LazyList <>).MakeGenericType(new Type[] { refEntityType }); var lazyCol = Activator.CreateInstance(collectionType, queryBuilder, relEntMap, serializer, session); pInfo.SetMethod(instanceEntity, lazyCol); } else { var collectionType = typeof(List <>).MakeGenericType(new Type[] { refEntityType }); pInfo.SetMethod(instanceEntity, Activator.CreateInstance(collectionType)); } } else { throw new MappingException(string.Format("property type mismatch: {0} should be IList<T>", rel.PropertyName)); } }