我提供一个方法,它允许多个过滤选项对查询时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操作来代替这一点。
取而代之的where(true)
尝试where(trueCondition())
(trueCondition()
是一个静态方法DSL
)。
请注意,where(trueCondition())
假定您将增加额外的谓词您来allBookQuery
使用and(Condition)
,并会当然是错的,如果你使用or(Condition)
。不依赖于这一点,因此,甚至更好的使用where(noCondition())
,对于没有SQL呈现。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句