private static bool TryGetWithRelationship(
            StorageAssociationSetMapping colocatedAssociationSetMap,
            EntitySetBase thisExtent,
            MemberPath sRootNode,
            ref List <SlotInfo> foreignKeySlots,
            out WithRelationship withRelationship)
        {
            DebugCheck.NotNull(foreignKeySlots);
            withRelationship = null;

            //Get the map for foreign key end
            var foreignKeyEndMap = GetForeignKeyEndMapFromAssocitionMap(colocatedAssociationSetMap);

            if (foreignKeyEndMap == null ||
                foreignKeyEndMap.EndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
                return(false);
            }

            var toEnd             = (AssociationEndMember)foreignKeyEndMap.EndMember;
            var fromEnd           = MetadataHelper.GetOtherAssociationEnd(toEnd);
            var toEndEntityType   = (EntityType)((RefType)(toEnd.TypeUsage.EdmType)).ElementType;
            var fromEndEntityType = (EntityType)(((RefType)fromEnd.TypeUsage.EdmType).ElementType);

            // Get the member path for AssociationSet
            var associationSet = (AssociationSet)colocatedAssociationSetMap.Set;
            var prefix         = new MemberPath(associationSet, toEnd);

            // Collect the member paths for edm scalar properties that belong to the target entity key.
            // These will be used as part of WITH RELATIONSHIP.
            // Get the key properties from edm type since the query parser depends on the order of key members
            var propertyMaps = foreignKeyEndMap.Properties.Cast <StorageScalarPropertyMapping>();
            var toEndEntityKeyMemberPaths = new List <MemberPath>();

            foreach (EdmProperty edmProperty in toEndEntityType.KeyMembers)
            {
                var scalarPropertyMaps = propertyMaps.Where(propMap => (propMap.EdmProperty.Equals(edmProperty)));
                Debug.Assert(scalarPropertyMaps.Count() == 1, "Can't Map the same column multiple times in the same end");
                var scalarPropertyMap = scalarPropertyMaps.First();

                // Create SlotInfo for Freign Key member that needs to be projected.
                var sSlot            = new MemberProjectedSlot(new MemberPath(sRootNode, scalarPropertyMap.ColumnProperty));
                var endMemberKeyPath = new MemberPath(prefix, edmProperty);
                toEndEntityKeyMemberPaths.Add(endMemberKeyPath);
                foreignKeySlots.Add(new SlotInfo(true, true, sSlot, endMemberKeyPath));
            }

            // Parent assignable from child: Ensures they are in the same hierarchy.
            if (thisExtent.ElementType.IsAssignableFrom(fromEndEntityType))
            {
                // Now create the WITH RELATIONSHIP with all the needed info.
                withRelationship = new WithRelationship(
                    associationSet, fromEnd, fromEndEntityType, toEnd, toEndEntityType, toEndEntityKeyMemberPaths);
                return(true);
            }
            else
            {
                return(false);
            }
        }
 protected MemberRestriction(
     MemberProjectedSlot slot,
     IEnumerable <Constant> values,
     IEnumerable <Constant> possibleValues)
     : this(slot, new Domain(values, possibleValues))
 {
 }
 protected MemberRestriction(MemberProjectedSlot slot, Constant value)
     : this(slot, (IEnumerable <Constant>) new Constant[1]
 {
     value
 })
 {
 }
Beispiel #4
0
        // requires: domain not have any Negated constants other than NotNull
        // Also, cellQuery contains all final oneOfConsts or all partial oneOfConsts
        // cellquery must contain a whereclause of the form "True", "OneOfConst" or "
        // "OneOfConst AND ... AND OneOfConst"
        // slot must present in cellQuery and incomingDomain is the domain for it
        // effects: Returns the set of values that slot can take as restricted by cellQuery's whereClause
        private static bool TryGetDomainRestrictedByWhereClause(
            IEnumerable <Constant> domain, MemberProjectedSlot slot, CellQuery cellQuery, out CellConstantSet result)
        {
            var conditionsForSlot = cellQuery.GetConjunctsFromWhereClause()
                                    .Where(
                restriction =>
                MemberPath.EqualityComparer.Equals(
                    restriction.RestrictedMemberSlot.MemberPath, slot.MemberPath))
                                    .Select(
                restriction => new CellConstantSet(restriction.Domain.Values, Constant.EqualityComparer));

            //Debug.Assert(!conditionsForSlot.Skip(1).Any(), "More than one Clause with the same path");

            if (!conditionsForSlot.Any())
            {
                // If the slot was not mentioned in the query return the domain without restricting it
                result = new CellConstantSet(domain);
                return(false);
            }

            // Now get all the possible values from domain and conditionValues
            var possibleValues = DeterminePossibleValues(conditionsForSlot.SelectMany(m => m.Select(c => c)), domain);

            var restrictedDomain = new Domain(domain, possibleValues);

            foreach (var conditionValues in conditionsForSlot)
            {
                // Domain derived from Edm-Type INTERSECTED with Conditions
                restrictedDomain = restrictedDomain.Intersect(new Domain(conditionValues, possibleValues));
            }

            result = new CellConstantSet(restrictedDomain.Values, Constant.EqualityComparer);
            return(!domain.SequenceEqual(result));
        }
 private static bool TryMergeSlots(
     ProjectedSlot[] slots1,
     ProjectedSlot[] slots2,
     out ProjectedSlot[] slots)
 {
     slots = new ProjectedSlot[slots1.Length];
     for (int index = 0; index < slots.Length; ++index)
     {
         ProjectedSlot projectedSlot1 = slots1[index];
         ProjectedSlot projectedSlot2 = slots2[index];
         if (projectedSlot1 == null)
         {
             slots[index] = projectedSlot2;
         }
         else if (projectedSlot2 == null)
         {
             slots[index] = projectedSlot1;
         }
         else
         {
             MemberProjectedSlot memberProjectedSlot1 = projectedSlot1 as MemberProjectedSlot;
             MemberProjectedSlot memberProjectedSlot2 = projectedSlot2 as MemberProjectedSlot;
             if (memberProjectedSlot1 != null && memberProjectedSlot2 != null && !ProjectedSlot.EqualityComparer.Equals((ProjectedSlot)memberProjectedSlot1, (ProjectedSlot)memberProjectedSlot2))
             {
                 return(false);
             }
             ProjectedSlot projectedSlot3 = memberProjectedSlot1 != null ? projectedSlot1 : projectedSlot2;
             slots[index] = projectedSlot3;
         }
     }
     return(true);
 }
Beispiel #6
0
        internal static Dictionary <MemberPath, Set <Constant> > ComputeConstantDomainSetsForSlotsInQueryViews(
            IEnumerable <Cell> cells,
            EdmItemCollection edmItemCollection,
            bool isValidationEnabled)
        {
            Dictionary <MemberPath, Set <Constant> > dictionary = new Dictionary <MemberPath, Set <Constant> >(MemberPath.EqualityComparer);

            foreach (Cell cell in cells)
            {
                foreach (MemberRestriction memberRestriction in cell.CQuery.GetConjunctsFromWhereClause())
                {
                    MemberProjectedSlot restrictedMemberSlot = memberRestriction.RestrictedMemberSlot;
                    Set <Constant>      set1 = Domain.DeriveDomainFromMemberPath(restrictedMemberSlot.MemberPath, edmItemCollection, isValidationEnabled);
                    set1.AddRange(memberRestriction.Domain.Values.Where <Constant>((Func <Constant, bool>)(c =>
                    {
                        if (!c.Equals((object)Constant.Null))
                        {
                            return(!c.Equals((object)Constant.NotNull));
                        }
                        return(false);
                    })));
                    Set <Constant> set2;
                    if (!dictionary.TryGetValue(restrictedMemberSlot.MemberPath, out set2))
                    {
                        dictionary[restrictedMemberSlot.MemberPath] = set1;
                    }
                    else
                    {
                        set2.AddRange((IEnumerable <Constant>)set1);
                    }
                }
            }
            return(dictionary);
        }
 internal static void PropagateUpdateDomainToQueryDomain(
     IEnumerable <Cell> cells,
     MemberDomainMap queryDomainMap,
     MemberDomainMap updateDomainMap)
 {
     foreach (Cell cell in cells)
     {
         CellQuery cquery = cell.CQuery;
         CellQuery squery = cell.SQuery;
         for (int slotNum = 0; slotNum < cquery.NumProjectedSlots; ++slotNum)
         {
             MemberProjectedSlot memberProjectedSlot1 = cquery.ProjectedSlotAt(slotNum) as MemberProjectedSlot;
             MemberProjectedSlot memberProjectedSlot2 = squery.ProjectedSlotAt(slotNum) as MemberProjectedSlot;
             if (memberProjectedSlot1 != null && memberProjectedSlot2 != null)
             {
                 MemberPath memberPath1 = memberProjectedSlot1.MemberPath;
                 MemberPath memberPath2 = memberProjectedSlot2.MemberPath;
                 queryDomainMap.GetDomainInternal(memberPath1).Unite(updateDomainMap.GetDomainInternal(memberPath2).Where <Constant>((Func <Constant, bool>)(constant =>
                 {
                     if (!constant.IsNull())
                     {
                         return(!(constant is NegatedConstant));
                     }
                     return(false);
                 })));
                 if (updateDomainMap.IsConditionMember(memberPath2) && !queryDomainMap.IsConditionMember(memberPath1))
                 {
                     queryDomainMap.m_projectedConditionMembers.Add(memberPath1);
                 }
             }
         }
     }
     MemberDomainMap.ExpandNegationsInDomainMap(queryDomainMap.m_conditionDomainMap);
     MemberDomainMap.ExpandNegationsInDomainMap(queryDomainMap.m_nonConditionDomainMap);
 }
Beispiel #8
0
 private static bool GetRestrictedOrUnrestrictedDomain(
     MemberProjectedSlot slot,
     CellQuery cellQuery,
     EdmItemCollection edmItemCollection,
     out Set <Constant> domain)
 {
     return(Domain.TryGetDomainRestrictedByWhereClause((IEnumerable <Constant>)Domain.DeriveDomainFromMemberPath(slot.MemberPath, edmItemCollection, true), slot, cellQuery, out domain));
 }
Beispiel #9
0
        //True = domain is restricted, False = domain is not restricted (because there is no condition)
        private static bool GetRestrictedOrUnrestrictedDomain(
            MemberProjectedSlot slot, CellQuery cellQuery, EdmItemCollection edmItemCollection, out CellConstantSet domain)
        {
            var domainValues = DeriveDomainFromMemberPath(slot.MemberPath, edmItemCollection, true /* leaveDomainUnbounded */);

            //Note, out domain is set even in the case where method call returns false
            return(TryGetDomainRestrictedByWhereClause(domainValues, slot, cellQuery, out domain));
        }
        internal static List <MemberProjectedSlot> GetKeySlots(
            IEnumerable <MemberProjectedSlot> slots,
            MemberPath prefix)
        {
            EntitySet        entitySet         = prefix.EntitySet;
            List <ExtentKey> keysForEntityType = ExtentKey.GetKeysForEntityType(prefix, entitySet.ElementType);

            return(MemberProjectedSlot.GetSlots(slots, keysForEntityType[0].KeyFields));
        }
Beispiel #11
0
 // <summary>
 // Creates a complete member restriction with the meaning "<paramref name="slot" /> in <paramref name="domain" />".
 // </summary>
 protected MemberRestriction(MemberProjectedSlot slot, Domain domain)
 {
     m_restrictedMemberSlot = slot;
     m_domain     = domain;
     m_isComplete = true;
     Debug.Assert(
         m_domain.Count != 0, "If you want a boolean that evaluates to false, " +
         "use the ConstantBool abstraction");
 }
 /// <summary>
 /// Creates a complete member restriction with the meaning "<paramref name="slot"/> in <paramref name="domain"/>".
 /// </summary>
 protected MemberRestriction(MemberProjectedSlot slot, Domain domain)
 {
     m_restrictedMemberSlot = slot;
     m_domain = domain;
     m_isComplete = true;
     Debug.Assert(
         m_domain.Count != 0, "If you want a boolean that evaluates to false, " +
                              "use the ConstantBool abstraction");
 }
        protected override bool IsEqualTo(ProjectedSlot right)
        {
            MemberProjectedSlot memberProjectedSlot = right as MemberProjectedSlot;

            if (memberProjectedSlot == null)
            {
                return(false);
            }
            return(MemberPath.EqualityComparer.Equals(this.m_memberPath, memberProjectedSlot.m_memberPath));
        }
Beispiel #14
0
 // effects: returns the index at which this slot appears in the projection
 // or -1 if it is not projected
 internal int GetProjectedPosition(MemberProjectedSlot slot)
 {
     for (var i = 0; i < m_projectedSlots.Length; i++)
     {
         if (ProjectedSlot.EqualityComparer.Equals(slot, m_projectedSlots[i]))
         {
             return(i);
         }
     }
     return(-1);
 }
 internal int GetProjectedPosition(MemberProjectedSlot slot)
 {
     for (int index = 0; index < this.m_projectedSlots.Length; ++index)
     {
         if (ProjectedSlot.EqualityComparer.Equals((ProjectedSlot)slot, this.m_projectedSlots[index]))
         {
             return(index);
         }
     }
     return(-1);
 }
 private IEnumerable <MemberProjectedSlot> GetMemberProjectedSlots()
 {
     foreach (ProjectedSlot projectedSlot in this.m_projectedSlots)
     {
         MemberProjectedSlot memberSlot = projectedSlot as MemberProjectedSlot;
         if (memberSlot != null)
         {
             yield return(memberSlot);
         }
     }
 }
        internal ErrorLog.Record CheckForProjectedNotNullSlots(
            Cell sourceCell,
            IEnumerable <Cell> associationSets)
        {
            StringBuilder stringBuilder = new StringBuilder();
            bool          flag1         = false;

            foreach (MemberRestriction condition in this.Conditions)
            {
                if (condition.Domain.ContainsNotNull() && MemberProjectedSlot.GetSlotForMember((IEnumerable <ProjectedSlot>) this.m_projectedSlots, condition.RestrictedMemberSlot.MemberPath) == null)
                {
                    bool flag2 = true;
                    if (this.Extent is EntitySet)
                    {
                        bool       flag3       = sourceCell.CQuery == this;
                        ViewTarget target      = flag3 ? ViewTarget.QueryView : ViewTarget.UpdateView;
                        CellQuery  cellQuery   = flag3 ? sourceCell.SQuery : sourceCell.CQuery;
                        EntitySet  rightExtent = cellQuery.Extent as EntitySet;
                        if (rightExtent != null)
                        {
                            foreach (AssociationSet associationSet in MetadataHelper.GetAssociationsForEntitySet((EntitySetBase)(cellQuery.Extent as EntitySet)).Where <AssociationSet>((Func <AssociationSet, bool>)(association => association.AssociationSetEnds.Any <AssociationSetEnd>((Func <AssociationSetEnd, bool>)(end =>
                            {
                                if (end.CorrespondingAssociationEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One)
                                {
                                    return(MetadataHelper.GetOppositeEnd(end).EntitySet.EdmEquals((MetadataItem)rightExtent));
                                }
                                return(false);
                            })))))
                            {
                                AssociationSet association = associationSet;
                                foreach (Cell cell in associationSets.Where <Cell>((Func <Cell, bool>)(c => c.GetRightQuery(target).Extent.EdmEquals((MetadataItem)association))))
                                {
                                    if (MemberProjectedSlot.GetSlotForMember((IEnumerable <ProjectedSlot>)cell.GetLeftQuery(target).ProjectedSlots, condition.RestrictedMemberSlot.MemberPath) != null)
                                    {
                                        flag2 = false;
                                    }
                                }
                            }
                        }
                    }
                    if (flag2)
                    {
                        stringBuilder.AppendLine(Strings.ViewGen_NotNull_No_Projected_Slot((object)condition.RestrictedMemberSlot.MemberPath.PathToString(new bool?(false))));
                        flag1 = true;
                    }
                }
            }
            if (!flag1)
            {
                return((ErrorLog.Record)null);
            }
            return(new ErrorLog.Record(ViewGenErrorCode.NotNullNoProjectedSlot, stringBuilder.ToString(), sourceCell, string.Empty));
        }
        internal ErrorLog.Record VerifyKeysPresent(
            Cell ownerCell,
            Func <object, object, string> formatEntitySetMessage,
            Func <object, object, object, string> formatAssociationSetMessage,
            ViewGenErrorCode errorCode)
        {
            List <MemberPath> memberPathList = new List <MemberPath>(1);
            List <ExtentKey>  extentKeyList  = new List <ExtentKey>(1);

            if (this.Extent is EntitySet)
            {
                MemberPath prefix = new MemberPath(this.Extent);
                memberPathList.Add(prefix);
                EntityType       elementType       = (EntityType)this.Extent.ElementType;
                List <ExtentKey> keysForEntityType = ExtentKey.GetKeysForEntityType(prefix, elementType);
                extentKeyList.Add(keysForEntityType[0]);
            }
            else
            {
                AssociationSet extent = (AssociationSet)this.Extent;
                foreach (AssociationSetEnd associationSetEnd in extent.AssociationSetEnds)
                {
                    AssociationEndMember associationEndMember = associationSetEnd.CorrespondingAssociationEndMember;
                    MemberPath           prefix = new MemberPath((EntitySetBase)extent, (EdmMember)associationEndMember);
                    memberPathList.Add(prefix);
                    List <ExtentKey> keysForEntityType = ExtentKey.GetKeysForEntityType(prefix, MetadataHelper.GetEntityTypeForEnd(associationEndMember));
                    extentKeyList.Add(keysForEntityType[0]);
                }
            }
            for (int index = 0; index < memberPathList.Count; ++index)
            {
                MemberPath prefix = memberPathList[index];
                if (MemberProjectedSlot.GetKeySlots(this.GetMemberProjectedSlots(), prefix) == null)
                {
                    ExtentKey extentKey = extentKeyList[index];
                    string    message;
                    if (this.Extent is EntitySet)
                    {
                        string userString = MemberPath.PropertiesToUserString(extentKey.KeyFields, true);
                        message = formatEntitySetMessage((object)userString, (object)this.Extent.Name);
                    }
                    else
                    {
                        string name       = prefix.RootEdmMember.Name;
                        string userString = MemberPath.PropertiesToUserString(extentKey.KeyFields, false);
                        message = formatAssociationSetMessage((object)userString, (object)name, (object)this.Extent.Name);
                    }
                    return(new ErrorLog.Record(errorCode, message, ownerCell, string.Empty));
                }
            }
            return((ErrorLog.Record)null);
        }
        internal Set <MemberPath> GetNonNullSlots()
        {
            Set <MemberPath> set = new Set <MemberPath>(MemberPath.EqualityComparer);

            foreach (ProjectedSlot projectedSlot in this.m_projectedSlots)
            {
                if (projectedSlot != null)
                {
                    MemberProjectedSlot memberProjectedSlot = projectedSlot as MemberProjectedSlot;
                    set.Add(memberProjectedSlot.MemberPath);
                }
            }
            return(set);
        }
        internal List <int> GetProjectedPositions(MemberPath member)
        {
            List <int> intList = new List <int>();

            for (int index = 0; index < this.m_projectedSlots.Length; ++index)
            {
                MemberProjectedSlot projectedSlot = this.m_projectedSlots[index] as MemberProjectedSlot;
                if (projectedSlot != null && MemberPath.EqualityComparer.Equals(member, projectedSlot.MemberPath))
                {
                    intList.Add(index);
                }
            }
            return(intList);
        }
        internal List <int> GetAssociationEndSlots(AssociationEndMember endMember)
        {
            List <int> intList = new List <int>();

            for (int index = 0; index < this.m_projectedSlots.Length; ++index)
            {
                MemberProjectedSlot projectedSlot = this.m_projectedSlots[index] as MemberProjectedSlot;
                if (projectedSlot != null && projectedSlot.MemberPath.RootEdmMember.Equals((object)endMember))
                {
                    intList.Add(index);
                }
            }
            return(intList);
        }
 // requires: slot corresponds to a slot in the corresponding
 // BasicCellRelation
 // effects: Given a slot in the corresponding basicCellRelation,
 // looks up the slot in this viewcellrelation and returns it. Returns
 // null if it does not find the slot in the left or right side of the viewrelation
 internal ViewCellSlot LookupViewSlot(MemberProjectedSlot slot)
 {
     // CHANGE_ADYA_IMPROVE: We could have a dictionary to speed this up
     foreach (var viewSlot in m_slots)
     {
         // If the left or right slots are equal, return the viewSlot
         if (ProjectedSlot.EqualityComparer.Equals(slot, viewSlot.CSlot)
             ||
             ProjectedSlot.EqualityComparer.Equals(slot, viewSlot.SSlot))
         {
             return viewSlot;
         }
     }
     return null;
 }
        private void GenerateCellRelations(int cellNumber)
        {
            List <ViewCellSlot> slots = new List <ViewCellSlot>();

            for (int slotNum = 0; slotNum < this.CQuery.NumProjectedSlots; ++slotNum)
            {
                ProjectedSlot       projectedSlot1 = this.CQuery.ProjectedSlotAt(slotNum);
                ProjectedSlot       projectedSlot2 = this.SQuery.ProjectedSlotAt(slotNum);
                MemberProjectedSlot cSlot          = (MemberProjectedSlot)projectedSlot1;
                MemberProjectedSlot sSlot          = (MemberProjectedSlot)projectedSlot2;
                ViewCellSlot        viewCellSlot   = new ViewCellSlot(slotNum, cSlot, sSlot);
                slots.Add(viewCellSlot);
            }
            this.m_viewCellRelation = new ViewCellRelation(this, slots, cellNumber);
        }
        internal static List <MemberProjectedSlot> GetSlots(
            IEnumerable <MemberProjectedSlot> slots,
            IEnumerable <MemberPath> members)
        {
            List <MemberProjectedSlot> memberProjectedSlotList = new List <MemberProjectedSlot>();

            foreach (MemberPath member in members)
            {
                MemberProjectedSlot slotForMember = MemberProjectedSlot.GetSlotForMember(Helpers.AsSuperTypeList <MemberProjectedSlot, ProjectedSlot>(slots), member);
                if (slotForMember == null)
                {
                    return((List <MemberProjectedSlot>)null);
                }
                memberProjectedSlotList.Add(slotForMember);
            }
            return(memberProjectedSlotList);
        }
        internal ErrorLog.Record CheckForDuplicateFields(CellQuery cQuery, Cell sourceCell)
        {
            KeyToListMap <MemberProjectedSlot, int> keyToListMap = new KeyToListMap <MemberProjectedSlot, int>((IEqualityComparer <MemberProjectedSlot>)ProjectedSlot.EqualityComparer);

            for (int index = 0; index < this.m_projectedSlots.Length; ++index)
            {
                MemberProjectedSlot projectedSlot = this.m_projectedSlots[index] as MemberProjectedSlot;
                keyToListMap.Add(projectedSlot, index);
            }
            StringBuilder stringBuilder1 = (StringBuilder)null;
            bool          flag           = false;

            foreach (MemberProjectedSlot key in keyToListMap.Keys)
            {
                ReadOnlyCollection <int> cSideSlotIndexes = keyToListMap.ListForKey(key);
                if (cSideSlotIndexes.Count > 1 && !cQuery.AreSlotsEquivalentViaRefConstraints(cSideSlotIndexes))
                {
                    flag = true;
                    if (stringBuilder1 == null)
                    {
                        stringBuilder1 = new StringBuilder(Strings.ViewGen_Duplicate_CProperties((object)this.Extent.Name));
                        stringBuilder1.AppendLine();
                    }
                    StringBuilder stringBuilder2 = new StringBuilder();
                    for (int index1 = 0; index1 < cSideSlotIndexes.Count; ++index1)
                    {
                        int index2 = cSideSlotIndexes[index1];
                        if (index1 != 0)
                        {
                            stringBuilder2.Append(", ");
                        }
                        MemberProjectedSlot projectedSlot = (MemberProjectedSlot)cQuery.m_projectedSlots[index2];
                        stringBuilder2.Append(projectedSlot.ToUserString());
                    }
                    stringBuilder1.AppendLine(Strings.ViewGen_Duplicate_CProperties_IsMapped((object)key.ToUserString(), (object)stringBuilder2.ToString()));
                }
            }
            if (!flag)
            {
                return((ErrorLog.Record)null);
            }
            return(new ErrorLog.Record(ViewGenErrorCode.DuplicateCPropertiesMapped, stringBuilder1.ToString(), sourceCell, string.Empty));
        }
        internal void CreateFieldAlignedCellQueries(
            CellQuery otherQuery,
            MemberProjectionIndex projectedSlotMap,
            out CellQuery newMainQuery,
            out CellQuery newOtherQuery)
        {
            int count = projectedSlotMap.Count;

            ProjectedSlot[] newSlots1 = new ProjectedSlot[count];
            ProjectedSlot[] newSlots2 = new ProjectedSlot[count];
            for (int index1 = 0; index1 < this.m_projectedSlots.Length; ++index1)
            {
                MemberProjectedSlot projectedSlot = this.m_projectedSlots[index1] as MemberProjectedSlot;
                int index2 = projectedSlotMap.IndexOf(projectedSlot.MemberPath);
                newSlots1[index2] = this.m_projectedSlots[index1];
                newSlots2[index2] = otherQuery.m_projectedSlots[index1];
            }
            newMainQuery  = new CellQuery(this, newSlots1);
            newOtherQuery = new CellQuery(otherQuery, newSlots2);
        }
        internal Set <EdmProperty> GetCSlotsForTableColumns(IEnumerable <MemberPath> columns)
        {
            List <int> projectedPositions = this.SQuery.GetProjectedPositions(columns);

            if (projectedPositions == null)
            {
                return((Set <EdmProperty>)null);
            }
            Set <EdmProperty> set = new Set <EdmProperty>();

            foreach (int slotNum in projectedPositions)
            {
                MemberProjectedSlot memberProjectedSlot = this.CQuery.ProjectedSlotAt(slotNum) as MemberProjectedSlot;
                if (memberProjectedSlot == null)
                {
                    return((Set <EdmProperty>)null);
                }
                set.Add((EdmProperty)memberProjectedSlot.MemberPath.LeafEdmMember);
            }
            return(set);
        }
        private static bool TryGetWithRelationship(
            AssociationSetMapping colocatedAssociationSetMap,
            EntitySetBase thisExtent,
            MemberPath sRootNode,
            ref List <SlotInfo> foreignKeySlots,
            out WithRelationship withRelationship)
        {
            withRelationship = (WithRelationship)null;
            EndPropertyMapping fromAssocitionMap = LeafCellTreeNode.GetForeignKeyEndMapFromAssocitionMap(colocatedAssociationSetMap);

            if (fromAssocitionMap == null || fromAssocitionMap.AssociationEnd.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
                return(false);
            }
            AssociationEndMember associationEnd      = fromAssocitionMap.AssociationEnd;
            AssociationEndMember otherAssociationEnd = MetadataHelper.GetOtherAssociationEnd(associationEnd);
            EntityType           elementType1        = (EntityType)((RefType)associationEnd.TypeUsage.EdmType).ElementType;
            EntityType           elementType2        = (EntityType)((RefType)otherAssociationEnd.TypeUsage.EdmType).ElementType;
            AssociationSet       set    = (AssociationSet)colocatedAssociationSetMap.Set;
            MemberPath           prefix = new MemberPath((EntitySetBase)set, (EdmMember)associationEnd);
            IEnumerable <ScalarPropertyMapping> source = fromAssocitionMap.PropertyMappings.Cast <ScalarPropertyMapping>();
            List <MemberPath> memberPathList           = new List <MemberPath>();

            foreach (EdmProperty keyMember in elementType1.KeyMembers)
            {
                EdmProperty           edmProperty           = keyMember;
                ScalarPropertyMapping scalarPropertyMapping = source.Where <ScalarPropertyMapping>((Func <ScalarPropertyMapping, bool>)(propMap => propMap.Property.Equals((object)edmProperty))).First <ScalarPropertyMapping>();
                MemberProjectedSlot   memberProjectedSlot   = new MemberProjectedSlot(new MemberPath(sRootNode, (EdmMember)scalarPropertyMapping.Column));
                MemberPath            outputMember          = new MemberPath(prefix, (EdmMember)edmProperty);
                memberPathList.Add(outputMember);
                foreignKeySlots.Add(new SlotInfo(true, true, (ProjectedSlot)memberProjectedSlot, outputMember));
            }
            if (!thisExtent.ElementType.IsAssignableFrom((EdmType)elementType2))
            {
                return(false);
            }
            withRelationship = new WithRelationship(set, otherAssociationEnd, elementType2, associationEnd, elementType1, (IEnumerable <MemberPath>)memberPathList);
            return(true);
        }
Beispiel #29
0
        private static bool TryGetDomainRestrictedByWhereClause(
            IEnumerable <Constant> domain,
            MemberProjectedSlot slot,
            CellQuery cellQuery,
            out Set <Constant> result)
        {
            IEnumerable <Set <Constant> > source = cellQuery.GetConjunctsFromWhereClause().Where <MemberRestriction>((Func <MemberRestriction, bool>)(restriction => MemberPath.EqualityComparer.Equals(restriction.RestrictedMemberSlot.MemberPath, slot.MemberPath))).Select <MemberRestriction, Set <Constant> >((Func <MemberRestriction, Set <Constant> >)(restriction => new Set <Constant>(restriction.Domain.Values, Constant.EqualityComparer)));

            if (!source.Any <Set <Constant> >())
            {
                result = new Set <Constant>(domain);
                return(false);
            }
            Set <Constant> possibleValues = Domain.DeterminePossibleValues(source.SelectMany <Set <Constant>, Constant>((Func <Set <Constant>, IEnumerable <Constant> >)(m => m.Select <Constant, Constant>((Func <Constant, Constant>)(c => c)))), domain);
            Domain         domain1        = new Domain(domain, (IEnumerable <Constant>)possibleValues);

            foreach (Set <Constant> set in source)
            {
                domain1 = domain1.Intersect(new Domain((IEnumerable <Constant>)set, (IEnumerable <Constant>)possibleValues));
            }
            result = new Set <Constant>(domain1.Values, Constant.EqualityComparer);
            return(!domain.SequenceEqual <Constant>((IEnumerable <Constant>)result));
        }
Beispiel #30
0
 // effects: returns the index at which this slot appears in the projection
 // or -1 if it is not projected
 internal int GetProjectedPosition(MemberProjectedSlot slot)
 {
     for (var i = 0; i < m_projectedSlots.Length; i++)
     {
         if (ProjectedSlot.EqualityComparer.Equals(slot, m_projectedSlots[i]))
         {
             return i;
         }
     }
     return -1;
 }
 // <summary>
 // Creates a complete member restriction with the meaning "<paramref name="slot" /> in <paramref name="values" />".
 // </summary>
 // <param name="possibleValues">
 // all the values that the <paramref name="slot" /> can take
 // </param>
 protected MemberRestriction(MemberProjectedSlot slot, IEnumerable<Constant> values, IEnumerable<Constant> possibleValues)
     : this(slot, new Domain(values, possibleValues))
 {
     DebugCheck.NotNull(possibleValues);
 }
Beispiel #32
0
 // <summary>
 // Creates an incomplete member restriction with the meaning "<paramref name="slot" /> in <paramref name="values" />".
 // </summary>
 protected MemberRestriction(MemberProjectedSlot slot, IEnumerable <Constant> values)
 {
     m_restrictedMemberSlot = slot;
     m_domain = new Domain(values, values);
 }
        // returns true when the case statement is completed
        private bool AddRewritingToCaseStatement(
            Tile<FragmentQuery> rewriting, CaseStatement caseStatement, MemberPath currentPath, Constant domainValue)
        {
            var whenCondition = BoolExpression.True;
            // check whether the rewriting is always true or always false
            // if it's always true, we don't need any other WHEN clauses in the case statement
            // if it's always false, we don't need to add this WHEN clause to the case statement
            // given: domainQuery is satisfied. Check (domainQuery -> rewriting)
            var isAlwaysTrue = _qp.IsContainedIn(CreateTile(_domainQuery), rewriting);
            var isAlwaysFalse = _qp.IsDisjointFrom(CreateTile(_domainQuery), rewriting);
            Debug.Assert(!(isAlwaysTrue && isAlwaysFalse));
            if (isAlwaysFalse)
            {
                return false; // don't need an unsatisfiable WHEN clause
            }
            if (isAlwaysTrue)
            {
                Debug.Assert(caseStatement.Clauses.Count == 0);
            }

            ProjectedSlot projectedSlot;
            if (domainValue.HasNotNull())
            {
                projectedSlot = new MemberProjectedSlot(currentPath);
            }
            else
            {
                projectedSlot = new ConstantProjectedSlot(domainValue);
            }

            if (!isAlwaysTrue)
            {
                whenCondition = TileToBoolExpr(rewriting);
            }
            else
            {
                whenCondition = BoolExpression.True;
            }
            caseStatement.AddWhenThen(whenCondition, projectedSlot);

            return isAlwaysTrue;
        }
 /// <summary>
 /// Creates a complete member restriction with the meaning "<paramref name="slot"/> in <paramref name="values"/>".
 /// </summary>
 /// <param name="possibleValues">all the values that the <paramref name="slot"/> can take</param>
 protected MemberRestriction(MemberProjectedSlot slot, IEnumerable<Constant> values, IEnumerable<Constant> possibleValues)
     : this(slot, new Domain(values, possibleValues))
 {
     Debug.Assert(possibleValues != null);
 }
 /// <summary>
 /// Creates an incomplete member restriction with the meaning "<paramref name="slot"/> in <paramref name="values"/>".
 /// </summary>
 protected MemberRestriction(MemberProjectedSlot slot, IEnumerable<Constant> values)
 {
     m_restrictedMemberSlot = slot;
     m_domain = new Domain(values, values);
 }
 /// <summary>
 /// Creates an incomplete member restriction with the meaning "<paramref name="slot"/> = <paramref name="value"/>".
 /// "Partial" means that the <see cref="Domain"/> in this restriction is partial - hence the operations on the restriction are limited.
 /// </summary>
 protected MemberRestriction(MemberProjectedSlot slot, Constant value)
     : this(slot, new[] { value })
 {
 }
        // requires: domain not have any Negated constants other than NotNull
        // Also, cellQuery contains all final oneOfConsts or all partial oneOfConsts
        // cellquery must contain a whereclause of the form "True", "OneOfConst" or "
        // "OneOfConst AND ... AND OneOfConst"
        // slot must present in cellQuery and incomingDomain is the domain for it
        // effects: Returns the set of values that slot can take as restricted by cellQuery's whereClause
        private static bool TryGetDomainRestrictedByWhereClause(
            IEnumerable<Constant> domain, MemberProjectedSlot slot, CellQuery cellQuery, out CellConstantSet result)
        {
            var conditionsForSlot = cellQuery.GetConjunctsFromWhereClause()
                                             .Where(
                                                 restriction =>
                                                 MemberPath.EqualityComparer.Equals(
                                                     restriction.RestrictedMemberSlot.MemberPath, slot.MemberPath))
                                             .Select(
                                                 restriction => new CellConstantSet(restriction.Domain.Values, Constant.EqualityComparer));

            //Debug.Assert(!conditionsForSlot.Skip(1).Any(), "More than one Clause with the same path");

            if (!conditionsForSlot.Any())
            {
                // If the slot was not mentioned in the query return the domain without restricting it 
                result = new CellConstantSet(domain);
                return false;
            }

            // Now get all the possible values from domain and conditionValues
            var possibleValues = DeterminePossibleValues(conditionsForSlot.SelectMany(m => m.Select(c => c)), domain);

            var restrictedDomain = new Domain(domain, possibleValues);
            foreach (var conditionValues in conditionsForSlot)
            {
                // Domain derived from Edm-Type INTERSECTED with Conditions
                restrictedDomain = restrictedDomain.Intersect(new Domain(conditionValues, possibleValues));
            }

            result = new CellConstantSet(restrictedDomain.Values, Constant.EqualityComparer);
            return !domain.SequenceEqual(result);
        }
Beispiel #38
0
        // requires: The Where clause satisfies the same requirements a GetConjunctsFromWhereClause
        // effects: For each slot that has a NotNull condition in the where
        // clause, checks if it is projected. If all such slots are
        // projected, returns null. Else returns an error record
        internal ErrorLog.Record CheckForProjectedNotNullSlots(Cell sourceCell, IEnumerable <Cell> associationSets)
        {
            var builder    = new StringBuilder();
            var foundError = false;

            foreach (var restriction in Conditions)
            {
                if (restriction.Domain.ContainsNotNull())
                {
                    var slot = MemberProjectedSlot.GetSlotForMember(m_projectedSlots, restriction.RestrictedMemberSlot.MemberPath);
                    if (slot == null) //member with not null condition is not mapped in this extent
                    {
                        var missingMapping = true;
                        if (Extent is EntitySet)
                        {
                            var isCQuery       = sourceCell.CQuery == this;
                            var target         = isCQuery ? ViewTarget.QueryView : ViewTarget.UpdateView;
                            var rightCellQuery = isCQuery ? sourceCell.SQuery : sourceCell.CQuery;

                            //Find out if there is an association mapping but only if the current Not Null condition is on an EntitySet
                            var rightExtent = rightCellQuery.Extent as EntitySet;
                            if (rightExtent != null)
                            {
                                var associations = MetadataHelper.GetAssociationsForEntitySet(rightCellQuery.Extent as EntitySet);
                                foreach (
                                    var association in
                                    associations.Where(
                                        association =>
                                        association.AssociationSetEnds.Any(
                                            end =>
                                            (end.CorrespondingAssociationEndMember.RelationshipMultiplicity
                                             == RelationshipMultiplicity.One &&
                                             (MetadataHelper.GetOppositeEnd(end).EntitySet.EdmEquals(rightExtent))))))
                                {
                                    foreach (
                                        var associationCell in
                                        associationSets.Where(c => c.GetRightQuery(target).Extent.EdmEquals(association)))
                                    {
                                        if (MemberProjectedSlot.GetSlotForMember(
                                                associationCell.GetLeftQuery(target).ProjectedSlots, restriction.RestrictedMemberSlot.MemberPath)
                                            != null)
                                        {
                                            missingMapping = false;
                                        }
                                    }
                                }
                            }
                        }

                        if (missingMapping)
                        {
                            // condition of NotNull and slot not being projected
                            builder.AppendLine(
                                Strings.ViewGen_NotNull_No_Projected_Slot(
                                    restriction.RestrictedMemberSlot.MemberPath.PathToString(false)));
                            foundError = true;
                        }
                    }
                }
            }
            if (false == foundError)
            {
                return(null);
            }
            var record = new ErrorLog.Record(ViewGenErrorCode.NotNullNoProjectedSlot, builder.ToString(), sourceCell, String.Empty);

            return(record);
        }
Beispiel #39
0
        // effects: Returns an error record if the keys of the extent/associationSet being mapped  are
        // present in the projected slots of this query. Returns null
        // otherwise. ownerCell indicates the cell that owns this and
        // resourceString is a resource used for error messages
        internal ErrorLog.Record VerifyKeysPresent(
            Cell ownerCell, Func <object, object, string> formatEntitySetMessage,
            Func <object, object, object, string> formatAssociationSetMessage, ViewGenErrorCode errorCode)
        {
            var prefixes = new List <MemberPath>(1);
            // Keep track of the key corresponding to each prefix
            var keys = new List <ExtentKey>(1);

            if (Extent is EntitySet)
            {
                // For entity set just get the full path of the key properties
                var prefix = new MemberPath(Extent);
                prefixes.Add(prefix);
                var entityType    = (EntityType)Extent.ElementType;
                var entitySetKeys = ExtentKey.GetKeysForEntityType(prefix, entityType);
                Debug.Assert(entitySetKeys.Count == 1, "Currently, we only support primary keys");
                keys.Add(entitySetKeys[0]);
            }
            else
            {
                var relationshipSet = (AssociationSet)Extent;
                // For association set, get the full path of the key
                // properties of each end

                foreach (var relationEnd in relationshipSet.AssociationSetEnds)
                {
                    var assocEndMember = relationEnd.CorrespondingAssociationEndMember;
                    var prefix         = new MemberPath(relationshipSet, assocEndMember);
                    prefixes.Add(prefix);
                    var endKeys = ExtentKey.GetKeysForEntityType(
                        prefix,
                        MetadataHelper.GetEntityTypeForEnd(assocEndMember));
                    Debug.Assert(endKeys.Count == 1, "Currently, we only support primary keys");
                    keys.Add(endKeys[0]);
                }
            }

            for (var i = 0; i < prefixes.Count; i++)
            {
                var prefix = prefixes[i];
                // Get all or none key slots that are being projected in this cell query
                var keySlots = MemberProjectedSlot.GetKeySlots(GetMemberProjectedSlots(), prefix);
                if (keySlots == null)
                {
                    var    key = keys[i];
                    string message;
                    if (Extent is EntitySet)
                    {
                        var keyPropertiesString = MemberPath.PropertiesToUserString(key.KeyFields, true);
                        message = formatEntitySetMessage(keyPropertiesString, Extent.Name);
                    }
                    else
                    {
                        var endName             = prefix.RootEdmMember.Name;
                        var keyPropertiesString = MemberPath.PropertiesToUserString(key.KeyFields, false);
                        message = formatAssociationSetMessage(keyPropertiesString, endName, Extent.Name);
                    }
                    var error = new ErrorLog.Record(errorCode, message, ownerCell, String.Empty);
                    return(error);
                }
            }
            return(null);
        }
 // effects: 
 /// <summary>
 ///     Creates a view cell slot that corresponds to <paramref name="slotNum" /> in some cell. The <paramref name="cSlot" /> and
 ///     <paramref
 ///         name="sSlot" />
 ///     represent the
 ///     slots in the left and right queries of the view cell.
 /// </summary>
 internal ViewCellSlot(int slotNum, MemberProjectedSlot cSlot, MemberProjectedSlot sSlot)
 {
     m_slotNum = slotNum;
     m_cSlot = cSlot;
     m_sSlot = sSlot;
 }
 // <summary>
 // Creates a scalar member restriction with the meaning "<paramref name="slot" /> in <paramref name="domain" />".
 // </summary>
 internal ScalarRestriction(MemberProjectedSlot slot, Domain domain)
     : base(slot, domain)
 {
 }
        //True = domain is restricted, False = domain is not restricted (because there is no condition)
        private static bool GetRestrictedOrUnrestrictedDomain(
            MemberProjectedSlot slot, CellQuery cellQuery, EdmItemCollection edmItemCollection, out CellConstantSet domain)
        {
            var domainValues = DeriveDomainFromMemberPath(slot.MemberPath, edmItemCollection, true /* leaveDomainUnbounded */);

            //Note, out domain is set even in the case where method call returns false
            return TryGetDomainRestrictedByWhereClause(domainValues, slot, cellQuery, out domain);
        }