Skip to content

Map boolean values to 0 or 1 if dialect is SqlServerDialect. #936

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-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-908-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-908-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
10 changes: 8 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-908-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-908-SNAPSHOT</version>
</parent>

<properties>
Expand Down Expand Up @@ -263,6 +263,12 @@
<version>${jmolecules-integration}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.dialect.Escaper;
import org.springframework.data.relational.core.dialect.SqlServerDialect;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.core.query.CriteriaDefinition;
Expand Down Expand Up @@ -402,11 +403,11 @@ private Condition createCondition(Column column, @Nullable Object mappedValue, i
}

if (comparator == Comparator.IS_TRUE) {
return column.isEqualTo(SQL.literalOf(true));
return column.isEqualTo(dialect.booleanLiteral(true));
}

if (comparator == Comparator.IS_FALSE) {
return column.isEqualTo(SQL.literalOf(false));
return column.isEqualTo(dialect.booleanLiteral(false));
}

Expression columnExpression = column;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.relational.core.dialect.H2Dialect;
import org.springframework.data.relational.core.dialect.SqlServerDialect;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.MappedCollection;
import org.springframework.data.relational.core.mapping.Table;
Expand All @@ -61,9 +62,14 @@
public class PartTreeJdbcQueryUnitTests {

private static final String TABLE = "\"users\"";
private static final String TABLE_SQL_SERVER = "users";
private static final String ALL_FIELDS = "\"users\".\"ID\" AS \"ID\", \"users\".\"AGE\" AS \"AGE\", \"hated\".\"USER\" AS \"HATED_USER\", \"users\".\"ACTIVE\" AS \"ACTIVE\", \"users\".\"LAST_NAME\" AS \"LAST_NAME\", \"users\".\"FIRST_NAME\" AS \"FIRST_NAME\", \"users\".\"DATE_OF_BIRTH\" AS \"DATE_OF_BIRTH\", \"users\".\"HOBBY_REFERENCE\" AS \"HOBBY_REFERENCE\", \"hated\".\"NAME\" AS \"HATED_NAME\", \"users\".\"USER_CITY\" AS \"USER_CITY\", \"users\".\"USER_STREET\" AS \"USER_STREET\"";
private static final String ALL_FIELDS_SQL_SERVER = "users.id AS id, users.age AS age, hated.user AS hated_user, users.active AS active, users.last_name AS last_name, users.first_name AS first_name, users.date_of_birth AS date_of_birth, users.hobby_reference AS hobby_reference, hated.name AS hated_name, users.user_city AS user_city, users.user_street AS user_street";
private static final String JOIN_CLAUSE = "FROM \"users\" LEFT OUTER JOIN \"HOBBY\" \"hated\" ON \"hated\".\"USER\" = \"users\".\"ID\"";
private static final String JOIN_CLAUSE_SQL_SERVER = "FROM users LEFT OUTER JOIN hobby hated ON hated.user = users.id";

private static final String BASE_SELECT = "SELECT " + ALL_FIELDS + " " + JOIN_CLAUSE;
private static final String BASE_SELECT_SQL_SERVER = "SELECT " + ALL_FIELDS_SQL_SERVER + " " + JOIN_CLAUSE_SQL_SERVER;

JdbcMappingContext mappingContext = new JdbcMappingContext();
JdbcConverter converter = new BasicJdbcConverter(mappingContext, mock(RelationResolver.class));
Expand Down Expand Up @@ -441,6 +447,17 @@ public void createsQueryToFindAllEntitiesByBooleanAttributeTrue() throws Excepti
assertThat(query.getQuery()).isEqualTo(BASE_SELECT + " WHERE " + TABLE + ".\"ACTIVE\" = TRUE");
}

@Test // issue-908
public void createsQueryToFindAllEntitiesByBooleanAttributeTrueForSqlServer() throws Exception {

JdbcQueryMethod queryMethod = getQueryMethod("findAllByActiveTrue");
PartTreeJdbcQuery jdbcQuery = createQueryForSqlServer(queryMethod);
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[0]);
ParametrizedQuery query = jdbcQuery.createQuery(accessor);

assertThat(query.getQuery()).isEqualTo(BASE_SELECT_SQL_SERVER + " WHERE " + TABLE_SQL_SERVER + ".active = 1");
}

@Test // DATAJDBC-318
public void createsQueryToFindAllEntitiesByBooleanAttributeFalse() throws Exception {

Expand All @@ -452,6 +469,17 @@ public void createsQueryToFindAllEntitiesByBooleanAttributeFalse() throws Except
assertThat(query.getQuery()).isEqualTo(BASE_SELECT + " WHERE " + TABLE + ".\"ACTIVE\" = FALSE");
}

@Test // issue-908
public void createsQueryToFindAllEntitiesByBooleanAttributeFalseForSqlServer() throws Exception {

JdbcQueryMethod queryMethod = getQueryMethod("findAllByActiveFalse");
PartTreeJdbcQuery jdbcQuery = createQueryForSqlServer(queryMethod);
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[0]);
ParametrizedQuery query = jdbcQuery.createQuery(accessor);

assertThat(query.getQuery()).isEqualTo(BASE_SELECT_SQL_SERVER + " WHERE " + TABLE_SQL_SERVER + ".active = 0");
}

@Test // DATAJDBC-318
public void createsQueryToFindAllEntitiesByStringAttributeIgnoringCase() throws Exception {

Expand Down Expand Up @@ -565,6 +593,11 @@ private PartTreeJdbcQuery createQuery(JdbcQueryMethod queryMethod) {
mock(NamedParameterJdbcOperations.class), mock(RowMapper.class));
}

private PartTreeJdbcQuery createQueryForSqlServer(JdbcQueryMethod queryMethod) {
return new PartTreeJdbcQuery(mappingContext, queryMethod, SqlServerDialect.INSTANCE, converter,
mock(NamedParameterJdbcOperations.class), mock(RowMapper.class));
}

private JdbcQueryMethod getQueryMethod(String methodName, Class<?>... parameterTypes) throws Exception {
Method method = UserRepository.class.getMethod(methodName, parameterTypes);
return new JdbcQueryMethod(method, new DefaultRepositoryMetadata(UserRepository.class),
Expand Down
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-908-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-908-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.springframework.data.relational.core.dialect;

import org.springframework.data.relational.core.sql.IdentifierProcessing;
import org.springframework.data.relational.core.sql.Literal;
import org.springframework.data.relational.core.sql.SQL;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.relational.core.sql.render.SelectRenderContext;

Expand Down Expand Up @@ -84,5 +86,15 @@ default Escaper getLikeEscaper() {

default IdGeneration getIdGeneration(){
return IdGeneration.DEFAULT;
};
}

/**
* Return the boolean literal based on dialect.
*
* @param value the boolean value
* @return the appropriate literal
*/
default Literal booleanLiteral(boolean value) {
return SQL.literalOf(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package org.springframework.data.relational.core.dialect;

import org.springframework.data.relational.core.sql.IdentifierProcessing;
import org.springframework.data.relational.core.sql.Literal;
import org.springframework.data.relational.core.sql.LockOptions;
import org.springframework.data.relational.core.sql.SQL;
import org.springframework.data.relational.core.sql.render.SelectRenderContext;
import org.springframework.data.util.Lazy;

Expand Down Expand Up @@ -150,4 +152,9 @@ public SelectRenderContext getSelectContext() {
public IdentifierProcessing getIdentifierProcessing() {
return IdentifierProcessing.NONE;
}

@Override
public Literal booleanLiteral(boolean value) {
return value ? SQL.literalOf(1) :SQL.literalOf(0);
}
}