From 1519ead0c20fa4f0de7e714e30e0cd165c485fed Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 25 Feb 2022 11:23:17 +0100 Subject: [PATCH] [jOOQ/jOOQ#13069] Support InlineDerivedTables --- .../java/org/jooq/impl/DefaultBinding.java | 20 ++++++++++++------- .../main/java/org/jooq/impl/DerivedTable.java | 8 ++++++-- .../org/jooq/impl/InlineDerivedTable.java | 18 +++++------------ .../java/org/jooq/impl/SelectQueryImpl.java | 10 +++++----- .../main/java/org/jooq/impl/TableImpl.java | 10 ++++++++++ 5 files changed, 39 insertions(+), 27 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultBinding.java b/jOOQ/src/main/java/org/jooq/impl/DefaultBinding.java index ac2bdeba66..ccc50e6841 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultBinding.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultBinding.java @@ -152,6 +152,7 @@ import static org.jooq.impl.Tools.emulateMultiset; import static org.jooq.impl.Tools.enums; // ... import static org.jooq.impl.Tools.getMappedUDTName; +import static org.jooq.impl.Tools.isEmpty; import static org.jooq.impl.Tools.map; import static org.jooq.impl.Tools.needsBackslashEscaping; import static org.jooq.impl.Tools.newRecord; @@ -3779,10 +3780,12 @@ public class DefaultBinding implements Binding { } static final R readMultiset(BindingGetResultSetContext ctx, DataType type) throws SQLException { - return DefaultResultBinding.readMultiset(ctx, (AbstractRow) type.getRow(), type.getType(), - s -> s.startsWith("[") || s.startsWith("{") ? "[" + s + "]" : s, - s -> s.startsWith("<") ? "" + s + "" : s - ).get(0); + Result result = DefaultResultBinding.readMultiset(ctx, (AbstractRow) type.getRow(), type.getType(), + s -> s != null && (s.startsWith("[") || s.startsWith("{")) ? "[" + s + "]" : s, + s -> s != null && (s.startsWith("<")) ? "" + s + "" : s + ); + + return isEmpty(result) ? null : result.get(0); } @Override @@ -4019,16 +4022,19 @@ public class DefaultBinding implements Binding { case JSON: case JSONB: - if (emulation == NestedCollectionEmulation.JSONB && EMULATE_AS_BLOB.contains(ctx.dialect())) + if (emulation == NestedCollectionEmulation.JSONB && EMULATE_AS_BLOB.contains(ctx.dialect())) { + byte[] bytes = ctx.resultSet().getBytes(ctx.index()); return apply( - jsonStringPatch.apply(Source.of(ctx.resultSet().getBytes(ctx.index()), ctx.configuration().charsetProvider().provide()).readString()), + jsonStringPatch.apply(bytes == null ? null : Source.of(bytes, ctx.configuration().charsetProvider().provide()).readString()), s -> readMultisetJSON(ctx, row, recordType, s) ); - else + } + else { return apply( jsonStringPatch.apply(ctx.resultSet().getString(ctx.index())), s -> readMultisetJSON(ctx, row, recordType, s) ); + } case XML: return apply( diff --git a/jOOQ/src/main/java/org/jooq/impl/DerivedTable.java b/jOOQ/src/main/java/org/jooq/impl/DerivedTable.java index 3724321d6d..99cbd07b82 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DerivedTable.java +++ b/jOOQ/src/main/java/org/jooq/impl/DerivedTable.java @@ -39,7 +39,7 @@ package org.jooq.impl; // ... -import static org.jooq.impl.Names.N_SELECT; +import static org.jooq.impl.Names.N_T; import static org.jooq.impl.Tools.visitSubquery; import org.jooq.Clause; @@ -59,7 +59,11 @@ class DerivedTable extends AbstractTable implements QOM.Der private final Select query; DerivedTable(Select query) { - super(TableOptions.expression(), N_SELECT); + this(query, N_T); + } + + DerivedTable(Select query, Name name) { + super(TableOptions.expression(), name); this.query = query; } diff --git a/jOOQ/src/main/java/org/jooq/impl/InlineDerivedTable.java b/jOOQ/src/main/java/org/jooq/impl/InlineDerivedTable.java index 3442bc08c2..41ba2de92a 100644 --- a/jOOQ/src/main/java/org/jooq/impl/InlineDerivedTable.java +++ b/jOOQ/src/main/java/org/jooq/impl/InlineDerivedTable.java @@ -54,22 +54,14 @@ import org.jetbrains.annotations.NotNull; */ final class InlineDerivedTable extends DerivedTable { - private final Table table; - private final Condition where; + final Table table; + final Condition condition; - InlineDerivedTable(Table table, Condition where) { - super(selectFrom(table).where(where)); + InlineDerivedTable(Table table, Condition condition) { + super(selectFrom(table).where(condition), table.getUnqualifiedName()); this.table = table; - this.where = where; - } - - final Table table() { - return table; - } - - final Condition condition() { - return where; + this.condition = condition; } @Override diff --git a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java index 85516f28ec..00ad5b1acc 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java @@ -2526,8 +2526,8 @@ final class SelectQueryImpl extends AbstractResultQuery imp private final void transformInlineDerivedTable0(Table table, TableList result, ConditionProviderImpl where) { if (table instanceof InlineDerivedTable) { InlineDerivedTable t = (InlineDerivedTable) table; - result.add(t.table()); - where.addConditions(t.condition()); + result.add(t.table); + where.addConditions(t.condition); } else if (table instanceof JoinTable) result.add(transformInlineDerivedTables0(table, where, false)); @@ -2538,10 +2538,10 @@ final class SelectQueryImpl extends AbstractResultQuery imp private final Table transformInlineDerivedTables0(Table table, ConditionProviderImpl where, boolean keepDerivedTable) { if (table instanceof InlineDerivedTable) { InlineDerivedTable t = (InlineDerivedTable) table; if (keepDerivedTable) - return t.query().asTable(t.table()); + return t.query().asTable(t.table); - where.addConditions(t.condition()); - return t.table(); + where.addConditions(t.condition); + return t.table; } else if (table instanceof JoinTable) { JoinTable j = (JoinTable) table; Table lhs; diff --git a/jOOQ/src/main/java/org/jooq/impl/TableImpl.java b/jOOQ/src/main/java/org/jooq/impl/TableImpl.java index 8ecdafbfab..7449dd272a 100644 --- a/jOOQ/src/main/java/org/jooq/impl/TableImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/TableImpl.java @@ -306,6 +306,8 @@ implements + + } throw new DataAccessException("The to-one ROW convenience feature is available in the commercial jOOQ distribution only. Please consider upgrading to the jOOQ Professional Edition or jOOQ Enterprise Edition."); @@ -339,6 +341,8 @@ implements + + } throw new DataAccessException("The one-to-many MULTISET convenience feature is available in the commercial jOOQ distribution only. Please consider upgrading to the jOOQ Professional Edition or jOOQ Enterprise Edition."); @@ -373,6 +377,8 @@ implements + + } throw new DataAccessException("The many-to-many MULTISET convenience feature is available in the commercial jOOQ distribution only. Please consider upgrading to the jOOQ Professional Edition or jOOQ Enterprise Edition."); @@ -475,6 +481,10 @@ implements + + + +