use of org.springframework.data.domain.Example in project spring-data-jdbc by spring-projects.
the class RelationalExampleMapper method getMappedExample.
/**
* Transform each property of the {@link Example}'s probe into a {@link Criteria} and assemble them into a
* {@link Query}.
*
* @param example
* @param entity
* @return query
*/
private <T> Query getMappedExample(Example<T> example, RelationalPersistentEntity<?> entity) {
Assert.notNull(example, "Example must not be null!");
Assert.notNull(entity, "RelationalPersistentEntity must not be null!");
PersistentPropertyAccessor<T> propertyAccessor = entity.getPropertyAccessor(example.getProbe());
ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher());
final List<Criteria> criteriaBasedOnProperties = new ArrayList<>();
entity.doWithProperties((PropertyHandler<RelationalPersistentProperty>) property -> {
if (matcherAccessor.isIgnoredPath(property.getName())) {
return;
}
Optional<?> optionalConvertedPropValue = matcherAccessor.getValueTransformerForPath(property.getName()).apply(Optional.ofNullable(propertyAccessor.getProperty(property)));
if (!optionalConvertedPropValue.isPresent()) {
return;
}
Object convPropValue = optionalConvertedPropValue.get();
boolean ignoreCase = matcherAccessor.isIgnoreCaseForPath(property.getName());
String column = property.getName();
switch(matcherAccessor.getStringMatcherForPath(property.getName())) {
case DEFAULT:
case EXACT:
criteriaBasedOnProperties.add(includeNulls(example) ? Criteria.where(column).isNull().or(column).is(convPropValue).ignoreCase(ignoreCase) : Criteria.where(column).is(convPropValue).ignoreCase(ignoreCase));
break;
case ENDING:
criteriaBasedOnProperties.add(includeNulls(example) ? Criteria.where(column).isNull().or(column).like("%" + convPropValue).ignoreCase(ignoreCase) : Criteria.where(column).like("%" + convPropValue).ignoreCase(ignoreCase));
break;
case STARTING:
criteriaBasedOnProperties.add(includeNulls(example) ? Criteria.where(column).isNull().or(column).like(convPropValue + "%").ignoreCase(ignoreCase) : Criteria.where(column).like(convPropValue + "%").ignoreCase(ignoreCase));
break;
case CONTAINING:
criteriaBasedOnProperties.add(includeNulls(example) ? Criteria.where(column).isNull().or(column).like("%" + convPropValue + "%").ignoreCase(ignoreCase) : Criteria.where(column).like("%" + convPropValue + "%").ignoreCase(ignoreCase));
break;
default:
throw new IllegalStateException(example.getMatcher().getDefaultStringMatcher() + " is not supported!");
}
});
// Criteria, assemble!
Criteria criteria = Criteria.empty();
for (Criteria propertyCriteria : criteriaBasedOnProperties) {
if (example.getMatcher().isAllMatching()) {
criteria = criteria.and(propertyCriteria);
} else {
criteria = criteria.or(propertyCriteria);
}
}
return Query.query(criteria);
}
use of org.springframework.data.domain.Example in project spring-data-mongodb by spring-projects.
the class SimpleReactiveMongoRepository method findOne.
// -------------------------------------------------------------------------
// Methods from ReactiveMongoRepository
// -------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#findOne(org.springframework.data.domain.Example)
*/
@Override
public <S extends T> Mono<S> findOne(Example<S> example) {
Assert.notNull(example, "Sample must not be null!");
Query query = //
new Query(new Criteria().alike(example)).collation(//
entityInformation.getCollation()).limit(2);
return mongoOperations.find(query, example.getProbeType(), entityInformation.getCollectionName()).buffer(2).map(vals -> {
if (vals.size() > 1) {
throw new IncorrectResultSizeDataAccessException(1);
}
return vals.iterator().next();
}).next();
}
use of org.springframework.data.domain.Example in project spring-data-jdbc by spring-projects.
the class RelationalExampleMapperTests method queryByExampleSupportsPropertyTransforms.
// GH-929
@Test
void queryByExampleSupportsPropertyTransforms() {
Person person = new Person();
person.setFirstname("Frodo");
person.setLastname("Baggins");
person.setSecret("I have the ring!");
ExampleMatcher matcher = //
matching().withTransformer("firstname", o -> {
if (o.isPresent()) {
return o.map(o1 -> ((String) o1).toUpperCase());
}
return o;
}).withTransformer("lastname", o -> {
if (o.isPresent()) {
return o.map(o1 -> ((String) o1).toLowerCase());
}
return o;
});
Example<Person> example = Example.of(person, matcher);
Query query = exampleMapper.getMappedExample(example);
//
assertThat(query.getCriteria().map(Object::toString).get()).contains(//
"(firstname = 'FRODO')", //
" AND ", //
"(lastname = 'baggins')", "(secret = 'I have the ring!')");
}
Aggregations