diff --git a/jOOQ/src/main/java/org/jooq/AggregateFilterStep.java b/jOOQ/src/main/java/org/jooq/AggregateFilterStep.java new file mode 100644 index 0000000000..f6f850ae6d --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/AggregateFilterStep.java @@ -0,0 +1,118 @@ +/** + * Copyright (c) 2009-2014, 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 java.util.Collection; + +import org.jooq.impl.DSL; + +/** + * The step in the specification of aggregate functions where the SQL:2003 + * standard FILTER clause can be added. + * + * @author Lukas Eder + */ +public interface AggregateFilterStep extends WindowBeforeOverStep { + + /** + * Add a FILTER clause to the aggregate function, connecting + * conditions with each other with {@link Operator#AND}. + */ + @Support + WindowBeforeOverStep filterWhere(Condition... conditions); + + /** + * Add a FILTER clause to the aggregate function, connecting + * conditions with each other with {@link Operator#AND}. + */ + @Support + WindowBeforeOverStep filterWhere(Collection conditions); + + /** + * Add a FILTER clause to the aggregate function, connecting + * conditions with each other with {@link Operator#AND}. + */ + @Support + WindowBeforeOverStep filterWhere(Field field); + + /** + * Add a FILTER clause to the aggregate function, connecting + * conditions with each other with {@link Operator#AND}. + *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! + * + * @see DSL#condition(String) + */ + @Support + WindowBeforeOverStep filterWhere(String sql); + + /** + * Add a FILTER clause to the aggregate function, connecting + * conditions with each other with {@link Operator#AND}. + *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! + * + * @see DSL#condition(String, Object...) + */ + @Support + WindowBeforeOverStep filterWhere(String sql, Object... bindings); + + /** + * Add a FILTER clause to the aggregate function, connecting + * conditions with each other with {@link Operator#AND}. + *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! + * + * @see DSL#condition(String, QueryPart...) + */ + @Support + WindowBeforeOverStep filterWhere(String sql, QueryPart... parts); + +} diff --git a/jOOQ/src/main/java/org/jooq/AggregateFunction.java b/jOOQ/src/main/java/org/jooq/AggregateFunction.java index 24f50dfb03..05dafa4060 100644 --- a/jOOQ/src/main/java/org/jooq/AggregateFunction.java +++ b/jOOQ/src/main/java/org/jooq/AggregateFunction.java @@ -40,14 +40,6 @@ */ package org.jooq; -import static org.jooq.SQLDialect.CUBRID; -// ... -import static org.jooq.SQLDialect.H2; -// ... -// ... -// ... -import static org.jooq.SQLDialect.POSTGRES; -// ... // ... import java.util.Collection; @@ -60,86 +52,7 @@ import java.util.Collection; * * @author Lukas Eder */ -public interface AggregateFunction extends Field, WindowOverStep { - - /** - * Turn this aggregate function into a window function. - *

- * An example:

-     * MAX(id) OVER (PARTITION BY 1)
-     * 
- *

- * Window functions are supported in CUBRID, DB2, Postgres, Oracle, SQL - * Server and Sybase. - */ - @Override - @Support({ CUBRID, H2, POSTGRES }) - WindowPartitionByStep over(); - - /** - * Turn this aggregate function into a window function referencing a window - * name. - *

- * An example:

-     * MAX(id) OVER my_window
-     * 
- *

- * Window functions are supported in CUBRID, DB2, Postgres, Oracle, SQL - * Server and Sybase. If the WINDOW clause is not supported - * (see {@link SelectWindowStep#window(WindowDefinition...)}, then - * referenced windows will be inlined. - */ - @Override - @Support({ CUBRID, POSTGRES }) - WindowFinalStep over(Name name); - - /** - * Turn this aggregate function into a window function referencing a window - * name. - *

- * An example:

-     * MAX(id) OVER my_window
-     * 
- *

- * Window functions are supported in CUBRID, DB2, Postgres, Oracle, SQL - * Server and Sybase. If the WINDOW clause is not supported - * (see {@link SelectWindowStep#window(WindowDefinition...)}, then - * referenced windows will be inlined. - */ - @Override - @Support({ CUBRID, POSTGRES }) - WindowFinalStep over(String name); - - /** - * Turn this aggregate function into a window function. - *

- * An example:

-     * MAX(id) OVER (PARTITION BY 1)
-     * 
- *

- * Window functions are supported in CUBRID, DB2, Postgres, Oracle, SQL - * Server and Sybase. - */ - @Override - @Support({ CUBRID, POSTGRES }) - WindowFinalStep over(WindowSpecification specification); - - /** - * Turn this aggregate function into a window function referencing a window - * definition. - *

- * An example:

-     * MAX(id) OVER my_window
-     * 
- *

- * Window functions are supported in CUBRID, DB2, Postgres, Oracle, SQL - * Server and Sybase. If the WINDOW clause is not supported - * (see {@link SelectWindowStep#window(WindowDefinition...)}, then - * referenced windows will be inlined. - */ - @Override - @Support({ CUBRID, POSTGRES }) - WindowFinalStep over(WindowDefinition definition); +public interface AggregateFunction extends AggregateFilterStep { /* [pro] xx xxx @@ -153,7 +66,7 @@ public interface AggregateFunction extends Field, WindowOverStep { x xxxxxxxxxx xxxx xxxx xxxx xxxxxx xxxxxxxxx xx xxxxxxxxxxxxx xxxxxxxxxx xx xxxxxxxxxxxxxxxx - xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx + xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx xxx x xxxxxxxx xxxx xxxxxxxxx xxxxxxxx xx xxxxxxxxxxxxxxxxxx xxxxxx @@ -166,7 +79,7 @@ public interface AggregateFunction extends Field, WindowOverStep { x xxxxxxxxxx xxxx xxxx xxxx xxxxxx xxxxxxxxx xx xxxxxxxxxxxxx xxxxxxxxxx xx xxxxxxxxxxxxxxxx - xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx + xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx xxx x xxxxxxxx xxxx xxxxxxxxx xxxxxxxx xx xxxxxxxxxxxxxxxxxx xxxxxx @@ -179,7 +92,7 @@ public interface AggregateFunction extends Field, WindowOverStep { x xxxxxxxxxx xxxx xxxx xxxx xxxxxx xxxxxxxxx xx xxxxxxxxxxxxx xxxxxxxxxx xx xxxxxxxxxxxxxxxx - xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxxxxx xxxxxxxx + xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxxxxx xxxxxxxx xxx x xxxxxxxx xxxx xxxxxxxxx xxxxxxxx xx xxxxxxxxxxxxxxxxxx xxxxxx @@ -192,7 +105,7 @@ public interface AggregateFunction extends Field, WindowOverStep { x xxxxxxxxxx xxxx xxxx xxxx xxxxxx xxxxxxxxx xx xxxxxxxxxxxxx xxxxxxxxxx xx xxxxxxxxxxxxxxxx - xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx + xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx xxx x xxxxxxxx xxxx xxxxxxxxx xxxxxxxx xx xxxxxxxxxxxxxxxxxx xxxxxx @@ -205,7 +118,7 @@ public interface AggregateFunction extends Field, WindowOverStep { x xxxxxxxxxx xxxx xxxx xxxx xxxxxx xxxxxxxxx xx xxxxxxxxxxxxx xxxxxxxxxx xx xxxxxxxxxxxxxxxx - xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx + xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx xxx x xxxxxxxx xxxx xxxxxxxxx xxxxxxxx xx xxxxxxxxxxxxxxxxxx xxxxxx @@ -218,6 +131,6 @@ public interface AggregateFunction extends Field, WindowOverStep { x xxxxxxxxxx xxxx xxxx xxxx xxxxxx xxxxxxxxx xx xxxxxxxxxxxxx xxxxxxxxxx xx xxxxxxxxxxxxxxxx - xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxxxxx xxxxxxxx + xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxxxxx xxxxxxxx xx [/pro] */ } diff --git a/jOOQ/src/main/java/org/jooq/OrderedAggregateFunction.java b/jOOQ/src/main/java/org/jooq/OrderedAggregateFunction.java index 6418febd5b..3eacdf0876 100644 --- a/jOOQ/src/main/java/org/jooq/OrderedAggregateFunction.java +++ b/jOOQ/src/main/java/org/jooq/OrderedAggregateFunction.java @@ -83,19 +83,19 @@ public interface OrderedAggregateFunction { * aggregate function */ @Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) - AggregateFunction withinGroupOrderBy(Field... fields); + AggregateFilterStep withinGroupOrderBy(Field... fields); /** * Add an WITHIN GROUP (ORDER BY ..) clause to the ordered * aggregate function */ @Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) - AggregateFunction withinGroupOrderBy(SortField... fields); + AggregateFilterStep withinGroupOrderBy(SortField... fields); /** * Add an WITHIN GROUP (ORDER BY ..) clause to the ordered * aggregate function */ @Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) - AggregateFunction withinGroupOrderBy(Collection> fields); + AggregateFilterStep withinGroupOrderBy(Collection> fields); } diff --git a/jOOQ/src/main/java/org/jooq/WindowBeforeOverStep.java b/jOOQ/src/main/java/org/jooq/WindowBeforeOverStep.java index ffd80aff04..786021778b 100644 --- a/jOOQ/src/main/java/org/jooq/WindowBeforeOverStep.java +++ b/jOOQ/src/main/java/org/jooq/WindowBeforeOverStep.java @@ -40,16 +40,6 @@ */ package org.jooq; -import static org.jooq.SQLDialect.CUBRID; -// ... -import static org.jooq.SQLDialect.DERBY; -import static org.jooq.SQLDialect.H2; -import static org.jooq.SQLDialect.HSQLDB; -// ... -// ... -import static org.jooq.SQLDialect.POSTGRES; -// ... -// ... /** @@ -73,11 +63,4 @@ import static org.jooq.SQLDialect.POSTGRES; */ public interface WindowBeforeOverStep extends WindowOverStep, Field { - /** - * Add an OVER clause - */ - @Override - @Support({ CUBRID, DERBY, H2, HSQLDB, POSTGRES }) - WindowPartitionByStep over(); - } diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 6b0275f845..8163c22424 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -9984,7 +9984,7 @@ public class DSL { */ @Support public static AggregateFunction count() { - return count(field("*", Integer.class)); + return count(Function.ASTERISK); } /** diff --git a/jOOQ/src/main/java/org/jooq/impl/Function.java b/jOOQ/src/main/java/org/jooq/impl/Function.java index 0e7db42ad7..5e86a20ed1 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Function.java +++ b/jOOQ/src/main/java/org/jooq/impl/Function.java @@ -49,8 +49,11 @@ import static org.jooq.SQLDialect.HSQLDB; import static org.jooq.SQLDialect.MARIADB; import static org.jooq.SQLDialect.MYSQL; import static org.jooq.SQLDialect.POSTGRES; +import static org.jooq.SQLDialect.POSTGRES_9_4; // ... +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.Term.LIST_AGG; import static org.jooq.impl.Term.ROW_NUMBER; import static org.jooq.impl.Utils.DATA_LOCALLY_SCOPED_DATA_MAP; @@ -60,7 +63,9 @@ import java.util.Arrays; import java.util.Collection; import java.util.Map; +import org.jooq.AggregateFilterStep; import org.jooq.AggregateFunction; +import org.jooq.Condition; import org.jooq.Context; import org.jooq.DataType; import org.jooq.Field; @@ -92,8 +97,6 @@ class Function extends AbstractField implements // Cascading interface implementations for aggregate function behaviour OrderedAggregateFunction, AggregateFunction, - WindowBeforeOverStep, - // and for window function behaviour WindowIgnoreNullsStep, WindowPartitionByStep, @@ -103,6 +106,8 @@ class Function extends AbstractField implements private static final long serialVersionUID = 347252741712134044L; + static final Field ASTERISK = DSL.field("*", Integer.class); + // Mutually exclusive attributes: super.getName(), this.name, this.term private final Name name; private final Term term; @@ -112,6 +117,7 @@ class Function extends AbstractField implements private final boolean distinct; private final SortFieldList withinGroupOrderBy; private final SortFieldList keepDenseRankOrderBy; + private Condition filter; private WindowSpecificationImpl windowSpecification; private WindowDefinitionImpl windowDefinition; private Name windowName; @@ -188,6 +194,7 @@ class Function extends AbstractField implements } else if (term == LIST_AGG && asList(POSTGRES).contains(ctx.family())) { toSQLStringAgg(ctx); + toSQLFilterClause(ctx); toSQLOverClause(ctx); } /* [pro] xx @@ -199,6 +206,7 @@ class Function extends AbstractField implements toSQLArguments(ctx); toSQLKeepDenseRankOrderByClause(ctx); toSQLWithinGroupClause(ctx); + toSQLFilterClause(ctx); toSQLOverClause(ctx); } } @@ -308,6 +316,18 @@ class Function extends AbstractField implements ctx.sql(")"); } + final void toSQLFilterClause(Context ctx) { + if (filter != null && POSTGRES_9_4.precedes(ctx.dialect())) { + ctx.sql(" ") + .keyword("filter") + .sql(" (") + .keyword("where") + .sql(" ") + .visit(filter) + .sql(")"); + } + } + final void toSQLOverClause(Context ctx) { QueryPart window = window(ctx); @@ -408,7 +428,18 @@ class Function extends AbstractField implements } if (!arguments.isEmpty()) { - ctx.visit(arguments); + if (filter == null || POSTGRES_9_4.precedes(ctx.dialect())) { + ctx.visit(arguments); + } + else { + QueryPartList> expressions = new QueryPartList>(); + + for (QueryPart argument : arguments) { + expressions.add(DSL.decode().when(filter, argument == ASTERISK ? one() : argument)); + } + + ctx.visit(expressions); + } } if (distinct) { @@ -483,41 +514,74 @@ class Function extends AbstractField implements /* [pro] xx xxxxxxxxx - xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx x + xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx x xxxxx x xxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxx xxxxx x xxxxxxxxx - xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx x + xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx x xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x xxxxxxxxx - xxxxxx xxxxx xxxxx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xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + x + + xxxxxxxxx + xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx xxxxxxxxxx xxxxxxxxxxx x + xxxxxxxxxxxxxxxxxxxxx x x xxx xxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxx x xx + xxxxxx xxxxx + x + + xxxxxxxxx + xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxx x + xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + x + + xxxxxxxxx + xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx xxxx x + xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxx + x + + xxxxxxxxx + xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx xxxx xxxxxxxxx xxxxxxxxx x + xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxx + x + + xxxxxxxxx + xxxxxx xxxxx xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx xxxx xxxxxxxxxxxx xxxxxx x + xxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx + x + xxxxxxxxx xxxxxx xxxxx xxxxxxxxxxxxxxxxx xxxxxxxxxxxxx x xxxxxxxxxxx x xxxxx