Gather() public static method

public static Gather ( Expression source ) : HashSet
source System.Linq.Expressions.Expression
return HashSet
Example #1
0
 private void MapAliases(Expression a, Expression b)
 {
     TableAlias[] prodA = TableAliasGatherer.Gather(a).ToArray();
     TableAlias[] prodB = TableAliasGatherer.Gather(b).ToArray();
     for (int i = 0, n = prodA.Length; i < n; i++)
     {
         this.aliasScope.Add(prodA[i], prodB[i]);
     }
 }
Example #2
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 TableExpression)
                {
                    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 <TableAlias> referencedAliases  = ReferencedAliasGatherer.Gather(selectWithoutWhere);
                        HashSet <TableAlias> declaredAliases    = TableAliasGatherer.Gather(join.Left);
                        referencedAliases.IntersectWith(declaredAliases);
                        if (referencedAliases.Count == 0)
                        {
                            Expression where = select.Where;
                            select           = selectWithoutWhere;
                            var pc = ColumnProjector.ProjectColumns(where, select.Columns, select.Alias, TableAliasGatherer.Gather(select.From));
                            select = select.SetColumns(pc.Columns);
                            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);
        }
Example #3
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  = TableAliasGatherer.Gather(join.Left);
         var declaredRight = TableAliasGatherer.Gather(join.Right);
         var declared      = new HashSet <TableAlias>(declaredLeft.Union(declaredRight), TableAlias.Comparer);
         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);
 }
Example #4
0
        private Expression MakeSubquery(Expression expression)
        {
            var newAlias = new TableAlias();
            var aliases  = TableAliasGatherer.Gather(expression);

            var decls = new List <ColumnDeclaration>();

            foreach (var ta in aliases)
            {
                foreach (var col in this.columns[ta])
                {
                    string name = decls.GetAvailableColumnName(col.Name);
                    var    decl = new ColumnDeclaration(name, col, col.SqlType);
                    decls.Add(decl);
                    var newCol = new ColumnExpression(col.Type, col.SqlType, newAlias, name);
                    this.map.Add(col, newCol);
                }
            }

            return(new SelectExpression(newAlias, decls, expression, null));
        }
Example #5
0
        protected override Expression VisitSelect(SelectExpression select)
        {
            bool saveIsOuterMostSelect = this.isOuterMostSelect;

            try
            {
                this.isOuterMostSelect = false;

                var saveSuppressOrderBy = this.suppressOrderby;
                this.suppressOrderby = false;

                var saveGatheredOrderings = this.gatheredOrderings;
                if (saveSuppressOrderBy)
                {
                    this.gatheredOrderings = null;
                    this.isOuterMostSelect = true;
                }

                select = (SelectExpression)base.VisitSelect(select);

                this.suppressOrderby = saveSuppressOrderBy;
                if (saveSuppressOrderBy)
                {
                    this.gatheredOrderings = saveGatheredOrderings;
                }

                bool hasOrderBy          = select.OrderBy != null && select.OrderBy.Count > 0;
                bool hasGroupBy          = select.GroupBy != null && select.GroupBy.Count > 0;
                bool canHaveOrderBy      = saveIsOuterMostSelect || select.Take != null || select.Skip != null;
                bool canReceiveOrderings = canHaveOrderBy && !hasGroupBy && !select.IsDistinct && !AggregateChecker.HasAggregates(select) && !suppressOrderby;

                if (hasOrderBy)
                {
                    this.PrependOrderings(select.OrderBy);
                }

                if (select.IsReverse)
                {
                    if (this.gatheredOrderings == null || gatheredOrderings.Count == 0)
                    {
                        throw new NotSupportedException(string.Format(Res.OperationNotSupported, "", "Reverse", "if no 'OrderBy' expression."));
                    }

                    for (int i = 0, n = this.gatheredOrderings.Count; i < n; i++)
                    {
                        var ord = this.gatheredOrderings[i];
                        this.gatheredOrderings[i] =
                            new OrderExpression(
                                ord.OrderType == OrderType.Ascending ? OrderType.Descending : OrderType.Ascending,
                                ord.Expression
                                );
                    }
                }

                IEnumerable <OrderExpression> orderings = null;
                if (canReceiveOrderings)
                {
                    orderings = this.gatheredOrderings;
                }
                else if (canHaveOrderBy)
                {
                    orderings = select.OrderBy;
                }

                bool canPassOnOrderings = !saveIsOuterMostSelect && !hasGroupBy && !select.IsDistinct && !suppressOrderby;
                ReadOnlyCollection <ColumnDeclaration> columns = select.Columns;
                if (this.gatheredOrderings != null)
                {
                    if (canPassOnOrderings)
                    {
                        var producedAliases = TableAliasGatherer.Gather(select.From);
                        // reproject order expressions using this select's alias so the outer select will have properly formed expressions
                        BindResult project = this.RebindOrderings(this.gatheredOrderings, select.Alias, producedAliases, select.Columns);
                        this.gatheredOrderings = null;
                        this.PrependOrderings(project.Orderings);
                        columns = project.Columns;
                    }
                    else
                    {
                        this.gatheredOrderings = null;
                    }
                }

                if (orderings != select.OrderBy || columns != select.Columns || select.IsReverse)
                {
                    select = new SelectExpression(select.Alias, columns, select.From, select.Where, orderings, select.GroupBy, select.IsDistinct, select.Skip, select.Take, false);
                }

                return(select);
            }
            finally
            {
                this.isOuterMostSelect = saveIsOuterMostSelect;
            }
        }