Search in sources :

Example 1 with ImmutableQuery

use of org.immutables.criteria.expression.ImmutableQuery in project immutables by immutables.

the class ReactiveFetcherDelegate method exists.

@Override
public Publisher<Boolean> exists() {
    // change query to count at most one record
    // select count(*) from (select * from foo where ... limit 1)
    ImmutableQuery query = this.query;
    if (!query.limit().isPresent()) {
        // for exists operation we need at most one (1) element (if limit is not set already)
        query = query.withLimit(1);
    }
    Publisher<Long> countPublisher = of(query, session).count();
    return Publishers.map(countPublisher, count -> count > 0);
}
Also used : ImmutableQuery(org.immutables.criteria.expression.ImmutableQuery)

Example 2 with ImmutableQuery

use of org.immutables.criteria.expression.ImmutableQuery in project immutables by immutables.

the class AggregationQueryTest method distinct2.

/**
 * Testing two field (s): {@code select distinct a, b from ...}
 */
@Test
void distinct2() {
    ImmutableQuery query = Query.of(Person.class).addProjections(Matchers.toExpression(person.nickName)).addProjections(Matchers.toExpression(person.age)).withDistinct(true);
    assertAgg(query, "{$project: {expr0: '$nickName', expr1: '$age'}}", "{$group: {_id: {expr0: '$expr0', expr1: '$expr1'}}}", "{$project: {expr0: '$_id.expr0', expr1: '$_id.expr1'}}");
    assertAgg(query.withLimit(1), "{$project: {expr0: '$nickName', expr1: '$age'}}", "{$group: {_id: {expr0: '$expr0', expr1: '$expr1'}}}", "{$project: {expr0: '$_id.expr0', expr1: '$_id.expr1'}}", "{$limit: 1}");
    assertAgg(query.withLimit(1).withCount(true), "{$project: {expr0: '$nickName', expr1: '$age'}}", "{$group: {_id: {expr0: '$expr0', expr1: '$expr1'}}}", "{$project: {expr0: '$_id.expr0', expr1: '$_id.expr1'}}", "{$limit: 1}", "{$count: 'count'}");
}
Also used : ImmutableQuery(org.immutables.criteria.expression.ImmutableQuery) Person(org.immutables.criteria.personmodel.Person) Test(org.junit.jupiter.api.Test)

Example 3 with ImmutableQuery

use of org.immutables.criteria.expression.ImmutableQuery in project immutables by immutables.

the class ReactiveFetcherDelegate method validateAsList.

private Publisher<T> validateAsList(Consumer<List<T>> validatorFn) {
    ImmutableQuery query = ImmutableQuery.copyOf(this.query);
    if (!query.limit().isPresent()) {
        // ensure at most one element
        // fail if there are 2 or more
        query = query.withLimit(2);
    }
    Publisher<List<T>> asList = Publishers.toList(session.execute(StandardOperations.Select.of(query)).publisher());
    Function<List<T>, List<T>> mapFn = list -> {
        validatorFn.accept(list);
        return list;
    };
    return Publishers.flatMapIterable(Publishers.map(asList, mapFn), x -> x);
}
Also used : NonUniqueResultException(org.immutables.criteria.backend.NonUniqueResultException) Matchers(org.immutables.criteria.matcher.Matchers) ProjectedTuple(org.immutables.criteria.backend.ProjectedTuple) Projection(org.immutables.criteria.matcher.Projection) Publisher(org.reactivestreams.Publisher) StandardOperations(org.immutables.criteria.backend.StandardOperations) UnaryOperator(java.util.function.UnaryOperator) Backend(org.immutables.criteria.backend.Backend) Function(java.util.function.Function) Query(org.immutables.criteria.expression.Query) Objects(java.util.Objects) Consumer(java.util.function.Consumer) List(java.util.List) Tuple(org.immutables.criteria.repository.Tuple) Publishers(org.immutables.criteria.repository.Publishers) ImmutableQuery(org.immutables.criteria.expression.ImmutableQuery) ImmutableQuery(org.immutables.criteria.expression.ImmutableQuery) List(java.util.List)

Example 4 with ImmutableQuery

use of org.immutables.criteria.expression.ImmutableQuery in project immutables by immutables.

the class AggregationQueryTest method distinct1.

/**
 * Testing single field: {@code select distinct nickName from ...}
 */
@Test
void distinct1() {
    ImmutableQuery query = Query.of(Person.class).addProjections(Matchers.toExpression(person.nickName)).withDistinct(true);
    assertAgg(query, "{$project: {expr0: '$nickName'}}", "{$group: {_id: '$expr0'}}", "{$project: {expr0: '$_id'}}");
    assertAgg(query.withLimit(1), "{$project: {expr0: '$nickName'}}", "{$group: {_id: '$expr0'}}", "{$project: {expr0: '$_id'}}", "{$limit: 1}");
    assertAgg(query.withLimit(1).withCount(true), "{$project: {expr0: '$nickName'}}", "{$group: {_id: '$expr0'}}", "{$project: {expr0: '$_id'}}", "{$limit: 1}", "{$count: 'count'}");
}
Also used : ImmutableQuery(org.immutables.criteria.expression.ImmutableQuery) Test(org.junit.jupiter.api.Test)

Aggregations

ImmutableQuery (org.immutables.criteria.expression.ImmutableQuery)4 Test (org.junit.jupiter.api.Test)2 List (java.util.List)1 Objects (java.util.Objects)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 UnaryOperator (java.util.function.UnaryOperator)1 Backend (org.immutables.criteria.backend.Backend)1 NonUniqueResultException (org.immutables.criteria.backend.NonUniqueResultException)1 ProjectedTuple (org.immutables.criteria.backend.ProjectedTuple)1 StandardOperations (org.immutables.criteria.backend.StandardOperations)1 Query (org.immutables.criteria.expression.Query)1 Matchers (org.immutables.criteria.matcher.Matchers)1 Projection (org.immutables.criteria.matcher.Projection)1 Person (org.immutables.criteria.personmodel.Person)1 Publishers (org.immutables.criteria.repository.Publishers)1 Tuple (org.immutables.criteria.repository.Tuple)1 Publisher (org.reactivestreams.Publisher)1