Skip to content

Commit 1600740

Browse files
committed
Polishing.
Code formatting. Adding author tags. Added test for jdbc. Fixed typo. See #1744 Original pull request #1745
1 parent 4a78717 commit 1600740

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/QueryMapper.java

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
*
5454
* @author Mark Paluch
5555
* @author Jens Schauder
56+
* @author Yan Qiang
5657
* @since 3.0
5758
*/
5859
public class QueryMapper {

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/QueryMapperUnitTests.java

+36-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
import static org.mockito.Mockito.*;
2020
import static org.springframework.data.domain.Sort.Order.*;
2121

22+
import java.util.Arrays;
23+
import java.util.Collection;
2224
import java.util.Collections;
2325
import java.util.List;
2426
import java.util.Objects;
2527

2628
import org.junit.jupiter.api.Test;
2729
import org.junit.jupiter.params.ParameterizedTest;
2830
import org.junit.jupiter.params.provider.ValueSource;
31+
import org.springframework.core.convert.converter.Converter;
2932
import org.springframework.data.domain.Sort;
3033
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
3134
import org.springframework.data.relational.core.mapping.Column;
@@ -52,6 +55,16 @@ public class QueryMapperUnitTests {
5255
QueryMapper mapper = new QueryMapper(converter);
5356
MapSqlParameterSource parameterSource = new MapSqlParameterSource();
5457

58+
QueryMapper createMapper(Converter<?, ?>... converters) {
59+
60+
JdbcCustomConversions conversions = new JdbcCustomConversions(Arrays.asList(converters));
61+
62+
JdbcConverter converter = new MappingJdbcConverter(context, mock(RelationResolver.class), conversions,
63+
mock(JdbcTypeFactory.class));
64+
65+
return new QueryMapper(converter);
66+
}
67+
5568
@Test // DATAJDBC-318
5669
public void shouldNotMapEmptyCriteria() {
5770

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

324+
@Test
325+
void shouldMapIsNotInWithCollectionToStringConverter() {
326+
327+
mapper = createMapper(CollectionToStringConverter.INSTANCE);
328+
329+
Criteria criteria = Criteria.where("name").notIn("a", "b", "c");
330+
331+
Condition bindings = map(criteria);
332+
333+
assertThat(bindings).hasToString("person.\"NAME\" NOT IN (?[:name], ?[:name1], ?[:name2])");
334+
}
335+
311336
@Test // DATAJDBC-318
312337
public void shouldMapIsGt() {
313338

@@ -415,7 +440,7 @@ public void shouldNotMapSortWithIllegalExpression(String input) {
415440

416441
assertThatThrownBy(
417442
() -> mapper.getMappedSort(Table.create("tbl"), sort, context.getRequiredPersistentEntity(Person.class)))
418-
.isInstanceOf(IllegalArgumentException.class);
443+
.isInstanceOf(IllegalArgumentException.class);
419444
}
420445

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

430455
assertThat(fields) //
431456
.extracting(Objects::toString) //
432-
.containsExactly( unsafeExpression + " ASC");
457+
.containsExactly(unsafeExpression + " ASC");
433458
}
434459

435460
private Condition map(Criteria criteria) {
@@ -443,4 +468,13 @@ static class Person {
443468
String name;
444469
@Column("another_name") String alternative;
445470
}
471+
472+
enum CollectionToStringConverter implements Converter<Collection<?>, String> {
473+
INSTANCE;
474+
475+
@Override
476+
public String convert(Collection<?> source) {
477+
return source.toString();
478+
}
479+
}
446480
}

spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/query/QueryMapper.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
* @author Roman Chigvintsev
5959
* @author Manousos Mathioudakis
6060
* @author Jens Schauder
61+
* @author Yan Qiang
6162
*/
6263
public class QueryMapper {
6364

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

144145
OrderByField simpleOrderByField = createSimpleOrderByField(table, entity, order);
145-
OrderByField orderBy = simpleOrderByField
146-
.withNullHandling(order.getNullHandling());
146+
OrderByField orderBy = simpleOrderByField.withNullHandling(order.getNullHandling());
147147
mappedOrder.add(order.isAscending() ? orderBy.asc() : orderBy.desc());
148148
}
149149

150150
return mappedOrder;
151151
}
152152

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

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

366365
Comparator comparator = criteria.getComparator();
367-
if (criteria.getValue()instanceof Parameter parameter) {
366+
if (criteria.getValue() instanceof Parameter parameter) {
368367

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

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

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

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/query/QueryMapperUnitTests.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
import static org.mockito.Mockito.*;
2020
import static org.springframework.data.domain.Sort.Order.*;
2121

22-
import java.util.*;
22+
import java.util.Arrays;
23+
import java.util.Collection;
24+
import java.util.Collections;
25+
import java.util.List;
26+
import java.util.Objects;
2327

2428
import org.junit.jupiter.api.Test;
2529
import org.springframework.core.convert.converter.Converter;
@@ -50,6 +54,7 @@
5054
* @author Mark Paluch
5155
* @author Mingyuan Wu
5256
* @author Jens Schauder
57+
* @author Yan Qiang
5358
*/
5459
class QueryMapperUnitTests {
5560

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

6368
QueryMapper createMapper(R2dbcDialect dialect, Converter<?, ?>... converters) {
69+
6470
R2dbcCustomConversions conversions = R2dbcCustomConversions.of(dialect, Arrays.asList(converters));
6571

6672
R2dbcMappingContext context = new R2dbcMappingContext();
@@ -359,9 +365,10 @@ void shouldMapIsNotIn() {
359365
}
360366

361367
@Test
362-
void sholdMapIsNotInWithCollectionToStringConverter() {
368+
void shouldMapIsNotInWithCollectionToStringConverter() {
363369

364-
mapper = createMapper(PostgresDialect.INSTANCE, JsonNodeToStringConverter.INSTANCE, StringToJsonNodeConverter.INSTANCE, CollectionToStringConverter.INSTANCE);
370+
mapper = createMapper(PostgresDialect.INSTANCE, JsonNodeToStringConverter.INSTANCE,
371+
StringToJsonNodeConverter.INSTANCE, CollectionToStringConverter.INSTANCE);
365372

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

@@ -479,14 +486,14 @@ public void shouldMapSortWithAllowedSpecialCharacters() {
479486
.containsExactly("tbl.x(._)x DESC");
480487
}
481488

482-
483489
@Test // GH-1507
484490
public void shouldNotMapSortWithIllegalExpression() {
485491

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

488494
assertThatThrownBy(() -> mapper.getMappedSort(Table.create("tbl"), sort,
489-
mapper.getMappingContext().getRequiredPersistentEntity(Person.class))).isInstanceOf(IllegalArgumentException.class);
495+
mapper.getMappingContext().getRequiredPersistentEntity(Person.class)))
496+
.isInstanceOf(IllegalArgumentException.class);
490497
}
491498

492499
@Test // gh-369
@@ -588,13 +595,13 @@ public String convert(JsonNode source) {
588595

589596
enum CollectionToStringConverter implements Converter<Collection<?>, String> {
590597
INSTANCE;
598+
591599
@Override
592600
public String convert(Collection<?> source) {
593601
return source.toString();
594602
}
595603
}
596604

597-
598605
enum StringToJsonNodeConverter implements Converter<String, JsonNode> {
599606
INSTANCE;
600607

0 commit comments

Comments
 (0)