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();
}
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);
}
}
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;
}
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));
}
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;
}
Aggregations