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));
         }
         SelectExpression right = join.Right as SelectExpression;
         if ((((right != null) && (right.Take == null)) && ((right.Skip == null) && !AggregateChecker.HasAggregates(right))) && ((right.GroupBy == null) || (right.GroupBy.Count == 0)))
         {
             SelectExpression     source = right.SetWhere(null);
             HashSet <TableAlias> set    = ReferencedAliasGatherer.Gather(source);
             HashSet <TableAlias> other  = DeclaredAliasGatherer.Gather(join.Left);
             set.IntersectWith(other);
             if (set.Count == 0)
             {
                 Expression where = right.Where;
                 right            = source;
                 ProjectedColumns columns = ColumnProjector.ProjectColumns(this.language, where, right.Columns, right.Alias, DeclaredAliasGatherer.Gather(right.From));
                 right = right.SetColumns(columns.Columns);
                 where = columns.Projector;
                 return(new JoinExpression((where == null) ? JoinType.CrossJoin : ((join.Join == JoinType.CrossApply) ? JoinType.InnerJoin : JoinType.LeftOuter), join.Left, right, where));
             }
         }
     }
     return(join);
 }
Esempio n. 2
0
 protected override Expression VisitJoin(JoinExpression join)
 {
     join = (JoinExpression)base.VisitJoin(join);
     if ((join.Join == JoinType.CrossJoin) && (this.currentWhere != null))
     {
         Func <Expression, bool> predicate     = null;
         HashSet <TableAlias>    declaredLeft  = DeclaredAliasGatherer.Gather(join.Left);
         HashSet <TableAlias>    declaredRight = DeclaredAliasGatherer.Gather(join.Right);
         HashSet <TableAlias>    declared      = new HashSet <TableAlias>(declaredLeft.Union <TableAlias>(declaredRight));
         Expression[]            source        = this.currentWhere.Split(new ExpressionType[] { ExpressionType.And, ExpressionType.AndAlso });
         List <Expression>       good          = (from e in source
                                                  where this.CanBeJoinCondition(e, declaredLeft, declaredRight, declared)
                                                  select e).ToList <Expression>();
         if (good.Count <= 0)
         {
             return(join);
         }
         Expression condition = good.Join(ExpressionType.And);
         join = base.UpdateJoin(join, JoinType.InnerJoin, join.Left, join.Right, condition);
         if (predicate == null)
         {
             predicate = e => !good.Contains(e);
         }
         Expression expression2 = source.Where <Expression>(predicate).Join(ExpressionType.And);
         this.currentWhere = expression2;
     }
     return(join);
 }
        protected override Expression VisitSelect(SelectExpression select)
        {
            Expression expression;
            bool       isOuterMostSelect = this.isOuterMostSelect;

            try
            {
                this.isOuterMostSelect = false;
                select = (SelectExpression)base.VisitSelect(select);
                bool flag2 = (select.OrderBy != null) && (select.OrderBy.Count > 0);
                bool flag3 = (select.GroupBy != null) && (select.GroupBy.Count > 0);
                bool flag4 = (isOuterMostSelect || (select.Take != null)) || (select.Skip != null);
                bool flag5 = ((flag4 && !flag3) && !select.IsDistinct) && !AggregateChecker.HasAggregates(select);
                if (flag2)
                {
                    this.PrependOrderings(select.OrderBy);
                }
                if (select.IsReverse)
                {
                    this.ReverseOrderings();
                }
                IEnumerable <OrderExpression> orderBy = null;
                if (flag5)
                {
                    orderBy = this.gatheredOrderings;
                }
                else if (flag4)
                {
                    orderBy = select.OrderBy;
                }
                bool flag6 = (!isOuterMostSelect && !flag3) && !select.IsDistinct;
                ReadOnlyCollection <ColumnDeclaration> columns = select.Columns;
                if (this.gatheredOrderings != null)
                {
                    if (flag6)
                    {
                        HashSet <TableAlias> existingAliases = DeclaredAliasGatherer.Gather(select.From);
                        BindResult           result          = this.RebindOrderings(this.gatheredOrderings, select.Alias, existingAliases, select.Columns);
                        this.gatheredOrderings = null;
                        this.PrependOrderings(result.Orderings);
                        columns = result.Columns;
                    }
                    else
                    {
                        this.gatheredOrderings = null;
                    }
                }
                if (((orderBy != select.OrderBy) || (columns != select.Columns)) || select.IsReverse)
                {
                    select = new SelectExpression(select.Alias, columns, select.From, select.Where, orderBy, select.GroupBy, select.IsDistinct, select.Skip, select.Take, false);
                }
                expression = select;
            }
            finally
            {
                this.isOuterMostSelect = isOuterMostSelect;
            }
            return(expression);
        }
        private void MapAliases(Expression a, Expression b)
        {
            TableAlias[] aliasArray  = DeclaredAliasGatherer.Gather(a).ToArray <TableAlias>();
            TableAlias[] aliasArray2 = DeclaredAliasGatherer.Gather(b).ToArray <TableAlias>();
            int          index       = 0;
            int          length      = aliasArray.Length;

            while (index < length)
            {
                this.aliasScope.Add(aliasArray[index], aliasArray2[index]);
                index++;
            }
        }
        private Expression MakeSubquery(Expression expression)
        {
            TableAlias               alias   = new TableAlias();
            HashSet <TableAlias>     set     = DeclaredAliasGatherer.Gather(expression);
            List <ColumnDeclaration> columns = new List <ColumnDeclaration>();

            foreach (TableAlias alias2 in set)
            {
                foreach (ColumnExpression expression2 in this.columns[alias2])
                {
                    ColumnDeclaration item = new ColumnDeclaration(columns.GetAvailableColumnName(expression2.Name), expression2, expression2.QueryType);
                    columns.Add(item);
                    ColumnExpression expression3 = new ColumnExpression(expression2.Type, expression2.QueryType, alias, expression2.Name);
                    this.map.Add(expression2, expression3);
                }
            }
            return(new SelectExpression(alias, columns, expression, null));
        }
Esempio n. 6
0
        public virtual Expression GetOuterJoinTest(SelectExpression select)
        {
            List <ColumnExpression> list = JoinColumnGatherer.Gather(DeclaredAliasGatherer.Gather(select.From), select).ToList <ColumnExpression>();

            if (list.Count > 0)
            {
                foreach (ColumnExpression expression in list)
                {
                    foreach (ColumnDeclaration declaration in select.Columns)
                    {
                        if (expression.Equals(declaration.Expression))
                        {
                            return(expression);
                        }
                    }
                }
                return(list[0]);
            }
            return(Expression.Constant(1, typeof(int?)));
        }