A SQL aggregate expression such as MAX(columnn) or COUNT(*) or COUNT(DISTINCT column)
Inheritance: SqlBaseExpression
Beispiel #1
0
        protected static SqlAggregateExpression UpdateAggregate(SqlAggregateExpression sqlAggregate, Type type, SqlAggregateType aggType, Expression arg, bool isDistinct)
        {
            if (type != sqlAggregate.Type || aggType != sqlAggregate.AggregateType || arg != sqlAggregate.Argument || isDistinct != sqlAggregate.IsDistinct)
            {
                return new SqlAggregateExpression(type, aggType, arg, isDistinct);
            }

            return sqlAggregate;
        }
        protected static SqlAggregateExpression UpdateAggregate(SqlAggregateExpression sqlAggregate, Type type, SqlAggregateType aggType, Expression arg, bool isDistinct)
        {
            if (type != sqlAggregate.Type || aggType != sqlAggregate.AggregateType || arg != sqlAggregate.Argument || isDistinct != sqlAggregate.IsDistinct)
            {
                return(new SqlAggregateExpression(type, aggType, arg, isDistinct));
            }

            return(sqlAggregate);
        }
        protected override Expression VisitSelect(SqlSelectExpression selectExpression)
        {
            if (selectExpression.Columns.Count == 1
                && selectExpression.From.NodeType == (ExpressionType)SqlExpressionType.Select
                && selectExpression.Columns[0].Expression.NodeType == (ExpressionType)SqlExpressionType.Aggregate)
            {
                var from = (SqlSelectExpression)selectExpression.From;
                var aggregateExpression = (SqlAggregateExpression)selectExpression.Columns[0].Expression;

                if (from.Columns.Count > 1
                    || aggregateExpression.IsDistinct
                    || from.Distinct
                    || from.Take != null
                    || from.Skip != null
                    || from.GroupBy != null
                    /* Don't fold a from with an orderby into the outer select if it has a count or other aggregate */
                    || from.OrderBy != null && from.OrderBy.Count > 0 && SqlAggregateChecker.HasAggregates(selectExpression))
                {
                    return base.VisitSelect(selectExpression);
                }

                var newColumns = new List<SqlColumnDeclaration>();

                if (from.Columns.Count == 1)
                {
                    foreach (var column in from.Columns)
                    {
                        if (column.Expression.NodeType != (ExpressionType)SqlExpressionType.Column)
                        {
                            return base.VisitSelect(selectExpression);
                        }

                        var newAggregate = new SqlAggregateExpression
                        (
                            aggregateExpression.Type,
                            aggregateExpression.AggregateType,
                            column.Expression,
                            aggregateExpression.IsDistinct
                        );

                        newColumns.Add(new SqlColumnDeclaration(column.Name, newAggregate));
                    }
                }
                else
                {
                    newColumns.Add(selectExpression.Columns[0]);
                }

                var where = this.Visit(from.Where);

                return from.ChangeWhereAndColumns(where, newColumns.ToReadOnlyList(), from.ForUpdate || selectExpression.ForUpdate);
            }

            return base.VisitSelect(selectExpression);
        }
		protected override Expression VisitAggregate(SqlAggregateExpression sqlAggregate)
		{
			if (this.currentProjection != null && sqlAggregate.AggregateType == SqlAggregateType.Sum && sqlAggregate.Type.IsNullableType())
			{
				var defaultValue = this.currentProjection.DefaultValueExpression ?? Expression.Constant(Nullable.GetUnderlyingType(sqlAggregate.Type).GetDefaultValue());

				return new SqlFunctionCallExpression(sqlAggregate.Type, SqlFunction.Coalesce, sqlAggregate, defaultValue);
			}

			return sqlAggregate;
		}
Beispiel #5
0
        protected override Expression VisitAggregate(SqlAggregateExpression sqlAggregate)
        {
            SqlAggregateExpression current;

            if (!TryGetCurrent(sqlAggregate, out current))
            {
                return(sqlAggregate);
            }

            result &= current.IsDistinct == sqlAggregate.IsDistinct &&
                      current.AggregateType == sqlAggregate.AggregateType;

            if (result)
            {
                currentObject = current.Argument;
                Visit(sqlAggregate.Argument);
                currentObject = current;
            }

            return(sqlAggregate);
        }
Beispiel #6
0
 protected override Expression VisitAggregate(SqlAggregateExpression expression)
 {
     this.hashCode ^= expression.IsDistinct ? 1649375286 : 0;
     this.hashCode ^= expression.AggregateType.GetHashCode();
     return(base.VisitAggregate(expression));
 }
 protected override Expression VisitAggregate(SqlAggregateExpression expression)
 {
     this.hashCode ^= expression.IsDistinct ? 1649375286 : 0;
     this.hashCode ^= expression.AggregateType.GetHashCode();
     return base.VisitAggregate(expression);
 }
        protected override Expression VisitAggregate(SqlAggregateExpression expression)
        {
            SqlAggregateExpression current;
            if (!TryGetCurrent(expression, out current))
            {
                return expression;
            }

            if (!(this.result &= current.IsDistinct == expression.IsDistinct))
            {
                return expression;
            }

            if (!(this.result &= current.AggregateType == expression.AggregateType))
            {
                return expression;
            }

            if (!(this.result &= current.NodeType == expression.NodeType))
            {
                return expression;
            }

            if (!(this.result &= current.Type == expression.Type))
            {
                return expression;
            }

            this.currentObject = current.Argument;
            this.Visit(expression.Argument);
            if (!this.result)
            {
                return expression;
            }

            this.currentObject = current;
            return expression;
        }
Beispiel #9
0
        protected override Expression VisitAggregate(SqlAggregateExpression sqlAggregate)
        {
            this.Write(GetAggregateName(sqlAggregate.AggregateType));

            this.Write("(");

            if (sqlAggregate.IsDistinct)
            {
                this.Write("DISTINCT ");
            }

            if (sqlAggregate.Argument != null)
            {
                this.Visit(sqlAggregate.Argument);
            }
            else if (this.RequiresAsteriskWhenNoArgument(sqlAggregate.AggregateType))
            {
                this.Write("*");
            }

            this.Write(")");

            return sqlAggregate;
        }
Beispiel #10
0
		protected virtual Expression VisitAggregate(SqlAggregateExpression sqlAggregate)
		{
			var arg = this.Visit(sqlAggregate.Argument);

			return UpdateAggregate(sqlAggregate, sqlAggregate.Type, sqlAggregate.AggregateType, arg, sqlAggregate.IsDistinct);
		}
Beispiel #11
0
        protected override Expression VisitAggregate(SqlAggregateExpression aggregate)
        {
            this.aggregatesFound.Add(aggregate);

            return base.VisitAggregate(aggregate);
        }
Beispiel #12
0
 protected override Expression VisitAggregate(SqlAggregateExpression sqlAggregate)
 {
     return(base.VisitAggregate(sqlAggregate));
 }
Beispiel #13
0
        protected override Expression VisitAggregate(SqlAggregateExpression sqlAggregate)
        {
            this.output.Append(sqlAggregate.ToString());

            return sqlAggregate;
        }
Beispiel #14
0
        protected override Expression VisitAggregate(SqlAggregateExpression sqlAggregate)
        {
            this.hasAggregate = true;

            return sqlAggregate;
        }
Beispiel #15
0
        protected virtual Expression VisitAggregate(SqlAggregateExpression sqlAggregate)
        {
            var arg = this.Visit(sqlAggregate.Argument);

            return(UpdateAggregate(sqlAggregate, sqlAggregate.Type, sqlAggregate.AggregateType, arg, sqlAggregate.IsDistinct));
        }