简体   繁体   English

在jooq中设置NULL值

[英]Set NULL value in jooq

I have an big query and my problem is setting NULL values using jooq.我有一个很大的查询,我的问题是使用 jooq 设置NULL值。 For example, I have this piece of sql query:例如,我有这样一段 sql 查询:

IF(t.PANEL_ID IS NULL, t.ID, NULL) AS testId

If transform this into jooq implementation something like this will come out:如果将其转换为 jooq 实现,则会出现以下内容:

when(TEST.PANEL_ID.isNull(), TEST.ID).otherwise(null)).as("testId")

but this is ambiguous method call.但这是模棱两可的方法调用。

I made some research, and find this snippet:我做了一些研究,找到了这个片段:

DSL.val((String) null)

but it didn't work, because it cannot resolve method with jooq.Param<String> .但它不起作用,因为它无法使用jooq.Param<String>解析方法。

How should I proceed?我应该如何进行?

Your NULL expression must be of the same type as your TEST.ID column.您的NULL表达式必须与TEST.ID列的类型相同。 I would imagine this is not a String column, but some numeric one.我想这不是一个String列,而是一些数字列。 Irrespective of the actual data type, you can always create a bind value using the data type of another expression, eg无论实际数据类型如何,您始终可以使用另一个表达式的数据类型创建绑定值,例如

// Bind variable:
DSL.val(null, TEST.ID)

// Inline value / NULL literal
DSL.inline(null, TEST.ID)

If you're doing this a lot, you could also extract your own utility like this:如果您经常这样做,您还可以像这样提取自己的实用程序:

public static <T> util(Field<?> nullable, Field<T> other) {
    return when(nullable.isNull(), other).otherwise(inline(null, other));
}

Notice, jOOQ has a built-in method NVL2 for this purpose:请注意,为此,jOOQ 有一个内置方法NVL2

nvl2(TEST.PANEL_ID, inline(null, TEST.ID), TEST.ID).as("testId")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM