Search in sources :

Example 6 with Parameters

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

the class SpannerRepositoryFactory 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(SpannerRepositoryFactory.this.applicationContext);
            evaluationContext.addPropertyAccessor(new BeanFactoryAccessor());
            evaluationContext.setBeanResolver(new BeanFactoryResolver(SpannerRepositoryFactory.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 7 with Parameters

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

the class SpannerQueryLookupStrategyTests method resolveSqlQueryTest.

@Test
public void resolveSqlQueryTest() {
    String queryName = "fakeNamedQueryName";
    String query = "fake query";
    when(this.queryMethod.getNamedQueryName()).thenReturn(queryName);
    NamedQueries namedQueries = mock(NamedQueries.class);
    Parameters parameters = mock(Parameters.class);
    // @formatter:off
    Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(parameters);
    // @formatter:off
    when(parameters.getNumberOfParameters()).thenReturn(1);
    when(parameters.getParameter(anyInt())).thenAnswer((invocation) -> {
        Parameter param = mock(Parameter.class);
        when(param.getName()).thenReturn(Optional.of("tag"));
        // @formatter:off
        Mockito.<Class>when(param.getType()).thenReturn(Object.class);
        // @formatter:on
        return param;
    });
    when(namedQueries.hasQuery(eq(queryName))).thenReturn(true);
    when(namedQueries.getQuery(eq(queryName))).thenReturn(query);
    this.spannerQueryLookupStrategy.resolveQuery(null, null, null, namedQueries);
    verify(this.spannerQueryLookupStrategy, times(1)).createSqlSpannerQuery(eq(Object.class), same(this.queryMethod), eq(query), eq(false));
}
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 8 with Parameters

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

the class GqlDatastoreQueryTests method pageableTestPage.

@Test
public void pageableTestPage() {
    String gql = "SELECT * FROM trades WHERE price=@price";
    String expected = "SELECT * FROM trades WHERE price=@price LIMIT @limit OFFSET @offset";
    Object[] paramVals = new Object[] { 1, PageRequest.of(0, 2) };
    String[] paramNames = new String[] { "price", null };
    Parameters parameters = buildParameters(paramVals, paramNames);
    Mockito.<Class>when(this.queryMethod.getReturnedObjectType()).thenReturn(Trade.class);
    when(parameters.hasPageableParameter()).thenReturn(true);
    when(parameters.getPageableIndex()).thenReturn(1);
    GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, true, true);
    Cursor cursor = Cursor.copyFrom("abc".getBytes());
    doAnswer((invocation) -> {
        GqlQuery statement = invocation.getArgument(0);
        assertThat(statement.getQueryString().equals(gql) || statement.getQueryString().equals(expected)).isEqualTo(true);
        Map<String, Value> paramMap = statement.getNamedBindings();
        if (statement.getQueryString().equals(expected)) {
            assertThat(paramMap.size()).isEqualTo(3);
            assertThat(paramMap.get("price").get()).isEqualTo(1L);
            assertThat(paramMap.get("limit").get()).isEqualTo(2L);
            assertThat(paramMap.get("offset").get()).isEqualTo(0L);
            return new DatastoreResultsIterable(Collections.emptyList(), cursor);
        } else if (statement.getQueryString().equals(gql)) {
            assertThat(paramMap.size()).isEqualTo(1);
            assertThat(paramMap.get("price").get()).isEqualTo(1L);
            return new DatastoreResultsIterable(Arrays.asList(1L, 2L), cursor);
        }
        return null;
    }).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));
    doReturn(false).when(gqlDatastoreQuery).isNonEntityReturnedType(any());
    doAnswer((invocation) -> invocation.getArgument(0)).when(gqlDatastoreQuery).processRawObjectForProjection(any());
    Slice result = (Page) gqlDatastoreQuery.execute(paramVals);
    assertThat(((DatastorePageable) result.getPageable()).toCursor()).isEqualTo(cursor);
    assertThat(((DatastorePageable) result.getPageable()).getTotalCount()).isEqualTo(2L);
    assertThat(((Page) result).getTotalElements()).isEqualTo(2L);
    verify(this.datastoreTemplate, times(2)).queryKeysOrEntities(any(), eq(Trade.class));
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) Page(org.springframework.data.domain.Page) Cursor(com.google.cloud.datastore.Cursor) GqlQuery(com.google.cloud.datastore.GqlQuery) Slice(org.springframework.data.domain.Slice) LongValue(com.google.cloud.datastore.LongValue) DoubleValue(com.google.cloud.datastore.DoubleValue) KeyValue(com.google.cloud.datastore.KeyValue) Value(com.google.cloud.datastore.Value) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) Test(org.junit.Test)

Example 9 with Parameters

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

the class GqlDatastoreQueryTests method buildParameters.

private Parameters buildParameters(Object[] params, String[] paramNames) {
    Parameters parameters = mock(Parameters.class);
    Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(parameters);
    when(parameters.getNumberOfParameters()).thenReturn(paramNames.length);
    when(parameters.getParameter(anyInt())).thenAnswer((invocation) -> {
        int index = invocation.getArgument(0);
        Parameter param = mock(Parameter.class);
        when(param.getName()).thenReturn(paramNames[index] == null ? Optional.empty() : Optional.of(paramNames[index]));
        Mockito.<Class>when(param.getType()).thenReturn(params[index].getClass());
        return param;
    });
    return parameters;
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) Parameter(org.springframework.data.repository.query.Parameter)

Example 10 with Parameters

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

the class GqlDatastoreQueryTests method pageableTest.

@Test
public void pageableTest() {
    String gql = "SELECT * FROM trades WHERE price=@price";
    Object[] paramVals = new Object[] { 1, PageRequest.of(0, 2) };
    String[] paramNames = new String[] { "price", null };
    Parameters parameters = buildParameters(paramVals, paramNames);
    when(parameters.hasPageableParameter()).thenReturn(true);
    when(parameters.getPageableIndex()).thenReturn(1);
    GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, false, false);
    doAnswer((invocation) -> {
        GqlQuery statement = invocation.getArgument(0);
        assertThat(statement.getQueryString()).isEqualTo("SELECT * FROM trades WHERE price=@price LIMIT @limit OFFSET @offset");
        Map<String, Value> paramMap = statement.getNamedBindings();
        assertThat(paramMap.get("price").get()).isEqualTo(1L);
        assertThat(paramMap.get("limit").get()).isEqualTo(2L);
        assertThat(paramMap.get("offset").get()).isEqualTo(0L);
        return null;
    }).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));
    doReturn(false).when(gqlDatastoreQuery).isNonEntityReturnedType(any());
    gqlDatastoreQuery.execute(paramVals);
    verify(this.datastoreTemplate, times(1)).queryKeysOrEntities(any(), eq(Trade.class));
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) LongValue(com.google.cloud.datastore.LongValue) DoubleValue(com.google.cloud.datastore.DoubleValue) KeyValue(com.google.cloud.datastore.KeyValue) Value(com.google.cloud.datastore.Value) GqlQuery(com.google.cloud.datastore.GqlQuery) Test(org.junit.Test)

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