Skip to content

Commit 5c921c5

Browse files
committed
#349 - Fix parameter binding for Between queries.
We now bind the correct parameter when issuing Between queries.
1 parent ee46695 commit 5c921c5

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ private Condition createCondition(Column column, @Nullable Object mappedValue, C
496496

497497
Expression begin = bind(pair.getFirst(), valueType, bindings,
498498
bindings.nextMarker(column.getName().getReference()), ignoreCase);
499-
Expression end = bind(mappedValue, valueType, bindings, bindings.nextMarker(column.getName().getReference()),
499+
Expression end = bind(pair.getSecond(), valueType, bindings, bindings.nextMarker(column.getName().getReference()),
500500
ignoreCase);
501501

502502
return comparator == Comparator.BETWEEN ? Conditions.between(columnExpression, begin, end)

src/test/java/org/springframework/data/r2dbc/repository/query/PartTreeR2dbcQueryUnitTests.java

+19-6
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,26 @@ public void createsQueryToFindAllEntitiesByOneOfTwoStringAttributes() throws Exc
149149
+ ".last_name = $1 OR (" + TABLE + ".first_name = $2)");
150150
}
151151

152-
@Test // gh-282
152+
@Test // gh-282, gh-349
153153
public void createsQueryToFindAllEntitiesByDateAttributeBetween() throws Exception {
154154

155155
R2dbcQueryMethod queryMethod = getQueryMethod("findAllByDateOfBirthBetween", Date.class, Date.class);
156156
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
157157
dataAccessStrategy);
158-
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { new Date(), new Date() });
158+
Date from = new Date();
159+
Date to = new Date();
160+
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { from, to });
159161
BindableQuery bindableQuery = r2dbcQuery.createQuery(accessor);
160162

161163
assertThat(bindableQuery.get())
162164
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".date_of_birth BETWEEN $1 AND $2");
165+
166+
DatabaseClient.BindSpec bindSpecMock = mock(DatabaseClient.BindSpec.class);
167+
when(bindSpecMock.bind(anyInt(), any())).thenReturn(bindSpecMock);
168+
bindableQuery.bind(bindSpecMock);
169+
170+
verify(bindSpecMock, times(1)).bind(0, from);
171+
verify(bindSpecMock, times(1)).bind(1, to);
163172
}
164173

165174
@Test // gh-282
@@ -559,24 +568,28 @@ public void throwsExceptionWhenInvalidNumberOfParameterIsGiven() throws Exceptio
559568

560569
@Test // gh-282
561570
public void createsQueryWithLimitToFindEntitiesByStringAttribute() throws Exception {
571+
562572
R2dbcQueryMethod queryMethod = getQueryMethod("findTop3ByFirstName", String.class);
563573
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
564574
dataAccessStrategy);
565575
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "John" });
566576
BindableQuery bindableQuery = r2dbcQuery.createQuery(accessor);
567-
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1 LIMIT 3";
568-
assertThat(bindableQuery.get()).isEqualTo(expectedSql);
577+
578+
assertThat(bindableQuery.get())
579+
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1 LIMIT 3");
569580
}
570581

571582
@Test // gh-282
572583
public void createsQueryToFindFirstEntityByStringAttribute() throws Exception {
584+
573585
R2dbcQueryMethod queryMethod = getQueryMethod("findFirstByFirstName", String.class);
574586
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
575587
dataAccessStrategy);
576588
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "John" });
577589
BindableQuery bindableQuery = r2dbcQuery.createQuery(accessor);
578-
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1 LIMIT 1";
579-
assertThat(bindableQuery.get()).isEqualTo(expectedSql);
590+
591+
assertThat(bindableQuery.get())
592+
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1 LIMIT 1");
580593
}
581594

582595
private R2dbcQueryMethod getQueryMethod(String methodName, Class<?>... parameterTypes) throws Exception {

0 commit comments

Comments
 (0)