Skip to content

Commit

Permalink
Allow to add a collation to a query
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed May 7, 2020
1 parent 23ac152 commit 5c0a0a2
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.Optional;
import java.util.stream.Stream;

import com.mongodb.client.model.Collation;

import io.quarkus.panache.common.Page;

/**
Expand Down Expand Up @@ -134,6 +136,14 @@ public interface PanacheQuery<Entity> {
*/
public <T extends Entity> PanacheQuery<T> range(int startIndex, int lastIndex);

/**
* Define the collation used for this query.
*
* @param collation the collation to be used for this query.
* @return this query, modified
*/
public <T extends Entity> PanacheQuery<T> withCollation(Collation collation);

// Results

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.Optional;
import java.util.stream.Stream;

import com.mongodb.client.model.Collation;

import io.quarkus.panache.common.Page;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
Expand Down Expand Up @@ -136,6 +138,14 @@ public interface ReactivePanacheQuery<Entity> {
*/
public <T extends Entity> ReactivePanacheQuery<T> range(int startIndex, int lastIndex);

/**
* Define the collation used for this query.
*
* @param collation the collation to be used for this query.
* @return this query, modified
*/
public <T extends Entity> ReactivePanacheQuery<T> withCollation(Collation collation);

// Results

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.bson.Document;
import org.bson.conversions.Bson;

import com.mongodb.client.model.Collation;

import io.quarkus.mongodb.FindOptions;
import io.quarkus.mongodb.panache.reactive.ReactivePanacheQuery;
import io.quarkus.mongodb.panache.runtime.MongoPropertyUtil;
Expand All @@ -28,6 +30,8 @@ public class ReactivePanacheQueryImpl<Entity> implements ReactivePanacheQuery<En

private Range range;

private Collation collation;

ReactivePanacheQueryImpl(ReactiveMongoCollection<? extends Entity> collection, Bson mongoQuery, Bson sort) {
this.collection = collection;
this.mongoQuery = mongoQuery;
Expand All @@ -41,6 +45,8 @@ private ReactivePanacheQueryImpl(ReactivePanacheQueryImpl previousQuery, Bson pr
this.projections = projections;
this.page = previousQuery.page;
this.count = previousQuery.count;
this.range = previousQuery.range;
this.collation = previousQuery.collation;
}

// Builder
Expand Down Expand Up @@ -144,6 +150,12 @@ public <T extends Entity> ReactivePanacheQuery<T> range(int startIndex, int last
return (ReactivePanacheQuery<T>) this;
}

@Override
public <T extends Entity> ReactivePanacheQuery<T> withCollation(Collation collation) {
this.collation = collation;
return (ReactivePanacheQuery<T>) this;
}

// Results

@Override
Expand Down Expand Up @@ -220,6 +232,9 @@ private FindOptions buildOptions() {
if (projections != null) {
options.projection(this.projections);
}
if (this.collation != null) {
options.collation(collation);
}
return options;
}

Expand All @@ -235,6 +250,9 @@ private FindOptions buildOptions(int maxResults) {
if (projections != null) {
options.projection(this.projections);
}
if (this.collation != null) {
options.collation(collation);
}
return options.limit(maxResults);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Collation;

import io.quarkus.mongodb.panache.PanacheQuery;
import io.quarkus.panache.common.Page;
Expand All @@ -29,6 +30,8 @@ public class PanacheQueryImpl<Entity> implements PanacheQuery<Entity> {

private Range range;

private Collation collation;

PanacheQueryImpl(MongoCollection<? extends Entity> collection, Bson mongoQuery, Bson sort) {
this.collection = collection;
this.mongoQuery = mongoQuery;
Expand All @@ -42,6 +45,8 @@ private PanacheQueryImpl(PanacheQueryImpl previousQuery, Bson projections) {
this.projections = projections;
this.page = previousQuery.page;
this.count = previousQuery.count;
this.range = previousQuery.range;
this.collation = previousQuery.collation;
}

// Builder
Expand Down Expand Up @@ -144,6 +149,12 @@ public <T extends Entity> PanacheQuery<T> range(int startIndex, int lastIndex) {
return (PanacheQuery<T>) this;
}

@Override
public <T extends Entity> PanacheQuery<T> withCollation(Collation collation) {
this.collation = collation;
return (PanacheQuery<T>) this;
}

// Results

@Override
Expand All @@ -167,6 +178,9 @@ private <T extends Entity> List<T> list(Integer limit) {
if (this.projections != null) {
find.projection(projections);
}
if (this.collation != null) {
find.collation(collation);
}
manageOffsets(find, limit);
MongoCursor<T> cursor = find.sort(sort).iterator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.bson.Document;
import org.junit.jupiter.api.Assertions;

import com.mongodb.client.model.Collation;
import com.mongodb.client.model.CollationStrength;

import io.quarkus.mongodb.panache.PanacheQuery;
import io.quarkus.mongodb.panache.reactive.ReactivePanacheQuery;
import io.quarkus.panache.common.Page;
Expand Down Expand Up @@ -86,6 +89,27 @@ public Response testImperativeEntity() {
entityA.delete();
entityZ.delete();

// collation
TestImperativeEntity entityALower = new TestImperativeEntity("aaa", "aaa", "aaa");
entityALower.persist();
TestImperativeEntity entityAUpper = new TestImperativeEntity("AAA", "AAA", "AAA");
entityAUpper.persist();
TestImperativeEntity entityB = new TestImperativeEntity("BBB", "BBB", "BBB");
entityB.persistOrUpdate();
List<TestImperativeEntity> results = TestImperativeEntity.<TestImperativeEntity> listAll(Sort.ascending("title"));
Assertions.assertEquals("AAA", results.get(0).title);
Assertions.assertEquals("BBB", results.get(1).title);
Assertions.assertEquals("aaa", results.get(2).title);
Collation collation = Collation.builder().caseLevel(true).collationStrength(CollationStrength.PRIMARY).locale("fr")
.build();
results = TestImperativeEntity.<TestImperativeEntity> findAll(Sort.ascending("title")).withCollation(collation).list();
Assertions.assertEquals("aaa", results.get(0).title);
Assertions.assertEquals("AAA", results.get(1).title);
Assertions.assertEquals("BBB", results.get(2).title);
entityAUpper.delete();
entityALower.delete();
entityB.delete();

// count
Assertions.assertEquals(5, TestImperativeEntity.count("category", "category0"));
Assertions.assertEquals(5, TestImperativeEntity.count("category = ?1", "category0"));
Expand Down Expand Up @@ -199,6 +223,28 @@ public Response testImperativeRepository() {
testImperativeRepository.delete(entityA);
testImperativeRepository.delete(entityZ);

// collation
TestImperativeEntity entityALower = new TestImperativeEntity("aaa", "aaa", "aaa");
testImperativeRepository.persist(entityALower);
TestImperativeEntity entityAUpper = new TestImperativeEntity("AAA", "AAA", "AAA");
testImperativeRepository.persist(entityAUpper);
TestImperativeEntity entityB = new TestImperativeEntity("BBB", "BBB", "BBB");
testImperativeRepository.persistOrUpdate(entityB);
List<TestImperativeEntity> results = testImperativeRepository.<TestImperativeEntity> listAll(Sort.ascending("title"));
Assertions.assertEquals("AAA", results.get(0).title);
Assertions.assertEquals("BBB", results.get(1).title);
Assertions.assertEquals("aaa", results.get(2).title);
Collation collation = Collation.builder().caseLevel(true).collationStrength(CollationStrength.PRIMARY).locale("fr")
.build();
results = testImperativeRepository.<TestImperativeEntity> findAll(Sort.ascending("title")).withCollation(collation)
.list();
Assertions.assertEquals("aaa", results.get(0).title);
Assertions.assertEquals("AAA", results.get(1).title);
Assertions.assertEquals("BBB", results.get(2).title);
testImperativeRepository.delete(entityALower);
testImperativeRepository.delete(entityAUpper);
testImperativeRepository.delete(entityB);

// count
Assertions.assertEquals(5, testImperativeRepository.count("category", "category0"));
Assertions.assertEquals(5, testImperativeRepository.count("category = ?1", "category0"));
Expand Down Expand Up @@ -387,6 +433,29 @@ public Response testReactiveEntity() {
entityA.delete().await().indefinitely();
entityZ.delete().await().indefinitely();

// collation
TestReactiveEntity entityALower = new TestReactiveEntity("aaa", "aaa", "aaa");
entityALower.persist().await().indefinitely();
TestReactiveEntity entityAUpper = new TestReactiveEntity("AAA", "AAA", "AAA");
entityAUpper.persist().await().indefinitely();
TestReactiveEntity entityB = new TestReactiveEntity("BBB", "BBB", "BBB");
entityB.persistOrUpdate().await().indefinitely();
List<TestReactiveEntity> results = TestReactiveEntity.<TestReactiveEntity> listAll(Sort.ascending("title")).await()
.indefinitely();
Assertions.assertEquals("AAA", results.get(0).title);
Assertions.assertEquals("BBB", results.get(1).title);
Assertions.assertEquals("aaa", results.get(2).title);
Collation collation = Collation.builder().caseLevel(true).collationStrength(CollationStrength.PRIMARY).locale("fr")
.build();
results = TestReactiveEntity.<TestReactiveEntity> findAll(Sort.ascending("title")).withCollation(collation).list()
.await().indefinitely();
Assertions.assertEquals("aaa", results.get(0).title);
Assertions.assertEquals("AAA", results.get(1).title);
Assertions.assertEquals("BBB", results.get(2).title);
entityAUpper.delete().await().indefinitely();
entityALower.delete().await().indefinitely();
entityB.delete().await().indefinitely();

// count
Assertions.assertEquals(5, TestReactiveEntity.count("category", "category0").await().indefinitely());
Assertions.assertEquals(5, TestReactiveEntity.count("category = ?1", "category0").await().indefinitely());
Expand Down Expand Up @@ -507,6 +576,28 @@ public Response testReactiveRepository() {
testReactiveRepository.delete(entityA).await().indefinitely();
testReactiveRepository.delete(entityZ).await().indefinitely();

// collation
TestReactiveEntity entityALower = new TestReactiveEntity("aaa", "aaa", "aaa");
testReactiveRepository.persist(entityALower).await().indefinitely();
TestReactiveEntity entityAUpper = new TestReactiveEntity("AAA", "AAA", "AAA");
testReactiveRepository.persist(entityAUpper).await().indefinitely();
TestReactiveEntity entityB = new TestReactiveEntity("BBB", "BBB", "BBB");
testReactiveRepository.persistOrUpdate(entityB).await().indefinitely();
List<TestReactiveEntity> results = testReactiveRepository.listAll(Sort.ascending("title")).await().indefinitely();
Assertions.assertEquals("AAA", results.get(0).title);
Assertions.assertEquals("BBB", results.get(1).title);
Assertions.assertEquals("aaa", results.get(2).title);
Collation collation = Collation.builder().caseLevel(true).collationStrength(CollationStrength.PRIMARY).locale("fr")
.build();
results = testReactiveRepository.findAll(Sort.ascending("title")).withCollation(collation).list().await()
.indefinitely();
Assertions.assertEquals("aaa", results.get(0).title);
Assertions.assertEquals("AAA", results.get(1).title);
Assertions.assertEquals("BBB", results.get(2).title);
testReactiveRepository.delete(entityALower).await().indefinitely();
testReactiveRepository.delete(entityAUpper).await().indefinitely();
testReactiveRepository.delete(entityB).await().indefinitely();

// count
Assertions.assertEquals(5,
testReactiveRepository.count("category", "category0").await().indefinitely());
Expand Down

0 comments on commit 5c0a0a2

Please sign in to comment.