protected override Expression VisitMemberAccess(MemberExpression m)
        {
            Expression source = this.Visit(m.Expression);
            DbEntityExpression ex = source as DbEntityExpression;

            if (ex != null && this.mapping.IsAssociation(ex.Entity, m.Member))
            {
                DbProjectionExpression projection = (DbProjectionExpression)this.Visit(this.mapping.CreateMemberExpression(source, ex.Entity, m.Member));
                if (this.currentFrom != null && this.mapping.IsSingletonAssociation(ex.Entity, m.Member))
                {
                    // convert singleton associations directly to OUTER APPLY
                    projection = projection.AddOuterJoinTest();
                    Expression newFrom = new DbJoinExpression(DbJoinType.OuterApply, this.currentFrom, projection.Select, null);
                    this.currentFrom = newFrom;
                    return projection.Projector;
                }
                return projection;
            }
            else
            {
                Expression result = QueryBinder.BindMember(source, m.Member);
                MemberExpression mex = result as MemberExpression;
                if (mex != null && mex.Member == m.Member && mex.Expression == m.Expression)
                {
                    return m;
                }
                return result;
            }
        }
        protected virtual bool CompareJoin(DbJoinExpression a, DbJoinExpression b)
        {
            if (a.Join != b.Join || !Compare(a.Left, b.Left))
            {
                return(false);
            }

            if (a.Join == DbJoinType.CrossApply || a.Join == DbJoinType.OuterApply)
            {
                ScopedDictionary <DbTableAlias, DbTableAlias> save = _aliasScope;
                try
                {
                    _aliasScope = new ScopedDictionary <DbTableAlias, DbTableAlias>(_aliasScope);
                    MapAliases(a.Left, b.Left);

                    return(Compare(a.Right, b.Right) &&
                           Compare(a.Condition, b.Condition));
                }
                finally
                {
                    _aliasScope = save;
                }
            }
            else
            {
                return(Compare(a.Right, b.Right) &&
                       Compare(a.Condition, b.Condition));
            }
        }
        private Expression FindSimilarRight(DbJoinExpression join, DbJoinExpression compareTo)
        {
            if (join == null)
            {
                return(null);
            }
            if (join.Join == compareTo.Join)
            {
                if (join.Right.NodeType == compareTo.Right.NodeType &&
                    DbExpressionComparer.AreEqual(join.Right, compareTo.Right))
                {
                    if (join.Condition == compareTo.Condition)
                    {
                        return(join.Right);
                    }
                    var scope = new ScopedDictionary <DbTableAlias, DbTableAlias>(null);
                    scope.Add(((DbAliasedExpression)join.Right).Alias, ((DbAliasedExpression)compareTo.Right).Alias);
                    if (DbExpressionComparer.AreEqual(null, scope, join.Condition, compareTo.Condition))
                    {
                        return(join.Right);
                    }
                }
            }
            Expression result = FindSimilarRight(join.Left as DbJoinExpression, compareTo);

            if (result == null)
            {
                result = FindSimilarRight(join.Right as DbJoinExpression, compareTo);
            }
            return(result);
        }
예제 #4
0
 protected override Expression VisitJoin(DbJoinExpression join)
 {
     if (join.Join == DbJoinType.SingletonLeftOuter)
     {
         // first visit right side w/o looking at condition
         Expression          right = this.Visit(join.Right);
         DbAliasedExpression ax    = right as DbAliasedExpression;
         if (ax != null && !this.allColumnsUsed.ContainsKey(ax.Alias))
         {
             // if nothing references the alias on the right, then the join is redundant
             return(this.Visit(join.Left));
         }
         // otherwise do it the right way
         Expression cond = this.Visit(join.Condition);
         Expression left = this.Visit(join.Left);
         right = this.Visit(join.Right);
         return(this.UpdateJoin(join, join.Join, left, right, cond));
     }
     else
     {
         // visit join in reverse order
         Expression condition = this.Visit(join.Condition);
         Expression right     = this.VisitSource(join.Right);
         Expression left      = this.VisitSource(join.Left);
         return(this.UpdateJoin(join, join.Join, left, right, condition));
     }
 }
예제 #5
0
        protected override Expression VisitMemberAccess(MemberExpression m)
        {
            Expression         source = this.Visit(m.Expression);
            DbEntityExpression ex     = source as DbEntityExpression;

            if (ex != null && this.mapping.IsAssociation(ex.Entity, m.Member))
            {
                DbProjectionExpression projection = (DbProjectionExpression)this.Visit(this.mapping.CreateMemberExpression(source, ex.Entity, m.Member));
                if (this.currentFrom != null && this.mapping.IsSingletonAssociation(ex.Entity, m.Member))
                {
                    // convert singleton associations directly to OUTER APPLY
                    projection = projection.AddOuterJoinTest();
                    Expression newFrom = new DbJoinExpression(DbJoinType.OuterApply, this.currentFrom, projection.Select, null);
                    this.currentFrom = newFrom;
                    return(projection.Projector);
                }
                return(projection);
            }
            else
            {
                Expression       result = QueryBinder.BindMember(source, m.Member);
                MemberExpression mex    = result as MemberExpression;
                if (mex != null && mex.Member == m.Member && mex.Expression == m.Expression)
                {
                    return(m);
                }
                return(result);
            }
        }
예제 #6
0
        protected virtual Expression VisitJoin(DbJoinExpression join)
        {
            Expression left      = VisitSource(join.Left);
            Expression right     = VisitSource(join.Right);
            Expression condition = Visit(join.Condition);

            return(UpdateJoin(join, join.Join, left, right, condition));
        }
        protected override Expression VisitProjection(DbProjectionExpression proj)
        {
            DbSelectExpression save = this.currentSelect;

            this.currentSelect = proj.Select;
            try
            {
                if (!this.isTopLevel)
                {
                    if (this.CanJoinOnClient(this.currentSelect))
                    {
                        // make a query that combines all the constraints from the outer queries into a single select
                        DbSelectExpression newOuterSelect = (DbSelectExpression)QueryDuplicator.Duplicate(save);

                        // remap any references to the outer select to the new alias;
                        DbSelectExpression newInnerSelect = (DbSelectExpression)ColumnMapper.Map(proj.Select, newOuterSelect.Alias, save.Alias);
                        // add outer-join test
                        DbProjectionExpression newInnerProjection = new DbProjectionExpression(newInnerSelect, proj.Projector).AddOuterJoinTest();
                        newInnerSelect = newInnerProjection.Select;
                        Expression newProjector = newInnerProjection.Projector;

                        DbTableAlias newAlias = new DbTableAlias();
                        var          pc       = ColumnProjector.ProjectColumns(this.language.CanBeColumn, newProjector, newOuterSelect.Columns, newAlias, newOuterSelect.Alias, newInnerSelect.Alias);

                        DbJoinExpression   join         = new DbJoinExpression(DbJoinType.OuterApply, newOuterSelect, newInnerSelect, null);
                        DbSelectExpression joinedSelect = new DbSelectExpression(newAlias, pc.Columns, join, null, null, null, proj.IsSingleton, null, null);

                        // apply client-join treatment recursively
                        this.currentSelect = joinedSelect;
                        newProjector       = this.Visit(pc.Projector);

                        // compute keys (this only works if join condition was a single column comparison)
                        List <Expression> outerKeys = new List <Expression>();
                        List <Expression> innerKeys = new List <Expression>();
                        if (this.GetEquiJoinKeyExpressions(newInnerSelect.Where, newOuterSelect.Alias, outerKeys, innerKeys))
                        {
                            // outerKey needs to refer to the outer-scope's alias
                            var outerKey = outerKeys.Select(k => ColumnMapper.Map(k, save.Alias, newOuterSelect.Alias));
                            // innerKey needs to refer to the new alias for the select with the new join
                            var innerKey = innerKeys.Select(k => ColumnMapper.Map(k, joinedSelect.Alias, ((DbColumnExpression)k).Alias));
                            DbProjectionExpression newProjection = new DbProjectionExpression(joinedSelect, newProjector, proj.Aggregator);
                            return(new DbClientJoinExpression(newProjection, outerKey, innerKey));
                        }
                    }
                }
                else
                {
                    this.isTopLevel = false;
                }

                return(base.VisitProjection(proj));
            }
            finally
            {
                this.currentSelect = save;
            }
        }
        protected override Expression VisitProjection(DbProjectionExpression proj)
        {
            DbSelectExpression save = this.currentSelect;
            this.currentSelect = proj.Select;
            try
            {
                if (!this.isTopLevel)
                {
                    if (this.CanJoinOnClient(this.currentSelect))
                    {
                        // make a query that combines all the constraints from the outer queries into a single select
                        DbSelectExpression newOuterSelect = (DbSelectExpression)QueryDuplicator.Duplicate(save);

                        // remap any references to the outer select to the new alias;
                        DbSelectExpression newInnerSelect = (DbSelectExpression)ColumnMapper.Map(proj.Select, newOuterSelect.Alias, save.Alias);
                        // add outer-join test
                        DbProjectionExpression newInnerProjection = new DbProjectionExpression(newInnerSelect, proj.Projector).AddOuterJoinTest();
                        newInnerSelect = newInnerProjection.Select;
                        Expression newProjector = newInnerProjection.Projector;

                        DbTableAlias newAlias = new DbTableAlias();
                        var pc = ColumnProjector.ProjectColumns(this.language.CanBeColumn, newProjector, newOuterSelect.Columns, newAlias, newOuterSelect.Alias, newInnerSelect.Alias);

                        DbJoinExpression join = new DbJoinExpression(DbJoinType.OuterApply, newOuterSelect, newInnerSelect, null);
                        DbSelectExpression joinedSelect = new DbSelectExpression(newAlias, pc.Columns, join, null, null, null, proj.IsSingleton, null, null);

                        // apply client-join treatment recursively
                        this.currentSelect = joinedSelect;
                        newProjector = this.Visit(pc.Projector); 

                        // compute keys (this only works if join condition was a single column comparison)
                        List<Expression> outerKeys = new List<Expression>();
                        List<Expression> innerKeys = new List<Expression>();
                        if (this.GetEquiJoinKeyExpressions(newInnerSelect.Where, newOuterSelect.Alias, outerKeys, innerKeys))
                        {
                            // outerKey needs to refer to the outer-scope's alias
                            var outerKey = outerKeys.Select(k => ColumnMapper.Map(k, save.Alias, newOuterSelect.Alias));
                            // innerKey needs to refer to the new alias for the select with the new join
                            var innerKey = innerKeys.Select(k => ColumnMapper.Map(k, joinedSelect.Alias, ((DbColumnExpression)k).Alias));
                            DbProjectionExpression newProjection = new DbProjectionExpression(joinedSelect, newProjector, proj.Aggregator);
                            return new DbClientJoinExpression(newProjection, outerKey, innerKey);
                        }
                    }
                }
                else
                {
                    this.isTopLevel = false;
                }

                return base.VisitProjection(proj);
            }
            finally 
            {
                this.currentSelect = save;
            }
        }
예제 #9
0
        protected DbJoinExpression UpdateJoin(DbJoinExpression join, DbJoinType joinType,
                                              Expression left, Expression right, Expression condition)
        {
            if (joinType != join.Join || left != join.Left ||
                right != join.Right || condition != join.Condition)
            {
                return(new DbJoinExpression(joinType, left, right, condition));
            }

            return(join);
        }
        protected override Expression VisitJoin(DbJoinExpression join)
        {
            var saveLastJoin = this.lastJoin;
            this.lastJoin = join.Join;
            join = (DbJoinExpression)base.VisitJoin(join);
            this.lastJoin = saveLastJoin;

            if (this.lastJoin != null && (join.Join == DbJoinType.CrossJoin) != (this.lastJoin == DbJoinType.CrossJoin))
            {
                var result = this.MakeSubquery(join);
                return result;
            }
            return join;
        }
예제 #11
0
        protected override Expression VisitJoin(DbJoinExpression join)
        {
            var saveLastJoin = this.lastJoin;

            this.lastJoin = join.Join;
            join          = (DbJoinExpression)base.VisitJoin(join);
            this.lastJoin = saveLastJoin;

            if (this.lastJoin != null && (join.Join == DbJoinType.CrossJoin) != (this.lastJoin == DbJoinType.CrossJoin))
            {
                var result = this.MakeSubquery(join);
                return(result);
            }
            return(join);
        }
예제 #12
0
        protected virtual Expression BindSelectMany(Type resultType, Expression source, LambdaExpression collectionSelector, LambdaExpression resultSelector)
        {
            DbProjectionExpression projection = this.VisitSequence(source);

            this.map[collectionSelector.Parameters[0]] = projection.Projector;

            Expression collection = collectionSelector.Body;

            // check for DefaultIfEmpty
            bool defaultIfEmpty      = false;
            MethodCallExpression mcs = collection as MethodCallExpression;

            if (mcs != null && mcs.Method.Name == "DefaultIfEmpty" && mcs.Arguments.Count == 1 &&
                (mcs.Method.DeclaringType == typeof(Queryable) || mcs.Method.DeclaringType == typeof(Enumerable)))
            {
                collection     = mcs.Arguments[0];
                defaultIfEmpty = true;
            }

            DbProjectionExpression collectionProjection = (DbProjectionExpression)this.VisitSequence(collection);
            bool       isTable  = collectionProjection.Select.From is DbTableExpression;
            DbJoinType joinType = isTable ? DbJoinType.CrossJoin : defaultIfEmpty ? DbJoinType.OuterApply : DbJoinType.CrossApply;

            if (joinType == DbJoinType.OuterApply)
            {
                collectionProjection = collectionProjection.AddOuterJoinTest();
            }
            DbJoinExpression join = new DbJoinExpression(joinType, projection.Select, collectionProjection.Select, null);

            var alias = this.GetNextAlias();
            ProjectedColumns pc;

            if (resultSelector == null)
            {
                pc = this.ProjectColumns(collectionProjection.Projector, alias, projection.Select.Alias, collectionProjection.Select.Alias);
            }
            else
            {
                this.map[resultSelector.Parameters[0]] = projection.Projector;
                this.map[resultSelector.Parameters[1]] = collectionProjection.Projector;
                Expression result = this.Visit(resultSelector.Body);
                pc = this.ProjectColumns(result, alias, projection.Select.Alias, collectionProjection.Select.Alias);
            }
            return(new DbProjectionExpression(
                       new DbSelectExpression(alias, pc.Columns, join, null),
                       pc.Projector
                       ));
        }
예제 #13
0
            private static DbSelectExpression GetLeftMostSelect(Expression source)
            {
                DbSelectExpression select = source as DbSelectExpression;

                if (select != null)
                {
                    return(select);
                }
                DbJoinExpression join = source as DbJoinExpression;

                if (join != null)
                {
                    return(GetLeftMostSelect(join.Left));
                }
                return(null);
            }
        protected override Expression VisitJoin(DbJoinExpression join)
        {
            join = (DbJoinExpression)base.VisitJoin(join);

            if (join.Join == DbJoinType.CrossApply || join.Join == DbJoinType.OuterApply)
            {
                if (join.Right is DbTableExpression)
                {
                    return new DbJoinExpression(DbJoinType.CrossJoin, join.Left, join.Right, null);
                }
                else
                {
                    DbSelectExpression select = join.Right as DbSelectExpression;
                    // Only consider rewriting cross apply if 
                    //   1) right side is a select
                    //   2) other than in the where clause in the right-side select, no left-side declared aliases are referenced
                    //   3) and has no behavior that would change semantics if the where clause is removed (like groups, aggregates, take, skip, etc).
                    // Note: it is best to attempt this after redundant subqueries have been removed.
                    if (select != null
                        && select.Take == null
                        && select.Skip == null
                        && !AggregateChecker.HasAggregates(select)
                        && (select.GroupBy == null || select.GroupBy.Count == 0))
                    {
                        DbSelectExpression selectWithoutWhere = select.SetWhere(null);
                        HashSet<DbTableAlias> referencedAliases = ReferencedAliasGatherer.Gather(selectWithoutWhere);
                        HashSet<DbTableAlias> declaredAliases = DeclaredAliasGatherer.Gather(join.Left);
                        referencedAliases.IntersectWith(declaredAliases);
                        if (referencedAliases.Count == 0)
                        {
                            Expression where = select.Where;
                            select = selectWithoutWhere;
                            var pc = ColumnProjector.ProjectColumns(this.CanBeColumn, where, select.Columns, select.Alias, DeclaredAliasGatherer.Gather(select.From));
                            select = select.SetColumns(pc.Columns);
                            where = pc.Projector;
                            DbJoinType jt = (where == null) ? DbJoinType.CrossJoin : 
                                (join.Join == DbJoinType.CrossApply ? DbJoinType.InnerJoin : DbJoinType.LeftOuter);
                            return new DbJoinExpression(jt, join.Left, select, where);
                        }
                    }
                }
            }

            return join;
        }
예제 #15
0
        protected override Expression VisitJoin(DbJoinExpression join)
        {
            join = (DbJoinExpression)base.VisitJoin(join);

            if (join.Join == DbJoinType.CrossApply || join.Join == DbJoinType.OuterApply)
            {
                if (join.Right is DbTableExpression)
                {
                    return(new DbJoinExpression(DbJoinType.CrossJoin, join.Left, join.Right, null));
                }
                else
                {
                    DbSelectExpression select = join.Right as DbSelectExpression;
                    // Only consider rewriting cross apply if
                    //   1) right side is a select
                    //   2) other than in the where clause in the right-side select, no left-side declared aliases are referenced
                    //   3) and has no behavior that would change semantics if the where clause is removed (like groups, aggregates, take, skip, etc).
                    // Note: it is best to attempt this after redundant subqueries have been removed.
                    if (select != null &&
                        select.Take == null &&
                        select.Skip == null &&
                        !AggregateChecker.HasAggregates(select) &&
                        (select.GroupBy == null || select.GroupBy.Count == 0))
                    {
                        DbSelectExpression     selectWithoutWhere = select.SetWhere(null);
                        HashSet <DbTableAlias> referencedAliases  = ReferencedAliasGatherer.Gather(selectWithoutWhere);
                        HashSet <DbTableAlias> declaredAliases    = DeclaredAliasGatherer.Gather(join.Left);
                        referencedAliases.IntersectWith(declaredAliases);
                        if (referencedAliases.Count == 0)
                        {
                            Expression where = select.Where;
                            select           = selectWithoutWhere;
                            var pc = ColumnProjector.ProjectColumns(this.CanBeColumn, where, select.Columns, select.Alias, DeclaredAliasGatherer.Gather(select.From));
                            select = select.SetColumns(pc.Columns);
                            where  = pc.Projector;
                            DbJoinType jt = (where == null) ? DbJoinType.CrossJoin :
                                            (join.Join == DbJoinType.CrossApply ? DbJoinType.InnerJoin : DbJoinType.LeftOuter);
                            return(new DbJoinExpression(jt, join.Left, select, where));
                        }
                    }
                }
            }

            return(join);
        }
예제 #16
0
        protected override Expression VisitProjection(DbProjectionExpression proj)
        {
            if (isTopLevel)
            {
                isTopLevel         = false;
                this.currentSelect = proj.Select;
                Expression projector = this.Visit(proj.Projector);
                if (projector != proj.Projector || this.currentSelect != proj.Select)
                {
                    return(new DbProjectionExpression(this.currentSelect, projector, proj.Aggregator));
                }
                return(proj);
            }

            if (proj.IsSingleton && this.CanJoinOnServer(this.currentSelect))
            {
                DbTableAlias newAlias = new DbTableAlias();
                this.currentSelect = this.currentSelect.AddRedundantSelect(newAlias);

                // remap any references to the outer select to the new alias;
                DbSelectExpression source = (DbSelectExpression)ColumnMapper.Map(proj.Select, newAlias, this.currentSelect.Alias);

                // add outer-join test
                DbProjectionExpression pex = new DbProjectionExpression(source, proj.Projector).AddOuterJoinTest();

                var pc = ColumnProjector.ProjectColumns(this.language.CanBeColumn, pex.Projector, this.currentSelect.Columns,
                                                        this.currentSelect.Alias, newAlias, proj.Select.Alias);

                DbJoinExpression join = new DbJoinExpression(DbJoinType.OuterApply, this.currentSelect.From, pex.Select, null);

                this.currentSelect = new DbSelectExpression(this.currentSelect.Alias, pc.Columns, join, null);
                return(this.Visit(pc.Projector));
            }

            var saveTop    = this.isTopLevel;
            var saveSelect = this.currentSelect;

            this.isTopLevel    = true;
            this.currentSelect = null;
            Expression result = base.VisitProjection(proj);

            this.isTopLevel    = saveTop;
            this.currentSelect = saveSelect;
            return(result);
        }
 protected override Expression VisitJoin(DbJoinExpression join)
 {
     Expression result = base.VisitJoin(join);
     join = result as DbJoinExpression;
     if (join != null)
     {
         DbAliasedExpression right = join.Right as DbAliasedExpression;
         if (right != null)
         {
             DbAliasedExpression similarRight = (DbAliasedExpression)this.FindSimilarRight(join.Left as DbJoinExpression, join);
             if (similarRight != null)
             {
                 this.map.Add(right.Alias, similarRight.Alias);
                 return join.Left;
             }
         }
     }
     return result;
 }
예제 #18
0
        protected override Expression VisitJoin(DbJoinExpression join)
        {
            // make sure order by expressions lifted up from the left side are not lost
            // when visiting the right side
            Expression left = this.VisitSource(join.Left);
            IList <DbOrderExpression> leftOrders = this.gatheredOrderings;

            this.gatheredOrderings = null; // start on the right with a clean slate
            Expression right = this.VisitSource(join.Right);

            this.PrependOrderings(leftOrders);
            Expression condition = this.Visit(join.Condition);

            if (left != join.Left || right != join.Right || condition != join.Condition)
            {
                return(new DbJoinExpression(join.Join, left, right, condition));
            }
            return(join);
        }
        protected override Expression VisitProjection(DbProjectionExpression proj)
        {
            if (isTopLevel)
            {
                isTopLevel = false;
                this.currentSelect = proj.Select;
                Expression projector = this.Visit(proj.Projector);
                if (projector != proj.Projector || this.currentSelect != proj.Select)
                {
                    return new DbProjectionExpression(this.currentSelect, projector, proj.Aggregator);
                }
                return proj;
            }

            if (proj.IsSingleton && this.CanJoinOnServer(this.currentSelect))
            {
                DbTableAlias newAlias = new DbTableAlias();
                this.currentSelect = this.currentSelect.AddRedundantSelect(newAlias);

                // remap any references to the outer select to the new alias;
                DbSelectExpression source = (DbSelectExpression)ColumnMapper.Map(proj.Select, newAlias, this.currentSelect.Alias);

                // add outer-join test
                DbProjectionExpression pex = new DbProjectionExpression(source, proj.Projector).AddOuterJoinTest();

                var pc = ColumnProjector.ProjectColumns(this.language.CanBeColumn, pex.Projector, this.currentSelect.Columns, 
                    this.currentSelect.Alias, newAlias, proj.Select.Alias);

                DbJoinExpression join = new DbJoinExpression(DbJoinType.OuterApply, this.currentSelect.From, pex.Select, null);

                this.currentSelect = new DbSelectExpression(this.currentSelect.Alias, pc.Columns, join, null);
                return this.Visit(pc.Projector);
            }

            var saveTop = this.isTopLevel;
            var saveSelect = this.currentSelect;
            this.isTopLevel = true;
            this.currentSelect = null;
            Expression result = base.VisitProjection(proj);
            this.isTopLevel = saveTop;
            this.currentSelect = saveSelect;
            return result;
        }
        protected override Expression VisitJoin(DbJoinExpression join)
        {
            Expression result = base.VisitJoin(join);

            join = result as DbJoinExpression;
            if (join != null)
            {
                DbAliasedExpression right = join.Right as DbAliasedExpression;
                if (right != null)
                {
                    DbAliasedExpression similarRight = (DbAliasedExpression)this.FindSimilarRight(join.Left as DbJoinExpression, join);
                    if (similarRight != null)
                    {
                        this.map.Add(right.Alias, similarRight.Alias);
                        return(join.Left);
                    }
                }
            }
            return(result);
        }
예제 #21
0
 protected override Expression VisitJoin(DbJoinExpression join)
 {
     join = (DbJoinExpression)base.VisitJoin(join);
     if (join.Join == DbJoinType.CrossJoin && this.currentWhere != null)
     {
         // try to figure out which parts of the current where expression can be used for a join condition
         var declaredLeft  = DeclaredAliasGatherer.Gather(join.Left);
         var declaredRight = DeclaredAliasGatherer.Gather(join.Right);
         var declared      = new HashSet <DbTableAlias>(declaredLeft.Union(declaredRight));
         var exprs         = this.currentWhere.Split(ExpressionType.And, ExpressionType.AndAlso);
         var good          = exprs.Where(e => CanBeJoinCondition(e, declaredLeft, declaredRight, declared)).ToList();
         if (good.Count > 0)
         {
             var condition = good.Join(ExpressionType.And);
             join = this.UpdateJoin(join, DbJoinType.InnerJoin, join.Left, join.Right, condition);
             var newWhere = exprs.Where(e => !good.Contains(e)).Join(ExpressionType.And);
             this.currentWhere = newWhere;
         }
     }
     return(join);
 }
 protected override Expression VisitJoin(DbJoinExpression join)
 {
     join = (DbJoinExpression)base.VisitJoin(join);
     if (join.Join == DbJoinType.CrossJoin && this.currentWhere != null)
     {
         // try to figure out which parts of the current where expression can be used for a join condition
         var declaredLeft = DeclaredAliasGatherer.Gather(join.Left);
         var declaredRight = DeclaredAliasGatherer.Gather(join.Right);
         var declared = new HashSet<DbTableAlias>(declaredLeft.Union(declaredRight));
         var exprs = this.currentWhere.Split(ExpressionType.And, ExpressionType.AndAlso);
         var good = exprs.Where(e => CanBeJoinCondition(e, declaredLeft, declaredRight, declared)).ToList();
         if (good.Count > 0)
         {
             var condition = good.Join(ExpressionType.And);
             join = this.UpdateJoin(join, DbJoinType.InnerJoin, join.Left, join.Right, condition);
             var newWhere = exprs.Where(e => !good.Contains(e)).Join(ExpressionType.And);
             this.currentWhere = newWhere;
         }
     }
     return join;
 }
예제 #23
0
        protected virtual Expression BindJoin(Type resultType, Expression outerSource, Expression innerSource, LambdaExpression outerKey, LambdaExpression innerKey, LambdaExpression resultSelector)
        {
            DbProjectionExpression outerProjection = this.VisitSequence(outerSource);
            DbProjectionExpression innerProjection = this.VisitSequence(innerSource);

            this.map[outerKey.Parameters[0]] = outerProjection.Projector;
            Expression outerKeyExpr = this.Visit(outerKey.Body);

            this.map[innerKey.Parameters[0]] = innerProjection.Projector;
            Expression innerKeyExpr = this.Visit(innerKey.Body);

            this.map[resultSelector.Parameters[0]] = outerProjection.Projector;
            this.map[resultSelector.Parameters[1]] = innerProjection.Projector;
            Expression       resultExpr = this.Visit(resultSelector.Body);
            DbJoinExpression join       = new DbJoinExpression(DbJoinType.InnerJoin, outerProjection.Select, innerProjection.Select, outerKeyExpr.Equal(innerKeyExpr));
            var alias           = this.GetNextAlias();
            ProjectedColumns pc = this.ProjectColumns(resultExpr, alias, outerProjection.Select.Alias, innerProjection.Select.Alias);

            return(new DbProjectionExpression(
                       new DbSelectExpression(alias, pc.Columns, join, null),
                       pc.Projector
                       ));
        }
 private Expression FindSimilarRight(DbJoinExpression join, DbJoinExpression compareTo)
 {
     if (join == null)
         return null;
     if (join.Join == compareTo.Join)
     {
         if (join.Right.NodeType == compareTo.Right.NodeType
             && DbExpressionComparer.AreEqual(join.Right, compareTo.Right))
         {
             if (join.Condition == compareTo.Condition)
                 return join.Right;
             var scope = new ScopedDictionary<DbTableAlias, DbTableAlias>(null);
             scope.Add(((DbAliasedExpression)join.Right).Alias, ((DbAliasedExpression)compareTo.Right).Alias);
             if (DbExpressionComparer.AreEqual(null, scope, join.Condition, compareTo.Condition))
                 return join.Right;
         }
     }
     Expression result = FindSimilarRight(join.Left as DbJoinExpression, compareTo);
     if (result == null)
     {
         result = FindSimilarRight(join.Right as DbJoinExpression, compareTo);
     }
     return result;
 }
 protected override Expression VisitJoin(DbJoinExpression join)
 {
     if (join.Join == DbJoinType.SingletonLeftOuter)
     {
         // first visit right side w/o looking at condition
         Expression right = this.Visit(join.Right);
         DbAliasedExpression ax = right as DbAliasedExpression;
         if (ax != null && !this.allColumnsUsed.ContainsKey(ax.Alias))
         {
             // if nothing references the alias on the right, then the join is redundant
             return this.Visit(join.Left);
         }
         // otherwise do it the right way
         Expression cond = this.Visit(join.Condition);
         Expression left = this.Visit(join.Left);
         right = this.Visit(join.Right);
         return this.UpdateJoin(join, join.Join, left, right, cond);
     }
     else
     {
         // visit join in reverse order
         Expression condition = this.Visit(join.Condition);
         Expression right = this.VisitSource(join.Right);
         Expression left = this.VisitSource(join.Left);
         return this.UpdateJoin(join, join.Join, left, right, condition);
     }
 }
예제 #26
0
        protected virtual Expression BindSelectMany(Type resultType, Expression source, LambdaExpression collectionSelector, LambdaExpression resultSelector)
        {
            DbProjectionExpression projection = this.VisitSequence(source);
            this.map[collectionSelector.Parameters[0]] = projection.Projector;

            Expression collection = collectionSelector.Body;

            // check for DefaultIfEmpty
            bool defaultIfEmpty = false;
            MethodCallExpression mcs = collection as MethodCallExpression;
            if (mcs != null && mcs.Method.Name == "DefaultIfEmpty" && mcs.Arguments.Count == 1 &&
                (mcs.Method.DeclaringType == typeof(Queryable) || mcs.Method.DeclaringType == typeof(Enumerable)))
            {
                collection = mcs.Arguments[0];
                defaultIfEmpty = true;
            }

            DbProjectionExpression collectionProjection = (DbProjectionExpression)this.VisitSequence(collection);
            bool isTable = collectionProjection.Select.From is DbTableExpression;
            DbJoinType joinType = isTable ? DbJoinType.CrossJoin : defaultIfEmpty ? DbJoinType.OuterApply : DbJoinType.CrossApply;
            if (joinType == DbJoinType.OuterApply)
            {
                collectionProjection = collectionProjection.AddOuterJoinTest();
            }
            DbJoinExpression join = new DbJoinExpression(joinType, projection.Select, collectionProjection.Select, null);

            var alias = this.GetNextAlias();
            ProjectedColumns pc;
            if (resultSelector == null)
            {
                pc = this.ProjectColumns(collectionProjection.Projector, alias, projection.Select.Alias, collectionProjection.Select.Alias);
            }
            else
            {
                this.map[resultSelector.Parameters[0]] = projection.Projector;
                this.map[resultSelector.Parameters[1]] = collectionProjection.Projector;
                Expression result = this.Visit(resultSelector.Body);
                pc = this.ProjectColumns(result, alias, projection.Select.Alias, collectionProjection.Select.Alias);
            }
            return new DbProjectionExpression(
                new DbSelectExpression(alias, pc.Columns, join, null),
                pc.Projector
                );
        }
 protected DbJoinExpression UpdateJoin(DbJoinExpression join, DbJoinType joinType, 
     Expression left, Expression right, Expression condition)
 {
     if (joinType != join.Join || left != join.Left || 
         right != join.Right || condition != join.Condition)
         return new DbJoinExpression(joinType, left, right, condition);
     
     return join;
 }
        protected virtual Expression VisitJoin(DbJoinExpression join)
        {
            Expression left = VisitSource(join.Left);
            Expression right = VisitSource(join.Right);
            Expression condition = Visit(join.Condition);

            return UpdateJoin(join, join.Join, left, right, condition);
        }
예제 #29
0
 protected virtual Expression BindJoin(Type resultType, Expression outerSource, Expression innerSource, LambdaExpression outerKey, LambdaExpression innerKey, LambdaExpression resultSelector)
 {
     DbProjectionExpression outerProjection = this.VisitSequence(outerSource);
     DbProjectionExpression innerProjection = this.VisitSequence(innerSource);
     this.map[outerKey.Parameters[0]] = outerProjection.Projector;
     Expression outerKeyExpr = this.Visit(outerKey.Body);
     this.map[innerKey.Parameters[0]] = innerProjection.Projector;
     Expression innerKeyExpr = this.Visit(innerKey.Body);
     this.map[resultSelector.Parameters[0]] = outerProjection.Projector;
     this.map[resultSelector.Parameters[1]] = innerProjection.Projector;
     Expression resultExpr = this.Visit(resultSelector.Body);
     DbJoinExpression join = new DbJoinExpression(DbJoinType.InnerJoin, outerProjection.Select, innerProjection.Select, outerKeyExpr.Equal(innerKeyExpr));
     var alias = this.GetNextAlias();
     ProjectedColumns pc = this.ProjectColumns(resultExpr, alias, outerProjection.Select.Alias, innerProjection.Select.Alias);
     return new DbProjectionExpression(
         new DbSelectExpression(alias, pc.Columns, join, null),
         pc.Projector
         );
 }
        protected virtual bool CompareJoin(DbJoinExpression a, DbJoinExpression b)
        {
            if (a.Join != b.Join || !Compare(a.Left, b.Left))
                return false;

            if (a.Join == DbJoinType.CrossApply || a.Join == DbJoinType.OuterApply)
            {
                ScopedDictionary<DbTableAlias, DbTableAlias> save = _aliasScope;
                try
                {
                    _aliasScope = new ScopedDictionary<DbTableAlias, DbTableAlias>(_aliasScope);
                    MapAliases(a.Left, b.Left);

                    return Compare(a.Right, b.Right)
                        && Compare(a.Condition, b.Condition);
                }
                finally
                {
                    _aliasScope = save;
                }
            }
            else
            {
                return Compare(a.Right, b.Right)
                    && Compare(a.Condition, b.Condition);
            }
        }
예제 #31
0
 protected override Expression VisitJoin(DbJoinExpression join)
 {
     // make sure order by expressions lifted up from the left side are not lost
     // when visiting the right side
     Expression left = this.VisitSource(join.Left);
     IList<DbOrderExpression> leftOrders = this.gatheredOrderings;
     this.gatheredOrderings = null; // start on the right with a clean slate
     Expression right = this.VisitSource(join.Right);
     this.PrependOrderings(leftOrders);
     Expression condition = this.Visit(join.Condition);
     if (left != join.Left || right != join.Right || condition != join.Condition)
     {
         return new DbJoinExpression(join.Join, left, right, condition);
     }
     return join;
 }
예제 #32
0
 protected override Expression VisitJoin(DbJoinExpression join)
 {
     this.VisitJoinLeft(join.Left);
     this.WriteLine(Indentation.Same);
     switch (join.Join)
     {
         case DbJoinType.CrossJoin:
             this.Write("CROSS JOIN ");
             break;
         case DbJoinType.InnerJoin:
             this.Write("INNER JOIN ");
             break;
         case DbJoinType.CrossApply:
             this.Write("CROSS APPLY ");
             break;
         case DbJoinType.OuterApply:
             this.Write("OUTER APPLY ");
             break;
         case DbJoinType.LeftOuter:
         case DbJoinType.SingletonLeftOuter:
             this.Write("LEFT OUTER JOIN ");
             break;
     }
     this.VisitJoinRight(join.Right);
     if (join.Condition != null)
     {
         this.WriteLine(Indentation.Inner);
         this.Write("ON ");
         this.VisitPredicate(join.Condition);
         this.Indent(Indentation.Outer);
     }
     return join;
 }