Search in sources :

Example 1 with ParametersParameterAccessor

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

the class SpelQueryEngineUnitTests method createQueryForMethodWithArgs.

private static SpelCriteria createQueryForMethodWithArgs(String methodName, Object... args) throws Exception {
    List<Class<?>> types = new ArrayList<>(args.length);
    for (Object arg : args) {
        types.add(arg.getClass());
    }
    Method method = PersonRepository.class.getMethod(methodName, types.toArray(new Class<?>[types.size()]));
    RepositoryMetadata metadata = mock(RepositoryMetadata.class);
    doReturn(method.getReturnType()).when(metadata).getReturnedDomainClass(method);
    PartTree partTree = new PartTree(method.getName(), method.getReturnType());
    SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor(new QueryMethod(method, metadata, new SpelAwareProxyProjectionFactory()).getParameters(), args));
    return new SpelCriteria(creator.createQuery().getCriteria(), new StandardEvaluationContext(args));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) QueryMethod(org.springframework.data.repository.query.QueryMethod) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) ArrayList(java.util.ArrayList) QueryMethod(org.springframework.data.repository.query.QueryMethod) Method(java.lang.reflect.Method) RepositoryMetadata(org.springframework.data.repository.core.RepositoryMetadata) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) PartTree(org.springframework.data.repository.query.parser.PartTree) SpelQueryCreator(org.springframework.data.keyvalue.repository.query.SpelQueryCreator)

Example 2 with ParametersParameterAccessor

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

use of org.springframework.data.repository.query.ParametersParameterAccessor 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)

Example 4 with ParametersParameterAccessor

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

the class KeyValuePartTreeQuery method execute.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.repository.query.RepositoryQuery#execute(java.lang.Object[])
	 */
@Override
public Object execute(Object[] parameters) {
    ParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters);
    KeyValueQuery<?> query = prepareQuery(parameters);
    ResultProcessor processor = queryMethod.getResultProcessor().withDynamicProjection(accessor);
    return processor.processResult(doExecute(parameters, query));
}
Also used : ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) ResultProcessor(org.springframework.data.repository.query.ResultProcessor)

Aggregations

ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)4 Method (java.lang.reflect.Method)2 SpelAwareProxyProjectionFactory (org.springframework.data.projection.SpelAwareProxyProjectionFactory)2 QueryMethod (org.springframework.data.repository.query.QueryMethod)2 PartTree (org.springframework.data.repository.query.parser.PartTree)2 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 ArrayList (java.util.ArrayList)1 Pageable (org.springframework.data.domain.Pageable)1 Sort (org.springframework.data.domain.Sort)1 SpelCriteria (org.springframework.data.keyvalue.core.SpelCriteria)1 KeyValueQuery (org.springframework.data.keyvalue.core.query.KeyValueQuery)1 SpelQueryCreator (org.springframework.data.keyvalue.repository.query.SpelQueryCreator)1 RepositoryMetadata (org.springframework.data.repository.core.RepositoryMetadata)1 ParameterAccessor (org.springframework.data.repository.query.ParameterAccessor)1 ResultProcessor (org.springframework.data.repository.query.ResultProcessor)1 EvaluationContext (org.springframework.expression.EvaluationContext)1