示例#1
0
        protected override Expression VisitMemberAccess(MemberExpression m)
        {
            Expression source = this.Visit(m.Expression);
            EntityExpression ex = source as EntityExpression;

            if (ex != null && this.mapping.IsRelationship(ex.Entity, m.Member))
            {
                ProjectionExpression projection = (ProjectionExpression)this.Visit(this.mapper.GetMemberExpression(source, ex.Entity, m.Member));
                if (this.currentFrom != null && this.mapping.IsSingletonRelationship(ex.Entity, m.Member))
                {
                    // convert singleton associations directly to OUTER APPLY
                    projection = this.language.AddOuterJoinTest(projection);
                    Expression newFrom = new JoinExpression(JoinType.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;
                DynamicExpression dex = result as DynamicExpression;

                if ((mex != null && mex.Member == m.Member && mex.Expression == m.Expression)
                    || (dex != null
                    && ((GetMemberBinder)dex.Binder).Name == m.Member.Name)
                    && (dex.Arguments[0] == source || dex.Arguments[0] == ((UnaryExpression)source).Operand))
                {
                    return m;
                }
                return result;
            }
        }
示例#2
0
 protected override Expression VisitJoin(JoinExpression join)
 {
     if (join.Join == JoinType.SingletonLeftOuter)
     {
         // first visit right side w/o looking at condition
         Expression right = this.Visit(join.Right);
         AliasedExpression ax = right as AliasedExpression;
         if (ax != null && !this.allFieldsUsed.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);
     }
 }
示例#3
0
        protected override Expression VisitJoin(JoinExpression join)
        {
            join = (JoinExpression)base.VisitJoin(join);

            if (join.Join == JoinType.CrossApply || join.Join == JoinType.OuterApply)
            {
                if (join.Right is IdentifiableExpression)
                {
                    return new JoinExpression(JoinType.CrossJoin, join.Left, join.Right, null);
                }
                else
                {
                    SelectExpression select = join.Right as SelectExpression;
                    // 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))
                    {
                        SelectExpression selectWithoutWhere = select.SetWhere(null);
                        HashSet<IdentifiableAlias> referencedAliases = ReferencedAliasGatherer.Gather(selectWithoutWhere);
                        HashSet<IdentifiableAlias> declaredAliases = DeclaredAliasGatherer.Gather(join.Left);
                        referencedAliases.IntersectWith(declaredAliases);
                        if (referencedAliases.Count == 0)
                        {
                            Expression where = select.Where;
                            select = selectWithoutWhere;
                            var pc = FieldProjector.ProjectFields(this.language, where, select.Fields, select.Alias, DeclaredAliasGatherer.Gather(select.From));
                            select = select.SetFields(pc.Fields);
                            where = pc.Projector;
                            JoinType jt = (where == null) ? JoinType.CrossJoin : (join.Join == JoinType.CrossApply ? JoinType.InnerJoin : JoinType.LeftOuter);
                            return new JoinExpression(jt, join.Left, select, where);
                        }
                    }
                }
            }

            return join;
        }
示例#4
0
 protected override Expression VisitJoin(JoinExpression join)
 {
     Expression result = base.VisitJoin(join);
     join = result as JoinExpression;
     if (join != null)
     {
         AliasedExpression right = join.Right as AliasedExpression;
         if (right != null)
         {
             AliasedExpression similarRight = (AliasedExpression)this.FindSimilarRight(join.Left as JoinExpression, join);
             if (similarRight != null)
             {
                 this.map.Add(right.Alias, similarRight.Alias);
                 return join.Left;
             }
         }
     }
     return result;
 }
示例#5
0
 protected override Expression VisitJoin(JoinExpression join)
 {
     join = (JoinExpression)base.VisitJoin(join);
     if (join.Join == JoinType.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<IdentifiableAlias>(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, JoinType.InnerJoin, join.Left, join.Right, condition);
             var newWhere = exprs.Where(e => !good.Contains(e)).Join(ExpressionType.And);
             this.currentWhere = newWhere;
         }
     }
     return join;
 }
示例#6
0
 private Expression FindSimilarRight(JoinExpression join, JoinExpression 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<IdentifiableAlias, IdentifiableAlias>(null);
             scope.Add(((AliasedExpression)join.Right).Alias, ((AliasedExpression)compareTo.Right).Alias);
             if (DbExpressionComparer.AreEqual(null, scope, join.Condition, compareTo.Condition))
                 return join.Right;
         }
     }
     Expression result = FindSimilarRight(join.Left as JoinExpression, compareTo);
     if (result == null)
     {
         result = FindSimilarRight(join.Right as JoinExpression, compareTo);
     }
     return result;
 }
示例#7
0
 protected virtual Expression VisitJoin(JoinExpression join)
 {
     var left = this.VisitSource(join.Left);
     var right = this.VisitSource(join.Right);
     var condition = this.Visit(join.Condition);
     return this.UpdateJoin(join, join.Join, left, right, condition);
 }
示例#8
0
 protected JoinExpression UpdateJoin(JoinExpression join, JoinType joinType, Expression left, Expression right, Expression condition)
 {
     if (joinType != join.Join || left != join.Left || right != join.Right || condition != join.Condition)
     {
         return new JoinExpression(joinType, left, right, condition);
     }
     return join;
 }
        protected override Expression VisitProjection(ProjectionExpression proj)
        {
            SelectExpression 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
                        SelectExpression newOuterSelect = (SelectExpression)QueryDuplicator.Duplicate(save);

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

                        IdentifiableAlias newAlias = new IdentifiableAlias();
                        var pc = FieldProjector.ProjectFields(this.language, newProjector, null, newAlias, newOuterSelect.Alias, newInnerSelect.Alias);

                        JoinExpression join = new JoinExpression(JoinType.OuterApply, newOuterSelect, newInnerSelect, null);
                        SelectExpression joinedSelect = new SelectExpression(newAlias, pc.Fields, join, null, null, null, proj.IsSingleton, null, null, false);

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

                        // compute keys (this only works if join condition was a single field 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 => FieldMapper.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 => FieldMapper.Map(k, joinedSelect.Alias, ((FieldExpression)k).Alias));
                            ProjectionExpression newProjection = new ProjectionExpression(joinedSelect, newProjector, proj.Aggregator);
                            return new ClientJoinExpression(newProjection, outerKey, innerKey);
                        }
                    }
                    else
                    {
                        bool saveJoin = this.canJoinOnClient;
                        this.canJoinOnClient = false;
                        var result = base.VisitProjection(proj);
                        this.canJoinOnClient = saveJoin;
                        return result;
                    }
                }
                else
                {
                    this.isTopLevel = false;
                }
                return base.VisitProjection(proj);
            }
            finally
            {
                this.currentSelect = save;
            }
        }
        protected override Expression VisitProjection(ProjectionExpression 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 ProjectionExpression(this.currentSelect, projector, proj.Aggregator);
                }
                return proj;
            }

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

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

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

                var pc = FieldProjector.ProjectFields(this.language, pex.Projector, this.currentSelect.Fields, this.currentSelect.Alias, newAlias, proj.Select.Alias);

                JoinExpression join = new JoinExpression(JoinType.OuterApply, this.currentSelect.From, pex.Select, null);

                this.currentSelect = new SelectExpression(this.currentSelect.Alias, pc.Fields, 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;
        }
示例#11
0
 protected override Expression VisitJoin(JoinExpression join)
 {
     this.VisitJoinLeft(join.Left);
     this.WriteLine(Indentation.Same);
     switch (join.Join)
     {
         case JoinType.CrossJoin:
             this.Write("CROSS JOIN ");
             break;
         case JoinType.InnerJoin:
             this.Write("INNER JOIN ");
             break;
         case JoinType.CrossApply:
             this.Write("CROSS APPLY ");
             break;
         case JoinType.OuterApply:
             this.Write("OUTER APPLY ");
             break;
         case JoinType.LeftOuter:
         case JoinType.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;
 }
示例#12
0
        protected virtual bool CompareJoin(JoinExpression a, JoinExpression b)
        {
            if (a.Join != b.Join || !this.Compare(a.Left, b.Left))
                return false;

            if (a.Join == JoinType.CrossApply || a.Join == JoinType.OuterApply)
            {
                var save = this.aliasScope;
                try
                {
                    this.aliasScope = new ScopedDictionary<IdentifiableAlias, IdentifiableAlias>(this.aliasScope);
                    this.MapAliases(a.Left, b.Left);

                    return this.Compare(a.Right, b.Right)
                        && this.Compare(a.Condition, b.Condition);
                }
                finally
                {
                    this.aliasScope = save;
                }
            }
            else
            {
                return this.Compare(a.Right, b.Right)
                    && this.Compare(a.Condition, b.Condition);
            }
        }
示例#13
0
        protected virtual Expression BindSelectMany(Type resultType, Expression source, LambdaExpression collectionSelector, LambdaExpression resultSelector)
        {
            ProjectionExpression 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;
            }

            ProjectionExpression collectionProjection = (ProjectionExpression)this.VisitSequence(collection);
            bool isTable = collectionProjection.Select.From is IdentifiableExpression;
            JoinType joinType = isTable ? JoinType.CrossJoin : defaultIfEmpty ? JoinType.OuterApply : JoinType.CrossApply;
            if (joinType == JoinType.OuterApply)
            {
                collectionProjection = this.language.AddOuterJoinTest(collectionProjection);
            }
            JoinExpression join = new JoinExpression(joinType, projection.Select, collectionProjection.Select, null);

            var alias = this.GetNextAlias();
            ProjectedFields pc;
            if (resultSelector == null)
            {
                pc = this.ProjectFields(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.ProjectFields(result, alias, projection.Select.Alias, collectionProjection.Select.Alias);
            }
            return new ProjectionExpression(
                new SelectExpression(alias, pc.Fields, join, null),
                pc.Projector
                );
        }
示例#14
0
 protected virtual Expression BindJoin(Type resultType, Expression outerSource, Expression innerSource, LambdaExpression outerKey, LambdaExpression innerKey, LambdaExpression resultSelector)
 {
     ProjectionExpression outerProjection = this.VisitSequence(outerSource);
     ProjectionExpression 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);
     JoinExpression join = new JoinExpression(JoinType.InnerJoin, outerProjection.Select, innerProjection.Select, outerKeyExpr.Equal(innerKeyExpr));
     var alias = this.GetNextAlias();
     ProjectedFields pc = this.ProjectFields(resultExpr, alias, outerProjection.Select.Alias, innerProjection.Select.Alias);
     return new ProjectionExpression(
         new SelectExpression(alias, pc.Fields, join, null),
         pc.Projector
         );
 }