internal static List <ForeignConstraint> GetForeignConstraints(
            EntityContainer container)
        {
            List <ForeignConstraint> foreignConstraintList = new List <ForeignConstraint>();

            foreach (EntitySetBase baseEntitySet in container.BaseEntitySets)
            {
                AssociationSet i_fkeySet = baseEntitySet as AssociationSet;
                if (i_fkeySet != null)
                {
                    Dictionary <string, EntitySet> dictionary = new Dictionary <string, EntitySet>();
                    foreach (AssociationSetEnd associationSetEnd in i_fkeySet.AssociationSetEnds)
                    {
                        dictionary.Add(associationSetEnd.Name, associationSetEnd.EntitySet);
                    }
                    foreach (ReferentialConstraint referentialConstraint in i_fkeySet.ElementType.ReferentialConstraints)
                    {
                        EntitySet         i_parentTable     = dictionary[referentialConstraint.FromRole.Name];
                        EntitySet         i_childTable      = dictionary[referentialConstraint.ToRole.Name];
                        ForeignConstraint foreignConstraint = new ForeignConstraint(i_fkeySet, i_parentTable, i_childTable, referentialConstraint.FromProperties, referentialConstraint.ToProperties);
                        foreignConstraintList.Add(foreignConstraint);
                    }
                }
            }
            return(foreignConstraintList);
        }
        private bool CheckIfConstraintMappedToForeignKeyAssociation(
            QueryRewriter childRewriter,
            QueryRewriter parentRewriter,
            Set <Cell> cells)
        {
            ViewgenContext            viewgenContext1 = childRewriter.ViewgenContext;
            ViewgenContext            viewgenContext2 = parentRewriter.ViewgenContext;
            List <Set <EdmProperty> > source1         = new List <Set <EdmProperty> >();
            List <Set <EdmProperty> > source2         = new List <Set <EdmProperty> >();

            foreach (Cell cell in cells)
            {
                if (cell.CQuery.Extent.BuiltInTypeKind != BuiltInTypeKind.AssociationSet)
                {
                    Set <EdmProperty> cslotsForTableColumns1 = cell.GetCSlotsForTableColumns(this.ChildColumns);
                    if (cslotsForTableColumns1 != null && cslotsForTableColumns1.Count != 0)
                    {
                        source1.Add(cslotsForTableColumns1);
                    }
                    Set <EdmProperty> cslotsForTableColumns2 = cell.GetCSlotsForTableColumns(this.ParentColumns);
                    if (cslotsForTableColumns2 != null && cslotsForTableColumns2.Count != 0)
                    {
                        source2.Add(cslotsForTableColumns2);
                    }
                }
            }
            if (source1.Count != 0 && source2.Count != 0)
            {
                foreach (AssociationType associationType in viewgenContext1.EntityContainerMapping.EdmEntityContainer.BaseEntitySets.OfType <AssociationSet>().Where <AssociationSet>((Func <AssociationSet, bool>)(it => it.ElementType.IsForeignKey)).Select <AssociationSet, AssociationType>((Func <AssociationSet, AssociationType>)(it => it.ElementType)))
                {
                    ReferentialConstraint            refConstraint = associationType.ReferentialConstraints.FirstOrDefault <ReferentialConstraint>();
                    IEnumerable <Set <EdmProperty> > source3       = source1.Where <Set <EdmProperty> >((Func <Set <EdmProperty>, bool>)(it => it.SetEquals(new Set <EdmProperty>((IEnumerable <EdmProperty>)refConstraint.ToProperties))));
                    IEnumerable <Set <EdmProperty> > source4       = source2.Where <Set <EdmProperty> >((Func <Set <EdmProperty>, bool>)(it => it.SetEquals(new Set <EdmProperty>((IEnumerable <EdmProperty>)refConstraint.FromProperties))));
                    if (source3.Count <Set <EdmProperty> >() != 0 && source4.Count <Set <EdmProperty> >() != 0)
                    {
                        foreach (IEnumerable <EdmProperty> properties1_1 in source4)
                        {
                            Set <int> propertyIndexes = ForeignConstraint.GetPropertyIndexes(properties1_1, refConstraint.FromProperties);
                            foreach (IEnumerable <EdmProperty> properties1_2 in source3)
                            {
                                if (ForeignConstraint.GetPropertyIndexes(properties1_2, refConstraint.ToProperties).SequenceEqual <int>((IEnumerable <int>)propertyIndexes))
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }
            }
            return(false);
        }
Пример #3
0
        // effects: Given a store-side container, returns all the foreign key
        // constraints specified for different tables
        internal static List <ForeignConstraint> GetForeignConstraints(EntityContainer container)
        {
            var foreignKeyConstraints = new List <ForeignConstraint>();

            // Go through all the extents and get the associations
            foreach (var extent in container.BaseEntitySets)
            {
                var relationSet = extent as AssociationSet;

                if (relationSet == null)
                {
                    continue;
                }
                // Keep track of the end to EntitySet mapping
                var endToExtents = new Dictionary <string, EntitySet>();

                foreach (var end in relationSet.AssociationSetEnds)
                {
                    endToExtents.Add(end.Name, end.EntitySet);
                }

                var relationType = relationSet.ElementType;
                // Go through each referential constraint, determine the name
                // of the tables that the constraint refers to and then
                // create the foreign key constraint between the tables
                // Wow! We go to great lengths to make it cumbersome for a
                // programmer to deal with foreign keys
                foreach (var constraint in relationType.ReferentialConstraints)
                {
                    // Note: We are correlating the constraint's roles with
                    // the ends above using the role names, i.e.,
                    // FromRole.Name and ToRole.Name here and end.Role above
                    var parentExtent         = endToExtents[constraint.FromRole.Name];
                    var childExtent          = endToExtents[constraint.ToRole.Name];
                    var foreignKeyConstraint = new ForeignConstraint(
                        relationSet, parentExtent, childExtent,
                        constraint.FromProperties, constraint.ToProperties);
                    foreignKeyConstraints.Add(foreignKeyConstraint);
                }
            }
            return(foreignKeyConstraints);
        }
        private bool CheckParentColumnsForForeignKey(
            Cell cell,
            IEnumerable <Cell> cells,
            AssociationEndMember parentEnd,
            ref List <ErrorLog.Record> errorList)
        {
            EntitySet entitySetAtEnd = MetadataHelper.GetEntitySetAtEnd((AssociationSet)cell.CQuery.Extent, parentEnd);

            if (ForeignConstraint.FindEntitySetForColumnsMappedToEntityKeys(cells, this.ParentColumns).Contains(entitySetAtEnd))
            {
                return(true);
            }
            if (errorList == null)
            {
                errorList = new List <ErrorLog.Record>();
            }
            ErrorLog.Record record = new ErrorLog.Record(ViewGenErrorCode.ForeignKeyParentTableNotMappedToEnd, Strings.ViewGen_Foreign_Key_ParentTable_NotMappedToEnd((object)this.ToUserString(), (object)this.ChildTable.Name, (object)cell.CQuery.Extent.Name, (object)parentEnd.Name, (object)this.ParentTable.Name, (object)entitySetAtEnd.Name), cell, string.Empty);
            errorList.Add(record);
            return(false);
        }
        // effects: Given a store-side container, returns all the foreign key
        // constraints specified for different tables
        internal static List<ForeignConstraint> GetForeignConstraints(EntityContainer container)
        {
            var foreignKeyConstraints = new List<ForeignConstraint>();

            // Go through all the extents and get the associations
            foreach (var extent in container.BaseEntitySets)
            {
                var relationSet = extent as AssociationSet;

                if (relationSet == null)
                {
                    continue;
                }
                // Keep track of the end to EntitySet mapping
                var endToExtents = new Dictionary<string, EntitySet>();

                foreach (var end in relationSet.AssociationSetEnds)
                {
                    endToExtents.Add(end.Name, end.EntitySet);
                }

                var relationType = relationSet.ElementType;
                // Go through each referential constraint, determine the name
                // of the tables that the constraint refers to and then
                // create the foreign key constraint between the tables
                // Wow! We go to great lengths to make it cumbersome for a
                // programmer to deal with foreign keys
                foreach (var constraint in relationType.ReferentialConstraints)
                {
                    // Note: We are correlating the constraint's roles with
                    // the ends above using the role names, i.e.,
                    // FromRole.Name and ToRole.Name here and end.Role above
                    var parentExtent = endToExtents[constraint.FromRole.Name];
                    var childExtent = endToExtents[constraint.ToRole.Name];
                    var foreignKeyConstraint = new ForeignConstraint(
                        relationSet, parentExtent, childExtent,
                        constraint.FromProperties, constraint.ToProperties);
                    foreignKeyConstraints.Add(foreignKeyConstraint);
                }
            }
            return foreignKeyConstraints;
        }
        private bool CheckForeignKeyColumnOrder(Set <Cell> cells, ErrorLog errorLog)
        {
            List <Cell> cellList1 = new List <Cell>();
            List <Cell> cellList2 = new List <Cell>();

            foreach (Cell cell in cells)
            {
                if (cell.SQuery.Extent.Equals((object)this.ChildTable))
                {
                    cellList2.Add(cell);
                }
                if (cell.SQuery.Extent.Equals((object)this.ParentTable))
                {
                    cellList1.Add(cell);
                }
            }
            foreach (Cell cell1 in cellList2)
            {
                List <List <int> > slotNumsForColumns1 = ForeignConstraint.GetSlotNumsForColumns(cell1, this.ChildColumns);
                if (slotNumsForColumns1.Count != 0)
                {
                    List <MemberPath> memberPathList1 = (List <MemberPath>)null;
                    List <MemberPath> memberPathList2 = (List <MemberPath>)null;
                    Cell cell2 = (Cell)null;
                    foreach (List <int> intList1 in slotNumsForColumns1)
                    {
                        memberPathList1 = new List <MemberPath>(intList1.Count);
                        foreach (int slotNum in intList1)
                        {
                            MemberProjectedSlot memberProjectedSlot = (MemberProjectedSlot)cell1.CQuery.ProjectedSlotAt(slotNum);
                            memberPathList1.Add(memberProjectedSlot.MemberPath);
                        }
                        foreach (Cell cell3 in cellList1)
                        {
                            List <List <int> > slotNumsForColumns2 = ForeignConstraint.GetSlotNumsForColumns(cell3, this.ParentColumns);
                            if (slotNumsForColumns2.Count != 0)
                            {
                                foreach (List <int> intList2 in slotNumsForColumns2)
                                {
                                    memberPathList2 = new List <MemberPath>(intList2.Count);
                                    foreach (int slotNum in intList2)
                                    {
                                        MemberProjectedSlot memberProjectedSlot = (MemberProjectedSlot)cell3.CQuery.ProjectedSlotAt(slotNum);
                                        memberPathList2.Add(memberProjectedSlot.MemberPath);
                                    }
                                    if (memberPathList1.Count == memberPathList2.Count)
                                    {
                                        bool flag = false;
                                        for (int index = 0; index < memberPathList1.Count && !flag; ++index)
                                        {
                                            MemberPath memberPath = memberPathList2[index];
                                            MemberPath path1      = memberPathList1[index];
                                            if (!memberPath.LeafEdmMember.Equals((object)path1.LeafEdmMember))
                                            {
                                                if (memberPath.IsEquivalentViaRefConstraint(path1))
                                                {
                                                    return(true);
                                                }
                                                flag = true;
                                            }
                                        }
                                        if (!flag)
                                        {
                                            return(true);
                                        }
                                        cell2 = cell3;
                                    }
                                }
                            }
                        }
                    }
                    ErrorLog.Record record = new ErrorLog.Record(ViewGenErrorCode.ForeignKeyColumnOrderIncorrect, Strings.ViewGen_Foreign_Key_ColumnOrder_Incorrect((object)this.ToUserString(), (object)MemberPath.PropertiesToUserString(this.ChildColumns, false), (object)this.ChildTable.Name, (object)MemberPath.PropertiesToUserString((IEnumerable <MemberPath>)memberPathList1, false), (object)cell1.CQuery.Extent.Name, (object)MemberPath.PropertiesToUserString(this.ParentColumns, false), (object)this.ParentTable.Name, (object)MemberPath.PropertiesToUserString((IEnumerable <MemberPath>)memberPathList2, false), (object)cell2.CQuery.Extent.Name), (IEnumerable <Cell>) new Cell[2]
                    {
                        cell2,
                        cell1
                    }, string.Empty);
                    errorLog.AddEntry(record);
                    return(false);
                }
            }
            return(true);
        }
        private void GuaranteeMappedRelationshipForForeignKey(
            QueryRewriter childRewriter,
            QueryRewriter parentRewriter,
            IEnumerable <Cell> cells,
            ErrorLog errorLog,
            ConfigViewGenerator config)
        {
            ViewgenContext           viewgenContext1 = childRewriter.ViewgenContext;
            ViewgenContext           viewgenContext2 = parentRewriter.ViewgenContext;
            IEnumerable <MemberPath> keyFields       = ExtentKey.GetPrimaryKeyForEntityType(new MemberPath((EntitySetBase)this.ChildTable), this.ChildTable.ElementType).KeyFields;
            bool flag1 = false;
            bool flag2 = false;
            List <ErrorLog.Record> errorList = (List <ErrorLog.Record>)null;

            foreach (Cell cell in cells)
            {
                if (cell.SQuery.Extent.Equals((object)this.ChildTable))
                {
                    AssociationEndMember relationEndForColumns = ForeignConstraint.GetRelationEndForColumns(cell, this.ChildColumns);
                    if (relationEndForColumns == null || this.CheckParentColumnsForForeignKey(cell, cells, relationEndForColumns, ref errorList))
                    {
                        flag2 = true;
                        if (ForeignConstraint.GetRelationEndForColumns(cell, keyFields) != null && relationEndForColumns != null && ForeignConstraint.FindEntitySetForColumnsMappedToEntityKeys(cells, keyFields).Count > 0)
                        {
                            flag1 = true;
                            this.CheckConstraintWhenParentChildMapped(cell, errorLog, relationEndForColumns, config);
                            break;
                        }
                        if (relationEndForColumns != null)
                        {
                            AssociationSet extent = (AssociationSet)cell.CQuery.Extent;
                            MetadataHelper.GetEntitySetAtEnd(extent, relationEndForColumns);
                            flag1 = ForeignConstraint.CheckConstraintWhenOnlyParentMapped(extent, relationEndForColumns, childRewriter, parentRewriter);
                            if (flag1)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            if (!flag2)
            {
                foreach (ErrorLog.Record record in errorList)
                {
                    errorLog.AddEntry(record);
                }
            }
            else
            {
                if (flag1)
                {
                    return;
                }
                string message = Strings.ViewGen_Foreign_Key_Missing_Relationship_Mapping((object)this.ToUserString());
                IEnumerable <LeftCellWrapper> wrappersFromContext1 = (IEnumerable <LeftCellWrapper>)ForeignConstraint.GetWrappersFromContext(viewgenContext2, (EntitySetBase)this.ParentTable);
                IEnumerable <LeftCellWrapper> wrappersFromContext2 = (IEnumerable <LeftCellWrapper>)ForeignConstraint.GetWrappersFromContext(viewgenContext1, (EntitySetBase)this.ChildTable);
                Set <LeftCellWrapper>         set = new Set <LeftCellWrapper>(wrappersFromContext1);
                set.AddRange(wrappersFromContext2);
                ErrorLog.Record record = new ErrorLog.Record(ViewGenErrorCode.ForeignKeyMissingRelationshipMapping, message, (IEnumerable <LeftCellWrapper>)set, string.Empty);
                errorLog.AddEntry(record);
            }
        }