Skip to content

isEqualTo run before filter ? #657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pondered opened this issue Aug 11, 2023 · 7 comments
Closed

isEqualTo run before filter ? #657

pondered opened this issue Aug 11, 2023 · 7 comments

Comments

@pondered
Copy link

employeeId = null

mapper.select(dsl -> dsl
            .where(id, isEqualTo(()->employeeId).filter(Objects.nonNull(employeeId))));

Does not run when employee is null. but throw exception, Is there any other way.

@pondered
Copy link
Author

and

mapper.select(dsl -> dsl
            .where(id, isEqualTo(()->dto.getEmployeeId()).filter(Objects.nonNull(dot))));

@jeffgbutler
Copy link
Member

In your first example, the exception thrown is probably NonRenderingWhereClauseException. This exception is thrown because you coded a where clause, but the condition didn't render which in this case will cause all rows to be returned. See this page for details about how to configure this behavior: https://mybatis.org/mybatis-dynamic-sql/docs/configuration.html. You can write your statement like this:

mapper.select(dsl -> dsl.configureStatement(c -> c.setNonRenderingWhereClauseAllowed(true))
    .where(id, isEqualTo(employeeId).filter(Objects::nonNull)));

@jeffgbutler
Copy link
Member

In your second example, I assume you get a NullPointerException if dto is null. This because the filter can only be applied to the value in the condition - not the object providing the value. You can use a combination of filter and map to achieve your goal:

mapper.select(dsl -> dsl.configureStatement(c -> c.setNonRenderingWhereClauseAllowed(true))
    .where(id, isEqualTo(dto).filter(Objects::nonNull).map(MyDTO::getEmployeeId).filter(Objects::nonNull)));

@pondered
Copy link
Author

oh tanks

@pondered
Copy link
Author

hi, isIn not support this. just like

mapper.select(dsl -> dsl.configureStatement(c -> c.setNonRenderingWhereClauseAllowed(true))
    .where(id, isIn(dto).filter(Objects::nonNull).map(Dto::list)));

isIn(dto).filter(Objects::nonNull).map(Dto::list)).filter(Objects::nonNull)
this code return type is IsIn<List<Type>>
but accept IsIn<Type>

just like this issue,but now version code is gone
#239

@pondered pondered reopened this May 21, 2024
@jeffgbutler
Copy link
Member

You can do this:

isInWhenPresent(dto == null ? null : dto.list)

@pondered
Copy link
Author

lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants