/// <summary>
        /// Provides language specific query translation.  Use this to apply language specific rewrites or
        /// to make assertions/validations about the query.
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual Expression Translate(Expression expression)
        {
            // remove redundant layers again before cross apply rewrite
            expression = UnusedColumnRemover.Remove(expression);
            expression = RedundantColumnRemover.Remove(expression);
            expression = RedundantSubqueryRemover.Remove(expression);

            // convert cross-apply and outer-apply joins into inner & left-outer-joins if possible
            var rewritten = CrossApplyRewriter.Rewrite(this.language, expression);

            // convert cross joins into inner joins
            rewritten = CrossJoinRewriter.Rewrite(rewritten);

            if (rewritten != expression)
            {
                expression = rewritten;
                // do final reduction
                expression = UnusedColumnRemover.Remove(expression);
                expression = RedundantSubqueryRemover.Remove(expression);
                expression = RedundantJoinRemover.Remove(expression);
                expression = RedundantColumnRemover.Remove(expression);
            }

            return(expression);
        }
Exemple #2
0
        /// <summary>
        /// Apply mapping translations to this expression
        /// </summary>
        public virtual Expression Translate(Expression expression)
        {
            // convert references to LINQ operators into query specific nodes
            var bound = QueryBinder.Bind(this, expression);

            // move aggregate computations so they occur in same select as group-by
            var aggmoved = AggregateRewriter.Rewrite(this.Translator.Linguist.Language, bound);

            // do reduction so duplicate association's are likely to be clumped together
            var reduced = UnusedColumnRemover.Remove(aggmoved);

            reduced = RedundantColumnRemover.Remove(reduced);
            reduced = RedundantSubqueryRemover.Remove(reduced);
            reduced = RedundantJoinRemover.Remove(reduced);

            // convert references to association properties into correlated queries
            var rbound = RelationshipBinder.Bind(this, reduced);

            if (rbound != reduced)
            {
                // clean up after ourselves! (multiple references to same association property)
                rbound = RedundantColumnRemover.Remove(rbound);
                rbound = RedundantJoinRemover.Remove(rbound);
            }

            // rewrite comparision checks between entities and multi-valued constructs
            var result = ComparisonRewriter.Rewrite(this.Mapping, rbound);

            return(result);
        }
Exemple #3
0
        /// <summary>
        /// Translates the query expression to include changes that enforce the policy.
        /// This is where choices about inclusion of related objects and how heirarchies are materialized affect the definition of the queries.
        /// </summary>
        public virtual Expression Translate(Expression expression)
        {
            // add included relationships to client projection
            var rewritten = RelationshipIncluder.Include(this.Translator.Mapper, expression);

            if (rewritten != expression)
            {
                expression = rewritten;
                expression = UnusedColumnRemover.Remove(expression);
                expression = RedundantColumnRemover.Remove(expression);
                expression = RedundantSubqueryRemover.Remove(expression);
                expression = RedundantJoinRemover.Remove(expression);
            }

            // convert any singleton (1:1 or n:1) projections into server-side joins (cardinality is preserved)
            rewritten = SingletonProjectionRewriter.Rewrite(this.Translator.Linguist.Language, expression);
            if (rewritten != expression)
            {
                expression = rewritten;
                expression = UnusedColumnRemover.Remove(expression);
                expression = RedundantColumnRemover.Remove(expression);
                expression = RedundantSubqueryRemover.Remove(expression);
                expression = RedundantJoinRemover.Remove(expression);
            }

            // convert projections into client-side joins
            rewritten = ClientJoinedProjectionRewriter.Rewrite(this.Policy, this.Translator.Linguist.Language, expression);
            if (rewritten != expression)
            {
                expression = rewritten;
                expression = UnusedColumnRemover.Remove(expression);
                expression = RedundantColumnRemover.Remove(expression);
                expression = RedundantSubqueryRemover.Remove(expression);
                expression = RedundantJoinRemover.Remove(expression);
            }

            return(expression);
        }