Skip to content

Commit

Permalink
fix(sqlserver): prevent cast from NVARCHAR to VARCHAR
Browse files Browse the repository at this point in the history
Related: #158
  • Loading branch information
darrachequesne committed Mar 24, 2024
1 parent 326d1e8 commit f1e0ecd
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 1 deletion.
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,31 @@ jobs:
run: mvn -B test
env:
spring_profiles_active: mysql

test-sqlserver:
runs-on: ubuntu-latest

services:
sqlserver:
image: mcr.microsoft.com/mssql/server:2022-preview-ubuntu-22.04
ports:
- 1433:1433
env:
ACCEPT_EULA: Y
MSSQL_SA_PASSWORD: "Changeit_123"

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: adopt
cache: maven

- name: Test with Maven
run: mvn -B test
env:
spring_profiles_active: sqlserver
23 changes: 23 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
postgres:
image: postgres:14
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: "postgres"

sqlserver:
image: mcr.microsoft.com/mssql/server:2022-preview-ubuntu-22.04
ports:
- 1433:1433
environment:
ACCEPT_EULA: Y
MSSQL_SA_PASSWORD: "Changeit_123"

mysql:
image: mysql:5.7
ports:
- 3306:3306
environment:
MYSQL_DATABASE: test
MYSQL_ROOT_PASSWORD: changeit
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.6.1.jre11</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ private String escapeValue(String filterValue) {
@Override
public Predicate createPredicate(From<?, ?> from, CriteriaBuilder criteriaBuilder, String attributeName) {
Expression<?> expression = from.get(attributeName);
return criteriaBuilder.like(criteriaBuilder.lower(expression.as(String.class)), escapedRawValue, '~');
return criteriaBuilder.like(criteriaBuilder.lower(castAsStringIfNeeded(expression)), escapedRawValue, '~');
}

@SuppressWarnings("unchecked")
private Expression<String> castAsStringIfNeeded(Expression<?> expression) {
if (expression.getJavaType() == String.class) {
return (Expression<String>) expression;
} else {
return expression.as(String.class);
}
}

@Override
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/springframework/data/jpa/datatables/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ public DataSource dataSource_PostgreSQL() throws SQLException {
return dataSource;
}

@Bean
@Profile("sqlserver")
public DataSource dataSource_SQLServer() throws SQLException {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSource.setUrl("jdbc:sqlserver://localhost:1433;encrypt=false;");
dataSource.setUsername("sa");
dataSource.setPassword("Changeit_123");
return dataSource;
}

@Bean
public SessionFactory entityManagerFactory(DataSource dataSource) throws Exception {
LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.springframework.data.jpa.datatables.repository;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;
import org.hibernate.annotations.Nationalized;

@Entity
@Data
@Table(name = "users")
public class User {

@Id long id;
@Nationalized String name;

public User() {
}

public User(long id, String name) {
this.id = id;
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.springframework.data.jpa.datatables.repository;

public interface UserRepository extends DataTablesRepository<User, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.springframework.data.jpa.datatables.repository;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.datatables.Config;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.DisabledIf;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = Config.class)
public class UserRepositoryTest {

@Autowired private UserRepository userRepository;

private void init() {
userRepository.save(new User(1L, "r"));
userRepository.save(new User(2L, "ř"));
userRepository.save(new User(3L, "ŗ"));
userRepository.save(new User(4L, "ɍ"));
userRepository.save(new User(5L, "ȓ"));
}

@Test
@DisabledIf(value = "#{'${spring.profiles.active}' == 'mysql'}", loadContext = true)
void withNationalizedColumn() {
init();

DataTablesInput input = new DataTablesInput();
input.addColumn("name", true, true, "ř");

DataTablesOutput<User> output = userRepository.findAll(input);

assertThat(output.getRecordsFiltered()).isEqualTo(1);
assertThat(output.getData().get(0).getId()).isEqualTo(2L);
}

}

0 comments on commit f1e0ecd

Please sign in to comment.