How to query for String contains case-insensitive?

2,255 views
Skip to first unread message

Ed Erwin

unread,
Jun 4, 2015, 1:12:19 PM6/4/15
to jooq...@googlegroups.com
I would like to know the best way to query for a string "contains" another string in a case-insensitive manner, and let JOOQ automatically take care of all escape characters.

JOOQ 3.6 contains predicates for "likeIgnoreCase" and "contains", but not "containsIgnoreCase".


I can think of at least two logical ways to do this:

1. BOOK.TITLE.lower().contains(myString.toLowerCase());
2. BOOK.TITLE.lower().contains(DSL.lower(myString));

I would suspect that solution number 2 is best because the case conversion for both strings is being done in the SQL code.  In solution 1, the case conversion for one string is in SQL and the other is in Java, and it is possible that the two use different Locales.

I'm pretty sure that this third possibility would be wrong, because myString might contain "%":
3. BOOK.TITLE.likeIgnoreCase("%" + myString + "%");

Any advice?

Lukas Eder

unread,
Jun 5, 2015, 4:42:29 AM6/5/15
to jooq...@googlegroups.com
2015-06-04 19:12 GMT+02:00 Ed Erwin <enw...@gmail.com>:
I would like to know the best way to query for a string "contains" another string in a case-insensitive manner, and let JOOQ automatically take care of all escape characters.

JOOQ 3.6 contains predicates for "likeIgnoreCase" and "contains", but not "containsIgnoreCase".

Yes, we actually realised that there will be a lot of possible combinations of ignoring case with other like-esque utility methods, which may:

1. Bloat the API
2. Prevent supporting vendor-backed functions, such as "contains()", which might be supported in some databases.
3. Lower-casing values isn't a completely accurate implementation for "ignore case" in all collations / character sets / languages
 

I can think of at least two logical ways to do this:

1. BOOK.TITLE.lower().contains(myString.toLowerCase());
2. BOOK.TITLE.lower().contains(DSL.lower(myString));

I would suspect that solution number 2 is best because the case conversion for both strings is being done in the SQL code.  In solution 1, the case conversion for one string is in SQL and the other is in Java, and it is possible that the two use different Locales.

I'm pretty sure that this third possibility would be wrong, because myString might contain "%":
3. BOOK.TITLE.likeIgnoreCase("%" + myString + "%");

Any advice?

Go more low-level:

BOOK.TITLE.lower().like(myString.toLowerCase().replace("%", "!%"), '!');

Hope this helps,
Lukas
Reply all
Reply to author
Forward
0 new messages