NOOP where in JOOQ

rharriso :

I'm providing a method that allow multiple filtering options when querying against the books table. The first portion of the query either searches over all books, or only those owned by the current user.

 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)
}

The .where(true) expression is used to keep the type of allBookQuery consistent, so that later I can append other conditions without checking for types.

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
}

Unfortunately the where(Boolean) method has been deprecated. So while this works for now, I'd like to replace this with a NOOP operation that isn't deprecated.

knutwannheden :

Instead of where(true) try where(trueCondition()) (trueCondition() is a static method of DSL).

Note that where(trueCondition()) assumes that you will add additional predicates to your allBookQuery using and(Condition) and would of course be wrong if you were to use or(Condition). To be independent of this it is thus even better to use where(noCondition()), for which no SQL is rendered.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=122652&siteId=1