Skip to content

Add the Ability to Transform Condition Values After the "when()" Check #105

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
pky920216344 opened this issue Jul 2, 2019 · 6 comments · Fixed by #109
Closed

Add the Ability to Transform Condition Values After the "when()" Check #105

pky920216344 opened this issue Jul 2, 2019 · 6 comments · Fixed by #109

Comments

@pky920216344
Copy link

example:
image
image
like this, parameter is null, but "%"+parameter+"%" is not null
When do fuzzy query operation, i must coding:
if(null!=parameter){
xxx.and(column, ()-> "%"+parameter+"%");
}
it's not beautiful

@jeffgbutler
Copy link
Member

@pky920216344 Wow - this is a great idea - thanks!

I would implement it more like this:

public IsLike<T> then(UnaryOperator<T> transformer) {
    return shouldRender() ? new IsLike<>(() -> transformer.apply(value())) : this;
}

Then we could write code like this:

SelectStatementProvider selectStatement = select(id, firstName, lastName)
        .from(person)
        .where(firstName, isLike(fName).when(Objects::nonNull).then(s -> "%" + s + "%"))
        .and(lastName, isLike(lName).when(Objects::nonNull).then(s -> "%" + s + "%"))
        .build()
        .render(RenderingStrategy.MYBATIS3);

What do you think?

@jeffgbutler
Copy link
Member

If you write a little utility class like this:

public class SearchUtils {
    public static String addWildcards(String s) {
        return "%" + s + "%";
    }
}

Then you can write code like this:

SelectStatementProvider selectStatement = select(id, firstName, lastName)
    .from(person)
    .where(firstName, isLike(fName).when(Objects::nonNull).then(SearchUtils::addWildcards))
    .and(lastName, isLike(lName).when(Objects::nonNull).then(SearchUtils::addWildcards))
    .build()
    .render(RenderingStrategy.MYBATIS3);

I think this qualifies as beautiful 😄

@jeffgbutler jeffgbutler added this to the 1.1.2 milestone Jul 2, 2019
@pky920216344
Copy link
Author

@jeffgbutler better than me hope I can use it as soon as possible 😀

@jeffgbutler
Copy link
Member

I’ll add this and do a release very soon.

@jeffgbutler jeffgbutler changed the title Hope to add a method Add the Ability to Transform Condition Values After the "when()" Check Jul 3, 2019
jeffgbutler referenced this issue in jeffgbutler/mybatis-dynamic-sql Jul 3, 2019
jeffgbutler referenced this issue in jeffgbutler/mybatis-dynamic-sql Jul 3, 2019
@jeffgbutler
Copy link
Member

@pky920216344 this is released and available now in version 1.1.2

@pky920216344
Copy link
Author

@jeffgbutler Thank you 😄

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