Skip to content

By default R2DBC uses quoted identifiers #2066

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -7,7 +7,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-1993-force-quoting-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>4.0.0-SNAPSHOT</version>
<version>4.0.0-1993-force-quoting-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 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>4.0.0-SNAPSHOT</version>
<version>4.0.0-1993-force-quoting-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>4.0.0-SNAPSHOT</version>
<version>4.0.0-1993-force-quoting-SNAPSHOT</version>
</parent>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-r2dbc/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-r2dbc</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-1993-force-quoting-SNAPSHOT</version>

<name>Spring Data R2DBC</name>
<description>Spring Data module for R2DBC</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-1993-force-quoting-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
* R2DBC-specific extension to {@link RelationalMappingContext}.
*
* @author Mark Paluch
* @author Jens Schauder
*/
public class R2dbcMappingContext extends RelationalMappingContext {

/**
* Create a new {@link R2dbcMappingContext}.
*/
public R2dbcMappingContext() {
setForceQuote(false);
setForceQuote(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setForceQuote(…) could be removed entirely as it sets the value to its default value, see JdbcMappingContext.

}

/**
Expand All @@ -41,8 +42,10 @@ public R2dbcMappingContext() {
* @param namingStrategy must not be {@literal null}.
*/
public R2dbcMappingContext(NamingStrategy namingStrategy) {

super(namingStrategy);
setForceQuote(false);

setForceQuote(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setForceQuote(…) could be removed entirely as it sets the value to its default value, see JdbcMappingContext.

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* Integration test for {@link DatabaseClient} and repositories using H2.
*
* @author Mark Paluch
* @author Jens Schauder
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration
Expand All @@ -56,17 +57,17 @@ class H2IntegrationTests {
void before() {

try {
jdbc.execute("DROP TABLE legoset");
jdbc.execute("DROP TABLE \"legoset\"");
} catch (DataAccessException e) {}
jdbc.execute(H2TestSupport.CREATE_TABLE_LEGOSET);
}

@Test // gh-109
void shouldSelectCountWithDatabaseClient() {

jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
jdbc.execute("INSERT INTO \"legoset\" (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");

databaseClient.sql("SELECT COUNT(*) FROM legoset") //
databaseClient.sql("SELECT COUNT(*) FROM \"legoset\"") //
.map(it -> it.get(0, Long.class)) //
.all() //
.as(StepVerifier::create) //
Expand All @@ -77,7 +78,7 @@ void shouldSelectCountWithDatabaseClient() {
@Test // gh-109
void shouldSelectCountWithRepository() {

jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
jdbc.execute("INSERT INTO \"legoset\" (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");

repository.selectCount() //
.as(StepVerifier::create) //
Expand All @@ -99,7 +100,7 @@ public ConnectionFactory connectionFactory() {

interface H2Repository extends ReactiveCrudRepository<LegoSet, Integer> {

@Query("SELECT COUNT(*) FROM legoset")
@Query("SELECT COUNT(*) FROM \"legoset\"")
Mono<Long> selectCount();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@
*/
package org.springframework.data.r2dbc.convert;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

import io.r2dbc.spi.R2dbcType;
import io.r2dbc.spi.Row;
import io.r2dbc.spi.test.MockColumnMetadata;
import io.r2dbc.spi.test.MockRow;
import io.r2dbc.spi.test.MockRowMetadata;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -37,19 +47,11 @@
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.r2dbc.core.Parameter;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
* Unit tests for {@link MappingR2dbcConverter}.
*
* @author Mark Paluch
* @author Jens Schauder
*/
public class MappingR2dbcConverterUnitTests {

Expand Down Expand Up @@ -78,11 +80,11 @@ void shouldIncludeAllPropertiesInOutboundRow() {
LocalDateTime localDateTime = LocalDateTime.now();
converter.write(new Person("id", "Walter", "White", instant, localDateTime), row);

assertThat(row).containsEntry(SqlIdentifier.unquoted("id"), Parameter.fromOrEmpty("id", String.class));
assertThat(row).containsEntry(SqlIdentifier.unquoted("firstname"), Parameter.fromOrEmpty("Walter", String.class));
assertThat(row).containsEntry(SqlIdentifier.unquoted("lastname"), Parameter.fromOrEmpty("White", String.class));
assertThat(row).containsEntry(SqlIdentifier.unquoted("instant"), Parameter.from(instant));
assertThat(row).containsEntry(SqlIdentifier.unquoted("local_date_time"), Parameter.from(localDateTime));
assertThat(row).containsEntry(SqlIdentifier.quoted("ID"), Parameter.fromOrEmpty("id", String.class));
assertThat(row).containsEntry(SqlIdentifier.quoted("FIRSTNAME"), Parameter.fromOrEmpty("Walter", String.class));
assertThat(row).containsEntry(SqlIdentifier.quoted("LASTNAME"), Parameter.fromOrEmpty("White", String.class));
assertThat(row).containsEntry(SqlIdentifier.quoted("INSTANT"), Parameter.from(instant));
assertThat(row).containsEntry(SqlIdentifier.quoted("LOCAL_DATE_TIME"), Parameter.from(localDateTime));
}

@Test // gh-41
Expand Down Expand Up @@ -122,7 +124,7 @@ void shouldConvertMapToString() {
OutboundRow row = new OutboundRow();
converter.write(withMap, row);

assertThat(row).containsEntry(SqlIdentifier.unquoted("nested"), Parameter.from("map"));
assertThat(row).containsEntry(SqlIdentifier.quoted("NESTED"), Parameter.from("map"));
}

@Test // gh-59
Expand All @@ -143,7 +145,7 @@ void shouldConvertEnum() {
OutboundRow row = new OutboundRow();
converter.write(withMap, row);

assertThat(row).containsEntry(SqlIdentifier.unquoted("condition"), Parameter.from("Mint"));
assertThat(row).containsEntry(SqlIdentifier.quoted("CONDITION"), Parameter.from("Mint"));
}

@Test // gh-59
Expand All @@ -153,7 +155,7 @@ void shouldConvertNullEnum() {
OutboundRow row = new OutboundRow();
converter.write(withMap, row);

assertThat(row).containsEntry(SqlIdentifier.unquoted("condition"), Parameter.fromOrEmpty(null, String.class));
assertThat(row).containsEntry(SqlIdentifier.quoted("CONDITION"), Parameter.fromOrEmpty(null, String.class));
}

@Test // gh-59
Expand Down Expand Up @@ -215,7 +217,7 @@ void writeShouldWritePrimitiveIdIfValueIsNonZero() {
OutboundRow row = new OutboundRow();
converter.write(new WithPrimitiveId(1), row);

assertThat(row).containsEntry(SqlIdentifier.unquoted("id"), Parameter.fromOrEmpty(1L, Long.TYPE));
assertThat(row).containsEntry(SqlIdentifier.quoted("ID"), Parameter.fromOrEmpty(1L, Long.TYPE));
}

@Test // gh-59
Expand Down Expand Up @@ -257,12 +259,11 @@ void writeShouldObtainIdFromIdentifierAccessor() {
OutboundRow row = new OutboundRow();
converter.write(entity, row);

assertThat(row).containsEntry(SqlIdentifier.unquoted("id"), Parameter.from(42L));
assertThat(row).containsEntry(SqlIdentifier.quoted("ID"), Parameter.from(42L));
}

static class Person {
@Id
String id;
@Id String id;
String firstname, lastname;
Instant instant;
LocalDateTime localDateTime;
Expand Down Expand Up @@ -298,8 +299,7 @@ public void setLastname(String lastname) {
}

static class WithEnum {
@Id
String id;
@Id String id;
Condition condition;

public WithEnum(String id, Condition condition) {
Expand All @@ -313,8 +313,7 @@ enum Condition {
}

static class PersonWithConversions {
@Id
String id;
@Id String id;
Map<String, String> nested;
NonMappableEntity unsupported;

Expand All @@ -325,8 +324,7 @@ public PersonWithConversions(String id, Map<String, String> nested, NonMappableE
}
}

record WithPrimitiveId (
@Id long id){
record WithPrimitiveId(@Id long id) {
}

static class CustomConversionPerson {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* MySQL-specific unit tests for {@link MappingR2dbcConverter}.
*
* @author Mark Paluch
* @author Jens Schauder
*/
class MySqlMappingR2dbcConverterUnitTests {

Expand Down Expand Up @@ -68,8 +69,9 @@ void shouldWriteBooleanToByte() {

converter.write(object, row);

OutboundRowAssert.assertThat(row).containsColumnWithValue("flag1", (byte) 1).containsColumnWithValue("flag2",
(byte) 0);
OutboundRowAssert.assertThat(row) //
.containsColumnWithValue("FLAG1", (byte) 1) //
.containsColumnWithValue("FLAG2", (byte) 0);
}

@Test // gh-589
Expand All @@ -96,18 +98,17 @@ void shouldPreserveByteValue() {

converter.write(object, row);

OutboundRowAssert.assertThat(row).containsColumnWithValue("state", (byte) 3);
OutboundRowAssert.assertThat(row).containsColumnWithValue("STATE", (byte) 3);
}

record BooleanMapping(

Integer id, boolean flag1, boolean flag2) {
}

record WithByte (
record WithByte(

Integer id,
byte state){
Integer id, byte state) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* Postgres-specific unit tests for {@link MappingR2dbcConverter}.
*
* @author Mark Paluch
* @author Jens Schauder
*/
class PostgresMappingR2dbcConverterUnitTests {

Expand Down Expand Up @@ -79,7 +80,7 @@ void shouldPassThruJson() {
OutboundRow row = new OutboundRow();
converter.write(person, row);

assertThat(row).containsEntry(SqlIdentifier.unquoted("json_value"), Parameter.from(person.jsonValue));
assertThat(row).containsEntry(SqlIdentifier.quoted("JSON_VALUE"), Parameter.from(person.jsonValue));
}

@Test // gh-453
Expand Down Expand Up @@ -127,24 +128,18 @@ void shouldApplyCustomWritingConverter() {
OutboundRow row = new OutboundRow();
converter.write(object, row);

Parameter parameter = row.get(SqlIdentifier.unquoted("holder"));
Parameter parameter = row.get(SqlIdentifier.quoted("HOLDER"));
assertThat(parameter).isNotNull();
assertThat(parameter.getValue()).isInstanceOf(Json.class);
}

record JsonPerson(
@Id Long id,
Json jsonValue) {
record JsonPerson(@Id Long id, Json jsonValue) {
}

record ConvertedJson(
@Id Long id,
String jsonString,
byte[] jsonBytes) {
record ConvertedJson(@Id Long id, String jsonString, byte[] jsonBytes) {
}

record WithJsonHolder(
JsonHolder holder) {
record WithJsonHolder(JsonHolder holder) {
}

@ReadingConverter
Expand Down
Loading