[#3329] Support the SQL standard array_agg function

This commit is contained in:
lukaseder 2015-01-20 19:24:14 +01:00
parent 9abd23a916
commit 759daa2a55
6 changed files with 119 additions and 11 deletions

View File

@ -0,0 +1,75 @@
/**
* Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* This work is dual-licensed
* - under the Apache Software License 2.0 (the "ASL")
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
* =============================================================================
* You may choose which license applies to you:
*
* - If you're using this work with Open Source databases, you may choose
* either ASL or jOOQ License.
* - If you're using this work with at least one commercial database, you must
* choose jOOQ License
*
* For more information, please visit http://www.jooq.org/licenses
*
* Apache Software License 2.0:
* -----------------------------------------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* jOOQ License and Maintenance Agreement:
* -----------------------------------------------------------------------------
* Data Geekery grants the Customer the non-exclusive, timely limited and
* non-transferable license to install and use the Software under the terms of
* the jOOQ License and Maintenance Agreement.
*
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
*/
package org.jooq;
import static org.jooq.SQLDialect.HSQLDB;
import static org.jooq.SQLDialect.POSTGRES;
import java.util.Collection;
import org.jooq.impl.DSL;
/**
* The SQL standard <code>ARRAY_AGG()</code> function.
*
* @author Lukas Eder
* @see DSL#arrayAgg(Field)
*/
public interface ArrayAggOrderByStep<T> extends AggregateFilterStep<T> {
/**
* Add an <code>ORDER BY</code> clause to the function.
*/
@Support({ HSQLDB, POSTGRES })
AggregateFilterStep<T> orderBy(Field<?>... fields);
/**
* Add an <code>ORDER BY</code> clause to the function.
*/
@Support({ HSQLDB, POSTGRES })
AggregateFilterStep<T> orderBy(SortField<?>... fields);
/**
* Add an <code>ORDER BY</code> clause to the function.
*/
@Support({ HSQLDB, POSTGRES })
AggregateFilterStep<T> orderBy(Collection<? extends SortField<?>> fields);
}

View File

@ -65,19 +65,19 @@ import org.jooq.impl.DSL;
public interface GroupConcatOrderByStep extends GroupConcatSeparatorStep {
/**
* Add an <code>ORDER BY</code> clause to the query
* Add an <code>ORDER BY</code> clause to the function.
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
GroupConcatSeparatorStep orderBy(Field<?>... fields);
/**
* Add an <code>ORDER BY</code> clause to the query
* Add an <code>ORDER BY</code> clause to the function.
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
GroupConcatSeparatorStep orderBy(SortField<?>... fields);
/**
* Add an <code>ORDER BY</code> clause to the query
* Add an <code>ORDER BY</code> clause to the function.
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
GroupConcatSeparatorStep orderBy(Collection<? extends SortField<?>> fields);

View File

@ -63,7 +63,7 @@ import org.jooq.impl.DSL;
public interface GroupConcatSeparatorStep extends AggregateFunction<String> {
/**
* Specify the separator on the <code>GROUP_CONCAT</code> function
* Specify the separator on the <code>GROUP_CONCAT</code> function.
*/
@Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
AggregateFunction<String> separator(String separator);

View File

@ -90,6 +90,7 @@ import javax.sql.DataSource;
import org.jooq.AggregateFunction;
import org.jooq.AlterSequenceRestartStep;
import org.jooq.AlterTableStep;
import org.jooq.ArrayAggOrderByStep;
// ...
import org.jooq.Case;
import org.jooq.CommonTableExpression;
@ -10441,6 +10442,14 @@ public class DSL {
return new Every(condition);
}
/**
* Get the <code>array_agg()</code> aggregate function.
*/
@Support({ HSQLDB, POSTGRES })
public static <T> ArrayAggOrderByStep<T[]> arrayAgg(Field<T> field) {
return new Function<T[]>(Term.ARRAY_AGG, field.getDataType().getArrayDataType(), nullSafe(field));
}
/**
* Get the max value over a field: max(field).
*/

View File

@ -55,6 +55,7 @@ import static org.jooq.impl.DSL.condition;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.one;
import static org.jooq.impl.DSL.percentileCont;
import static org.jooq.impl.Term.ARRAY_AGG;
import static org.jooq.impl.Term.LIST_AGG;
import static org.jooq.impl.Term.MEDIAN;
import static org.jooq.impl.Term.ROW_NUMBER;
@ -68,6 +69,7 @@ import java.util.Map;
import org.jooq.AggregateFilterStep;
import org.jooq.AggregateFunction;
import org.jooq.ArrayAggOrderByStep;
import org.jooq.Condition;
import org.jooq.Context;
import org.jooq.DataType;
@ -99,6 +101,7 @@ class Function<T> extends AbstractField<T> implements
// Cascading interface implementations for aggregate function behaviour
OrderedAggregateFunction<T>,
ArrayAggOrderByStep<T>,
AggregateFunction<T>,
// and for window function behaviour
WindowIgnoreNullsStep<T>,
@ -192,7 +195,10 @@ class Function<T> extends AbstractField<T> implements
@Override
public /* final */ void accept(Context<?> ctx) {
if (term == LIST_AGG && asList(CUBRID, H2, HSQLDB, MARIADB, MYSQL).contains(ctx.family())) {
if (term == ARRAY_AGG && asList(HSQLDB, POSTGRES).contains(ctx.family())) {
toSQLGroupConcat(ctx);
}
else if (term == LIST_AGG && asList(CUBRID, H2, HSQLDB, MARIADB, MYSQL).contains(ctx.family())) {
toSQLGroupConcat(ctx);
}
else if (term == LIST_AGG && asList(POSTGRES).contains(ctx.family())) {
@ -649,20 +655,32 @@ class Function<T> extends AbstractField<T> implements
}
@Override
public final WindowRowsStep<T> orderBy(Field<?>... fields) {
windowSpecification.orderBy(fields);
public final Function<T> orderBy(Field<?>... fields) {
if (windowSpecification != null)
windowSpecification.orderBy(fields);
else
withinGroupOrderBy(fields);
return this;
}
@Override
public final WindowRowsStep<T> orderBy(SortField<?>... fields) {
windowSpecification.orderBy(fields);
public final Function<T> orderBy(SortField<?>... fields) {
if (windowSpecification != null)
windowSpecification.orderBy(fields);
else
withinGroupOrderBy(fields);
return this;
}
@Override
public final WindowRowsStep<T> orderBy(Collection<? extends SortField<?>> fields) {
windowSpecification.orderBy(fields);
public final Function<T> orderBy(Collection<? extends SortField<?>> fields) {
if (windowSpecification != null)
windowSpecification.orderBy(fields);
else
withinGroupOrderBy(fields);
return this;
}

View File

@ -50,6 +50,12 @@ import org.jooq.SQLDialect;
*/
enum Term {
ARRAY_AGG {
@Override
public String translate(SQLDialect dialect) {
return "array_agg";
}
},
ATAN2 {
@Override
public String translate(SQLDialect dialect) {