簡體   English   中英

jOOQ查詢中的重復次數更少

[英]Less repetition in jOOQ query

關於如何以更少的重復次數定義以下jOOQ查詢的任何想法?

我正在使用jOOQ 3.11.4。

db.insertInto(ACCOUNT,
        ACCOUNT.ACCOUNT_ID,
        ACCOUNT.EMAIL,
        ACCOUNT.FIRST_NAME,
        ACCOUNT.LAST_NAME,
        ACCOUNT.IS_ADMIN,
        ACCOUNT.PASSWORD)
        .values(account.accountId,
            account.email,
            account.firstName,
            account.lastName,
            account.isAdmin,
            account.password)
        .onConflict(ACCOUNT.ACCOUNT_ID)
        .doUpdate()
        .set(ACCOUNT.EMAIL, account.email)
        .set(ACCOUNT.FIRST_NAME, account.firstName)
        .set(ACCOUNT.LAST_NAME, account.lastName)
        .set(ACCOUNT.IS_ADMIN, account.isAdmin)
        .set(ACCOUNT.PASSWORD, account.password)
        .returning(
            ACCOUNT.ACCOUNT_ID,
            ACCOUNT.EMAIL,
            ACCOUNT.FIRST_NAME,
            ACCOUNT.LAST_NAME,
            ACCOUNT.IS_ADMIN,
            ACCOUNT.PASSWORD
        )
        .fetchOne()

(我發現我的問題主要是代碼,StackOverflow不允許我按原樣發布它,而沒有添加更多細節,我認為這對我的問題不是必需的,但是他們希望我發布更多文本,我現在正在通過鍵入此消息進行操作,希望您不必讀到最后。)

由於將所有列傳遞給insert語句,因此可以這樣編寫:

// Create an AccountRecord that contains your POJO data
Record rec = db.newRecord(ACCOUNT);
rec.from(account);

// Don't pass the columns to the insert statement explicitly
db.insertInto(ACCOUNT)

// But pass the record to the set method. It will use all the changed values
  .set(rec)

// Use the MySQL syntax, which can be emulated on PostgreSQL using ON CONFLICT
  .onDuplicateKeyUpdate()

// But pass the record to the set method again
  .set(rec)

// Don't specify any columns to the returning clause. It will take all the ACCOUNT columns
  .returning()
  .fetchOne();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM