Skip to content

Commit 516bc49

Browse files
committed
feat: update n1query builder
Signed-off-by: Otavio Santana <[email protected]>
1 parent 2a58fda commit 516bc49

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

jnosql-couchbase/src/main/java/org/eclipse/jnosql/databases/couchbase/communication/N1QLBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.couchbase.client.java.json.JsonObject;
1818
import jakarta.data.Direction;
1919
import org.eclipse.jnosql.communication.TypeReference;
20+
import org.eclipse.jnosql.communication.driver.StringMatch;
2021
import org.eclipse.jnosql.communication.semistructured.CriteriaCondition;
2122
import org.eclipse.jnosql.communication.semistructured.Element;
2223
import org.eclipse.jnosql.communication.semistructured.SelectQuery;
@@ -113,6 +114,15 @@ private void condition(CriteriaCondition condition, StringBuilder n1ql, JsonObje
113114
case LIKE:
114115
predicate(n1ql, " LIKE ", document, params);
115116
return;
117+
case CONTAINS:
118+
predicate(n1ql, " LIKE ", Element.of(document.name(), StringMatch.CONTAINS.format(document.get(String.class))), params);
119+
return;
120+
case STARTS_WITH:
121+
predicate(n1ql, " LIKE ", Element.of(document.name(), StringMatch.STARTS_WITH.format(document.get(String.class))), params);
122+
return;
123+
case ENDS_WITH:
124+
predicate(n1ql, " LIKE ", Element.of(document.name(), StringMatch.ENDS_WITH.format(document.get(String.class))), params);
125+
return;
116126
case NOT:
117127
n1ql.append(" NOT ");
118128
condition(document.get(CriteriaCondition.class), n1ql, params, ids);

jnosql-couchbase/src/test/java/org/eclipse/jnosql/databases/couchbase/communication/CouchbaseDocumentManagerTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@
2222
import org.eclipse.jnosql.communication.TypeReference;
2323
import org.eclipse.jnosql.communication.keyvalue.BucketManager;
2424
import org.eclipse.jnosql.communication.semistructured.CommunicationEntity;
25+
import org.eclipse.jnosql.communication.semistructured.CriteriaCondition;
2526
import org.eclipse.jnosql.communication.semistructured.DeleteQuery;
2627
import org.eclipse.jnosql.communication.semistructured.Element;
2728
import org.eclipse.jnosql.communication.semistructured.Elements;
2829
import org.eclipse.jnosql.communication.semistructured.SelectQuery;
30+
import org.eclipse.jnosql.mapping.semistructured.MappingQuery;
2931
import org.junit.jupiter.api.AfterEach;
3032
import org.junit.jupiter.api.BeforeEach;
3133
import org.junit.jupiter.api.Test;
3234
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
3335

3436
import java.util.ArrayList;
37+
import java.util.Collections;
3538
import java.util.HashMap;
3639
import java.util.HashSet;
3740
import java.util.List;
@@ -294,6 +297,51 @@ void shouldUpdateNull(){
294297
});
295298
}
296299

300+
@Test
301+
void shouldFindContains() {
302+
var entity = getEntity();
303+
304+
entityManager.insert(entity);
305+
var query = new MappingQuery(Collections.emptyList(), 0L, 0L, CriteriaCondition.contains(Element.of("name",
306+
"lia")), COLLECTION_PERSON_NAME, Collections.emptyList());
307+
308+
var result = entityManager.select(query).toList();
309+
SoftAssertions.assertSoftly(softly -> {
310+
softly.assertThat(result).hasSize(1);
311+
softly.assertThat(result.get(0).find("name").orElseThrow().get(String.class)).isEqualTo("Poliana");
312+
});
313+
}
314+
315+
@Test
316+
void shouldStartsWith() {
317+
var entity = getEntity();
318+
319+
entityManager.insert(entity);
320+
var query = new MappingQuery(Collections.emptyList(), 0L, 0L, CriteriaCondition.startsWith(Element.of("name",
321+
"Pol")), COLLECTION_PERSON_NAME, Collections.emptyList());
322+
323+
var result = entityManager.select(query).toList();
324+
SoftAssertions.assertSoftly(softly -> {
325+
softly.assertThat(result).hasSize(1);
326+
softly.assertThat(result.get(0).find("name").orElseThrow().get(String.class)).isEqualTo("Poliana");
327+
});
328+
}
329+
330+
@Test
331+
void shouldEndsWith() {
332+
var entity = getEntity();
333+
334+
entityManager.insert(entity);
335+
var query = new MappingQuery(Collections.emptyList(), 0L, 0L, CriteriaCondition.endsWith(Element.of("name",
336+
"ana")), COLLECTION_PERSON_NAME, Collections.emptyList());
337+
338+
var result = entityManager.select(query).toList();
339+
SoftAssertions.assertSoftly(softly -> {
340+
softly.assertThat(result).hasSize(1);
341+
softly.assertThat(result.get(0).find("name").orElseThrow().get(String.class)).isEqualTo("Poliana");
342+
});
343+
}
344+
297345
private CommunicationEntity getEntity() {
298346
CommunicationEntity entity = CommunicationEntity.of(COLLECTION_PERSON_NAME);
299347
Map<String, Object> map = new HashMap<>();

0 commit comments

Comments
 (0)