Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Code formatting.
Adding author tags.
Added test for jdbc.
Fixed typo.

See #1744
Original pull request #1745
  • Loading branch information
schauder committed Mar 13, 2024
1 parent 4a78717 commit 1600740
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
*
* @author Mark Paluch
* @author Jens Schauder
* @author Yan Qiang
* @since 3.0
*/
public class QueryMapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
import static org.mockito.Mockito.*;
import static org.springframework.data.domain.Sort.Order.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Sort;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.relational.core.mapping.Column;
Expand All @@ -52,6 +55,16 @@ public class QueryMapperUnitTests {
QueryMapper mapper = new QueryMapper(converter);
MapSqlParameterSource parameterSource = new MapSqlParameterSource();

QueryMapper createMapper(Converter<?, ?>... converters) {

JdbcCustomConversions conversions = new JdbcCustomConversions(Arrays.asList(converters));

JdbcConverter converter = new MappingJdbcConverter(context, mock(RelationResolver.class), conversions,
mock(JdbcTypeFactory.class));

return new QueryMapper(converter);
}

@Test // DATAJDBC-318
public void shouldNotMapEmptyCriteria() {

Expand Down Expand Up @@ -308,6 +321,18 @@ public void shouldMapIsNotIn() {
assertThat(condition).hasToString("person.\"NAME\" NOT IN (?[:name], ?[:name1], ?[:name2])");
}

@Test
void shouldMapIsNotInWithCollectionToStringConverter() {

mapper = createMapper(CollectionToStringConverter.INSTANCE);

Criteria criteria = Criteria.where("name").notIn("a", "b", "c");

Condition bindings = map(criteria);

assertThat(bindings).hasToString("person.\"NAME\" NOT IN (?[:name], ?[:name1], ?[:name2])");
}

@Test // DATAJDBC-318
public void shouldMapIsGt() {

Expand Down Expand Up @@ -415,7 +440,7 @@ public void shouldNotMapSortWithIllegalExpression(String input) {

assertThatThrownBy(
() -> mapper.getMappedSort(Table.create("tbl"), sort, context.getRequiredPersistentEntity(Person.class)))
.isInstanceOf(IllegalArgumentException.class);
.isInstanceOf(IllegalArgumentException.class);
}

@Test // GH-1507
Expand All @@ -429,7 +454,7 @@ public void shouldMapSortWithUnsafeExpression() {

assertThat(fields) //
.extracting(Objects::toString) //
.containsExactly( unsafeExpression + " ASC");
.containsExactly(unsafeExpression + " ASC");
}

private Condition map(Criteria criteria) {
Expand All @@ -443,4 +468,13 @@ static class Person {
String name;
@Column("another_name") String alternative;
}

enum CollectionToStringConverter implements Converter<Collection<?>, String> {
INSTANCE;

@Override
public String convert(Collection<?> source) {
return source.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
* @author Roman Chigvintsev
* @author Manousos Mathioudakis
* @author Jens Schauder
* @author Yan Qiang
*/
public class QueryMapper {

Expand Down Expand Up @@ -142,15 +143,13 @@ public List<OrderByField> getMappedSort(Table table, Sort sort, @Nullable Relati
SqlSort.validate(order);

OrderByField simpleOrderByField = createSimpleOrderByField(table, entity, order);
OrderByField orderBy = simpleOrderByField
.withNullHandling(order.getNullHandling());
OrderByField orderBy = simpleOrderByField.withNullHandling(order.getNullHandling());
mappedOrder.add(order.isAscending() ? orderBy.asc() : orderBy.desc());
}

return mappedOrder;
}


private OrderByField createSimpleOrderByField(Table table, RelationalPersistentEntity<?> entity, Sort.Order order) {

if (order instanceof SqlSort.SqlOrder sqlOrder && sqlOrder.isUnsafe()) {
Expand Down Expand Up @@ -364,7 +363,7 @@ private Condition mapCondition(CriteriaDefinition criteria, MutableBindings bind
Class<?> typeHint;

Comparator comparator = criteria.getComparator();
if (criteria.getValue()instanceof Parameter parameter) {
if (criteria.getValue() instanceof Parameter parameter) {

mappedValue = convertValue(comparator, parameter.getValue(), propertyField.getTypeHint());
typeHint = getTypeHint(mappedValue, actualType.getType(), parameter);
Expand Down Expand Up @@ -411,7 +410,8 @@ public Parameter getBindValue(Parameter value) {
@Nullable
private Object convertValue(Comparator comparator, @Nullable Object value, TypeInformation<?> typeHint) {

if ((Comparator.IN.equals(comparator) || Comparator.NOT_IN.equals(comparator)) && value instanceof Collection<?> collection && !collection.isEmpty()) {
if ((Comparator.IN.equals(comparator) || Comparator.NOT_IN.equals(comparator))
&& value instanceof Collection<?> collection && !collection.isEmpty()) {

Collection<Object> mapped = new ArrayList<>(collection.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
import static org.mockito.Mockito.*;
import static org.springframework.data.domain.Sort.Order.*;

import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.junit.jupiter.api.Test;
import org.springframework.core.convert.converter.Converter;
Expand Down Expand Up @@ -50,6 +54,7 @@
* @author Mark Paluch
* @author Mingyuan Wu
* @author Jens Schauder
* @author Yan Qiang
*/
class QueryMapperUnitTests {

Expand All @@ -61,6 +66,7 @@ QueryMapper createMapper(R2dbcDialect dialect) {
}

QueryMapper createMapper(R2dbcDialect dialect, Converter<?, ?>... converters) {

R2dbcCustomConversions conversions = R2dbcCustomConversions.of(dialect, Arrays.asList(converters));

R2dbcMappingContext context = new R2dbcMappingContext();
Expand Down Expand Up @@ -359,9 +365,10 @@ void shouldMapIsNotIn() {
}

@Test
void sholdMapIsNotInWithCollectionToStringConverter() {
void shouldMapIsNotInWithCollectionToStringConverter() {

mapper = createMapper(PostgresDialect.INSTANCE, JsonNodeToStringConverter.INSTANCE, StringToJsonNodeConverter.INSTANCE, CollectionToStringConverter.INSTANCE);
mapper = createMapper(PostgresDialect.INSTANCE, JsonNodeToStringConverter.INSTANCE,
StringToJsonNodeConverter.INSTANCE, CollectionToStringConverter.INSTANCE);

Criteria criteria = Criteria.where("name").notIn("a", "b", "c");

Expand Down Expand Up @@ -479,14 +486,14 @@ public void shouldMapSortWithAllowedSpecialCharacters() {
.containsExactly("tbl.x(._)x DESC");
}


@Test // GH-1507
public void shouldNotMapSortWithIllegalExpression() {

Sort sort = Sort.by(desc("unknown Field"));

assertThatThrownBy(() -> mapper.getMappedSort(Table.create("tbl"), sort,
mapper.getMappingContext().getRequiredPersistentEntity(Person.class))).isInstanceOf(IllegalArgumentException.class);
mapper.getMappingContext().getRequiredPersistentEntity(Person.class)))
.isInstanceOf(IllegalArgumentException.class);
}

@Test // gh-369
Expand Down Expand Up @@ -588,13 +595,13 @@ public String convert(JsonNode source) {

enum CollectionToStringConverter implements Converter<Collection<?>, String> {
INSTANCE;

@Override
public String convert(Collection<?> source) {
return source.toString();
}
}


enum StringToJsonNodeConverter implements Converter<String, JsonNode> {
INSTANCE;

Expand Down

0 comments on commit 1600740

Please sign in to comment.