use of org.springframework.data.repository.query.QueryMethod in project spring-data-keyvalue by spring-projects.
the class KeyValuePartTreeQueryUnitTests method shouldApplyDerivedMaxResultsToQueryWithParameters.
// DATAKV-142
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void shouldApplyDerivedMaxResultsToQueryWithParameters() throws SecurityException, NoSuchMethodException {
when(metadataMock.getDomainType()).thenReturn((Class) Person.class);
when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class);
QueryMethod qm = new QueryMethod(Repo.class.getMethod("findTop3ByFirstname", String.class), metadataMock, projectionFactoryMock);
KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE, kvOpsMock, SpelQueryCreator.class);
KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] { "firstname" });
assertThat(query.getCriteria(), is(notNullValue()));
assertThat(query.getCriteria(), IsInstanceOf.instanceOf(SpelCriteria.class));
assertThat(((SpelCriteria) query.getCriteria()).getExpression().getExpressionString(), is("#it?.firstname?.equals([0])"));
assertThat(query.getRows(), is(3));
}
use of org.springframework.data.repository.query.QueryMethod in project spring-data-keyvalue by spring-projects.
the class CachingKeyValuePartTreeQueryUnitTests method cachedSpelExpressionShouldBeReusedWithNewContext.
// DATAKV-137
@Test
public void cachedSpelExpressionShouldBeReusedWithNewContext() throws NoSuchMethodException, SecurityException {
QueryMethod qm = new QueryMethod(Repo.class.getMethod("findByFirstname", String.class), metadataMock, projectionFactoryMock);
KeyValuePartTreeQuery query = new CachingKeyValuePartTreeQuery(qm, DefaultEvaluationContextProvider.INSTANCE, kvOpsMock, SpelQueryCreator.class);
Object[] args = new Object[] { "foo" };
SpelCriteria first = (SpelCriteria) query.prepareQuery(args).getCriteria();
SpelCriteria second = (SpelCriteria) query.prepareQuery(args).getCriteria();
assertThat(first.getExpression(), sameInstance(second.getExpression()));
assertThat(first.getContext(), not(sameInstance(second.getContext())));
}
use of org.springframework.data.repository.query.QueryMethod in project spring-data-keyvalue by spring-projects.
the class SpelQueryCreatorUnitTests method createQueryForMethodWithArgs.
private KeyValueQuery<SpelExpression> createQueryForMethodWithArgs(String methodName, Object... args) throws NoSuchMethodException, SecurityException {
Class<?>[] argTypes = new Class<?>[args.length];
if (!ObjectUtils.isEmpty(args)) {
for (int i = 0; i < args.length; i++) {
argTypes[i] = args[i].getClass();
}
}
Method method = PersonRepository.class.getMethod(methodName, argTypes);
doReturn(Person.class).when(metadataMock).getReturnedDomainClass(method);
PartTree partTree = new PartTree(method.getName(), method.getReturnType());
SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor(new QueryMethod(method, metadataMock, new SpelAwareProxyProjectionFactory()).getParameters(), args));
KeyValueQuery<SpelExpression> q = creator.createQuery();
q.getCriteria().setEvaluationContext(new StandardEvaluationContext(args));
return q;
}
use of org.springframework.data.repository.query.QueryMethod in project spring-cloud-gcp by spring-cloud.
the class SpannerQueryLookupStrategy method resolveQuery.
@Override
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory, NamedQueries namedQueries) {
QueryMethod queryMethod = createQueryMethod(method, metadata, factory);
Class entityType = getEntityType(queryMethod);
if (namedQueries.hasQuery(queryMethod.getNamedQueryName())) {
String sql = namedQueries.getQuery(queryMethod.getNamedQueryName());
return createSqlSpannerQuery(entityType, queryMethod, sql);
}
return createPartTreeSpannerQuery(entityType, queryMethod);
}
use of org.springframework.data.repository.query.QueryMethod in project spring-data-jdbc by spring-projects.
the class CriteriaFactoryUnitTests method shouldConsiderIterableValuesInInOperator.
// DATAJDBC-539
@Test
void shouldConsiderIterableValuesInInOperator() {
QueryMethod queryMethod = getQueryMethod("findAllByNameIn", List.class);
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, Arrays.asList("foo", "bar"));
ParameterMetadataProvider parameterMetadata = new ParameterMetadataProvider(accessor);
CriteriaFactory criteriaFactory = new CriteriaFactory(parameterMetadata);
Part part = new Part("NameIn", User.class);
Criteria criteria = criteriaFactory.createCriteria(part);
assertThat(criteria.getValue()).isEqualTo(Arrays.asList("foo", "bar"));
}
Aggregations