From dbc4b6b402955ff733d64a75b93cc16238dd2b71 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 16 Sep 2020 17:03:55 +0200 Subject: [PATCH] [jOOQ/jOOQ#7312] Add Setting to transform table lists to ansi join --- .../org/jooq/impl/ConditionProviderImpl.java | 4 + .../main/java/org/jooq/impl/JoinTable.java | 10 + .../java/org/jooq/impl/SelectQueryImpl.java | 194 ++++++++++++++++-- .../main/java/org/jooq/impl/TableList.java | 5 + 4 files changed, 197 insertions(+), 16 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/ConditionProviderImpl.java b/jOOQ/src/main/java/org/jooq/impl/ConditionProviderImpl.java index aef7f455cc..a574395945 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ConditionProviderImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ConditionProviderImpl.java @@ -69,6 +69,10 @@ final class ConditionProviderImpl extends AbstractQueryPart implements Condition return hasWhere() ? condition : noCondition(); } + final void setWhere(Condition newCondition) { + this.condition = newCondition; + } + final boolean hasWhere() { return condition != null && !(condition instanceof NoCondition); } diff --git a/jOOQ/src/main/java/org/jooq/impl/JoinTable.java b/jOOQ/src/main/java/org/jooq/impl/JoinTable.java index 6711990430..699b5daf38 100755 --- a/jOOQ/src/main/java/org/jooq/impl/JoinTable.java +++ b/jOOQ/src/main/java/org/jooq/impl/JoinTable.java @@ -198,6 +198,16 @@ implements this.using = new QueryPartList<>(); } + JoinTable transform(Table newLhs, Table newRhs) { + if (lhs == newLhs && rhs == newRhs) + return this; + + JoinTable result = new JoinTable(newLhs, newRhs, type); + + // TODO: Retain partitionBy clause + return !using.isEmpty() ? result.using(using) : result.on(condition); + } + // ------------------------------------------------------------------------ // Table API // ------------------------------------------------------------------------ diff --git a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java index 2a9cc4576a..a7274d399a 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java @@ -56,6 +56,9 @@ import static org.jooq.Clause.SELECT_UNION; import static org.jooq.Clause.SELECT_UNION_ALL; import static org.jooq.Clause.SELECT_WHERE; import static org.jooq.Clause.SELECT_WINDOW; +import static org.jooq.JoinType.JOIN; +import static org.jooq.JoinType.LEFT_OUTER_JOIN; +import static org.jooq.JoinType.RIGHT_OUTER_JOIN; import static org.jooq.Operator.OR; // ... // ... @@ -107,6 +110,7 @@ import static org.jooq.impl.DSL.jsonObject; import static org.jooq.impl.DSL.jsonbArrayAgg; import static org.jooq.impl.DSL.jsonbObject; import static org.jooq.impl.DSL.name; +import static org.jooq.impl.DSL.noCondition; import static org.jooq.impl.DSL.one; import static org.jooq.impl.DSL.orderBy; import static org.jooq.impl.DSL.regexpReplaceAll; @@ -169,10 +173,13 @@ import static org.jooq.impl.Tools.DataKey.DATA_TOP_LEVEL_CTE; import static org.jooq.impl.Tools.DataKey.DATA_WINDOW_DEFINITIONS; import java.sql.ResultSetMetaData; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Deque; import java.util.EnumSet; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -1667,8 +1674,6 @@ final class SelectQueryImpl extends AbstractResultQuery imp - - tablelist = transformInlineDerivedTables(tablelist, where); context.formatSeparator() @@ -1980,9 +1985,8 @@ final class SelectQueryImpl extends AbstractResultQuery imp result.add(t.table()); where.addConditions(t.condition()); } - else if (table instanceof JoinTable) { + else if (table instanceof JoinTable) result.add(transformInlineDerivedTables0(table, where)); - } else result.add(table); } @@ -1994,19 +1998,11 @@ final class SelectQueryImpl extends AbstractResultQuery imp where.addConditions(t.condition()); return t.table(); } - else if (table instanceof JoinTable) { - JoinTable join = (JoinTable) table; - JoinTable result = new JoinTable( - transformInlineDerivedTables0(join.lhs, where), - transformInlineDerivedTables0(join.rhs, where), - join.type + else if (table instanceof JoinTable) + return ((JoinTable) table).transform( + transformInlineDerivedTables0(((JoinTable) table).lhs, where), + transformInlineDerivedTables0(((JoinTable) table).rhs, where) ); - - if (!join.using.isEmpty()) - return result.using(join.using); - else - return result.on(join.condition); - } else return table; } @@ -2081,6 +2077,172 @@ final class SelectQueryImpl extends AbstractResultQuery imp + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/jOOQ/src/main/java/org/jooq/impl/TableList.java b/jOOQ/src/main/java/org/jooq/impl/TableList.java index 7aea8b0921..0297463f2d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/TableList.java +++ b/jOOQ/src/main/java/org/jooq/impl/TableList.java @@ -69,6 +69,11 @@ final class TableList extends QueryPartList> { super(wrappedList); } + @SafeVarargs + TableList(Table... wrappedList) { + super(wrappedList); + } + @Override public final boolean rendersContent(Context ctx) { return true;