public override void Initialize()
        {
            m_CheckConstraints =
                Execute(CheckConstraintsCommandText)
                .Select(o => CheckConstraint.CreateFrom(SqlHelper, o))
                .ToList();
            m_ReferenceConstraintsByName =
                Execute(ReferenceConstraintsCommandText)
                .Select(o => ReferenceConstraint.CreateFrom(SqlHelper, o))
                .ToDictionary(x => x.ConstraintName);
            m_RelationConstraintsByName =
                Execute(RelationConstraintsCommandText)
                .Select(o => RelationConstraint.CreateFrom(SqlHelper, o))
                .ToDictionary(x => x.ConstraintName);

            m_ReferenceConstraintsByNameUq = m_ReferenceConstraintsByName.Values
                                             .ToMultiDictionary(x => x.ConstraintNameUq);
            m_ReferenceConstraintsByRelation = m_ReferenceConstraintsByName.Values
                                               .ToMultiDictionary(x => m_RelationConstraintsByName[x.ConstraintName].RelationName);
            m_RelationConstraintsByIndexName = m_RelationConstraintsByName.Values
                                               .Where(x => x.IndexName != null)
                                               .ToDictionary(x => x.IndexName);

            m_RelationConstraintsByRelation = m_RelationConstraintsByName.Values
                                              .ToMultiDictionary(x => x.RelationName);
        }
        private bool ConstraintPredicate(
            IMetadataConstraints otherConstraints,
            RelationConstraint c,
            RelationConstraintType relationConstraintType,
            Func <RelationConstraint, bool> predicate)
        {
            if (c.RelationConstraintType != relationConstraintType)
            {
                return(false);
            }
            if (!FilterSystemFlagUserPredicate(c.Relation))
            {
                return(false);
            }
            if (relationConstraintType != RelationConstraintType.FOREIGN_KEY && !Metadata.MetadataRelations.Relations.ContainsKey(c.RelationName))
            {
                return(false);
            }

            var otherConstraint = otherConstraints.RelationConstraintsByName.Values.FirstOrDefault(x => x == c);

            if (otherConstraint != null)
            {
                return(predicate == null
                           ? otherConstraint != c
                           : otherConstraint != c || predicate(otherConstraint));
            }

            return(true);
        }
Пример #3
0
        public void Configure(AssociationInfo associationInfo, DbSchemaBuilder builder)
        {
            var primary = associationInfo.PrimaryTable;
            var foreign = associationInfo.ForeignTable;

            DbTableInfoBuilder primaryTable = builder.Find(primary.TableName);
            DbTableInfoBuilder foreignTable = builder.Find(foreign.TableName);

            MemberInfo[] primaryKeyMembers =
                GetMembers(
                    primaryTable.EntityType,
                    primary.PropertyNames);

            MemberInfo[] foreignKeyMembers =
                GetMembers(
                    foreignTable.EntityType,
                    foreign.PropertyNames);

            IKeyInfo primaryKeyInfo =
                EnsureKey(
                    primaryKeyMembers,
                    true,
                    primaryTable);

            IKeyInfo foreignKeyInfo =
                EnsureKey(
                    foreignKeyMembers,
                    false,
                    foreignTable);

            IRelationContraint[] relationConstraints =
                new IRelationContraint[primaryKeyMembers.Length];

            for (int i = 0; i < relationConstraints.Length; i++)
            {
                relationConstraints[i] =
                    new RelationConstraint(primaryKeyMembers[i], foreignKeyMembers[i]);
            }

            Delegate primaryToForeignConverter =
                CreateConverter(
                    PrimaryToForeignConverterMethod,
                    primaryKeyInfo,
                    foreignKeyInfo,
                    relationConstraints);

            Delegate foreignToPrimaryConverter =
                CreateConverter(
                    ForeignToPrimaryConverterMethod,
                    primaryKeyInfo,
                    foreignKeyInfo,
                    relationConstraints);

            builder.Register(
                new DbRelationInfo(
                    primaryTable:               primary.TableName,
                    primaryKeyInfo:             primaryKeyInfo,
                    primaryToForeignConverter:  primaryToForeignConverter,
                    foreignTable:               foreign.TableName,
                    foreignKeyInfo:             foreignKeyInfo,
                    foreignToPrimaryConverter:  foreignToPrimaryConverter,
                    cascadedDelete:             associationInfo.CascadedDelete));
        }
 private bool UniqueConstraintPredicate(IMetadataConstraints constraints, IMetadataConstraints otherConstraints, RelationConstraint c)
 {
     return(ConstraintPredicate(otherConstraints, c, RelationConstraintType.UNIQUE, null));
 }
 private bool PrimaryKeyConstraintPredicate(IMetadataConstraints constraints, IMetadataConstraints otherConstraints, RelationConstraint c)
 {
     return(ConstraintPredicate(otherConstraints, c, RelationConstraintType.PRIMARY_KEY, null));
 }
 private bool ForeignKeyConstraintPredicate(IMetadataConstraints constraints, IMetadataConstraints otherConstraints, RelationConstraint c)
 {
     return(ConstraintPredicate(otherConstraints, c, RelationConstraintType.FOREIGN_KEY,
                                otherConstraint =>
                                !otherConstraints.RelationConstraintsByName.Values.Contains(c) ||
                                !otherConstraints.ReferenceConstraintsByName.Values.Contains(constraints.ReferenceConstraintsByName[c.ConstraintName])));
 }