Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b67359d

Browse files
manousosmp911de
authored andcommitted
Fix isTrue/isFalse comparison for SQL server by using bind values.
Closes: #698 Original pull request: #708.
1 parent 29d1fd4 commit b67359d

File tree

9 files changed

+58
-20
lines changed

9 files changed

+58
-20
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.regex.Pattern;
24-
2524
import org.springframework.data.domain.Sort;
2625
import org.springframework.data.mapping.MappingException;
2726
import org.springframework.data.mapping.PersistentPropertyPath;
@@ -81,8 +80,7 @@ public QueryMapper(R2dbcDialect dialect, R2dbcConverter converter) {
8180
}
8281

8382
/**
84-
* Render a {@link SqlIdentifier} for SQL usage.
85-
* The resulting String might contain quoting characters.
83+
* Render a {@link SqlIdentifier} for SQL usage. The resulting String might contain quoting characters.
8684
*
8785
* @param identifier the identifier to be rendered.
8886
* @return an identifier String.
@@ -473,11 +471,15 @@ private Condition createCondition(Column column, @Nullable Object mappedValue, C
473471
}
474472

475473
if (comparator == Comparator.IS_TRUE) {
476-
return column.isEqualTo(SQL.literalOf(true));
474+
Expression bind = booleanBind(column, mappedValue, valueType, bindings, ignoreCase);
475+
476+
return column.isEqualTo(bind);
477477
}
478478

479479
if (comparator == Comparator.IS_FALSE) {
480-
return column.isEqualTo(SQL.literalOf(false));
480+
Expression bind = booleanBind(column, mappedValue, valueType, bindings, ignoreCase);
481+
482+
return column.isEqualTo(bind);
481483
}
482484

483485
Expression columnExpression = column;
@@ -627,6 +629,13 @@ private Expression bind(@Nullable Object mappedValue, Class<?> valueType, Mutabl
627629
: SQL.bindMarker(bindMarker.getPlaceholder());
628630
}
629631

632+
private Expression booleanBind(Column column, Object mappedValue, Class<?> valueType, MutableBindings bindings,
633+
boolean ignoreCase) {
634+
BindMarker bindMarker = bindings.nextMarker(column.getName().getReference());
635+
636+
return bind(mappedValue, valueType, bindings, bindMarker, ignoreCase);
637+
}
638+
630639
/**
631640
* Value object to represent a field and its meta-information.
632641
*/
@@ -750,7 +759,9 @@ private boolean isPathToJavaLangClassProperty(PropertyPath path) {
750759

751760
/*
752761
* (non-Javadoc)
753-
* @see org.springframework.data.r2dbc.core.convert.QueryMapper.Field#getTypeHint()
762+
*
763+
* @see
764+
* org.springframework.data.r2dbc.core.convert.QueryMapper.Field#getTypeHint()
754765
*/
755766
@Override
756767
public TypeInformation<?> getTypeHint() {

‎src/test/java/org/springframework/data/r2dbc/repository/AbstractR2dbcRepositoryIntegrationTests.java‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ void before() {
106106
@Test
107107
void shouldInsertNewItems() {
108108

109-
LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12);
110-
LegoSet legoSet2 = new LegoSet(null, "FORSCHUNGSSCHIFF", 13);
109+
LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12, true);
110+
LegoSet legoSet2 = new LegoSet(null, "FORSCHUNGSSCHIFF", 13, false);
111111

112112
repository.saveAll(Arrays.asList(legoSet1, legoSet2)) //
113113
.as(StepVerifier::create) //
@@ -181,8 +181,8 @@ void shouldByStringQueryApplyingDtoProjection() {
181181
@Test // gh-344
182182
void shouldFindApplyingDistinctProjection() {
183183

184-
LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12);
185-
LegoSet legoSet2 = new LegoSet(null, "SCHAUFELRADBAGGER", 13);
184+
LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12, true);
185+
LegoSet legoSet2 = new LegoSet(null, "SCHAUFELRADBAGGER", 13, false);
186186

187187
repository.saveAll(Arrays.asList(legoSet1, legoSet2)) //
188188
.as(StepVerifier::create) //
@@ -211,6 +211,19 @@ void shouldFindApplyingSimpleTypeProjection() {
211211
}).verifyComplete();
212212
}
213213

214+
@Test // gh-698
215+
void shouldBeTrue() {
216+
shouldInsertNewItems();
217+
218+
repository.findLegoSetByFlag(true) //
219+
.map(a -> a.flag) //
220+
.collectList() //
221+
.as(StepVerifier::create) //
222+
.consumeNextWith(actual -> {
223+
assertThat(actual).hasSize(1).contains(true);
224+
}).verifyComplete();
225+
}
226+
214227
@Test
215228
void shouldDeleteUsingQueryMethod() {
216229

@@ -256,9 +269,8 @@ void shouldFindByPageable() {
256269
@Test // gh-335
257270
void shouldFindTop10() {
258271

259-
Flux<LegoSet> sets = Flux.fromStream(IntStream.range(0, 100).mapToObj(value -> {
260-
return new LegoSet(null, "Set " + value, value);
261-
}));
272+
Flux<LegoSet> sets = Flux
273+
.fromStream(IntStream.range(0, 100).mapToObj(value -> new LegoSet(null, "Set " + value, value, true)));
262274

263275
repository.saveAll(sets) //
264276
.as(StepVerifier::create) //
@@ -291,8 +303,8 @@ public void shouldInsertItemsTransactional() {
291303
R2dbcTransactionManager r2dbcTransactionManager = new R2dbcTransactionManager(connectionFactory);
292304
TransactionalOperator rxtx = TransactionalOperator.create(r2dbcTransactionManager);
293305

294-
LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12);
295-
LegoSet legoSet2 = new LegoSet(null, "FORSCHUNGSSCHIFF", 13);
306+
LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12, true);
307+
LegoSet legoSet2 = new LegoSet(null, "FORSCHUNGSSCHIFF", 13, false);
296308

297309
Mono<Map<String, Object>> transactional = repository.save(legoSet1) //
298310
.map(it -> jdbc.queryForMap("SELECT count(*) AS count FROM legoset")).as(rxtx::transactional);
@@ -407,6 +419,8 @@ interface LegoSetRepository extends ReactiveCrudRepository<LegoSet, Integer> {
407419
Mono<Integer> countByNameContains(String namePart);
408420

409421
Mono<Boolean> existsByName(String name);
422+
423+
Flux<LegoSet> findLegoSetByFlag(boolean flag);
410424
}
411425

412426
public interface Buildable {
@@ -421,13 +435,20 @@ public interface Buildable {
421435
public static class LegoSet extends Lego implements Buildable {
422436
String name;
423437
Integer manual;
438+
boolean flag;
424439

425440
@PersistenceConstructor
426441
LegoSet(Integer id, String name, Integer manual) {
427442
super(id);
428443
this.name = name;
429444
this.manual = manual;
430445
}
446+
447+
@PersistenceConstructor
448+
LegoSet(Integer id, String name, Integer manual, Boolean flag) {
449+
this(id, name, manual);
450+
this.flag = flag;
451+
}
431452
}
432453

433454
@AllArgsConstructor

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ void createsQueryToFindAllEntitiesByIntegerAttributeNotIn() throws Exception {
486486
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age NOT IN (1ドル)");
487487
}
488488

489-
@Test // gh-282
489+
@Test // gh-282, gh-698
490490
void createsQueryToFindAllEntitiesByBooleanAttributeTrue() throws Exception {
491491

492492
R2dbcQueryMethod queryMethod = getQueryMethod("findAllByActiveTrue");
@@ -496,10 +496,10 @@ void createsQueryToFindAllEntitiesByBooleanAttributeTrue() throws Exception {
496496
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
497497

498498
assertThat(preparedOperation.get())
499-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = TRUE");
499+
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = 1ドル");
500500
}
501501

502-
@Test // gh-282
502+
@Test // gh-282, gh-698
503503
void createsQueryToFindAllEntitiesByBooleanAttributeFalse() throws Exception {
504504

505505
R2dbcQueryMethod queryMethod = getQueryMethod("findAllByActiveFalse");
@@ -509,7 +509,7 @@ void createsQueryToFindAllEntitiesByBooleanAttributeFalse() throws Exception {
509509
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
510510

511511
assertThat(preparedOperation.get())
512-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = FALSE");
512+
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = 1ドル");
513513
}
514514

515515
@Test // gh-282

‎src/test/java/org/springframework/data/r2dbc/testing/H2TestSupport.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class H2TestSupport {
4545
+ " version integer NULL,\n" //
4646
+ " name varchar(255) NOT NULL,\n" //
4747
+ " extra varchar(255),\n" //
48+
+ " flag boolean,\n" //
4849
+ " manual integer NULL\n" //
4950
+ ");";
5051

‎src/test/java/org/springframework/data/r2dbc/testing/MariaDbTestSupport.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class MariaDbTestSupport {
5151
public static final String CREATE_TABLE_LEGOSET_WITH_ID_GENERATION = "CREATE TABLE legoset (\n" //
5252
+ " id integer AUTO_INCREMENT PRIMARY KEY,\n" //
5353
+ " name varchar(255) NOT NULL,\n" //
54+
+ " flag boolean NOT NULL,\n" //
5455
+ " manual integer NULL\n" //
5556
+ ") ENGINE=InnoDB;";
5657

‎src/test/java/org/springframework/data/r2dbc/testing/MySqlTestSupport.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class MySqlTestSupport {
5353
+ " id integer AUTO_INCREMENT PRIMARY KEY,\n" //
5454
+ " version integer NULL,\n" //
5555
+ " name varchar(255) NOT NULL,\n" //
56+
+ " flag boolean NULL,\n" //
5657
+ " manual integer NULL\n" //
5758
+ ") ENGINE=InnoDB;";
5859

‎src/test/java/org/springframework/data/r2dbc/testing/OracleTestSupport.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class OracleTestSupport {
5555
+ " id INTEGER GENERATED by default on null as IDENTITY PRIMARY KEY,\n" //
5656
+ " version INTEGER NULL,\n" //
5757
+ " name VARCHAR2(255) NOT NULL,\n" //
58+
+ " flag Boolean NULL,\n" //
5859
+ " manual INTEGER NULL\n" //
5960
+ ")";
6061

‎src/test/java/org/springframework/data/r2dbc/testing/PostgresTestSupport.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public class PostgresTestSupport {
2828
+ " id integer CONSTRAINT id1 PRIMARY KEY,\n" //
2929
+ " version integer NULL,\n" //
3030
+ " name varchar(255) NOT NULL,\n" //
31-
+ " manual integer NULL\n," //
31+
+ " manual integer NULL,\n" //
32+
+ " flag boolean NULL,\n" //
3233
+ " cert bytea NULL\n" //
3334
+ ");";
3435

‎src/test/java/org/springframework/data/r2dbc/testing/SqlServerTestSupport.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class SqlServerTestSupport {
2828
+ " id integer IDENTITY(1,1) PRIMARY KEY,\n" //
2929
+ " version integer NULL,\n" //
3030
+ " name varchar(255) NOT NULL,\n" //
31+
+ " flag bit NULL\n," //
3132
+ " extra varchar(255),\n" //
3233
+ " manual integer NULL\n" //
3334
+ ");";

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /