예제 #1
0
        /// <inheritdoc />
        /// <summary>
        /// Set data from data reader to entity property
        /// </summary>
        /// <param name="entity"> instance of the entity </param>
        /// <param name="reader"> data reader </param>
        /// <returns> the same instance as was passed to the entity param. (To make builder pattern)</returns>
        public override TEntity SetProperty(TEntity entity, DbDataReader reader, ReadSession session, int recursionDepth)
        {
            if (entity != null)
            {
                // Get collection, if not exists create it
                var collection = Getter(entity);
                if (collection == null)
                {
                    if (Setter != null)
                    {
                        Setter(entity, (collection = new List <TProperty>()));
                    }
                    else
                    {
                        throw new InvalidOperationException($"Please make sure that navigation property {typeof(TProperty)} collection is initialized or has ICollection<TProperty> signature");
                    }
                }

                // Get previous entity
                // Check collection is List for optimized access of previous entity
                var previousProperty = default(TProperty);
                if (typeof(List <TProperty>) == collection.GetType())
                {
                    var pkIdx = ForeignTable.PrimaryKey.GetColumnIndexByAlias(reader);
                    if (pkIdx.HasValue)
                    {
                        // TODO: Think about more effective way of reading previous entity
                        if (!ForeignTable.PrimaryKey.IsNull(reader, pkIdx.Value))
                        {
                            var pkValue = ForeignTable.PrimaryKey.GetValue(reader, /*shift*/ pkIdx.Value - ForeignTable.PrimaryKey.Index.Value);
                            previousProperty = collection.FirstOrDefault(e => ForeignTable.PrimaryKey.GetField(e).Equals(pkValue));
                        }
                    }

                    //if (collection.Count > 0)
                    //    previousProperty = ((List<TProperty>)collection)[collection.Count - 1];
                }
                else
                {
                    // Try to find entity with the same key in collection
                    var pkIdx = ForeignTable.PrimaryKey.GetColumnIndexByAlias(reader);
                    if (pkIdx.HasValue)
                    {
                        // TODO: Think about more effective way of reading previous entity
                        if (!ForeignTable.PrimaryKey.IsNull(reader, pkIdx.Value))
                        {
                            var pkValue = ForeignTable.PrimaryKey.GetValue(reader, /*shift*/ pkIdx.Value - ForeignTable.PrimaryKey.Index.Value);
                            previousProperty = collection.FirstOrDefault(e => ForeignTable.PrimaryKey.GetField(e).Equals(pkValue));
                        }
                    }
                }

                var propertyInstance = ForeignTable.ToEntity(reader, previousProperty, session, ++recursionDepth);
                if (propertyInstance != null && propertyInstance != previousProperty)
                {
                    collection?.Add(propertyInstance);
                }
            }
            return(entity);
        }
예제 #2
0
 /// <inheritdoc />
 /// <summary>
 /// Set data from data reader to entity property
 /// </summary>
 /// <param name="entity"> instance of the entity </param>
 /// <param name="reader"> data reader </param>
 /// <returns> the same instance as was passed to the entity param. (To make builder pattern)</returns>
 public override TEntity SetProperty(TEntity entity, DbDataReader reader, ReadSession session, int recursionDepth)
 {
     if (entity != null)
     {
         var previousProperty = Getter(entity);
         var currentProperty  = ForeignTable.ToEntity(reader, previousProperty, session, ++recursionDepth);
         Setter(entity, currentProperty);
     }
     return(entity);
 }
예제 #3
0
        public void SetForeignEnd(IKey key)
        {
            var oldForeignTable = ForeignTable;

            ForeignTable = key.Parent;
            ForeignKey   = key;

            oldForeignTable.RemoveRelationship(this);
            ForeignTable.AddRelationship(this);
        }
예제 #4
0
        public void DeleteSelf()
        {
            // Delete all mapped references
            //List<ITable> associationTables = this.GetMappingSet().GetAssociationTablesFor(this.Database).ToList();

            foreach (ITable associationTable in this.GetMappingSet().GetAssociationTablesFor(this.Database))
            {
                foreach (Relationship rel in associationTable.Relationships.Where(r => r.PrimaryTable == this.PrimaryTable || r.PrimaryTable == this.ForeignTable))
                {
                    foreach (var reference in associationTable.MappedReferences())
                    {
                        List <ITable> sourceEntityMappedTables = ((EntityLayer.EntityImpl)reference.SourceEntity).MappedTables().ToList();
                        List <ITable> targetEntityMappedTables = ((EntityLayer.EntityImpl)reference.TargetEntity).MappedTables().ToList();

                        if (sourceEntityMappedTables.Contains(this.ForeignTable) ||
                            sourceEntityMappedTables.Contains(this.PrimaryTable) ||
                            targetEntityMappedTables.Contains(this.ForeignTable) ||
                            targetEntityMappedTables.Contains(this.PrimaryTable))
                        {
                            reference.DeleteSelf();
                        }
                    }
                }
            }

            //List<EntityLayer.Reference> mappedReferences = this.MappedReferences().ToList();

            foreach (var reference in this.MappedReferences())
            {
                reference.DeleteSelf();
            }

            var mappingSet = this.GetMappingSet();

            if (mappingSet != null)
            {
                mappingSet.RemoveMappingsContaining(this);
            }

            if (PrimaryTable != null)
            {
                PrimaryTable.RemoveRelationship(this);
            }

            if (ForeignTable != null)
            {
                ForeignTable.RemoveRelationship(this);
            }

            Database.RemoveRelationship(this);
        }
예제 #5
0
파일: Field.cs 프로젝트: ganeshan/Serenity
        public LeftJoin ForeignJoin(Int32?foreignIndex = null)
        {
            if (ForeignTable.IsNullOrEmpty())
            {
                throw new ArgumentNullException("ForeignTable");
            }

            string foreignJoin;

            if (foreignIndex == null)
            {
                foreignJoin = Name;
                if (foreignJoin.EndsWith("Id", StringComparison.Ordinal))
                {
                    foreignJoin = foreignJoin.Substring(0, foreignJoin.Length - 2);
                }
                else if (foreignJoin.EndsWith("_ID", StringComparison.OrdinalIgnoreCase))
                {
                    foreignJoin = foreignJoin.Substring(0, foreignJoin.Length - 3);
                }

                foreignJoin = "j" + foreignJoin;
            }
            else
            {
                foreignJoin = foreignIndex.Value.TableAlias();
            }

            var joinKeyField   = ForeignField ?? Name;
            var sourceAlias    = "T0";
            var sourceKeyField = Name;

            var join = new LeftJoin(this.Fields.Joins, ForeignTable, foreignJoin,
                                    new Criteria(foreignJoin, joinKeyField) == new Criteria(sourceAlias, sourceKeyField));

            this.ForeignJoinAlias = join;
            return(join);
        }
예제 #6
0
        public virtual void CollectValidationErrors(ICollection <string> errors)
        {
            if (String.IsNullOrEmpty(Name))
            {
                errors.Add(ErrorMessages.ForeignKeyNameCannotBeNullOrEmpty);
            }

            if (String.IsNullOrEmpty(ForeignTable))
            {
                errors.Add(ErrorMessages.ForeignTableNameCannotBeNullOrEmpty);
            }

            if (String.IsNullOrEmpty(PrimaryTable))
            {
                errors.Add(ErrorMessages.PrimaryTableNameCannotBeNullOrEmpty);
            }

            if (!String.IsNullOrEmpty(ForeignTable) && !String.IsNullOrEmpty(PrimaryTable) && ForeignTable.Equals(PrimaryTable))
            {
                errors.Add(ErrorMessages.ForeignKeyCannotBeSelfReferential);
            }

            if (ForeignColumns.Count == 0)
            {
                errors.Add(ErrorMessages.ForeignKeyMustHaveOneOrMoreForeignColumns);
            }

            if (PrimaryColumns.Count == 0)
            {
                errors.Add(ErrorMessages.ForeignKeyMustHaveOneOrMorePrimaryColumns);
            }
        }