[jOOQ/jOOQ#10522] Emulate UPDATE .. SET row = (SELECT ...) using UPDATE .. FROM

This commit is contained in:
Lukas Eder 2021-05-07 17:40:02 +02:00
parent 8f057e653d
commit 39f57b928d
4 changed files with 85 additions and 62 deletions

View File

@ -76,6 +76,12 @@ public interface UpdateQuery<R extends Record> extends StoreQuery<R>, ConditionP
/**
* Specify a multi-column set clause for the <code>UPDATE</code> statement.
*/
@Support
void addValues(RowN row, RowN value);
/**
* Specify a multi-column set clause for the <code>UPDATE</code> statement.
*/
@ -211,8 +217,8 @@ public interface UpdateQuery<R extends Record> extends StoreQuery<R>, ConditionP
/**
* Specify a multi-column set clause for the <code>UPDATE</code> statement.
*/
@Support
void addValues(RowN row, RowN value);
@Support({ H2, HSQLDB, POSTGRES })
void addValues(RowN row, Select<? extends Record> select);
/**
* Specify a multi-column set clause for the <code>UPDATE</code> statement.
@ -346,12 +352,6 @@ public interface UpdateQuery<R extends Record> extends StoreQuery<R>, ConditionP
@Support({ H2, HSQLDB, POSTGRES })
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22> void addValues(Row22<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22> row, Select<? extends Record22<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22>> select);
/**
* Specify a multi-column set clause for the <code>UPDATE</code> statement.
*/
@Support({ H2, HSQLDB, POSTGRES })
void addValues(RowN row, Select<?> select);
/**

View File

@ -37,15 +37,7 @@
*/
package org.jooq;
// ...
// ...
// ...
import static org.jooq.SQLDialect.H2;
// ...
import static org.jooq.SQLDialect.HSQLDB;
// ...
// ...
import static org.jooq.SQLDialect.POSTGRES;
import static org.jooq.SQLDialect.*;
import org.jetbrains.annotations.NotNull;
@ -65,6 +57,18 @@ import org.jetbrains.annotations.NotNull;
*/
public interface UpdateSetFirstStep<R extends Record> extends UpdateSetStep<R> {
/**
* Specify a multi-column set clause for the <code>UPDATE</code> statement.
* <p>
* This is emulated using a subquery for the <code>value</code>, where row
* value expressions on the right hand side aren't supported, or with an
* ordinary SET clause, where row value expressions on the left hand side
* aren't supported.
*/
@NotNull @CheckReturnValue
@Support
UpdateFromStep<R> set(RowN row, RowN value);
/**
* Specify a multi-column set clause for the <code>UPDATE</code> statement.
* <p>
@ -331,15 +335,10 @@ public interface UpdateSetFirstStep<R extends Record> extends UpdateSetStep<R> {
/**
* Specify a multi-column set clause for the <code>UPDATE</code> statement.
* <p>
* This is emulated using a subquery for the <code>value</code>, where row
* value expressions on the right hand side aren't supported, or with an
* ordinary SET clause, where row value expressions on the left hand side
* aren't supported.
*/
@NotNull @CheckReturnValue
@Support
UpdateFromStep<R> set(RowN row, RowN value);
@Support({ H2, HSQLDB, POSTGRES })
UpdateFromStep<R> set(RowN row, Select<? extends Record> select);
/**
* Specify a multi-column set clause for the <code>UPDATE</code> statement.
@ -495,11 +494,4 @@ public interface UpdateSetFirstStep<R extends Record> extends UpdateSetStep<R> {
@Support({ H2, HSQLDB, POSTGRES })
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22> UpdateFromStep<R> set(Row22<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22> row, Select<? extends Record22<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22>> select);
/**
* Specify a multi-column set clause for the <code>UPDATE</code> statement.
*/
@NotNull @CheckReturnValue
@Support({ H2, HSQLDB, POSTGRES })
UpdateFromStep<R> set(RowN row, Select<?> select);
}

View File

@ -172,6 +172,12 @@ final class UpdateImpl<R extends Record>
@Override
public final UpdateFromStep<R> set(RowN row, RowN value) {
getDelegate().addValues(row, value);
return this;
}
@Override
public final <T1> UpdateFromStep<R> set(Row1<T1> row, Row1<T1> value) {
getDelegate().addValues(row, value);
@ -305,8 +311,8 @@ final class UpdateImpl<R extends Record>
}
@Override
public final UpdateFromStep<R> set(RowN row, RowN value) {
getDelegate().addValues(row, value);
public final UpdateFromStep<R> set(RowN row, Select<? extends Record> select) {
getDelegate().addValues(row, select);
return this;
}
@ -442,12 +448,6 @@ final class UpdateImpl<R extends Record>
return this;
}
@Override
public final UpdateFromStep<R> set(RowN row, Select<?> select) {
getDelegate().addValues(row, select);
return this;
}
@Override

View File

@ -68,12 +68,14 @@ import static org.jooq.SQLDialect.POSTGRES;
// ...
// ...
// ...
// ...
import static org.jooq.SQLDialect.SQLITE;
// ...
// ...
// ...
// ...
import static org.jooq.conf.SettingsTools.getExecuteUpdateWithoutWhere;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.row;
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.trueCondition;
@ -84,10 +86,12 @@ import static org.jooq.impl.Keywords.K_ROW;
import static org.jooq.impl.Keywords.K_SET;
import static org.jooq.impl.Keywords.K_UPDATE;
import static org.jooq.impl.Keywords.K_WHERE;
import static org.jooq.impl.Tools.fieldName;
import static org.jooq.impl.Tools.visitSubquery;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -166,6 +170,8 @@ final class UpdateQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
private static final Set<SQLDialect> SUPPORT_RVE_SET = SQLDialect.supportedBy(H2, HSQLDB, POSTGRES);
private static final Set<SQLDialect> REQUIRE_RVE_ROW_CLAUSE = SQLDialect.supportedBy(POSTGRES);
@ -200,6 +206,11 @@ final class UpdateQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
@Override
public final void addValues(RowN row, RowN value) {
addValues0(row, value);
}
@Override
public final <T1> void addValues(Row1<T1> row, Row1<T1> value) {
addValues0(row, value);
@ -311,8 +322,8 @@ final class UpdateQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
}
@Override
public final void addValues(RowN row, RowN value) {
addValues0(row, value);
public final void addValues(RowN row, Select<? extends Record> select) {
addValues0(row, select);
}
@Override
@ -425,11 +436,6 @@ final class UpdateQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
addValues0(row, select);
}
@Override
public final void addValues(RowN row, Select<?> select) {
addValues0(row, select);
}
final void addValues0(Row row, Row value) {
@ -552,14 +558,6 @@ final class UpdateQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
ctx.formatSeparator()
.start(UPDATE_SET)
.visit(K_SET)
@ -584,6 +582,24 @@ final class UpdateQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
.visit(map)
.formatIndentEnd();
}
else {
ctx.start(UPDATE_SET_ASSIGNMENT)
.formatIndentStart()
@ -645,14 +661,7 @@ final class UpdateQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
default:
ctx.start(UPDATE_FROM);
if (!from.isEmpty())
ctx.formatSeparator()
.visit(K_FROM).sql(' ')
.declareTables(true, c -> c.visit(from));
ctx.end(UPDATE_FROM);
acceptFrom(ctx);
break;
}
@ -707,6 +716,28 @@ final class UpdateQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
ctx.end(UPDATE_RETURNING);
}
private final void acceptFrom(Context<?> ctx) {
ctx.start(UPDATE_FROM);
TableList f;
f = from;
if (!f.isEmpty())
ctx.formatSeparator()
.visit(K_FROM).sql(' ')
.declareTables(true, c -> c.visit(f));
ctx.end(UPDATE_FROM);
}
@Override
public final Clause[] clauses(Context<?> ctx) {
return CLAUSES;