Search in sources :

Example 1 with Parameters

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

the class DatastoreRepositoryFactory method delegateContextProvider.

private QueryMethodEvaluationContextProvider delegateContextProvider(QueryMethodEvaluationContextProvider evaluationContextProvider) {
    return new QueryMethodEvaluationContextProvider() {

        @Override
        public <T extends Parameters<?, ?>> EvaluationContext getEvaluationContext(T parameters, Object[] parameterValues) {
            StandardEvaluationContext evaluationContext = (StandardEvaluationContext) evaluationContextProvider.getEvaluationContext(parameters, parameterValues);
            evaluationContext.setRootObject(DatastoreRepositoryFactory.this.applicationContext);
            evaluationContext.addPropertyAccessor(new BeanFactoryAccessor());
            evaluationContext.setBeanResolver(new BeanFactoryResolver(DatastoreRepositoryFactory.this.applicationContext));
            return evaluationContext;
        }
    };
}
Also used : BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) Parameters(org.springframework.data.repository.query.Parameters) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) QueryMethodEvaluationContextProvider(org.springframework.data.repository.query.QueryMethodEvaluationContextProvider) BeanFactoryAccessor(org.springframework.context.expression.BeanFactoryAccessor)

Example 2 with Parameters

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

the class PartTreeFirestoreQueryTests method setUpPartTreeFirestoreQuery.

private PartTreeFirestoreQuery setUpPartTreeFirestoreQuery(String methodName) {
    Parameters parametersMock = mock(Parameters.class);
    when(parametersMock.isEmpty()).thenReturn(true);
    when(this.queryMethod.getParameters()).thenReturn(parametersMock);
    when(this.queryMethod.getName()).thenReturn(methodName);
    ReturnedType returnedType = mock(ReturnedType.class);
    when(returnedType.getDomainType()).thenAnswer(invocation -> User.class);
    ResultProcessor resultProcessor = mock(ResultProcessor.class);
    when(resultProcessor.getReturnedType()).thenReturn(returnedType);
    when(this.queryMethod.getResultProcessor()).thenReturn(resultProcessor);
    return new PartTreeFirestoreQuery(this.queryMethod, this.firestoreTemplate, new FirestoreMappingContext(), this.classMapper);
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) ReturnedType(org.springframework.data.repository.query.ReturnedType) ResultProcessor(org.springframework.data.repository.query.ResultProcessor) FirestoreMappingContext(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreMappingContext)

Example 3 with Parameters

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

the class DatastoreQueryLookupStrategyTests method resolveSqlQueryTest.

@Test
public void resolveSqlQueryTest() {
    String queryName = "fakeNamedQueryName";
    String query = "fake query";
    when(this.queryMethod.getNamedQueryName()).thenReturn(queryName);
    Query queryAnnotation = mock(Query.class);
    when(this.queryMethod.getQueryAnnotation()).thenReturn(queryAnnotation);
    NamedQueries namedQueries = mock(NamedQueries.class);
    Parameters parameters = mock(Parameters.class);
    Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(parameters);
    when(parameters.getNumberOfParameters()).thenReturn(1);
    when(parameters.getParameter(anyInt())).thenAnswer((invocation) -> {
        Parameter param = mock(Parameter.class);
        when(param.getName()).thenReturn(Optional.of("tag"));
        Mockito.<Class>when(param.getType()).thenReturn(Object.class);
        return param;
    });
    when(namedQueries.hasQuery(eq(queryName))).thenReturn(true);
    when(namedQueries.getQuery(eq(queryName))).thenReturn(query);
    this.datastoreQueryLookupStrategy.resolveQuery(null, null, null, namedQueries);
    verify(this.datastoreQueryLookupStrategy, times(1)).createGqlDatastoreQuery(eq(Object.class), same(this.queryMethod), eq(query));
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) Parameter(org.springframework.data.repository.query.Parameter) NamedQueries(org.springframework.data.repository.core.NamedQueries) Test(org.junit.Test)

Example 4 with Parameters

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

the class JdbcRepositoryQueryUnitTests method setup.

@Before
public void setup() throws NoSuchMethodException {
    Parameters parameters = new DefaultParameters(JdbcRepositoryQueryUnitTests.class.getDeclaredMethod("dummyMethod"));
    queryMethod = mock(JdbcQueryMethod.class);
    when(queryMethod.getParameters()).thenReturn(parameters);
    context = mock(JdbcMappingContext.class, RETURNS_DEEP_STUBS);
    defaultRowMapper = mock(RowMapper.class);
}
Also used : DefaultParameters(org.springframework.data.repository.query.DefaultParameters) Parameters(org.springframework.data.repository.query.Parameters) DefaultParameters(org.springframework.data.repository.query.DefaultParameters) JdbcMappingContext(org.springframework.data.jdbc.mapping.model.JdbcMappingContext) RowMapper(org.springframework.jdbc.core.RowMapper) Before(org.junit.Before)

Example 5 with Parameters

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

the class GqlDatastoreQuery method setOriginalParamTags.

private void setOriginalParamTags() {
    this.originalParamTags = new ArrayList<>();
    Set<String> seen = new HashSet<>();
    Parameters parameters = getQueryMethod().getParameters();
    for (int i = 0; i < parameters.getNumberOfParameters(); i++) {
        Parameter param = parameters.getParameter(i);
        if (Pageable.class.isAssignableFrom(param.getType()) || Sort.class.isAssignableFrom(param.getType())) {
            continue;
        }
        Optional<String> paramName = param.getName();
        if (!paramName.isPresent()) {
            throw new DatastoreDataException("Query method has a parameter without a valid name: " + getQueryMethod().getName());
        }
        String name = paramName.get();
        if (seen.contains(name)) {
            throw new DatastoreDataException("More than one param has the same name: " + name);
        }
        seen.add(name);
        this.originalParamTags.add(name);
    }
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) Pageable(org.springframework.data.domain.Pageable) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) Parameter(org.springframework.data.repository.query.Parameter) Sort(org.springframework.data.domain.Sort) HashSet(java.util.HashSet)

Aggregations

Parameters (org.springframework.data.repository.query.Parameters)13 Test (org.junit.Test)7 DoubleValue (com.google.cloud.datastore.DoubleValue)5 GqlQuery (com.google.cloud.datastore.GqlQuery)5 KeyValue (com.google.cloud.datastore.KeyValue)5 LongValue (com.google.cloud.datastore.LongValue)5 Value (com.google.cloud.datastore.Value)5 Parameter (org.springframework.data.repository.query.Parameter)4 Cursor (com.google.cloud.datastore.Cursor)3 DatastoreResultsIterable (org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable)3 Slice (org.springframework.data.domain.Slice)3 BeanFactoryAccessor (org.springframework.context.expression.BeanFactoryAccessor)2 BeanFactoryResolver (org.springframework.context.expression.BeanFactoryResolver)2 Page (org.springframework.data.domain.Page)2 NamedQueries (org.springframework.data.repository.core.NamedQueries)2 QueryMethodEvaluationContextProvider (org.springframework.data.repository.query.QueryMethodEvaluationContextProvider)2 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1