Skip to content

Commit 6389d0f

Browse files
committed
Allow passing of tuples to methods.
Closes #1323
1 parent 6fea424 commit 6389d0f

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/StringBasedJdbcQuery.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ private void convertAndAddParameter(MapSqlParameterSource parameters, Parameter
204204
TypeInformation<?> typeInformation = parameter.getTypeInformation();
205205

206206
JdbcValue jdbcValue;
207-
if (typeInformation.isCollectionLike() && value instanceof Collection<?>) {
207+
if (typeInformation.isCollectionLike() //
208+
&& value instanceof Collection<?> //
209+
&& !typeInformation.getActualType().getType().isArray()
210+
) {
208211

209212
List<Object> mapped = new ArrayList<>();
210213
SQLType jdbcType = null;
@@ -221,6 +224,7 @@ private void convertAndAddParameter(MapSqlParameterSource parameters, Parameter
221224

222225
jdbcValue = JdbcValue.of(mapped, jdbcType);
223226
} else {
227+
224228
SQLType sqlType = parameter.getSqlType();
225229
jdbcValue = converter.writeJdbcValue(value, typeInformation, sqlType);
226230
}

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,13 @@ public class JdbcRepositoryIntegrationTests {
117117
@Autowired WithDelimitedColumnRepository withDelimitedColumnRepository;
118118

119119
private static DummyEntity createDummyEntity() {
120+
return createDummyEntity("Entity Name");
121+
}
122+
123+
private static DummyEntity createDummyEntity(String entityName) {
120124

121125
DummyEntity entity = new DummyEntity();
122-
entity.setName("Entity Name");
126+
entity.setName(entityName);
123127

124128
return entity;
125129
}
@@ -1334,6 +1338,21 @@ void withDelimitedColumnTest() {
13341338
assertThat(inDatabase.get().getIdentifier()).isEqualTo("UR-123");
13351339
}
13361340

1341+
@Test // GH-1323
1342+
void queryWithTupleIn() {
1343+
1344+
DummyEntity one = repository.save(createDummyEntity("one"));
1345+
DummyEntity two = repository.save(createDummyEntity( "two"));
1346+
DummyEntity three = repository.save(createDummyEntity( "three"));
1347+
1348+
List<Object[]> tuples = List.of(
1349+
new Object[]{two.idProp, "two"}, // matches "two"
1350+
new Object[]{three.idProp, "two"} // matches nothing
1351+
);
1352+
1353+
repository.findByListInTuple(tuples);
1354+
}
1355+
13371356
private Root createRoot(String namePrefix) {
13381357

13391358
return new Root(null, namePrefix,
@@ -1461,6 +1480,9 @@ interface DummyEntityRepository extends CrudRepository<DummyEntity, Long>, Query
14611480
Optional<DummyDto> findDtoByIdProp(Long idProp);
14621481

14631482
Optional<DummyAllArgsDto> findAllArgsDtoByIdProp(Long idProp);
1483+
1484+
@Query("SELECT * FROM DUMMY_ENTITY WHERE (ID_PROP, NAME) IN (:tuples)")
1485+
List<DummyEntity> findByListInTuple(List<Object[]> tuples);
14641486
}
14651487

14661488
interface RootRepository extends ListCrudRepository<Root, Long> {

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/StringBasedJdbcQueryUnitTests.java

+18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.sql.JDBCType;
2323
import java.sql.ResultSet;
2424
import java.util.ArrayList;
25+
import java.util.Arrays;
2526
import java.util.Iterator;
2627
import java.util.List;
2728
import java.util.Properties;
@@ -325,6 +326,20 @@ void appliesConverterToIterable() {
325326
assertThat(sqlParameterSource.getValue("value")).isEqualTo("one");
326327
}
327328

329+
@Test // GH-1323
330+
void queryByListOfTuples() {
331+
332+
String[][] tuples = {new String[]{"Albert", "Einstein"}, new String[]{"Richard", "Feynman"}};
333+
334+
SqlParameterSource parameterSource = forMethod("findByListOfTuples", List.class) //
335+
.withArguments(Arrays.asList(tuples))
336+
.extractParameterSource();
337+
338+
assertThat(parameterSource.getValue("tuples"))
339+
.asInstanceOf(LIST)
340+
.containsExactly(tuples);
341+
}
342+
328343
QueryFixture forMethod(String name, Class... paramTypes) {
329344
return new QueryFixture(createMethod(name, paramTypes));
330345
}
@@ -450,6 +465,9 @@ interface MyRepository extends Repository<Object, Long> {
450465

451466
@Query("SELECT * FROM person WHERE lastname = $1")
452467
Object unsupportedLimitQuery(@Param("lastname") String lastname, Limit limit);
468+
469+
@Query("select count(1) from person where (firstname, lastname) in (:tuples)")
470+
Object findByListOfTuples(@Param("tuples") List<Object[]> tuples);
453471
}
454472

455473
@Test // GH-619

0 commit comments

Comments
 (0)