From cab5e0d78b900de2a824d4cd7e880bc967682e6c Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 26 Apr 2023 17:04:48 +0200 Subject: [PATCH] [jOOQ/jOOQ#14985] Allow for specifying explicit path joins - Implement this for INNER JOIN and LEFT JOIN --- .../java/org/jooq/impl/DefaultRenderContext.java | 16 +++++++++++++--- jOOQ/src/main/java/org/jooq/impl/JoinTable.java | 14 ++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultRenderContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultRenderContext.java index 9c496b477e..22a487d966 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultRenderContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultRenderContext.java @@ -227,9 +227,19 @@ class DefaultRenderContext extends AbstractContext implements Ren Table child = root; List> tables = new ArrayList<>(); - while (root instanceof TableImpl && (child = ((TableImpl) root).child) != null) { - tables.add(root); - root = child; + // [#14985] Traverse paths only when referencing paths (!forceNew), + // not when declaring them in FROM (forceNew) + if (!forceNew) { + while (root instanceof TableImpl && (child = ((TableImpl) root).child) != null) { + + // [#14985] If a subpath has been declared in FROM, join + // to that, instead of the root. + if (scopeStack.get(root) != null) + break; + + tables.add(root); + root = child; + } } e = forceNew diff --git a/jOOQ/src/main/java/org/jooq/impl/JoinTable.java b/jOOQ/src/main/java/org/jooq/impl/JoinTable.java index d6be917b04..93c481aaf1 100755 --- a/jOOQ/src/main/java/org/jooq/impl/JoinTable.java +++ b/jOOQ/src/main/java/org/jooq/impl/JoinTable.java @@ -566,14 +566,20 @@ implements emulateNaturalLeftOuterJoin(ctx) || emulateNaturalRightOuterJoin(ctx) || emulateNaturalFullOuterJoin(ctx)) { - toSQLJoinCondition(ctx, naturalCondition()); } - // Regular JOIN condition - else { - toSQLJoinCondition(ctx, condition); + // [#14985] Path joins additional conditions + else if (rhs instanceof TableImpl && ((TableImpl) rhs).child != null) { + toSQLJoinCondition(ctx, new Join(((TableImpl) rhs).child, rhs) + .onKey(((TableImpl) rhs).childPath) + .condition.getWhere().and(condition.getWhere()) + ); } + + // Regular JOIN condition + else + toSQLJoinCondition(ctx, condition); } @SuppressWarnings({ "rawtypes", "unchecked" })