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);
}
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'}");
}
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);
}
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'}");
}
Aggregations