diff --git a/jOOQ/src/main/java/org/jooq/ArrayAggOrderByStep.java b/jOOQ/src/main/java/org/jooq/ArrayAggOrderByStep.java new file mode 100644 index 0000000000..eaa14c22a1 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/ArrayAggOrderByStep.java @@ -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 ARRAY_AGG() function. + * + * @author Lukas Eder + * @see DSL#arrayAgg(Field) + */ +public interface ArrayAggOrderByStep extends AggregateFilterStep { + + /** + * Add an ORDER BY clause to the function. + */ + @Support({ HSQLDB, POSTGRES }) + AggregateFilterStep orderBy(Field... fields); + + /** + * Add an ORDER BY clause to the function. + */ + @Support({ HSQLDB, POSTGRES }) + AggregateFilterStep orderBy(SortField... fields); + + /** + * Add an ORDER BY clause to the function. + */ + @Support({ HSQLDB, POSTGRES }) + AggregateFilterStep orderBy(Collection> fields); +} diff --git a/jOOQ/src/main/java/org/jooq/GroupConcatOrderByStep.java b/jOOQ/src/main/java/org/jooq/GroupConcatOrderByStep.java index 734fd95560..aab1f52350 100644 --- a/jOOQ/src/main/java/org/jooq/GroupConcatOrderByStep.java +++ b/jOOQ/src/main/java/org/jooq/GroupConcatOrderByStep.java @@ -65,19 +65,19 @@ import org.jooq.impl.DSL; public interface GroupConcatOrderByStep extends GroupConcatSeparatorStep { /** - * Add an ORDER BY clause to the query + * Add an ORDER BY clause to the function. */ @Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) GroupConcatSeparatorStep orderBy(Field... fields); /** - * Add an ORDER BY clause to the query + * Add an ORDER BY clause to the function. */ @Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) GroupConcatSeparatorStep orderBy(SortField... fields); /** - * Add an ORDER BY clause to the query + * Add an ORDER BY clause to the function. */ @Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) GroupConcatSeparatorStep orderBy(Collection> fields); diff --git a/jOOQ/src/main/java/org/jooq/GroupConcatSeparatorStep.java b/jOOQ/src/main/java/org/jooq/GroupConcatSeparatorStep.java index bd1792b95c..cacc7d75ec 100644 --- a/jOOQ/src/main/java/org/jooq/GroupConcatSeparatorStep.java +++ b/jOOQ/src/main/java/org/jooq/GroupConcatSeparatorStep.java @@ -63,7 +63,7 @@ import org.jooq.impl.DSL; public interface GroupConcatSeparatorStep extends AggregateFunction { /** - * Specify the separator on the GROUP_CONCAT function + * Specify the separator on the GROUP_CONCAT function. */ @Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) AggregateFunction separator(String separator); diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 6646f01ece..2846d1d3fa 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -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 array_agg() aggregate function. + */ + @Support({ HSQLDB, POSTGRES }) + public static ArrayAggOrderByStep arrayAgg(Field field) { + return new Function(Term.ARRAY_AGG, field.getDataType().getArrayDataType(), nullSafe(field)); + } + /** * Get the max value over a field: max(field). */ diff --git a/jOOQ/src/main/java/org/jooq/impl/Function.java b/jOOQ/src/main/java/org/jooq/impl/Function.java index 177c158ad1..4e95a7e1b6 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Function.java +++ b/jOOQ/src/main/java/org/jooq/impl/Function.java @@ -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 extends AbstractField implements // Cascading interface implementations for aggregate function behaviour OrderedAggregateFunction, + ArrayAggOrderByStep, AggregateFunction, // and for window function behaviour WindowIgnoreNullsStep, @@ -192,7 +195,10 @@ class Function extends AbstractField 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 extends AbstractField implements } @Override - public final WindowRowsStep orderBy(Field... fields) { - windowSpecification.orderBy(fields); + public final Function orderBy(Field... fields) { + if (windowSpecification != null) + windowSpecification.orderBy(fields); + else + withinGroupOrderBy(fields); + return this; } @Override - public final WindowRowsStep orderBy(SortField... fields) { - windowSpecification.orderBy(fields); + public final Function orderBy(SortField... fields) { + if (windowSpecification != null) + windowSpecification.orderBy(fields); + else + withinGroupOrderBy(fields); + return this; } @Override - public final WindowRowsStep orderBy(Collection> fields) { - windowSpecification.orderBy(fields); + public final Function orderBy(Collection> fields) { + if (windowSpecification != null) + windowSpecification.orderBy(fields); + else + withinGroupOrderBy(fields); + return this; } diff --git a/jOOQ/src/main/java/org/jooq/impl/Term.java b/jOOQ/src/main/java/org/jooq/impl/Term.java index 3169796b0b..4b7bc0393f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Term.java +++ b/jOOQ/src/main/java/org/jooq/impl/Term.java @@ -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) {