Skip to content

Commit 5564223

Browse files
committed
style: update document query conversor
Signed-off-by: Otavio Santana <[email protected]>
1 parent b638091 commit 5564223

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/DocumentQueryConversor.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,21 @@ public static Bson convert(CriteriaCondition condition) {
8282
};
8383
}
8484

85-
static String prepareRegexValue(String likePattern) {
85+
static String prepareRegexValue(String likePattern) {
8686
if (likePattern == null) {
8787
return "(?!)"; // never matches
8888
}
8989
StringBuilder sb = new StringBuilder("^");
9090
for (char c : likePattern.toCharArray()) {
9191
switch (c) {
92-
case '%': sb.append(".*"); break; // zero or more
93-
case '_': sb.append('.'); break; // exactly one
94-
default: sb.append(Pattern.quote(String.valueOf(c)));
92+
case '%':
93+
sb.append(".*");
94+
break;
95+
case '_':
96+
sb.append('.');
97+
break;
98+
default:
99+
sb.append(Pattern.quote(String.valueOf(c)));
95100
}
96101
}
97102
sb.append('$');

jnosql-tinkerpop/src/main/java/org/eclipse/jnosql/databases/tinkerpop/communication/LikeToRegex.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,32 @@ public enum LikeToRegex {
2020
INSTANCE;
2121

2222

23-
private static String LikeToRegex(String likePattern) {
24-
if (likePattern == null) {
25-
return "a^";
26-
} // match nothing
27-
StringBuilder sb = new StringBuilder("^");
28-
for (char c : likePattern.toCharArray()) {
29-
switch (c) {
30-
case '%': sb.append(".*"); break;
31-
case '_': sb.append('.'); break;
32-
default: sb.append(Pattern.quote(String.valueOf(c)));
23+
/**
24+
* Converts like pattern to regex pattern.
25+
* @param text the like pattern to convert
26+
* @return the regex pattern
27+
*/
28+
private static String LikeToRegex(Object text) {
29+
String like = text== null? null: text.toString();
30+
if (like == null) {
31+
return "(?!)";
32+
}
33+
StringBuilder rx = new StringBuilder("^");
34+
StringBuilder lit = new StringBuilder();
35+
for (int i = 0; i < like.length(); i++) {
36+
char c = like.charAt(i);
37+
if (c == '%' || c == '_') {
38+
if (!lit.isEmpty()) { rx.append(java.util.regex.Pattern.quote(lit.toString())); lit.setLength(0); }
39+
rx.append(c == '%' ? ".*" : ".");
40+
} else {
41+
lit.append(c);
3342
}
3443
}
35-
sb.append('$');
36-
return sb.toString();
44+
if (!lit.isEmpty()) {
45+
rx.append(java.util.regex.Pattern.quote(lit.toString()));
46+
}
47+
rx.append('$');
48+
return rx.toString();
3749
}
3850

3951
}

jnosql-tinkerpop/src/main/java/org/eclipse/jnosql/databases/tinkerpop/communication/TraversalExecutor.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.eclipse.jnosql.databases.tinkerpop.communication;
1616

1717
import org.apache.tinkerpop.gremlin.process.traversal.P;
18+
import org.apache.tinkerpop.gremlin.process.traversal.TextP;
1819
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
1920
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
2021
import org.apache.tinkerpop.gremlin.structure.Vertex;
@@ -41,6 +42,19 @@ static GraphTraversal<Vertex, Vertex> getPredicate(CriteriaCondition condition)
4142
case EQUALS -> {
4243
return __.has(name, P.eq(value));
4344
}
45+
case LIKE -> {
46+
TextP.
47+
return __.has(name, P.test(v -> v instanceof String && ((String) v).matches(regex)));
48+
}
49+
case ENDS_WITH -> {
50+
return __.has(name, TextP.endingWith(value == null ? "" : value.toString()));
51+
}
52+
case STARTS_WITH -> {
53+
__.has(name, TextP.startingWith(value == null ? "" : value.toString()));
54+
}
55+
case CONTAINS -> {
56+
return __.has(name, TextP.containing(value == null ? "" : value.toString()));
57+
}
4458
case GREATER_THAN -> {
4559
return __.has(name, P.gt(value));
4660
}
@@ -79,6 +93,7 @@ static GraphTraversal<Vertex, Vertex> getPredicate(CriteriaCondition condition)
7993
.reduce(GraphTraversal::or)
8094
.orElseThrow(() -> new UnsupportedOperationException("There is an inconsistency at the OR operator"));
8195
}
96+
8297
default ->
8398
throw new UnsupportedOperationException("There is not support to the type " + operator + " in graph");
8499
}

0 commit comments

Comments
 (0)