Skip to content

Commit d8a97f9

Browse files
committed
feat: update oracle nosql converter
Signed-off-by: Otavio Santana <[email protected]>
1 parent 395e108 commit d8a97f9

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

jnosql-oracle-nosql/src/main/java/org/eclipse/jnosql/databases/oracle/communication/OracleNoSqlLikeConverter.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,41 @@
1414
*/
1515
package org.eclipse.jnosql.databases.oracle.communication;
1616

17+
import java.util.Set;
1718
import java.util.regex.Pattern;
1819

1920
enum OracleNoSqlLikeConverter {
2021
INSTANCE;
2122

22-
static String convert(Object value) {
23-
24-
String like = value == null ? null : value.toString();
23+
// Regex metacharacters that must be escaped for Oracle NoSQL regex_like
24+
private static final Set<Character> META = Set.of(
25+
'.', '^', '$', '*', '+', '?', '(', ')', '[', ']', '{', '}', '\\', '|'
26+
);
2527

26-
if (like == null) {
27-
return "";
28-
}
28+
/**
29+
* SQL LIKE (%, _) -> Oracle NoSQL regex_like pattern.
30+
* Examples:
31+
* "Lu%" -> "Lu.*"
32+
* "%Lu" -> ".*Lu"
33+
* "%Lu%" -> ".*Lu.*"
34+
* "Lu" -> "Lu" // exact match equivalent in regex_like
35+
* "a.c" -> "a\\.c" // '.' escaped
36+
*/
37+
static String convert(Object value) {
38+
if (value == null) return ""; // let caller decide behavior for empty
39+
String like = value.toString();
2940
StringBuilder out = new StringBuilder(like.length());
30-
StringBuilder literal = new StringBuilder();
3141

3242
for (int i = 0; i < like.length(); i++) {
3343
char c = like.charAt(i);
34-
if (c == '%' || c == '_') {
35-
if (!literal.isEmpty()) {
36-
out.append(Pattern.quote(literal.toString()));
37-
literal.setLength(0);
38-
}
39-
out.append(c == '%' ? ".*" : ".");
40-
} else {
41-
literal.append(c);
44+
switch (c) {
45+
case '%': out.append(".*"); break; // zero or more
46+
case '_': out.append('.'); break; // exactly one
47+
default:
48+
if (META.contains(c)) out.append('\\');
49+
out.append(c);
4250
}
4351
}
44-
if (!literal.isEmpty()) {
45-
out.append(Pattern.quote(literal.toString()));
46-
}
4752
return out.toString();
4853
}
4954
}

0 commit comments

Comments
 (0)