Search in sources :

Example 6 with QueryMethod

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

the class KeyValuePartTreeQueryUnitTests method shouldApplyDerivedMaxResultsToQueryWithParameters.

// DATAKV-142
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void shouldApplyDerivedMaxResultsToQueryWithParameters() throws SecurityException, NoSuchMethodException {
    when(metadataMock.getDomainType()).thenReturn((Class) Person.class);
    when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class);
    QueryMethod qm = new QueryMethod(Repo.class.getMethod("findTop3ByFirstname", String.class), metadataMock, projectionFactoryMock);
    KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE, kvOpsMock, SpelQueryCreator.class);
    KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] { "firstname" });
    assertThat(query.getCriteria(), is(notNullValue()));
    assertThat(query.getCriteria(), IsInstanceOf.instanceOf(SpelCriteria.class));
    assertThat(((SpelCriteria) query.getCriteria()).getExpression().getExpressionString(), is("#it?.firstname?.equals([0])"));
    assertThat(query.getRows(), is(3));
}
Also used : SpelCriteria(org.springframework.data.keyvalue.core.SpelCriteria) QueryMethod(org.springframework.data.repository.query.QueryMethod) QueryMethod(org.springframework.data.repository.query.QueryMethod) Method(java.lang.reflect.Method) Person(org.springframework.data.keyvalue.Person) Test(org.junit.Test)

Example 7 with QueryMethod

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

the class CachingKeyValuePartTreeQueryUnitTests method cachedSpelExpressionShouldBeReusedWithNewContext.

// DATAKV-137
@Test
public void cachedSpelExpressionShouldBeReusedWithNewContext() throws NoSuchMethodException, SecurityException {
    QueryMethod qm = new QueryMethod(Repo.class.getMethod("findByFirstname", String.class), metadataMock, projectionFactoryMock);
    KeyValuePartTreeQuery query = new CachingKeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE, kvOpsMock, SpelQueryCreator.class);
    Object[] args = new Object[] { "foo" };
    SpelCriteria first = (SpelCriteria) query.prepareQuery(args).getCriteria();
    SpelCriteria second = (SpelCriteria) query.prepareQuery(args).getCriteria();
    assertThat(first.getExpression(), sameInstance(second.getExpression()));
    assertThat(first.getContext(), not(sameInstance(second.getContext())));
}
Also used : SpelCriteria(org.springframework.data.keyvalue.core.SpelCriteria) QueryMethod(org.springframework.data.repository.query.QueryMethod) Test(org.junit.Test)

Example 8 with QueryMethod

use of org.springframework.data.repository.query.QueryMethod 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 9 with QueryMethod

use of org.springframework.data.repository.query.QueryMethod in project spring-cloud-gcp by spring-cloud.

the class SpannerQueryLookupStrategy method resolveQuery.

@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory, NamedQueries namedQueries) {
    QueryMethod queryMethod = createQueryMethod(method, metadata, factory);
    Class entityType = getEntityType(queryMethod);
    if (namedQueries.hasQuery(queryMethod.getNamedQueryName())) {
        String sql = namedQueries.getQuery(queryMethod.getNamedQueryName());
        return createSqlSpannerQuery(entityType, queryMethod, sql);
    }
    return createPartTreeSpannerQuery(entityType, queryMethod);
}
Also used : QueryMethod(org.springframework.data.repository.query.QueryMethod)

Example 10 with QueryMethod

use of org.springframework.data.repository.query.QueryMethod in project spring-data-jdbc by spring-projects.

the class CriteriaFactoryUnitTests method shouldConsiderIterableValuesInInOperator.

// DATAJDBC-539
@Test
void shouldConsiderIterableValuesInInOperator() {
    QueryMethod queryMethod = getQueryMethod("findAllByNameIn", List.class);
    RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, Arrays.asList("foo", "bar"));
    ParameterMetadataProvider parameterMetadata = new ParameterMetadataProvider(accessor);
    CriteriaFactory criteriaFactory = new CriteriaFactory(parameterMetadata);
    Part part = new Part("NameIn", User.class);
    Criteria criteria = criteriaFactory.createCriteria(part);
    assertThat(criteria.getValue()).isEqualTo(Arrays.asList("foo", "bar"));
}
Also used : QueryMethod(org.springframework.data.repository.query.QueryMethod) Part(org.springframework.data.repository.query.parser.Part) Criteria(org.springframework.data.relational.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Aggregations

QueryMethod (org.springframework.data.repository.query.QueryMethod)10 Method (java.lang.reflect.Method)6 Test (org.junit.Test)5 Person (org.springframework.data.keyvalue.Person)4 Test (org.junit.jupiter.api.Test)2 SpelCriteria (org.springframework.data.keyvalue.core.SpelCriteria)2 SpelAwareProxyProjectionFactory (org.springframework.data.projection.SpelAwareProxyProjectionFactory)2 Criteria (org.springframework.data.relational.core.query.Criteria)2 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)2 Part (org.springframework.data.repository.query.parser.Part)2 PartTree (org.springframework.data.repository.query.parser.PartTree)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 ArrayList (java.util.ArrayList)1 Pageable (org.springframework.data.domain.Pageable)1 SpelQueryCreator (org.springframework.data.keyvalue.repository.query.SpelQueryCreator)1 RepositoryMetadata (org.springframework.data.repository.core.RepositoryMetadata)1 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)1