[#891] Let min() max(), etc functions return a new type AggregateFunction. This type can then be used as an entry-point for window functions

This commit is contained in:
Lukas Eder 2011-11-04 17:20:38 +00:00
parent a610cc851e
commit 30dd09dee2
4 changed files with 126 additions and 22 deletions

View File

@ -0,0 +1,47 @@
/**
* Copyright (c) 2009-2011, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq;
/**
* An aggregate function is a special field that is usually used in a
* <code>GROUP BY</code> context. It is also the base for window function
* construction.
*
* @author Lukas Eder
*/
public interface AggregateFunction<T> extends Field<T> {
}

View File

@ -0,0 +1,56 @@
/**
* Copyright (c) 2009-2011, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq.impl;
import org.jooq.AggregateFunction;
import org.jooq.DataType;
import org.jooq.QueryPart;
class AggregateFunctionImpl<T> extends Function<T> implements AggregateFunction<T> {
/**
* Generated UID
*/
private static final long serialVersionUID = 1952351506930280715L;
AggregateFunctionImpl(String name, DataType<T> type, QueryPart... arguments) {
super(name, type, arguments);
}
AggregateFunctionImpl(Term term, DataType<T> type, QueryPart... arguments) {
super(term, type, arguments);
}
}

View File

@ -43,7 +43,7 @@ import org.jooq.RenderContext;
/**
* @author Lukas Eder
*/
class Count extends Function<Integer> {
class Count extends AggregateFunctionImpl<Integer> {
private static final long serialVersionUID = 4685583105296376461L;

View File

@ -54,6 +54,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.jooq.AggregateFunction;
import org.jooq.ArrayRecord;
import org.jooq.Attachable;
import org.jooq.Batch;
@ -2107,50 +2108,50 @@ public class Factory implements FactoryOperations {
/**
* Get the count(*) function
*/
public static Field<Integer> count() {
public static AggregateFunction<Integer> count() {
return new Count(field("*", Integer.class), false);
}
/**
* Get the count(field) function
*/
public static Field<Integer> count(Field<?> field) {
public static AggregateFunction<Integer> count(Field<?> field) {
return new Count(field, false);
}
/**
* Get the count(distinct field) function
*/
public static Field<Integer> countDistinct(Field<?> field) {
public static AggregateFunction<Integer> countDistinct(Field<?> field) {
return new Count(field, true);
}
/**
* Get the max value over a field: max(field)
*/
public static <T> Field<T> max(Field<T> field) {
return function("max", field.getDataType(), field);
public static <T> AggregateFunction<T> max(Field<T> field) {
return new AggregateFunctionImpl<T>("max", field.getDataType(), field);
}
/**
* Get the min value over a field: min(field)
*/
public static <T> Field<T> min(Field<T> field) {
return function("min", field.getDataType(), field);
public static <T> AggregateFunction<T> min(Field<T> field) {
return new AggregateFunctionImpl<T>("min", field.getDataType(), field);
}
/**
* Get the sum over a numeric field: sum(field)
*/
public static Field<BigDecimal> sum(Field<? extends Number> field) {
return function("sum", SQLDataType.NUMERIC, field);
public static AggregateFunction<BigDecimal> sum(Field<? extends Number> field) {
return new AggregateFunctionImpl<BigDecimal>("sum", SQLDataType.NUMERIC, field);
}
/**
* Get the average over a numeric field: avg(field)
*/
public static Field<BigDecimal> avg(Field<? extends Number> field) {
return function("avg", SQLDataType.NUMERIC, field);
public static AggregateFunction<BigDecimal> avg(Field<? extends Number> field) {
return new AggregateFunctionImpl<BigDecimal>("avg", SQLDataType.NUMERIC, field);
}
/**
@ -2163,8 +2164,8 @@ public class Factory implements FactoryOperations {
* <li>Sybase SQL Anywhere</li>
* </ul>
*/
public static Field<BigDecimal> median(Field<? extends Number> field) {
return function("median", SQLDataType.NUMERIC, field);
public static AggregateFunction<BigDecimal> median(Field<? extends Number> field) {
return new AggregateFunctionImpl<BigDecimal>("median", SQLDataType.NUMERIC, field);
}
/**
@ -2184,8 +2185,8 @@ public class Factory implements FactoryOperations {
* <li>Sybase SQL Anywhere</li>
* </ul>
*/
public static Field<BigDecimal> stddevPop(Field<? extends Number> field) {
return new Function<BigDecimal>(Term.STDDEV_POP, SQLDataType.NUMERIC, field);
public static AggregateFunction<BigDecimal> stddevPop(Field<? extends Number> field) {
return new AggregateFunctionImpl<BigDecimal>(Term.STDDEV_POP, SQLDataType.NUMERIC, field);
}
/**
@ -2205,8 +2206,8 @@ public class Factory implements FactoryOperations {
* <li>Sybase SQL Anywhere</li>
* </ul>
*/
public static Field<BigDecimal> stddevSamp(Field<? extends Number> field) {
return new Function<BigDecimal>(Term.STDDEV_SAMP, SQLDataType.NUMERIC, field);
public static AggregateFunction<BigDecimal> stddevSamp(Field<? extends Number> field) {
return new AggregateFunctionImpl<BigDecimal>(Term.STDDEV_SAMP, SQLDataType.NUMERIC, field);
}
/**
@ -2226,8 +2227,8 @@ public class Factory implements FactoryOperations {
* <li>Sybase SQL Anywhere</li>
* </ul>
*/
public static Field<BigDecimal> varPop(Field<? extends Number> field) {
return new Function<BigDecimal>(Term.VAR_POP, SQLDataType.NUMERIC, field);
public static AggregateFunction<BigDecimal> varPop(Field<? extends Number> field) {
return new AggregateFunctionImpl<BigDecimal>(Term.VAR_POP, SQLDataType.NUMERIC, field);
}
/**
@ -2245,8 +2246,8 @@ public class Factory implements FactoryOperations {
* <li>Sybase SQL Anywhere</li>
* </ul>
*/
public static Field<BigDecimal> varSamp(Field<? extends Number> field) {
return new Function<BigDecimal>(Term.VAR_SAMP, SQLDataType.NUMERIC, field);
public static AggregateFunction<BigDecimal> varSamp(Field<? extends Number> field) {
return new AggregateFunctionImpl<BigDecimal>(Term.VAR_SAMP, SQLDataType.NUMERIC, field);
}
// -------------------------------------------------------------------------