Search in sources :

Example 1 with Parameter

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

the class MongoParametersUnitTests method discoversDistanceParameter.

@Test
void discoversDistanceParameter() throws NoSuchMethodException, SecurityException {
    Method method = PersonRepository.class.getMethod("findByLocationNear", Point.class, Distance.class);
    MongoParameters parameters = new MongoParameters(method, false);
    assertThat(parameters.getNumberOfParameters()).isEqualTo(2);
    assertThat(parameters.getMaxDistanceIndex()).isEqualTo(1);
    assertThat(parameters.getBindableParameters().getNumberOfParameters()).isOne();
    Parameter parameter = parameters.getParameter(1);
    assertThat(parameter.isSpecialParameter()).isTrue();
    assertThat(parameter.isBindable()).isFalse();
}
Also used : Parameter(org.springframework.data.repository.query.Parameter) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 2 with Parameter

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

the class RelationalQueryCreator method throwExceptionOnArgumentMismatch.

private static void throwExceptionOnArgumentMismatch(Part part, Parameters<?, ?> parameters, int index) {
    Part.Type type = part.getType();
    String property = part.getProperty().toDotPath();
    if (!parameters.getBindableParameters().hasParameterAt(index)) {
        String msgTemplate = "Query method expects at least %d arguments but only found %d. " + "This leaves an operator of type %s for property %s unbound.";
        String formattedMsg = String.format(msgTemplate, index + 1, index, type.name(), property);
        throw new IllegalStateException(formattedMsg);
    }
    Parameter parameter = parameters.getBindableParameter(index);
    if (expectsCollection(type) && !parameterIsCollectionLike(parameter)) {
        String message = wrongParameterTypeMessage(property, type, "Collection", parameter);
        throw new IllegalStateException(message);
    } else if (!expectsCollection(type) && !parameterIsScalarLike(parameter)) {
        String message = wrongParameterTypeMessage(property, type, "scalar", parameter);
        throw new IllegalStateException(message);
    }
}
Also used : Part(org.springframework.data.repository.query.parser.Part) Parameter(org.springframework.data.repository.query.Parameter)

Example 3 with Parameter

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

the class ParameterMetadataProvider method next.

/**
 * Creates new instance of {@link ParameterMetadata} for the given {@link Part} and next {@link Parameter}.
 */
public ParameterMetadata next(Part part) {
    Assert.isTrue(bindableParameterIterator.hasNext(), () -> String.format("No parameter available for part %s.", part));
    Parameter parameter = bindableParameterIterator.next();
    String parameterName = getParameterName(parameter, part.getProperty().getSegment());
    Object parameterValue = getParameterValue();
    Part.Type partType = part.getType();
    checkNullIsAllowed(parameterName, parameterValue, partType);
    Class<?> parameterType = parameter.getType();
    Object preparedParameterValue = prepareParameterValue(parameterValue, parameterType, partType);
    ParameterMetadata metadata = new ParameterMetadata(parameterName, preparedParameterValue, parameterType);
    parameterMetadata.add(metadata);
    return metadata;
}
Also used : Part(org.springframework.data.repository.query.parser.Part) Parameter(org.springframework.data.repository.query.Parameter)

Example 4 with Parameter

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

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

the class SqlSpannerQuery method getParamTags.

private List<String> getParamTags() {
    List<String> tags = 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 (isPageableOrSort(param.getType())) {
            continue;
        }
        Optional<String> paramName = param.getName();
        if (!paramName.isPresent()) {
            throw new SpannerDataException("Query method has a parameter without a valid name: " + getQueryMethod().getName());
        }
        String name = paramName.get();
        if (seen.contains(name)) {
            throw new SpannerDataException("More than one param has the same name: " + name);
        }
        seen.add(name);
        tags.add(name);
    }
    return tags;
}
Also used : ArrayList(java.util.ArrayList) Parameter(org.springframework.data.repository.query.Parameter) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) 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