internal QueryRewriter(
     EdmType generatedType,
     ViewgenContext context,
     ViewGenMode typesGenerationMode)
 {
     this._typesGenerationMode = typesGenerationMode;
     this._context             = context;
     this._generatedType       = generatedType;
     this._domainMap           = context.MemberMaps.LeftDomainMap;
     this._config        = context.Config;
     this._identifiers   = context.CqlIdentifiers;
     this._qp            = new RewritingProcessor <Tile <FragmentQuery> >((TileProcessor <Tile <FragmentQuery> >) new DefaultTileProcessor <FragmentQuery>((TileQueryProcessor <FragmentQuery>)context.LeftFragmentQP));
     this._extentPath    = new MemberPath(context.Extent);
     this._keyAttributes = new List <MemberPath>(MemberPath.GetKeyMembers(context.Extent, this._domainMap));
     foreach (LeftCellWrapper leftCellWrapper in this._context.AllWrappersForExtent)
     {
         FragmentQuery        fragmentQuery = leftCellWrapper.FragmentQuery;
         Tile <FragmentQuery> tile          = (Tile <FragmentQuery>)QueryRewriter.CreateTile(fragmentQuery);
         this._fragmentQueries.Add(fragmentQuery);
         this._views.Add(tile);
     }
     this.AdjustMemberDomainsForUpdateViews();
     this._domainQuery = this.GetDomainQuery(this.FragmentQueries, generatedType);
     this._usedViews   = new HashSet <FragmentQuery>();
 }
 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 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));
        }
        private IEnumerable <Tile <FragmentQuery> > GetRelevantViews(
            FragmentQuery query)
        {
            Set <MemberPath>             variables = QueryRewriter.GetVariables(query);
            Tile <FragmentQuery>         a1        = (Tile <FragmentQuery>)null;
            List <Tile <FragmentQuery> > tileList  = new List <Tile <FragmentQuery> >();
            Tile <FragmentQuery>         tile      = (Tile <FragmentQuery>)null;

            foreach (Tile <FragmentQuery> view in this._views)
            {
                if (QueryRewriter.GetVariables(view.Query).Overlaps(variables))
                {
                    a1 = a1 == null ? view : this._qp.Union(a1, view);
                    tileList.Add(view);
                }
                else if (this.IsTrue(view.Query) && tile == null)
                {
                    tile = view;
                }
            }
            if (a1 != null && this.IsTrue(a1.Query))
            {
                return((IEnumerable <Tile <FragmentQuery> >)tileList);
            }
            if (tile == null)
            {
                Tile <FragmentQuery> a2 = (Tile <FragmentQuery>)null;
                foreach (FragmentQuery fragmentQuery in this._fragmentQueries)
                {
                    a2 = a2 == null ? (Tile <FragmentQuery>)QueryRewriter.CreateTile(fragmentQuery) : this._qp.Union(a2, (Tile <FragmentQuery>)QueryRewriter.CreateTile(fragmentQuery));
                    if (this.IsTrue(a2.Query))
                    {
                        tile = QueryRewriter._trueViewSurrogate;
                        break;
                    }
                }
            }
            if (tile == null)
            {
                return((IEnumerable <Tile <FragmentQuery> >) this._views);
            }
            tileList.Add(tile);
            return((IEnumerable <Tile <FragmentQuery> >)tileList);
        }
        private bool AddRewritingToCaseStatement(
            Tile <FragmentQuery> rewriting,
            CaseStatement caseStatement,
            MemberPath currentPath,
            Constant domainValue)
        {
            BoolExpression boolExpression = BoolExpression.True;
            bool           flag           = this._qp.IsContainedIn((Tile <FragmentQuery>)QueryRewriter.CreateTile(this._domainQuery), rewriting);

            if (this._qp.IsDisjointFrom((Tile <FragmentQuery>)QueryRewriter.CreateTile(this._domainQuery), rewriting))
            {
                return(false);
            }
            int            num           = flag ? 1 : 0;
            ProjectedSlot  projectedSlot = !domainValue.HasNotNull() ? (ProjectedSlot) new ConstantProjectedSlot(domainValue) : (ProjectedSlot) new MemberProjectedSlot(currentPath);
            BoolExpression condition     = flag ? BoolExpression.True : QueryRewriter.TileToBoolExpr(rewriting);

            caseStatement.AddWhenThen(condition, projectedSlot);
            return(flag);
        }
        private bool CoverAttributes(
            ref Tile <FragmentQuery> rewriting,
            Dictionary <MemberPath, FragmentQuery> attributeConditions)
        {
            foreach (FragmentQuery view in new HashSet <FragmentQuery>(rewriting.GetNamedQueries()))
            {
                foreach (MemberPath nonKey in QueryRewriter.NonKeys((IEnumerable <MemberPath>)view.Attributes))
                {
                    this.CoverAttribute(nonKey, view, attributeConditions);
                }
                if (attributeConditions.Count == 0)
                {
                    return(true);
                }
            }
            Tile <FragmentQuery> tile = (Tile <FragmentQuery>)null;

            foreach (FragmentQuery fragmentQuery in this._fragmentQueries)
            {
                foreach (MemberPath nonKey in QueryRewriter.NonKeys((IEnumerable <MemberPath>)fragmentQuery.Attributes))
                {
                    if (this.CoverAttribute(nonKey, fragmentQuery, attributeConditions))
                    {
                        tile = tile == null ? (Tile <FragmentQuery>)QueryRewriter.CreateTile(fragmentQuery) : this._qp.Union(tile, (Tile <FragmentQuery>)QueryRewriter.CreateTile(fragmentQuery));
                    }
                }
                if (attributeConditions.Count == 0)
                {
                    break;
                }
            }
            if (attributeConditions.Count != 0)
            {
                return(false);
            }
            rewriting = this._qp.Join(rewriting, tile);
            return(true);
        }
        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 HashSet <FragmentQuery> GetUsedViewsAndRemoveTrueSurrogate(
            ref Tile <FragmentQuery> rewriting)
        {
            HashSet <FragmentQuery> first = new HashSet <FragmentQuery>(rewriting.GetNamedQueries());

            if (!first.Contains(QueryRewriter._trueViewSurrogate.Query))
            {
                return(first);
            }
            first.Remove(QueryRewriter._trueViewSurrogate.Query);
            Tile <FragmentQuery> tile = (Tile <FragmentQuery>)null;

            foreach (FragmentQuery query in first.Concat <FragmentQuery>((IEnumerable <FragmentQuery>) this._fragmentQueries))
            {
                tile = tile == null ? (Tile <FragmentQuery>)QueryRewriter.CreateTile(query) : this._qp.Union(tile, (Tile <FragmentQuery>)QueryRewriter.CreateTile(query));
                first.Add(query);
                if (this.IsTrue(tile.Query))
                {
                    rewriting = rewriting.Replace(QueryRewriter._trueViewSurrogate, tile);
                    return(first);
                }
            }
            return(first);
        }
        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);
        }