2017-10-13 79 views
1

我有一個DB表,其中包含有關發佈的信息。發行版有一個次要版本號,它是一個整數。我想執行一個查詢,它讀取給定發行版中的值,並返回表中所有字段的當前值(減去ID),次要版本增加1。jOOQ - 如何引用查詢中的字段值

以下查詢返回當前值,但我無法弄清楚如何取當前值並將其增加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 <-- 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(); 

回答

1

稍後我發現一個RTFMing,我發現答案是bea美麗的簡單。 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))