Search in sources :

Example 1 with Criteria

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

the class CriteriaFactoryUnitTests method shouldConsiderArrayValuesInInOperator.

// DATAJDBC-539
@Test
void shouldConsiderArrayValuesInInOperator() {
    QueryMethod queryMethod = getQueryMethod("findAllByNameIn", String[].class);
    RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { new String[] { "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"));
}
Also used : QueryMethod(org.springframework.data.repository.query.QueryMethod) Part(org.springframework.data.repository.query.parser.Part) Criteria(org.springframework.data.relational.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Example 2 with Criteria

use of org.springframework.data.relational.core.query.Criteria 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 3 with Criteria

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

the class CriteriaUnitTests method shouldBuildNotEqualsCriteria.

// DATAJDBC-513
@Test
void shouldBuildNotEqualsCriteria() {
    Criteria criteria = where("foo").not("bar");
    assertThat(criteria.getColumn()).isEqualTo(SqlIdentifier.unquoted("foo"));
    assertThat(criteria.getComparator()).isEqualTo(CriteriaDefinition.Comparator.NEQ);
    assertThat(criteria.getValue()).isEqualTo("bar");
}
Also used : Criteria(org.springframework.data.relational.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Example 4 with Criteria

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

the class CriteriaUnitTests method orGroupedCriteria.

// DATAJDBC-513
@Test
void orGroupedCriteria() {
    Criteria criteria = where("foo").is("bar").or(where("foo").is("baz"));
    assertThat(criteria.isGroup()).isTrue();
    assertThat(criteria.getGroup()).hasSize(1);
    assertThat(criteria.getGroup().get(0).getColumn()).isEqualTo(SqlIdentifier.unquoted("foo"));
    assertThat(criteria.getCombinator()).isEqualTo(Criteria.Combinator.OR);
    criteria = criteria.getPrevious();
    assertThat(criteria).isNotNull();
    assertThat(criteria.getColumn()).isEqualTo(SqlIdentifier.unquoted("foo"));
    assertThat(criteria.getComparator()).isEqualTo(CriteriaDefinition.Comparator.EQ);
    assertThat(criteria.getValue()).isEqualTo("bar");
}
Also used : Criteria(org.springframework.data.relational.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Example 5 with Criteria

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

the class CriteriaUnitTests method shouldBuildEqualsIgnoreCaseCriteria.

@Test
void shouldBuildEqualsIgnoreCaseCriteria() {
    Criteria criteria = where("foo").is("bar").ignoreCase(true);
    assertThat(criteria.getColumn()).isEqualTo(SqlIdentifier.unquoted("foo"));
    assertThat(criteria.getComparator()).isEqualTo(CriteriaDefinition.Comparator.EQ);
    assertThat(criteria.getValue()).isEqualTo("bar");
    assertThat(criteria.isIgnoreCase()).isTrue();
}
Also used : Criteria(org.springframework.data.relational.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Aggregations

Criteria (org.springframework.data.relational.core.query.Criteria)50 Test (org.junit.jupiter.api.Test)49 Condition (org.springframework.data.relational.core.sql.Condition)22 QueryMethod (org.springframework.data.repository.query.QueryMethod)2 Part (org.springframework.data.repository.query.parser.Part)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Example (org.springframework.data.domain.Example)1 ExampleMatcher (org.springframework.data.domain.ExampleMatcher)1 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)1 PropertyHandler (org.springframework.data.mapping.PropertyHandler)1 MappingContext (org.springframework.data.mapping.context.MappingContext)1 RelationalPersistentEntity (org.springframework.data.relational.core.mapping.RelationalPersistentEntity)1 RelationalPersistentProperty (org.springframework.data.relational.core.mapping.RelationalPersistentProperty)1 Query (org.springframework.data.relational.core.query.Query)1 ExampleMatcherAccessor (org.springframework.data.support.ExampleMatcherAccessor)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1 Assert (org.springframework.util.Assert)1