![](/img/trans.png)
[英]How can I safely proxy a lambda when I don't know whether it's a lambda up-front?
[英]In jooq, how do I add an "and" condition when don't know whether there is a "where" prior to it or not?
我有以下Jooq
代码:
if (...) {
query = query.where(...);
}
query = query.and(...);
问题是我不知道where()
语句是否会被添加。
如果不是,则将添加and()
部分而不会事先添加where()
,这应该是错误的。
我想知道如何解决这个问题。
我必须像下面这样写吗?
if (...) {
query = query.where(...);
}
else {
query = query.where(true); // This will make sure there is always a where statement, but it looks hacky...
}
query = query.and(...);
或者,我必须调整语句的顺序吗?
如果我想保留订单怎么办?
谢谢!
不要创建动态查询。 创造动态条件。
一如既往,假设这个 static 导入:
import static org.jooq.impl.DSL.*;
命令式
Condition condition = noCondition();
// And then
if (something1)
condition = condition.and(c1);
if (something2)
condition = condition.and(c2);
ctx.select(...)
.from(...)
.where(condition)
.fetch();
表达风格
ctx.select(...)
.from(...)
.where(something1 ? c1 : noCondition())
.and(something2 ? c2 : noCondition())
功能风格
ctx.select(...)
.from(...)
.where(Stream
.of(1, 2, 3)
.map(BOOK.ID::eq)
.reduce(noCondition(), Condition::or))
.fetch();
可能还有很多其他 styles。另请参阅:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.