Example #1
0
        private void CreateRelationColumns(IRelation relation)
        {
            EntityInfo relationInfo = CacheManager.GetEntityInfo(relation.RelatedObjectType);

            while (relationInfo != null)
            {
                ICollection <IColumn> relationKeys = relationInfo.GetKeys();
                foreach (IColumn relationKey in relationKeys)
                {
                    RelationColumnMapping matchingMapping = null;
                    foreach (RelationColumnMapping mapping in relation.TableColumnMappings)
                    {
                        if (mapping.ToField.Equals(relationKey.AttributeName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            matchingMapping = mapping;
                            break;
                        }
                    }

                    IColumn cloned = relationKey.Clone();
                    cloned.Key = false;
                    if (matchingMapping != null)
                    {
                        cloned.AttributeName = matchingMapping.FromField;
                        cloned.ColumnName    = AbstractColumn.PredictColumnName(matchingMapping.FromField);
                    }
                    cloned.Nullable = relation.Nullable;

                    _columns.Add(cloned);
                    _relationColumnInfoList.Add(new EntityRelationColumnInfo(cloned, relation, matchingMapping));
                }
                relationInfo = relationInfo.SuperEntityInfo;
            }
        }
Example #2
0
        public string CreateRelatedObjectsLoadQuery(IRelation relation)
        {
            var entityInfo = CacheManager.GetEntityInfo(relation.RelatedObjectType);

            var sb = new StringBuilder();

            sb.Append("SELECT * FROM ");
            sb.Append(entityInfo.TableInfo.TableName);
            sb.Append(" WHERE ");

            IEnumerator <RelationColumnMapping> enumerator = relation.TableColumnMappings.GetEnumerator();
            int i = 0;

            enumerator.Reset();
            while (enumerator.MoveNext())
            {
                RelationColumnMapping columnMapping = enumerator.Current;
                if (i != 0)
                {
                    sb.Append(" AND ");
                }
                sb.Append(entityInfo.FindColumnByAttribute(columnMapping.ToField).ColumnName);
                sb.Append("= ?");
                i++;
            }

            return(FixUpQuery(sb.ToString()));
        }
Example #3
0
        private static RelationColumnMapping[] CreateForeignKeyColumnMappings(ForeignKeyInfo foreignKeyInfo)
        {
            var objectMappings = new RelationColumnMapping[foreignKeyInfo.FromFieldMappings.Length];

            string[] fromFieldMappings = foreignKeyInfo.FromFieldMappings;
            string[] toFieldMappings   = foreignKeyInfo.ToFieldMappings;

            if (fromFieldMappings.Length != toFieldMappings.Length)
            {
                LogManager.GetLogger(typeof(EntityInfoCache)).Fatal(
                    "incorrect relation definition, no of from columns should ne equal to no of to columns");
                throw new IncorrectFieldDefinitionException(
                          "incorrect relation definition, no of from columns should ne equal to no of to columns");
            }

            for (int i = 0; i < fromFieldMappings.Length; i++)
            {
                string fromMapping = fromFieldMappings[i];
                string toMapping   = toFieldMappings[i];
                objectMappings[i] = new RelationColumnMapping(fromMapping, toMapping);
            }
            return(objectMappings);
        }
 public EntityRelationColumnInfo(IColumn column, IRelation relation, RelationColumnMapping mapping)
 {
     Column   = column;
     Relation = relation;
     Mapping  = mapping;
 }
Example #5
0
        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);
            }
        }
Example #6
0
        private static void SetChildPrimaryKeys(EntityFieldValue parentFieldValue, Type childEntityType
                                                , IEnumerable <IEntity> childObjects, RelationColumnMapping mapping)
        {
            bool       foundOnce        = false;
            EntityInfo parentEntityInfo = CacheManager.GetEntityInfo(childEntityType);
            EntityInfo entityInfo       = parentEntityInfo;

            while (entityInfo != null)
            {
                IColumn subLevelMatchedColumn = entityInfo.FindColumnByAttribute(mapping.ToField);

                if (subLevelMatchedColumn != null)
                {
                    foundOnce = true;
                    PropertyInfo setter = parentEntityInfo.GetProperty(subLevelMatchedColumn);
                    foreach (IReadOnlyEntity dbObject in childObjects)
                    {
                        ReflectionUtils.SetValue(setter, dbObject, parentFieldValue.Value);
                    }
                }
                entityInfo = entityInfo.SuperEntityInfo;
            }
            if (!foundOnce)
            {
                string message = String.Format("The field {0} does not have a matching field in the object {1}", mapping.ToField, childEntityType.FullName);
                throw new NoMatchingColumnFoundException(message);
            }
        }