I'm using Jooq and am trying to generate a near copy of a data set within the same table. In the process I want to update the value of one field to a known value. I've been looking at the docs & trying variations with no luck yet. Here is my approach updating the REGISTRATION table and setting the 'stage' field to the value 6 (where it was 5). So I'll end up with the original data plus a duplicate set with just the different stage value. in pseudo code
我正在使用Jooq并尝试在同一个表中生成数据集的近似副本。在此过程中,我想将一个字段的值更新为已知值。我一直在看文档和尝试变化,但没有运气。这是我更新REGISTRATION表并将'stage'字段设置为值6(它为5)的方法。所以我最终会得到原始数据加上一个只有不同阶段值的重复集合。在伪代码中
insert into Registration (select * from Registration where stage=5) set stage=6
I tried this code below and thinking I could add a ".set(...)" method to set the value but that doesn't seem to be valid.
我尝试下面的代码,并认为我可以添加一个“.set(...)”方法来设置值,但似乎没有效果。
create.insertInto(REGISTRATION)
.select(
(selectFrom(REGISTRATION)
.where(REGISTRATION.STAGE.eq(5))
)
).execute();
2 个解决方案
#1
3
I'm not aware of a database that supports an INSERT .. SELECT .. SET
syntax, and if there were such a syntax, it certainly isn't SQL standards compliant. The way forward here would be to write:
我不知道一个支持INSERT ... SELECT ... SET语法的数据库,如果有这样的语法,它肯定不符合SQL标准。这里的前进方向是写:
In SQL:
INSERT INTO registration (col1, col2, col3, stage, col4, col5)
SELECT col1, col2, col3, 6, col4, col5
FROM registration
WHERE stage = 5;
In jOOQ:
create.insertInto(REGISTRATION)
.columns(
REGISTRATION.COL1,
REGISTRATION.COL2,
REGISTRATION.COL3,
REGISTRATION.STAGE,
REGISTRATION.COL4,
REGISTRATION.COL5)
.select(
select(
REGISTRATION.COL1,
REGISTRATION.COL2,
REGISTRATION.COL3,
val(6),
REGISTRATION.COL4,
REGISTRATION.COL5)
.from(REGISTRATION)
.where(REGISTRATION.STAGE.eq(5)))
.execute();
The following static import is implied:
隐含以下静态导入:
import static org.jooq.impl.DSL.*;
In jOOQ, dynamically
Since you're looking for a dynamic SQL solution, here's how this could be done:
既然您正在寻找动态SQL解决方案,那么这是如何做到的:
static <T> int copy(
DSLContext create, Table<?> table, Field<T> field,
T oldValue, T newValue
) {
List<Field<?>> into = new ArrayList<>();
List<Field<?>> from = new ArrayList<>();
into.addAll(Stream.of(table.fields())
.filter(f -> !field.equals(f))
.collect(toList()));
from.addAll(into);
into.add(field);
from.add(val(newValue));
return
create.insertInto(table)
.columns(into)
.select(
select(from)
.from(table)
.where(field.eq(oldValue))
.execute();
}
#2
2
Thanks Lukas for your answer which I'll use a version of as it's nice and general. My own answer which I just got to work is less general but might be a useful reference for other people who come this way especially as it takes account of the identity field "id" which can otherwise cause problems.
感谢Lukas的答案,我将使用一个版本,因为它很好,一般。我刚才开始工作的答案不那么通用,但对于其他人来说这可能是一个有用的参考,特别是考虑到身份字段“id”,否则会导致问题。
public void duplicate(int baseStage, int newStage) {
Field<?>[] allFieldsExceptId = Stream.of(REGISTRATION.fields())
.filter(field -> !field.getName().equals("id"))
.toArray(Field[]::new);
Field<?>[] newFields = Stream.of(allFieldsExceptId).map(field -> {
if (field.getName().contentEquals("stage")) {
return val(newStage);
} else {
return field;
}
}).toArray(Field[]::new);
create.insertInto(REGISTRATION)
.columns(allFieldsExceptId)
.select(
select(newFields)
.from(REGISTRATION)
.where(REGISTRATION.STAGE.eq(baseStage)))
.execute();
}
#1
3
I'm not aware of a database that supports an INSERT .. SELECT .. SET
syntax, and if there were such a syntax, it certainly isn't SQL standards compliant. The way forward here would be to write:
我不知道一个支持INSERT ... SELECT ... SET语法的数据库,如果有这样的语法,它肯定不符合SQL标准。这里的前进方向是写:
In SQL:
INSERT INTO registration (col1, col2, col3, stage, col4, col5)
SELECT col1, col2, col3, 6, col4, col5
FROM registration
WHERE stage = 5;
In jOOQ:
create.insertInto(REGISTRATION)
.columns(
REGISTRATION.COL1,
REGISTRATION.COL2,
REGISTRATION.COL3,
REGISTRATION.STAGE,
REGISTRATION.COL4,
REGISTRATION.COL5)
.select(
select(
REGISTRATION.COL1,
REGISTRATION.COL2,
REGISTRATION.COL3,
val(6),
REGISTRATION.COL4,
REGISTRATION.COL5)
.from(REGISTRATION)
.where(REGISTRATION.STAGE.eq(5)))
.execute();
The following static import is implied:
隐含以下静态导入:
import static org.jooq.impl.DSL.*;
In jOOQ, dynamically
Since you're looking for a dynamic SQL solution, here's how this could be done:
既然您正在寻找动态SQL解决方案,那么这是如何做到的:
static <T> int copy(
DSLContext create, Table<?> table, Field<T> field,
T oldValue, T newValue
) {
List<Field<?>> into = new ArrayList<>();
List<Field<?>> from = new ArrayList<>();
into.addAll(Stream.of(table.fields())
.filter(f -> !field.equals(f))
.collect(toList()));
from.addAll(into);
into.add(field);
from.add(val(newValue));
return
create.insertInto(table)
.columns(into)
.select(
select(from)
.from(table)
.where(field.eq(oldValue))
.execute();
}
#2
2
Thanks Lukas for your answer which I'll use a version of as it's nice and general. My own answer which I just got to work is less general but might be a useful reference for other people who come this way especially as it takes account of the identity field "id" which can otherwise cause problems.
感谢Lukas的答案,我将使用一个版本,因为它很好,一般。我刚才开始工作的答案不那么通用,但对于其他人来说这可能是一个有用的参考,特别是考虑到身份字段“id”,否则会导致问题。
public void duplicate(int baseStage, int newStage) {
Field<?>[] allFieldsExceptId = Stream.of(REGISTRATION.fields())
.filter(field -> !field.getName().equals("id"))
.toArray(Field[]::new);
Field<?>[] newFields = Stream.of(allFieldsExceptId).map(field -> {
if (field.getName().contentEquals("stage")) {
return val(newStage);
} else {
return field;
}
}).toArray(Field[]::new);
create.insertInto(REGISTRATION)
.columns(allFieldsExceptId)
.select(
select(newFields)
.from(REGISTRATION)
.where(REGISTRATION.STAGE.eq(baseStage)))
.execute();
}