From 57fc2e252fc5bb980b7a8e18f3e0f5808b4b7702 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Tue, 5 Feb 2019 14:54:50 +0100 Subject: [PATCH] [#8293] Wrong Javadoc on SelectQuery.addHaving(Operator, Condition...) --- .../resources/org/jooq/web/grammar-3.12.txt | 12 ++++ .../main/java/org/jooq/ConditionProvider.java | 14 ++-- jOOQ/src/main/java/org/jooq/SelectQuery.java | 66 ++++++++++++++++++- .../main/java/org/jooq/SelectWindowStep.java | 6 +- .../src/main/java/org/jooq/impl/Keywords.java | 1 + .../main/java/org/jooq/impl/ParserImpl.java | 9 +++ .../main/java/org/jooq/impl/SelectImpl.java | 62 ++++++++++++++++- .../java/org/jooq/impl/SelectQueryImpl.java | 46 +++++++++++++ 8 files changed, 202 insertions(+), 14 deletions(-) diff --git a/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.12.txt b/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.12.txt index 23bed067b4..3f90bbd71c 100644 --- a/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.12.txt +++ b/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.12.txt @@ -52,6 +52,7 @@ proceduralStatement = [ label ] | declareStatement | assignmentStatement | ifStatement [ labelReference ] +| caseStatement [ labelReference ] | loopStatement [ labelReference ] | forStatement [ labelReference ] | whileStatement [ labelReference ] @@ -101,6 +102,16 @@ ifStatement = ) ; +caseStatement = + 'CASE' +( + field ( 'WHEN' field 'THEN' proceduralStatements )+ +| ( 'WHEN' condition 'THEN' proceduralStatements )+ +) + [ 'ELSE' proceduralStatements ] + 'END CASE' +; + loopStatement = 'LOOP' proceduralStatements 'END LOOP' ; @@ -474,6 +485,7 @@ tableExpression = [ groupBy ] [ 'HAVING' condition ] [ 'WINDOW' windows ] + [ 'QUALIFY' condition ] ; connectBy = diff --git a/jOOQ/src/main/java/org/jooq/ConditionProvider.java b/jOOQ/src/main/java/org/jooq/ConditionProvider.java index d96b465c83..2bb2ed8633 100644 --- a/jOOQ/src/main/java/org/jooq/ConditionProvider.java +++ b/jOOQ/src/main/java/org/jooq/ConditionProvider.java @@ -41,7 +41,7 @@ package org.jooq; import java.util.Collection; /** - * A common interface for all objects holding conditions (e.g. queries) + * A common interface for all objects holding conditions (e.g. queries). * * @author Lukas Eder * @deprecated - 2.6.0 [#1881] - This type will be removed from the public API, @@ -53,7 +53,7 @@ public interface ConditionProvider { /** * Adds a new condition to the query, connecting them to existing - * conditions with {@link Operator#AND} + * conditions with {@link Operator#AND}. * * @param condition The condition */ @@ -62,7 +62,7 @@ public interface ConditionProvider { /** * Adds new conditions to the query, connecting them to existing - * conditions with {@link Operator#AND} + * conditions with {@link Operator#AND}. * * @param conditions The condition */ @@ -71,7 +71,7 @@ public interface ConditionProvider { /** * Adds new conditions to the query, connecting them to existing - * conditions with {@link Operator#AND} + * conditions with {@link Operator#AND}. * * @param conditions The condition */ @@ -80,7 +80,7 @@ public interface ConditionProvider { /** * Adds a new condition to the query, connecting them to existing - * conditions with the provided operator + * conditions with the provided operator. * * @param condition The condition */ @@ -89,7 +89,7 @@ public interface ConditionProvider { /** * Adds new conditions to the query, connecting them to existing - * conditions with the provided operator + * conditions with the provided operator. * * @param conditions The condition */ @@ -98,7 +98,7 @@ public interface ConditionProvider { /** * Adds new conditions to the query, connecting them to existing - * conditions with the provided operator + * conditions with the provided operator. * * @param conditions The condition */ diff --git a/jOOQ/src/main/java/org/jooq/SelectQuery.java b/jOOQ/src/main/java/org/jooq/SelectQuery.java index c8c517cdcd..fb6be86943 100644 --- a/jOOQ/src/main/java/org/jooq/SelectQuery.java +++ b/jOOQ/src/main/java/org/jooq/SelectQuery.java @@ -347,7 +347,7 @@ public interface SelectQuery extends Select, ConditionProvi /** * Adds a new condition to the having clause of query, connecting it with - * each other with {@link Operator#AND}. + * each other with operator. * * @param operator The operator to use to add the conditions to the existing * conditions @@ -358,7 +358,7 @@ public interface SelectQuery extends Select, ConditionProvi /** * Adds new conditions to the having clause of query, connecting them with - * each other with {@link Operator#AND}. + * each other with operator. * * @param operator The operator to use to add the conditions to the existing * conditions @@ -369,7 +369,7 @@ public interface SelectQuery extends Select, ConditionProvi /** * Adds new conditions to the having clause of query, connecting them with - * each other with {@link Operator#AND}. + * each other with operator. * * @param operator The operator to use to add the conditions to the existing * conditions @@ -394,6 +394,66 @@ public interface SelectQuery extends Select, ConditionProvi @Support({ CUBRID, FIREBIRD_3_0, H2, MARIADB, MYSQL_8_0, POSTGRES, SQLITE }) void addWindow(Collection definitions); + /** + * Adds a new condition to the qualify clause of the query, connecting it + * with each other with {@link Operator#AND}. + * + * @param condition The condition + */ + @Support({ H2 }) + void addQualify(Condition condition); + + /** + * Adds new conditions to the qualify clause of the query, connecting them + * with each other with {@link Operator#AND}. + * + * @param conditions The condition + */ + @Support({ H2 }) + void addQualify(Condition... conditions); + + /** + * Adds new conditions to the qualify clause of the query, connecting them + * with each other with {@link Operator#AND}. + * + * @param conditions The condition + */ + @Support({ H2 }) + void addQualify(Collection conditions); + + /** + * Adds a new condition to the qualify clause of query, connecting it with + * each other with operator. + * + * @param operator The operator to use to add the conditions to the existing + * conditions + * @param condition The condition + */ + @Support({ H2 }) + void addQualify(Operator operator, Condition condition); + + /** + * Adds new conditions to the qualify clause of query, connecting them with + * each other with operator. + * + * @param operator The operator to use to add the conditions to the existing + * conditions + * @param conditions The condition + */ + @Support({ H2 }) + void addQualify(Operator operator, Condition... conditions); + + /** + * Adds new conditions to the qualify clause of query, connecting them with + * each other with operator. + * + * @param operator The operator to use to add the conditions to the existing + * conditions + * @param conditions The condition + */ + @Support({ H2 }) + void addQualify(Operator operator, Collection conditions); + /** * Add an Oracle-style hint to the select clause. *

diff --git a/jOOQ/src/main/java/org/jooq/SelectWindowStep.java b/jOOQ/src/main/java/org/jooq/SelectWindowStep.java index d4d40d24ee..0b36fe90ce 100644 --- a/jOOQ/src/main/java/org/jooq/SelectWindowStep.java +++ b/jOOQ/src/main/java/org/jooq/SelectWindowStep.java @@ -117,7 +117,7 @@ import java.util.Collection; * * @author Lukas Eder */ -public interface SelectWindowStep extends SelectOrderByStep { +public interface SelectWindowStep extends SelectQualifyStep { /** * Add a WINDOW clause to the statement. @@ -142,7 +142,7 @@ public interface SelectWindowStep extends SelectOrderByStep * will inline window definitions where they are referenced. */ @Support({ CUBRID, FIREBIRD_3_0, H2, MARIADB, MYSQL_8_0, POSTGRES, SQLITE }) - SelectOrderByStep window(WindowDefinition... definitions); + SelectQualifyStep window(WindowDefinition... definitions); /** * Add a WINDOW clause to the statement. @@ -167,5 +167,5 @@ public interface SelectWindowStep extends SelectOrderByStep * will inline window definitions where they are referenced. */ @Support({ CUBRID, FIREBIRD_3_0, H2, MARIADB, MYSQL_8_0, POSTGRES, SQLITE }) - SelectOrderByStep window(Collection definitions); + SelectQualifyStep window(Collection definitions); } diff --git a/jOOQ/src/main/java/org/jooq/impl/Keywords.java b/jOOQ/src/main/java/org/jooq/impl/Keywords.java index 98d4f30e7c..3a0a371b2c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Keywords.java +++ b/jOOQ/src/main/java/org/jooq/impl/Keywords.java @@ -232,6 +232,7 @@ final class Keywords { static final Keyword K_PRIMARY_KEY = keyword("primary key"); static final Keyword K_PRIOR = keyword("prior"); static final Keyword K_PUBLIC = keyword("public"); + static final Keyword K_QUALIFY = keyword("qualify"); static final Keyword K_RAISE = keyword("raise"); static final Keyword K_RAISERROR = keyword("raiserror"); static final Keyword K_RECORD = keyword("record"); diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index 28463380e5..fa355afb32 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -1192,6 +1192,7 @@ final class ParserImpl implements Parser { List groupBy = null; Condition having = null; List windows = null; + Condition qualify = null; if (parseKeywordIf(ctx, "INTO")) into = parseTableName(ctx); @@ -1266,6 +1267,9 @@ final class ParserImpl implements Parser { if (parseKeywordIf(ctx, "WINDOW")) windows = parseWindowDefinitions(ctx); + if (parseKeywordIf(ctx, "QUALIFY")) + qualify = parseCondition(ctx); + SelectQueryImpl result = new SelectQueryImpl(ctx.dsl.configuration(), with); if (hints != null) result.addHint(hints); @@ -1306,6 +1310,9 @@ final class ParserImpl implements Parser { if (windows != null) result.addWindow(windows); + if (qualify != null) + result.addQualify(qualify); + if (limit != null) if (offset != null) result.addLimit(inline((int) (long) offset), inline((int) (long) limit)); @@ -9730,6 +9737,7 @@ final class ParserImpl implements Parser { "OFFSET", "ORDER BY", "PARTITION BY", + "QUALIFY", "RETURNING", "START WITH", "UNION", @@ -9778,6 +9786,7 @@ final class ParserImpl implements Parser { "ORDER BY", "OUTER APPLY", "PARTITION BY", + "QUALIFY", "RETURNING", "RIGHT ANTI JOIN", "RIGHT JOIN", diff --git a/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java b/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java index c2b35d388d..e209126ca8 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SelectImpl.java @@ -99,6 +99,7 @@ import org.jooq.SelectLimitPercentStep; import org.jooq.SelectOnConditionStep; import org.jooq.SelectOnStep; import org.jooq.SelectOptionalOnStep; +import org.jooq.SelectQualifyConditionStep; import org.jooq.SelectQuery; import org.jooq.SelectSeekLimitStep; import org.jooq.SelectSeekStep1; @@ -147,6 +148,7 @@ final class SelectImpl, SelectConnectByAfterStartWithStep, SelectHavingConditionStep, + SelectQualifyConditionStep, // [jooq-tools] START [implements-select-seek-step] SelectSeekStep1, SelectSeekStep2, @@ -422,6 +424,9 @@ final class SelectImpl conditions) { + conditionStep = ConditionStep.QUALIFY; + getQuery().addQualify(conditions); + return this; + } + + @Override + public final SelectImpl qualify(Field condition) { + return qualify(condition(condition)); + } + + @Override + public final SelectImpl qualify(SQL sql) { + return qualify(condition(sql)); + } + + @Override + public final SelectImpl qualify(String sql) { + return qualify(condition(sql)); + } + + @Override + public final SelectImpl qualify(String sql, Object... bindings) { + return qualify(condition(sql, bindings)); + } + + @Override + public final SelectImpl qualify(String sql, QueryPart... parts) { + return qualify(condition(sql, parts)); + } + @Override public final SelectImpl on(Condition conditions) { conditionStep = ConditionStep.ON; @@ -3878,6 +3932,12 @@ final class SelectImplHAVING clause that is * currently being added. */ - HAVING + HAVING, + + /** + * Additional conditions go to the QUALIFY clause that is + * currently being added. + */ + QUALIFY } } diff --git a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java index 374168666b..bcf125b983 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java @@ -115,6 +115,7 @@ import static org.jooq.impl.Keywords.K_OF; import static org.jooq.impl.Keywords.K_ORDER; import static org.jooq.impl.Keywords.K_ORDER_BY; import static org.jooq.impl.Keywords.K_PERCENT; +import static org.jooq.impl.Keywords.K_QUALIFY; import static org.jooq.impl.Keywords.K_SELECT; import static org.jooq.impl.Keywords.K_SIBLINGS; import static org.jooq.impl.Keywords.K_START_WITH; @@ -255,6 +256,7 @@ final class SelectQueryImpl extends AbstractResultQuery imp private QueryPartList groupBy; private final ConditionProviderImpl having; private WindowList window; + private final ConditionProviderImpl qualify; private final SortFieldList orderBy; private boolean orderBySiblings; private final QueryPartList> seek; @@ -291,6 +293,7 @@ final class SelectQueryImpl extends AbstractResultQuery imp this.connectBy = new ConditionProviderImpl(); this.connectByStartWith = new ConditionProviderImpl(); this.having = new ConditionProviderImpl(); + this.qualify = new ConditionProviderImpl(); this.orderBy = new SortFieldList(); this.seek = new QueryPartList>(); this.limit = new Limit(); @@ -1443,6 +1446,15 @@ final class SelectQueryImpl extends AbstractResultQuery imp context.end(SELECT_HAVING); + // QUALIFY clause + // ------------- + + if (getQualify().hasWhere()) + context.formatSeparator() + .visit(K_QUALIFY) + .sql(' ') + .visit(getQualify()); + // WINDOW clause // ------------- context.start(SELECT_WINDOW); @@ -2151,6 +2163,10 @@ final class SelectQueryImpl extends AbstractResultQuery imp return having; } + final ConditionProviderImpl getQualify() { + return qualify; + } + final SortFieldList getOrderBy() { return (unionOp.size() == 0) ? orderBy : unionOrderBy; } @@ -2397,6 +2413,36 @@ final class SelectQueryImpl extends AbstractResultQuery imp window.addAll(definitions); } + @Override + public final void addQualify(Condition conditions) { + getQualify().addConditions(conditions); + } + + @Override + public final void addQualify(Condition... conditions) { + getQualify().addConditions(conditions); + } + + @Override + public final void addQualify(Collection conditions) { + getQualify().addConditions(conditions); + } + + @Override + public final void addQualify(Operator operator, Condition conditions) { + getQualify().addConditions(operator, conditions); + } + + @Override + public final void addQualify(Operator operator, Condition... conditions) { + getQualify().addConditions(operator, conditions); + } + + @Override + public final void addQualify(Operator operator, Collection conditions) { + getQualify().addConditions(operator, conditions); + } + private final Select combine(CombineOperator op, Select other) { int index = unionOp.size() - 1;