无值处理

无值处理

无值策略用于处理动态条件参数为 null 或空集合的场景。条件 lambda 中字面量 field == null 和 field != null 表示 SQL IS NULL 和 IS NOT NULL。

默认行为

动态条件值进入无值策略后,默认逻辑如下:

控制动态无值条件

使用 Kotlin 条件决定动态谓词是否参与查询。条件为 true 时用 .takeIf(...) 保留谓词,条件为 false 时用 .takeUnless(...) 保留谓词;缺失值需要显式 fallback 时使用普通 if/else 或 when。

val age: Int? = null
val namePattern: String? = null

where { (it.age == age).takeIf(age != null) }
where { (it.status == 0).takeUnless(includeInactive) }
where {
    if (namePattern != null) {
        it.name like namePattern
    } else {
        false.asSql()
    }
}
where { it.age.isNull.takeIf(age == null) }

takeIf/takeUnless 的 Boolean 参数以及 if/when 的条件都是普通 Kotlin 控制流,不会生成 SQL 谓词。

无值处理结果

Kronos 内部处理无值条件时会使用以下结果:

参数说明类型默认值
Ignore忽略该条件语句处理结果ignore
False条件语句为 false处理结果false
True条件语句为 true处理结果true
JudgeNull转换为 `is null` 或 `is not null`处理结果judgeNull
Auto使用 Kronos 默认策略处理结果auto