NOOP在JOOQ中

rha*_*iso 3 java jooq kotlin

我提供一种在对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)
}
Run Code Online (Sandbox Code Playgroud)

.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
}
Run Code Online (Sandbox Code Playgroud)

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

knu*_*den 5

代替where(true)try where(trueCondition())trueCondition()是的静态方法DSL)。

请注意,where(trueCondition())假设您要在allBookQuery使用中添加其他谓词,and(Condition)并且如果要使用当然会是错误的or(Condition)。独立于此,因此最好使用where(noCondition()),而不会呈现任何SQL。