Пример #1
0
 public static SelectExpression Remove(SelectExpression outerSelect, IEnumerable <SelectExpression> selectsToRemove)
 {
     return((SelectExpression) new SubqueryRemover(selectsToRemove).Visit(outerSelect));
 }
Пример #2
0
            public static HashSet <ColumnExpression> Gather(HashSet <TableAlias> aliases, SelectExpression select)
            {
                var gatherer = new JoinColumnGatherer(aliases);

                gatherer.Gather(select.Where);
                return(gatherer.columns);
            }
Пример #3
0
 public static SelectExpression Remove(SelectExpression outerSelect, params SelectExpression[] selectsToRemove)
 {
     return(Remove(outerSelect, (IEnumerable <SelectExpression>)selectsToRemove));
 }
Пример #4
0
        protected override Expression VisitSelect(SelectExpression select)
        {
            // visit column projection first
            ReadOnlyCollection <ColumnDeclaration> columns = select.Columns;

            var wasRetained = this.retainAllColumns;

            this.retainAllColumns = false;

            List <ColumnDeclaration> alternate = null;

            for (int i = 0, n = select.Columns.Count; i < n; i++)
            {
                ColumnDeclaration decl = select.Columns[i];
                if (wasRetained || select.IsDistinct || IsColumnUsed(select.Alias, decl.Name))
                {
                    Expression expr = this.Visit(decl.Expression);
                    if (expr != decl.Expression)
                    {
                        decl = new ColumnDeclaration(decl.Name, expr, decl.QueryType);
                    }
                }
                else
                {
                    decl = null;  // null means it gets omitted
                }
                if (decl != select.Columns[i] && alternate == null)
                {
                    alternate = new List <ColumnDeclaration>();
                    for (int j = 0; j < i; j++)
                    {
                        alternate.Add(select.Columns[j]);
                    }
                }
                if (decl != null && alternate != null)
                {
                    alternate.Add(decl);
                }
            }
            if (alternate != null)
            {
                columns = alternate.AsReadOnly();
            }

            Expression take = this.Visit(select.Take);
            Expression skip = this.Visit(select.Skip);
            ReadOnlyCollection <Expression>      groupbys = this.VisitExpressionList(select.GroupBy);
            ReadOnlyCollection <OrderExpression> orderbys = this.VisitOrderBy(select.OrderBy);

            Expression where = this.Visit(select.Where);

            Expression from = this.Visit(select.From);

            ClearColumnsUsed(select.Alias);

            if (columns != select.Columns ||
                take != select.Take ||
                skip != select.Skip ||
                orderbys != select.OrderBy ||
                groupbys != select.GroupBy ||
                where != select.Where ||
                from != select.From)
            {
                select = new SelectExpression(select.Alias, columns, from, where, orderbys, groupbys, select.IsDistinct, skip, take, select.IsReverse);
            }

            this.retainAllColumns = wasRetained;

            return(select);
        }
Пример #5
0
            private static bool CanMergeWithFrom(SelectExpression select, bool isTopLevel)
            {
                SelectExpression fromSelect = GetLeftMostSelect(select.From);

                if (fromSelect == null)
                {
                    return(false);
                }
                if (!IsColumnProjection(fromSelect))
                {
                    return(false);
                }
                bool selHasNameMapProjection = IsNameMapProjection(select);
                bool selHasOrderBy           = select.OrderBy != null && select.OrderBy.Count > 0;
                bool selHasGroupBy           = select.GroupBy != null && select.GroupBy.Count > 0;
                bool selHasAggregates        = AggregateChecker.HasAggregates(select);
                bool selHasJoin       = select.From is JoinExpression;
                bool frmHasOrderBy    = fromSelect.OrderBy != null && fromSelect.OrderBy.Count > 0;
                bool frmHasGroupBy    = fromSelect.GroupBy != null && fromSelect.GroupBy.Count > 0;
                bool frmHasAggregates = AggregateChecker.HasAggregates(fromSelect);

                // both cannot have orderby
                if (selHasOrderBy && frmHasOrderBy)
                {
                    return(false);
                }
                // both cannot have groupby
                if (selHasGroupBy && frmHasGroupBy)
                {
                    return(false);
                }
                // these are distinct operations
                if (select.IsReverse || fromSelect.IsReverse)
                {
                    return(false);
                }
                // cannot move forward order-by if outer has group-by
                if (frmHasOrderBy && (selHasGroupBy || selHasAggregates || select.IsDistinct))
                {
                    return(false);
                }
                // cannot move forward group-by if outer has where clause
                if (frmHasGroupBy /*&& (select.Where != null)*/) // need to assert projection is the same in order to move group-by forward
                {
                    return(false);
                }
                // cannot move forward a take if outer has take or skip or distinct
                if (fromSelect.Take != null && (select.Take != null || select.Skip != null || select.IsDistinct || selHasAggregates || selHasGroupBy || selHasJoin))
                {
                    return(false);
                }
                // cannot move forward a skip if outer has skip or distinct
                if (fromSelect.Skip != null && (select.Skip != null || select.IsDistinct || selHasAggregates || selHasGroupBy || selHasJoin))
                {
                    return(false);
                }
                // cannot move forward a distinct if outer has take, skip, groupby or a different projection
                if (fromSelect.IsDistinct && (select.Take != null || select.Skip != null || !selHasNameMapProjection || selHasGroupBy || selHasAggregates || (selHasOrderBy && !isTopLevel) || selHasJoin))
                {
                    return(false);
                }
                if (frmHasAggregates && (select.Take != null || select.Skip != null || select.IsDistinct || selHasAggregates || selHasGroupBy || selHasJoin))
                {
                    return(false);
                }
                return(true);
            }
Пример #6
0
 internal static bool IsInitialProjection(SelectExpression select)
 {
     return(select.From is TableExpression);
 }
Пример #7
0
 protected override Expression VisitSelect(SelectExpression select)
 {
     this.selects.Add(select);
     return(select); // don't visit sub-queries
 }