internal FragmentQuery GetDomainQuery(
            IEnumerable <FragmentQuery> fragmentQueries,
            EdmType generatedType)
        {
            if (this._context.ViewTarget != ViewTarget.QueryView)
            {
                return(FragmentQuery.Create((IEnumerable <MemberPath>) this._keyAttributes, BoolExpression.CreateOr(fragmentQueries.Select <FragmentQuery, BoolExpression>((Func <FragmentQuery, BoolExpression>)(fragmentQuery => fragmentQuery.Condition)).ToArray <BoolExpression>())));
            }
            BoolExpression literal;

            if (generatedType == null)
            {
                literal = BoolExpression.True;
            }
            else
            {
                IEnumerable <EdmType> types;
                if (this._typesGenerationMode == ViewGenMode.OfTypeOnlyViews)
                {
                    types = (IEnumerable <EdmType>) new HashSet <EdmType>()
                    {
                        this._generatedType
                    }
                }
                ;
                else
                {
                    types = MetadataHelper.GetTypeAndSubtypesOf(generatedType, (ItemCollection)this._context.EdmItemCollection, false);
                }
                literal = BoolExpression.CreateLiteral((BoolLiteral) new TypeRestriction(new MemberProjectedSlot(this._extentPath), new Domain(QueryRewriter.GetTypeConstants(types), this._domainMap.GetDomain(this._extentPath))), this._domainMap);
            }
            return(FragmentQuery.Create((IEnumerable <MemberPath>) this._keyAttributes, literal));
        }
        internal static FragmentQuery CreateMemberConditionQuery(
            MemberPath currentPath,
            Constant domainValue,
            IEnumerable <MemberPath> keyAttributes,
            MemberDomainMap domainMap)
        {
            BoolExpression           memberCondition = FragmentQuery.CreateMemberCondition(currentPath, domainValue, domainMap);
            IEnumerable <MemberPath> attrs           = keyAttributes;

            if (domainValue is NegatedConstant)
            {
                attrs = keyAttributes.Concat <MemberPath>((IEnumerable <MemberPath>) new MemberPath[1]
                {
                    currentPath
                });
            }
            return(FragmentQuery.Create(attrs, memberCondition));
        }
        private bool CoverAttribute(
            MemberPath projectedAttribute,
            FragmentQuery view,
            Dictionary <MemberPath, FragmentQuery> attributeConditions)
        {
            FragmentQuery fragmentQuery;

            if (!attributeConditions.TryGetValue(projectedAttribute, out fragmentQuery))
            {
                return(false);
            }
            FragmentQuery query = FragmentQuery.Create(BoolExpression.CreateAndNot(fragmentQuery.Condition, view.Condition));

            if (this._qp.IsEmpty((Tile <FragmentQuery>)QueryRewriter.CreateTile(query)))
            {
                attributeConditions.Remove(projectedAttribute);
            }
            else
            {
                attributeConditions[projectedAttribute] = query;
            }
            return(true);
        }
        private bool RewriteQuery(
            Tile <FragmentQuery> toFill,
            Tile <FragmentQuery> toAvoid,
            out Tile <FragmentQuery> rewriting,
            out IEnumerable <MemberPath> notCoveredAttributes,
            bool isRelaxed)
        {
            notCoveredAttributes = (IEnumerable <MemberPath>) new List <MemberPath>();
            FragmentQuery query1 = toFill.Query;

            if (this._context.TryGetCachedRewriting(query1, out rewriting))
            {
                return(true);
            }
            IEnumerable <Tile <FragmentQuery> > relevantViews = this.GetRelevantViews(query1);
            FragmentQuery query2 = query1;

            if (!this.RewriteQueryCached((Tile <FragmentQuery>)QueryRewriter.CreateTile(FragmentQuery.Create(query1.Condition)), toAvoid, relevantViews, out rewriting))
            {
                if (!isRelaxed)
                {
                    return(false);
                }
                query1 = FragmentQuery.Create((IEnumerable <MemberPath>)query1.Attributes, BoolExpression.CreateAndNot(query1.Condition, rewriting.Query.Condition));
                if (this._qp.IsEmpty((Tile <FragmentQuery>)QueryRewriter.CreateTile(query1)) || !this.RewriteQueryCached((Tile <FragmentQuery>)QueryRewriter.CreateTile(FragmentQuery.Create(query1.Condition)), toAvoid, relevantViews, out rewriting))
                {
                    return(false);
                }
            }
            if (query1.Attributes.Count == 0)
            {
                return(true);
            }
            Dictionary <MemberPath, FragmentQuery> attributeConditions = new Dictionary <MemberPath, FragmentQuery>();

            foreach (MemberPath nonKey in QueryRewriter.NonKeys((IEnumerable <MemberPath>)query1.Attributes))
            {
                attributeConditions[nonKey] = query1;
            }
            if (attributeConditions.Count == 0 || this.CoverAttributes(ref rewriting, attributeConditions))
            {
                this.GetUsedViewsAndRemoveTrueSurrogate(ref rewriting);
                this._context.SetCachedRewriting(query2, rewriting);
                return(true);
            }
            if (isRelaxed)
            {
                foreach (MemberPath nonKey in QueryRewriter.NonKeys((IEnumerable <MemberPath>)query1.Attributes))
                {
                    FragmentQuery fragmentQuery;
                    attributeConditions[nonKey] = !attributeConditions.TryGetValue(nonKey, out fragmentQuery) ? query1 : FragmentQuery.Create(BoolExpression.CreateAndNot(query1.Condition, fragmentQuery.Condition));
                }
                if (this.CoverAttributes(ref rewriting, attributeConditions))
                {
                    this.GetUsedViewsAndRemoveTrueSurrogate(ref rewriting);
                    this._context.SetCachedRewriting(query2, rewriting);
                    return(true);
                }
            }
            notCoveredAttributes = (IEnumerable <MemberPath>)attributeConditions.Keys;
            return(false);
        }
        private bool FindRewriting(
            IEnumerable <MemberPath> attributes,
            BoolExpression whereClause,
            out Tile <FragmentQuery> rewriting,
            out IEnumerable <MemberPath> notCoveredAttributes)
        {
            Tile <FragmentQuery> tile1 = (Tile <FragmentQuery>)QueryRewriter.CreateTile(FragmentQuery.Create(attributes, whereClause));
            Tile <FragmentQuery> tile2 = (Tile <FragmentQuery>)QueryRewriter.CreateTile(FragmentQuery.Create((IEnumerable <MemberPath>) this._keyAttributes, BoolExpression.CreateNot(whereClause)));
            bool isRelaxed             = this._context.ViewTarget == ViewTarget.UpdateView;

            return(this.RewriteQuery(tile1, tile2, out rewriting, out notCoveredAttributes, isRelaxed));
        }
 internal void EnsureExtentIsFullyMapped(HashSet <FragmentQuery> outputUsedViews)
 {
     if (this._context.ViewTarget == ViewTarget.QueryView && this._config.IsValidationEnabled)
     {
         this.EnsureConfigurationIsFullyMapped(this._extentPath, BoolExpression.True, outputUsedViews, this._errorLog);
         if (this._errorLog.Count <= 0)
         {
             return;
         }
         ExceptionHelpers.ThrowMappingException(this._errorLog, this._config);
     }
     else
     {
         if (this._config.IsValidationEnabled)
         {
             foreach (MemberPath member in this._context.MemberMaps.ProjectedSlotMap.Members)
             {
                 Constant defaultConstant;
                 if (member.IsScalarType() && !member.IsPartOfKey && (!this._domainMap.IsConditionMember(member) && !Domain.TryGetDefaultValueForMemberPath(member, out defaultConstant)))
                 {
                     HashSet <MemberPath> memberPathSet = new HashSet <MemberPath>((IEnumerable <MemberPath>) this._keyAttributes);
                     memberPathSet.Add(member);
                     foreach (LeftCellWrapper leftCellWrapper in this._context.AllWrappersForExtent)
                     {
                         FragmentQuery            fragmentQuery = leftCellWrapper.FragmentQuery;
                         Tile <FragmentQuery>     rewriting;
                         IEnumerable <MemberPath> notCoveredAttributes;
                         if (!this.RewriteQuery((Tile <FragmentQuery>)QueryRewriter.CreateTile(new FragmentQuery(fragmentQuery.Description, fragmentQuery.FromVariable, (IEnumerable <MemberPath>)memberPathSet, fragmentQuery.Condition)), (Tile <FragmentQuery>)QueryRewriter.CreateTile(FragmentQuery.Create((IEnumerable <MemberPath>) this._keyAttributes, BoolExpression.CreateNot(fragmentQuery.Condition))), out rewriting, out notCoveredAttributes, false))
                         {
                             Domain.GetDefaultValueForMemberPath(member, (IEnumerable <LeftCellWrapper>) new LeftCellWrapper[1]
                             {
                                 leftCellWrapper
                             }, this._config);
                         }
                     }
                 }
             }
         }
         foreach (Tile <FragmentQuery> view in this._views)
         {
             Tile <FragmentQuery>     toFill = view;
             Tile <FragmentQuery>     tile   = (Tile <FragmentQuery>)QueryRewriter.CreateTile(FragmentQuery.Create((IEnumerable <MemberPath>) this._keyAttributes, BoolExpression.CreateNot(toFill.Query.Condition)));
             Tile <FragmentQuery>     rewriting;
             IEnumerable <MemberPath> notCoveredAttributes;
             if (!this.RewriteQuery(toFill, tile, out rewriting, out notCoveredAttributes, true))
             {
                 LeftCellWrapper leftCellWrapper = this._context.AllWrappersForExtent.First <LeftCellWrapper>((Func <LeftCellWrapper, bool>)(lcr => lcr.FragmentQuery.Equals((object)toFill.Query)));
                 this._errorLog.AddEntry(new ErrorLog.Record(ViewGenErrorCode.ImpopssibleCondition, Strings.Viewgen_QV_RewritingNotFound((object)leftCellWrapper.RightExtent.ToString()), leftCellWrapper.Cells, string.Empty));
             }
             else
             {
                 outputUsedViews.UnionWith(rewriting.GetNamedQueries());
             }
         }
     }
 }
 private bool IsTrue(FragmentQuery query)
 {
     return(!this._context.LeftFragmentQP.IsSatisfiable(FragmentQuery.Create(BoolExpression.CreateNot(query.Condition))));
 }