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,15 +25,14 @@
* 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);
}
public R2dbcMappingContext() {}

/**
* Create a new {@link R2dbcMappingContext} using the given {@link NamingStrategy}.
Expand All @@ -42,7 +41,6 @@ public R2dbcMappingContext() {
*/
public R2dbcMappingContext(NamingStrategy namingStrategy) {
super(namingStrategy);
setForceQuote(false);
}

@Override
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 All @@ -65,7 +67,7 @@ void before() {
StringToSimplePersonConverter.INSTANCE));

mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());

mappingContext.setForceQuote(false);
converter = new MappingR2dbcConverter(mappingContext, conversions);
}

Expand Down Expand Up @@ -261,8 +263,7 @@ void writeShouldObtainIdFromIdentifierAccessor() {
}

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 @@ -34,11 +34,13 @@
import org.springframework.data.r2dbc.mapping.R2dbcMappingContext;
import org.springframework.data.r2dbc.testing.OutboundRowAssert;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.sql.SqlIdentifier;

/**
* MySQL-specific unit tests for {@link MappingR2dbcConverter}.
*
* @author Mark Paluch
* @author Jens Schauder
*/
class MySqlMappingR2dbcConverterUnitTests {

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

converter.write(object, row);

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

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

converter.write(object, row);

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

record BooleanMapping(

Integer id, boolean flag1, boolean flag2) {
}

record WithByte (

Integer id,
byte state){
record WithByte(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
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.r2dbc.convert.EnumWriteSupport;
import org.springframework.data.r2dbc.convert.MappingR2dbcConverter;
import org.springframework.data.r2dbc.convert.R2dbcCustomConversions;
import org.springframework.data.r2dbc.core.StatementMapper.InsertSpec;
import org.springframework.data.r2dbc.dialect.PostgresDialect;
import org.springframework.data.r2dbc.mapping.OutboundRow;
import org.springframework.data.r2dbc.mapping.R2dbcMappingContext;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.r2dbc.core.Parameter;
import org.springframework.r2dbc.core.PreparedOperation;
Expand All @@ -45,14 +48,34 @@
* {@link PostgresDialect} specific tests for {@link ReactiveDataAccessStrategy}.
*
* @author Mark Paluch
* @author Jens Schauder
*/
public class PostgresReactiveDataAccessStrategyTests extends ReactiveDataAccessStrategyTestSupport {

private final ReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE,
Arrays.asList(DurationToIntervalConverter.INSTANCE, IntervalToDurationConverter.INSTANCE));
private R2dbcMappingContext context;
private final ReactiveDataAccessStrategy strategy;

{
context = new R2dbcMappingContext();
context.setForceQuote(false);

DefaultReactiveDataAccessStrategy strategy1 = createReactiveDataAccessStrategy(DurationToIntervalConverter.INSTANCE,
IntervalToDurationConverter.INSTANCE);
strategy = strategy1;
}

private DefaultReactiveDataAccessStrategy createReactiveDataAccessStrategy(Object... converters) {
R2dbcCustomConversions customConversions = R2dbcCustomConversions.of(PostgresDialect.INSTANCE, converters);

MappingR2dbcConverter converter = new MappingR2dbcConverter(context, customConversions);
DefaultReactiveDataAccessStrategy strategy1 = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE,
converter);
return strategy1;
}

@Override
protected ReactiveDataAccessStrategy getStrategy() {

return strategy;
}

Expand Down Expand Up @@ -98,8 +121,6 @@ void shouldConvertCollectionToArray() {
@Test // gh-139
void shouldConvertToArray() {

DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);

WithArray withArray = new WithArray();
withArray.stringArray = new String[] { "hello", "world" };
withArray.stringList = Arrays.asList("hello", "world");
Expand All @@ -113,8 +134,7 @@ void shouldConvertToArray() {
@Test // gh-139
void shouldApplyCustomConversion() {

DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE,
Collections.singletonList(MyObjectsToStringConverter.INSTANCE));
DefaultReactiveDataAccessStrategy strategy = createReactiveDataAccessStrategy(MyObjectsToStringConverter.INSTANCE);

WithConversion withConversion = new WithConversion();
withConversion.myObjects = Arrays.asList(new MyObject("one"), new MyObject("two"));
Expand All @@ -127,8 +147,7 @@ void shouldApplyCustomConversion() {
@Test // gh-139
void shouldApplyCustomConversionForNull() {

DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE,
Collections.singletonList(MyObjectsToStringConverter.INSTANCE));
DefaultReactiveDataAccessStrategy strategy = createReactiveDataAccessStrategy(MyObjectsToStringConverter.INSTANCE);

WithConversion withConversion = new WithConversion();
withConversion.myObjects = null;
Expand All @@ -152,8 +171,6 @@ void shouldApplyCustomConversionForEmptyList() {
@Test // gh-252, gh-593
void shouldConvertCollectionOfEnumToString() {

DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);

WithEnumCollections withEnums = new WithEnumCollections();
withEnums.enumSet = EnumSet.of(MyEnum.ONE, MyEnum.TWO);
withEnums.enumList = Arrays.asList(MyEnum.ONE, MyEnum.TWO);
Expand All @@ -170,8 +187,6 @@ void shouldConvertCollectionOfEnumToString() {
@Test // gh-593
void shouldCorrectlyWriteConvertedEnumNullValues() {

DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);

WithEnumCollections withEnums = new WithEnumCollections();

OutboundRow outboundRow = strategy.getOutboundRow(withEnums);
Expand All @@ -185,8 +200,6 @@ void shouldCorrectlyWriteConvertedEnumNullValues() {
@Test // gh-1544
void shouldCorrectlyWriteConvertedEmptyEnumCollections() {

DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE);

WithEnumCollections withEnums = new WithEnumCollections();
withEnums.enumArray = new MyEnum[0];
withEnums.enumList = Collections.emptyList();
Expand All @@ -203,8 +216,7 @@ void shouldCorrectlyWriteConvertedEmptyEnumCollections() {
@Test // gh-593
void shouldConvertCollectionOfEnumNatively() {

DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE,
Collections.singletonList(new MyEnumSupport()));
DefaultReactiveDataAccessStrategy strategy = createReactiveDataAccessStrategy(new MyEnumSupport());

WithEnumCollections withEnums = new WithEnumCollections();
withEnums.enumSet = EnumSet.of(MyEnum.ONE, MyEnum.TWO);
Expand All @@ -222,8 +234,7 @@ void shouldConvertCollectionOfEnumNatively() {
@Test // gh-593
void shouldCorrectlyWriteNativeEnumNullValues() {

DefaultReactiveDataAccessStrategy strategy = new DefaultReactiveDataAccessStrategy(PostgresDialect.INSTANCE,
Collections.singletonList(new MyEnumSupport()));
DefaultReactiveDataAccessStrategy strategy = createReactiveDataAccessStrategy(new MyEnumSupport());

WithEnumCollections withEnums = new WithEnumCollections();

Expand Down
Loading