Search in sources :

Example 6 with Parameter

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

the class StringBasedJdbcQuery method bindParameters.

private MapSqlParameterSource bindParameters(RelationalParameterAccessor accessor) {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    Parameters<?, ?> bindableParameters = accessor.getBindableParameters();
    for (Parameter bindableParameter : bindableParameters) {
        convertAndAddParameter(parameters, bindableParameter, accessor.getBindableValue(bindableParameter.getIndex()));
    }
    return parameters;
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Parameter(org.springframework.data.repository.query.Parameter)

Example 7 with Parameter

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

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

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

Parameter (org.springframework.data.repository.query.Parameter)9 Parameters (org.springframework.data.repository.query.Parameters)4 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 NamedQueries (org.springframework.data.repository.core.NamedQueries)2 Part (org.springframework.data.repository.query.parser.Part)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1 DatastoreDataException (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException)1 SpannerDataException (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)1 Pageable (org.springframework.data.domain.Pageable)1 Sort (org.springframework.data.domain.Sort)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1