Search in sources :

Example 36 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-keyvalue by spring-projects.

the class SpelQueryCreatorUnitTests method createQueryForMethodWithArgs.

private KeyValueQuery<SpelExpression> createQueryForMethodWithArgs(String methodName, Object... args) throws NoSuchMethodException, SecurityException {
    Class<?>[] argTypes = new Class<?>[args.length];
    if (!ObjectUtils.isEmpty(args)) {
        for (int i = 0; i < args.length; i++) {
            argTypes[i] = args[i].getClass();
        }
    }
    Method method = PersonRepository.class.getMethod(methodName, argTypes);
    doReturn(Person.class).when(metadataMock).getReturnedDomainClass(method);
    PartTree partTree = new PartTree(method.getName(), method.getReturnType());
    SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor(new QueryMethod(method, metadataMock, new SpelAwareProxyProjectionFactory()).getParameters(), args));
    KeyValueQuery<SpelExpression> q = creator.createQuery();
    q.getCriteria().setEvaluationContext(new StandardEvaluationContext(args));
    return q;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) QueryMethod(org.springframework.data.repository.query.QueryMethod) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) QueryMethod(org.springframework.data.repository.query.QueryMethod) Method(java.lang.reflect.Method) PartTree(org.springframework.data.repository.query.parser.PartTree)

Example 37 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-keyvalue by spring-projects.

the class KeyValuePartTreeQuery method createQuery.

/**
 * Create a {@link KeyValueQuery} given {@link ParameterAccessor}.
 *
 * @param accessor must not be {@literal null}.
 * @return the {@link KeyValueQuery}.
 */
public KeyValueQuery<?> createQuery(ParameterAccessor accessor) {
    PartTree tree = new PartTree(getQueryMethod().getName(), getQueryMethod().getEntityInformation().getJavaType());
    AbstractQueryCreator<? extends KeyValueQuery<?>, ?> queryCreator = queryCreatorFactory.queryCreatorFor(tree, accessor);
    KeyValueQuery<?> query = queryCreator.createQuery();
    if (tree.isLimiting()) {
        query.setRows(tree.getMaxResults());
    }
    return query;
}
Also used : PartTree(org.springframework.data.repository.query.parser.PartTree)

Example 38 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-mongodb by spring-projects.

the class MongoQueryCreatorUnitTests method bindsNullValueToContainsClause.

// DATAMONGO-1342
@Test
void bindsNullValueToContainsClause() {
    PartTree partTree = new PartTree("emailAddressesContains", User.class);
    ConvertingParameterAccessor accessor = getAccessor(converter, new Object[] { null });
    Query query = new MongoQueryCreator(partTree, accessor, context).createQuery();
    assertThat(query).isEqualTo(query(where("emailAddresses").in((Object) null)));
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) PartTree(org.springframework.data.repository.query.parser.PartTree) Test(org.junit.jupiter.api.Test)

Example 39 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-mongodb by spring-projects.

the class MongoQueryCreatorUnitTests method createsLessThanEqualQueryCorrectly.

@Test
void createsLessThanEqualQueryCorrectly() {
    PartTree tree = new PartTree("findByAgeLessThanEqual", Person.class);
    MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, 18), context);
    Query reference = query(where("age").lte(18));
    assertThat(creator.createQuery()).isEqualTo(reference);
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) PartTree(org.springframework.data.repository.query.parser.PartTree) Test(org.junit.jupiter.api.Test)

Example 40 with PartTree

use of org.springframework.data.repository.query.parser.PartTree in project spring-data-mongodb by spring-projects.

the class MongoQueryCreatorUnitTests method betweenShouldAllowSingleRageParameter.

// DATAMONGO-2071
@Test
void betweenShouldAllowSingleRageParameter() {
    PartTree tree = new PartTree("findByAgeBetween", Person.class);
    MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, Range.of(Bound.exclusive(10), Bound.exclusive(11))), context);
    assertThat(creator.createQuery()).isEqualTo(query(where("age").gt(10).lt(11)));
}
Also used : PartTree(org.springframework.data.repository.query.parser.PartTree) Test(org.junit.jupiter.api.Test)

Aggregations

PartTree (org.springframework.data.repository.query.parser.PartTree)60 Test (org.junit.jupiter.api.Test)51 Query (org.springframework.data.mongodb.core.query.Query)44 GeoJsonPoint (org.springframework.data.mongodb.core.geo.GeoJsonPoint)8 Point (org.springframework.data.geo.Point)6 Method (java.lang.reflect.Method)4 Distance (org.springframework.data.geo.Distance)4 SpelAwareProxyProjectionFactory (org.springframework.data.projection.SpelAwareProxyProjectionFactory)4 QueryMethod (org.springframework.data.repository.query.QueryMethod)3 Part (org.springframework.data.repository.query.parser.Part)3 DefaultRepositoryMetadata (org.springframework.data.repository.core.support.DefaultRepositoryMetadata)2 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 MongoException (com.mongodb.MongoException)1 ArrayList (java.util.ArrayList)1 Pattern (java.util.regex.Pattern)1 Document (org.bson.Document)1 ObjectId (org.bson.types.ObjectId)1 NotNull (org.jetbrains.annotations.NotNull)1 Test (org.junit.Test)1