Search in sources :

Example 1 with ProjectedTuple

use of org.immutables.criteria.backend.ProjectedTuple in project immutables by immutables.

the class TupleCodecProviderTest method optionalAttribute_nickname.

/**
 * Projection of an optional attribute
 */
@Test
@SuppressWarnings("unchecked")
public void optionalAttribute_nickname() {
    Query query = Query.of(Person.class).addProjections(Matchers.toExpression(PersonCriteria.person.nickName));
    Path idPath = Visitors.toPath(KeyExtractor.defaultFactory().create(Person.class).metadata().keys().get(0));
    TupleCodecProvider provider = new TupleCodecProvider(query, new MongoPathNaming(idPath, PathNaming.defaultNaming()).toExpression());
    Codec<ProjectedTuple> codec = provider.get(ProjectedTuple.class, registry);
    ProjectedTuple tuple1 = codec.decode(new BsonDocumentReader(new BsonDocument("nickName", new BsonString("aaa"))), DecoderContext.builder().build());
    check(tuple1.values()).hasSize(1);
    check((Optional<String>) tuple1.values().get(0)).is(Optional.of("aaa"));
    ProjectedTuple tuple2 = codec.decode(new BsonDocumentReader(new BsonDocument()), DecoderContext.builder().build());
    check(tuple2.values()).hasSize(1);
    check((Optional<String>) tuple2.values().get(0)).is(Optional.empty());
}
Also used : Path(org.immutables.criteria.expression.Path) Query(org.immutables.criteria.expression.Query) BsonDocument(org.bson.BsonDocument) Optional(java.util.Optional) BsonString(org.bson.BsonString) BsonDocumentReader(org.bson.BsonDocumentReader) ProjectedTuple(org.immutables.criteria.backend.ProjectedTuple) Person(org.immutables.criteria.personmodel.Person) Test(org.junit.jupiter.api.Test)

Example 2 with ProjectedTuple

use of org.immutables.criteria.backend.ProjectedTuple in project immutables by immutables.

the class AggregateQueryBuilder method processResult.

List<ProjectedTuple> processResult(Json.Result result) {
    final List<ProjectedTuple> tuples = new ArrayList<>();
    if (result.aggregations() != null) {
        Converter<String, Expression> converter = naming.asConverter().reverse();
        // collect values
        Json.visitValueNodes(result.aggregations(), m -> {
            Map<Expression, Object> values = Maps.newHashMapWithExpectedSize(query.projections().size());
            for (String field : m.keySet()) {
                Expression expression = converter.convert(field);
                Object value = m.get(field);
                if (value == null) {
                    // otherwise jackson returns null even for optionals
                    value = NullNode.getInstance();
                } else if (value instanceof Number && (expression.returnType() == LocalDate.class || expression.returnType() == LocalDateTime.class)) {
                    // hack/work-around because JavaTimeModule doesn't handle epoch millis for LocalDate and LocalDateTime
                    // and elastic always returns epoch millis
                    // this ideally should be handled directly by Deserializer
                    Instant instant = Instant.ofEpochMilli(((Number) value).longValue());
                    value = nodeFactory.textNode(instant.toString());
                }
                values.put(expression, mapper.convertValue(value, mapper.getTypeFactory().constructType(expression.returnType())));
            }
            List<Object> projections = query.projections().stream().map(values::get).collect(Collectors.toList());
            tuples.add(ProjectedTuple.of(query.projections(), projections));
        });
    }
    // elastic exposes total number of documents matching a query in "/hits/total" path
    // this can be used for simple "select count(*) from table"
    final long total = result.searchHits().total().value();
    return tuples;
}
Also used : Expression(org.immutables.criteria.expression.Expression) Instant(java.time.Instant) ArrayList(java.util.ArrayList) ProjectedTuple(org.immutables.criteria.backend.ProjectedTuple)

Example 3 with ProjectedTuple

use of org.immutables.criteria.backend.ProjectedTuple in project immutables by immutables.

the class TupleCodecProviderTest method localDate.

@Test
void localDate() {
    LocalDateHolderCriteria criteria = LocalDateHolderCriteria.localDateHolder;
    Query query = Query.of(TypeHolder.LocalDateHolder.class).addProjections(Matchers.toExpression(criteria.value), Matchers.toExpression(criteria.nullable), Matchers.toExpression(criteria.optional));
    Path idPath = Visitors.toPath(KeyExtractor.defaultFactory().create(TypeHolder.LocalDateHolder.class).metadata().keys().get(0));
    TupleCodecProvider provider = new TupleCodecProvider(query, new MongoPathNaming(idPath, PathNaming.defaultNaming()).toExpression());
    Codec<ProjectedTuple> codec = provider.get(ProjectedTuple.class, registry);
    LocalDate now = LocalDate.now();
    final long millisEpoch = now.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli();
    BsonDocument doc = new BsonDocument().append("id", new BsonString("id1")).append("value", new BsonDateTime(millisEpoch)).append("nullable", BsonNull.VALUE).append("optional", BsonNull.VALUE).append("array", new BsonArray()).append("list", new BsonArray());
    ProjectedTuple tuple = codec.decode(new BsonDocumentReader(doc), DecoderContext.builder().build());
    check(tuple.get(Matchers.toExpression(criteria.value))).is(now);
    check(tuple.get(Matchers.toExpression(criteria.nullable))).isNull();
    check(tuple.get(Matchers.toExpression(criteria.optional))).is(Optional.empty());
}
Also used : Path(org.immutables.criteria.expression.Path) Query(org.immutables.criteria.expression.Query) BsonDocumentReader(org.bson.BsonDocumentReader) LocalDate(java.time.LocalDate) TypeHolder(org.immutables.criteria.typemodel.TypeHolder) LocalDateHolderCriteria(org.immutables.criteria.typemodel.LocalDateHolderCriteria) BsonDocument(org.bson.BsonDocument) BsonDateTime(org.bson.BsonDateTime) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ProjectedTuple(org.immutables.criteria.backend.ProjectedTuple) Test(org.junit.jupiter.api.Test)

Example 4 with ProjectedTuple

use of org.immutables.criteria.backend.ProjectedTuple in project immutables by immutables.

the class TupleCodecProviderTest method age.

@Test
public void age() {
    Query query = Query.of(Person.class).addProjections(Matchers.toExpression(PersonCriteria.person.age));
    Path idPath = Visitors.toPath(KeyExtractor.defaultFactory().create(Person.class).metadata().keys().get(0));
    TupleCodecProvider provider = new TupleCodecProvider(query, new MongoPathNaming(idPath, PathNaming.defaultNaming()).toExpression());
    Codec<ProjectedTuple> codec = provider.get(ProjectedTuple.class, registry);
    ProjectedTuple tuple = codec.decode(new BsonDocumentReader(new BsonDocument("age", new BsonInt32(10))), DecoderContext.builder().build());
    check(tuple.values()).hasSize(1);
    check(tuple.values().get(0)).asString().is("10");
}
Also used : Path(org.immutables.criteria.expression.Path) BsonInt32(org.bson.BsonInt32) Query(org.immutables.criteria.expression.Query) BsonDocument(org.bson.BsonDocument) BsonDocumentReader(org.bson.BsonDocumentReader) ProjectedTuple(org.immutables.criteria.backend.ProjectedTuple) Person(org.immutables.criteria.personmodel.Person) Test(org.junit.jupiter.api.Test)

Aggregations

ProjectedTuple (org.immutables.criteria.backend.ProjectedTuple)4 BsonDocument (org.bson.BsonDocument)3 BsonDocumentReader (org.bson.BsonDocumentReader)3 Path (org.immutables.criteria.expression.Path)3 Query (org.immutables.criteria.expression.Query)3 Test (org.junit.jupiter.api.Test)3 BsonString (org.bson.BsonString)2 Person (org.immutables.criteria.personmodel.Person)2 Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 BsonArray (org.bson.BsonArray)1 BsonDateTime (org.bson.BsonDateTime)1 BsonInt32 (org.bson.BsonInt32)1 Expression (org.immutables.criteria.expression.Expression)1 LocalDateHolderCriteria (org.immutables.criteria.typemodel.LocalDateHolderCriteria)1 TypeHolder (org.immutables.criteria.typemodel.TypeHolder)1