Search in sources :

Example 1 with SpelCriteria

use of org.springframework.data.keyvalue.core.SpelCriteria 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 2 with SpelCriteria

use of org.springframework.data.keyvalue.core.SpelCriteria 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 3 with SpelCriteria

use of org.springframework.data.keyvalue.core.SpelCriteria in project spring-data-keyvalue by spring-projects.

the class KeyValuePartTreeQuery method prepareQuery.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected KeyValueQuery<?> prepareQuery(KeyValueQuery<?> instance, Object[] parameters) {
    ParametersParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters);
    Object criteria = instance.getCriteria();
    if (criteria instanceof SpelCriteria || criteria instanceof SpelExpression) {
        SpelExpression spelExpression = getSpelExpression(criteria);
        EvaluationContext context = this.evaluationContextProvider.getEvaluationContext(getQueryMethod().getParameters(), parameters);
        criteria = new SpelCriteria(spelExpression, context);
    }
    KeyValueQuery<?> query = new KeyValueQuery(criteria);
    Pageable pageable = accessor.getPageable();
    Sort sort = accessor.getSort();
    query.setOffset(pageable.toOptional().map(Pageable::getOffset).orElse(-1L));
    if (pageable.isPaged()) {
        query.setRows(pageable.getPageSize());
    } else if (instance.getRows() >= 0) {
        query.setRows(instance.getRows());
    }
    query.setSort(sort.isUnsorted() ? instance.getSort() : sort);
    return query;
}
Also used : SpelCriteria(org.springframework.data.keyvalue.core.SpelCriteria) Pageable(org.springframework.data.domain.Pageable) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Sort(org.springframework.data.domain.Sort) EvaluationContext(org.springframework.expression.EvaluationContext) KeyValueQuery(org.springframework.data.keyvalue.core.query.KeyValueQuery)

Aggregations

SpelCriteria (org.springframework.data.keyvalue.core.SpelCriteria)3 Test (org.junit.Test)2 QueryMethod (org.springframework.data.repository.query.QueryMethod)2 Method (java.lang.reflect.Method)1 Pageable (org.springframework.data.domain.Pageable)1 Sort (org.springframework.data.domain.Sort)1 Person (org.springframework.data.keyvalue.Person)1 KeyValueQuery (org.springframework.data.keyvalue.core.query.KeyValueQuery)1 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)1 EvaluationContext (org.springframework.expression.EvaluationContext)1 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)1