【问题标题】:jOOQ - How to refer to the value of a field in a queryjOOQ - 如何在查询中引用字段的值
【发布时间】:2018-03-25 13:57:45
【问题描述】:

我有一个包含发布信息的数据库表。一个版本有一个次要版本号,它是一个整数。我想执行一个查询,从给定版本中读取值并返回表中所有字段的当前值(减去 ID),并且次要版本增加一。

下面的查询返回当前值,但我不知道如何将当前值加一。

select(Arrays
    .stream(V_RELEASE.fields())
    .filter(f -> !f.equals(V_RELEASE.ID))
    .map(f -> f.equals(V_RELEASE.MINOR_VERSION)
        ? V_RELEASE.MINOR_VERSION   <-- I want to increase this by 1
        : V_RELEASE.field(f))
    .collect(Collectors.toList()))
    .from(V_RELEASE)
    .where(V_RELEASE.ID.eq(id))

我确信这有一个非常简单的解决方案,但我已经把头撞在墙上几个小时了,但无济于事。任何人都可以用正确的语法帮助 jOOQ 新手吗?

添加上下文
我的查询基于对以下问题的答复:

JOOQ fetch record from table and insert it into another table

完整的查询应该是在现有记录的基础上创建一条新记录,次要版本增加 1,如下所示:

ctx
    .insertInto(V_RELEASE)
    .columns(Arrays.stream(V_RELEASE.fields())
        .filter(f -> !f.equals(V_RELEASE.ID))
        .collect(Collectors.toList()))
    .select(
        select(Arrays
            .stream(V_RELEASE.fields())
            .filter(f -> !f.equals(V_RELEASE.ID))
            .map(f -> f.equals(V_RELEASE.MINOR_VERSION)
                  // I want to increase the minor version number by 1
                ? V_RELEASE.MINOR_VERSION 

                : V_RELEASE.field(f))
            .collect(Collectors.toList()))
            .from(V_RELEASE)
            .where(V_RELEASE.ID.eq(id))
    )
    .execute();

【问题讨论】:

    标签: sql jooq


    【解决方案1】:

    稍后我发现了一些 RTFMing 答案非常简单。 jOOQ 支持arithmetic functions on columns,所以我只需在列上调用 .add(1) 即可到达我想要的位置:

    select(Arrays
        .stream(V_RELEASE.fields())
        .filter(f -> !f.equals(V_RELEASE.ID))
        .map(f -> f.equals(V_RELEASE.MINOR_VERSION)
            ? V_RELEASE.MINOR_VERSION.add(1)
            : V_RELEASE.field(f))
        .collect(Collectors.toList()))
        .from(V_RELEASE)
        .where(V_RELEASE.ID.eq(id))
    

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2015-04-15
      • 2018-07-17
      • 2014-11-30
      • 2021-10-24
      • 2015-06-13
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多