예제 #1
0
        private static ICollection <EntityFieldValue> ExtractValues(IEntity entity, bool key, Type typeToLoad)
        {
            ICollection <EntityFieldValue> entityFieldValues = new List <EntityFieldValue>();
            EntityInfo parentEntityInfo = CacheManager.GetEntityInfo(entity);
            EntityInfo entityInfo       = parentEntityInfo;

            while (entityInfo != null)
            {
                if (typeToLoad != null && typeToLoad != entityInfo.EntityType)
                {
                    entityInfo = entityInfo.SuperEntityInfo;
                    continue;
                }
                ICollection <IColumn> subLevelColumns = entityInfo.Columns;
                foreach (IColumn subLevelColumn in subLevelColumns)
                {
                    if (!key || (subLevelColumn.Key && key))
                    {
                        if (AlreadyHasTheColumnAdded(entityFieldValues, subLevelColumn))
                        {
                            continue;
                        }

                        EntityRelationColumnInfo relationColumnInfo = entityInfo.FindRelationColumnInfo(subLevelColumn.AttributeName);
                        if (relationColumnInfo != null)
                        {
                            ICollection <IEntity> relationEntities = GetRelationEntities(entity, relationColumnInfo.Relation);
                            if (relationEntities.Count > 0)
                            {
                                foreach (IEntity relationEntity in relationEntities)
                                {
                                    IEntityFieldValueList keyValueList = ExtractEntityKeyValues(relationEntity);
                                    EntityFieldValue      fieldValue   = keyValueList.GetFieldValue(relationColumnInfo.Mapping.ToField);
                                    entityFieldValues.Add(new EntityFieldValue(fieldValue.Value, subLevelColumn));
                                }
                            }
                            else
                            {
                                entityFieldValues.Add(new EntityFieldValue(null, subLevelColumn));
                            }
                        }
                        else
                        {
                            PropertyInfo getter = entityInfo.GetProperty(subLevelColumn.AttributeName);
                            Object       value  = ReflectionUtils.GetValue(getter, entity);

                            entityFieldValues.Add(new EntityFieldValue(value, subLevelColumn));
                        }
                    }
                }
                entityInfo = entityInfo.SuperEntityInfo;
            }
            return(entityFieldValues);
        }
예제 #2
0
        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);
        }