Search in sources :

Example 6 with Query

use of org.springframework.data.relational.core.query.Query in project spring-data-jdbc by spring-projects.

the class RelationalExampleMapperTests method queryByExampleWithFirstnameWithStringMatchingOnTheEndingIncludingNull.

// GH-929
@Test
void queryByExampleWithFirstnameWithStringMatchingOnTheEndingIncludingNull() {
    Person person = new Person();
    person.setFirstname("do");
    ExampleMatcher matcher = matching().withStringMatcher(ENDING).withIncludeNullValues();
    Example<Person> example = Example.of(person, matcher);
    Query query = exampleMapper.getMappedExample(example);
    // 
    assertThat(query.getCriteria()).map(// 
    Object::toString).hasValue("(firstname IS NULL OR firstname LIKE '%do')");
}
Also used : Query(org.springframework.data.relational.core.query.Query) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Test(org.junit.jupiter.api.Test)

Example 7 with Query

use of org.springframework.data.relational.core.query.Query 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);
}
Also used : Query(org.springframework.data.relational.core.query.Query) MappingContext(org.springframework.data.mapping.context.MappingContext) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Example(org.springframework.data.domain.Example) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ArrayList(java.util.ArrayList) PropertyHandler(org.springframework.data.mapping.PropertyHandler) ExampleMatcherAccessor(org.springframework.data.support.ExampleMatcherAccessor) List(java.util.List) Optional(java.util.Optional) Criteria(org.springframework.data.relational.core.query.Criteria) RelationalPersistentEntity(org.springframework.data.relational.core.mapping.RelationalPersistentEntity) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty) Assert(org.springframework.util.Assert) Optional(java.util.Optional) ArrayList(java.util.ArrayList) ExampleMatcherAccessor(org.springframework.data.support.ExampleMatcherAccessor) Criteria(org.springframework.data.relational.core.query.Criteria) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty)

Example 8 with Query

use of org.springframework.data.relational.core.query.Query 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!')");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StringMatcher(org.springframework.data.domain.ExampleMatcher.StringMatcher) RelationalMappingContext(org.springframework.data.relational.core.mapping.RelationalMappingContext) GenericPropertyMatchers(org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers) Query(org.springframework.data.relational.core.query.Query) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Example(org.springframework.data.domain.Example) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) Data(lombok.Data) Assertions(org.assertj.core.api.Assertions) AllArgsConstructor(lombok.AllArgsConstructor) Id(org.springframework.data.annotation.Id) NoArgsConstructor(lombok.NoArgsConstructor) Query(org.springframework.data.relational.core.query.Query) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Test(org.junit.jupiter.api.Test)

Example 9 with Query

use of org.springframework.data.relational.core.query.Query in project spring-data-jdbc by spring-projects.

the class RelationalExampleMapperTests method queryByExampleWithNullMatchingFirstnameAndLastname.

// GH-929
@Test
void queryByExampleWithNullMatchingFirstnameAndLastname() {
    Person person = new Person();
    person.setFirstname("Bilbo");
    person.setLastname("Baggins");
    ExampleMatcher matcher = matching().withIncludeNullValues();
    Example<Person> example = Example.of(person, matcher);
    Query query = exampleMapper.getMappedExample(example);
    // 
    assertThat(query.getCriteria().map(Object::toString).get()).contains(// 
    "(firstname IS NULL OR firstname = 'Bilbo')", // 
    " AND ", "(lastname IS NULL OR lastname = 'Baggins')");
}
Also used : Query(org.springframework.data.relational.core.query.Query) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Test(org.junit.jupiter.api.Test)

Example 10 with Query

use of org.springframework.data.relational.core.query.Query in project spring-data-jdbc by spring-projects.

the class RelationalExampleMapperTests method queryByExampleWithFirstnameWithStringMatchingOnTheEnding.

// GH-929
@Test
void queryByExampleWithFirstnameWithStringMatchingOnTheEnding() {
    Person person = new Person();
    person.setFirstname("do");
    ExampleMatcher matcher = matching().withStringMatcher(ENDING);
    Example<Person> example = Example.of(person, matcher);
    Query query = exampleMapper.getMappedExample(example);
    // 
    assertThat(query.getCriteria()).map(// 
    Object::toString).hasValue("(firstname LIKE '%do')");
}
Also used : Query(org.springframework.data.relational.core.query.Query) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) Test(org.junit.jupiter.api.Test)

Aggregations

Query (org.springframework.data.relational.core.query.Query)22 Test (org.junit.jupiter.api.Test)21 ExampleMatcher (org.springframework.data.domain.ExampleMatcher)18 Example (org.springframework.data.domain.Example)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 AllArgsConstructor (lombok.AllArgsConstructor)1 Data (lombok.Data)1 NoArgsConstructor (lombok.NoArgsConstructor)1 Assertions (org.assertj.core.api.Assertions)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Id (org.springframework.data.annotation.Id)1 GenericPropertyMatchers (org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers)1 StringMatcher (org.springframework.data.domain.ExampleMatcher.StringMatcher)1 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)1 PropertyHandler (org.springframework.data.mapping.PropertyHandler)1 MappingContext (org.springframework.data.mapping.context.MappingContext)1 RelationalMappingContext (org.springframework.data.relational.core.mapping.RelationalMappingContext)1