Skip to content

MSSQL: Using IsTrue or True in Query Method doesn't work on BIT columns #708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>1.5.0-SNAPSHOT</version>
<version>1.5.0-698-SNAPSHOT</version>

<name>Spring Data R2DBC</name>
<description>Spring Data module for R2DBC</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.springframework.data.domain.Sort;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentPropertyPath;
Expand Down Expand Up @@ -81,8 +80,7 @@ public QueryMapper(R2dbcDialect dialect, R2dbcConverter converter) {
}

/**
* Render a {@link SqlIdentifier} for SQL usage.
* The resulting String might contain quoting characters.
* Render a {@link SqlIdentifier} for SQL usage. The resulting String might contain quoting characters.
*
* @param identifier the identifier to be rendered.
* @return an identifier String.
Expand Down Expand Up @@ -473,11 +471,15 @@ private Condition createCondition(Column column, @Nullable Object mappedValue, C
}

if (comparator == Comparator.IS_TRUE) {
return column.isEqualTo(SQL.literalOf(true));
Expression bind = booleanBind(column, mappedValue, valueType, bindings, ignoreCase);

return column.isEqualTo(bind);
}

if (comparator == Comparator.IS_FALSE) {
return column.isEqualTo(SQL.literalOf(false));
Expression bind = booleanBind(column, mappedValue, valueType, bindings, ignoreCase);

return column.isEqualTo(bind);
}

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

private Expression booleanBind(Column column, Object mappedValue, Class<?> valueType, MutableBindings bindings,
boolean ignoreCase) {
BindMarker bindMarker = bindings.nextMarker(column.getName().getReference());

return bind(mappedValue, valueType, bindings, bindMarker, ignoreCase);
}

/**
* Value object to represent a field and its meta-information.
*/
Expand Down Expand Up @@ -750,7 +759,9 @@ private boolean isPathToJavaLangClassProperty(PropertyPath path) {

/*
* (non-Javadoc)
* @see org.springframework.data.r2dbc.core.convert.QueryMapper.Field#getTypeHint()
*
* @see
* org.springframework.data.r2dbc.core.convert.QueryMapper.Field#getTypeHint()
*/
@Override
public TypeInformation<?> getTypeHint() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ void before() {
@Test
void shouldInsertNewItems() {

LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12);
LegoSet legoSet2 = new LegoSet(null, "FORSCHUNGSSCHIFF", 13);
LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12, true);
LegoSet legoSet2 = new LegoSet(null, "FORSCHUNGSSCHIFF", 13, false);

repository.saveAll(Arrays.asList(legoSet1, legoSet2)) //
.as(StepVerifier::create) //
Expand Down Expand Up @@ -181,8 +181,8 @@ void shouldByStringQueryApplyingDtoProjection() {
@Test // gh-344
void shouldFindApplyingDistinctProjection() {

LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12);
LegoSet legoSet2 = new LegoSet(null, "SCHAUFELRADBAGGER", 13);
LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12, true);
LegoSet legoSet2 = new LegoSet(null, "SCHAUFELRADBAGGER", 13, false);

repository.saveAll(Arrays.asList(legoSet1, legoSet2)) //
.as(StepVerifier::create) //
Expand Down Expand Up @@ -211,6 +211,19 @@ void shouldFindApplyingSimpleTypeProjection() {
}).verifyComplete();
}

@Test // gh-698
void shouldBeTrue() {
shouldInsertNewItems();

repository.findLegoSetByFlag(true) //
.map(a -> a.flag) //
.collectList() //
.as(StepVerifier::create) //
.consumeNextWith(actual -> {
assertThat(actual).hasSize(1).contains(true);
}).verifyComplete();
}

@Test
void shouldDeleteUsingQueryMethod() {

Expand Down Expand Up @@ -256,9 +269,8 @@ void shouldFindByPageable() {
@Test // gh-335
void shouldFindTop10() {

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

repository.saveAll(sets) //
.as(StepVerifier::create) //
Expand Down Expand Up @@ -291,8 +303,8 @@ public void shouldInsertItemsTransactional() {
R2dbcTransactionManager r2dbcTransactionManager = new R2dbcTransactionManager(connectionFactory);
TransactionalOperator rxtx = TransactionalOperator.create(r2dbcTransactionManager);

LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12);
LegoSet legoSet2 = new LegoSet(null, "FORSCHUNGSSCHIFF", 13);
LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12, true);
LegoSet legoSet2 = new LegoSet(null, "FORSCHUNGSSCHIFF", 13, false);

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

Mono<Boolean> existsByName(String name);

Flux<LegoSet> findLegoSetByFlag(boolean flag);
}

public interface Buildable {
Expand All @@ -421,13 +435,20 @@ public interface Buildable {
public static class LegoSet extends Lego implements Buildable {
String name;
Integer manual;
boolean flag;

@PersistenceConstructor
LegoSet(Integer id, String name, Integer manual) {
super(id);
this.name = name;
this.manual = manual;
}

@PersistenceConstructor
LegoSet(Integer id, String name, Integer manual, Boolean flag) {
this(id, name, manual);
this.flag = flag;
}
}

@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ void createsQueryToFindAllEntitiesByIntegerAttributeNotIn() throws Exception {
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age NOT IN ($1)");
}

@Test // gh-282
@Test // gh-282, gh-698
void createsQueryToFindAllEntitiesByBooleanAttributeTrue() throws Exception {

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

assertThat(preparedOperation.get())
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = TRUE");
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = $1");
}

@Test // gh-282
@Test // gh-282, gh-698
void createsQueryToFindAllEntitiesByBooleanAttributeFalse() throws Exception {

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

assertThat(preparedOperation.get())
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = FALSE");
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = $1");
}

@Test // gh-282
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class H2TestSupport {
+ " version integer NULL,\n" //
+ " name varchar(255) NOT NULL,\n" //
+ " extra varchar(255),\n" //
+ " flag boolean,\n" //
+ " manual integer NULL\n" //
+ ");";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class MariaDbTestSupport {
public static final String CREATE_TABLE_LEGOSET_WITH_ID_GENERATION = "CREATE TABLE legoset (\n" //
+ " id integer AUTO_INCREMENT PRIMARY KEY,\n" //
+ " name varchar(255) NOT NULL,\n" //
+ " flag boolean NOT NULL,\n" //
+ " manual integer NULL\n" //
+ ") ENGINE=InnoDB;";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class MySqlTestSupport {
+ " id integer AUTO_INCREMENT PRIMARY KEY,\n" //
+ " version integer NULL,\n" //
+ " name varchar(255) NOT NULL,\n" //
+ " flag boolean NULL,\n" //
+ " manual integer NULL\n" //
+ ") ENGINE=InnoDB;";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class OracleTestSupport {
+ " id INTEGER GENERATED by default on null as IDENTITY PRIMARY KEY,\n" //
+ " version INTEGER NULL,\n" //
+ " name VARCHAR2(255) NOT NULL,\n" //
+ " flag Boolean NULL,\n" //
+ " manual INTEGER NULL\n" //
+ ")";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public class PostgresTestSupport {
+ " id integer CONSTRAINT id1 PRIMARY KEY,\n" //
+ " version integer NULL,\n" //
+ " name varchar(255) NOT NULL,\n" //
+ " manual integer NULL\n," //
+ " manual integer NULL,\n" //
+ " flag boolean NULL,\n" //
+ " cert bytea NULL\n" //
+ ");";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class SqlServerTestSupport {
+ " id integer IDENTITY(1,1) PRIMARY KEY,\n" //
+ " version integer NULL,\n" //
+ " name varchar(255) NOT NULL,\n" //
+ " flag bit NULL\n," //
+ " extra varchar(255),\n" //
+ " manual integer NULL\n" //
+ ");";
Expand Down