在JOOQ中

时间:2019-07-08 16:06:38

标签: java kotlin jooq

我提供一种在对books表进行查询时允许多个过滤选项的方法。查询的第一部分要么搜索所有书籍,要么仅搜索当前用户拥有的书籍。

 val allBookQuery = if(searchParameters.isOnShelf == true) {
    val context = environment.getContext<AuthorizedContext>()
    context?.currentUser ?: throw GraphQLException("No Current User Authenticated")
    val userUUID = UUID.fromString(context.currentUser.uuid)

    select.from(BOOKS, USER_BOOKS, USERS)
    .where(USERS.UUID.eq(userUUID))
    .and(USERS.ID.eq(USER_BOOKS.USER_ID))
    .and(USER_BOOKS.BOOK_ID.eq(BOOKS.ID))
    .and(BOOKS.IS_ARCHIVED.eq(false))
} else {
    select.from(BOOKS).where(true)
}

.where(true)表达式用于使allBookQuery的类型保持一致,以便以后我可以附加其他条件而无需检查类型。

val filteredBooksQuery = if (searchParameters.bookIds != null) {
    allBookQuery.and(BOOKS.ID.`in`(searchParameters.bookIds))
} else {
    allBookQuery
}

val finalQuery = if (searchParameters.isAcademic == true) {
    filteredBooksQuery.and(BOOKS.IS_ACADEMIC.eq((true)))
} else {
    filteredBooksQuery
}

不幸的是,where(Boolean)方法已被弃用。因此,尽管目前可以使用,但我想用不推荐使用的NOOP操作代替它。

1 个答案:

答案 0 :(得分:4)

请尝试使用where(true)where(trueCondition())trueCondition()的静态方法)来代替DSL

请注意,where(trueCondition())假定您将使用allBookQueryand(Condition)添加其他谓词,并且如果要使用or(Condition)当然是错误的。因此,独立于此,最好使用where(noCondition()),因为它不呈现任何SQL。

相关问题