【问题标题】:NOOP where in JOOQNOOP 在 JOOQ 的什么地方
【发布时间】:2019-11-18 04:39:32
【问题描述】:

我提供了一种在查询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 操作。

【问题讨论】:

标签: java kotlin jooq


【解决方案1】:

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

请注意,where(trueCondition()) 假定您将使用 and(Condition) 向您的 allBookQuery 添加额外的谓词,如果您要使用 or(Condition),那当然是错误的。因此,为了独立于此,最好使用where(noCondition()),因为它不会呈现任何 SQL。

【讨论】:

    猜你喜欢
    • 2020-11-17
    • 2012-09-06
    • 2012-09-17
    • 1970-01-01
    • 2017-08-13
    • 2011-01-05
    • 2020-12-17
    • 2014-06-30
    • 1970-01-01
    相关资源
    最近更新 更多