Skip to content

Commit f0a0031

Browse files
committed
By default R2DBC uses quoted identifiers
Most are test more or less obvious test fixes. Interesting things that became obvious: - SpEL expressions get the transformed (e.g. upper case) and quoted table name!? See TableNameQueryPreprocessorUnitTests and SqlInspectingR2dbcRepositoryUnitTests Closes #1993 Original pull request #2066
1 parent 002bd1b commit f0a0031

File tree

39 files changed

+634
-524
lines changed

39 files changed

+634
-524
lines changed

spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/mapping/R2dbcMappingContext.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
* R2DBC-specific extension to {@link RelationalMappingContext}.
2626
*
2727
* @author Mark Paluch
28+
* @author Jens Schauder
2829
*/
2930
public class R2dbcMappingContext extends RelationalMappingContext {
3031

3132
/**
3233
* Create a new {@link R2dbcMappingContext}.
3334
*/
3435
public R2dbcMappingContext() {
35-
setForceQuote(false);
36+
setForceQuote(true);
3637
}
3738

3839
/**
@@ -41,8 +42,10 @@ public R2dbcMappingContext() {
4142
* @param namingStrategy must not be {@literal null}.
4243
*/
4344
public R2dbcMappingContext(NamingStrategy namingStrategy) {
45+
4446
super(namingStrategy);
45-
setForceQuote(false);
47+
48+
setForceQuote(true);
4649
}
4750

4851
@Override

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/config/H2IntegrationTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* Integration test for {@link DatabaseClient} and repositories using H2.
4343
*
4444
* @author Mark Paluch
45+
* @author Jens Schauder
4546
*/
4647
@ExtendWith(SpringExtension.class)
4748
@ContextConfiguration
@@ -56,17 +57,17 @@ class H2IntegrationTests {
5657
void before() {
5758

5859
try {
59-
jdbc.execute("DROP TABLE legoset");
60+
jdbc.execute("DROP TABLE \"legoset\"");
6061
} catch (DataAccessException e) {}
6162
jdbc.execute(H2TestSupport.CREATE_TABLE_LEGOSET);
6263
}
6364

6465
@Test // gh-109
6566
void shouldSelectCountWithDatabaseClient() {
6667

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

69-
databaseClient.sql("SELECT COUNT(*) FROM legoset") //
70+
databaseClient.sql("SELECT COUNT(*) FROM \"legoset\"") //
7071
.map(it -> it.get(0, Long.class)) //
7172
.all() //
7273
.as(StepVerifier::create) //
@@ -77,7 +78,7 @@ void shouldSelectCountWithDatabaseClient() {
7778
@Test // gh-109
7879
void shouldSelectCountWithRepository() {
7980

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

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

100101
interface H2Repository extends ReactiveCrudRepository<LegoSet, Integer> {
101102

102-
@Query("SELECT COUNT(*) FROM legoset")
103+
@Query("SELECT COUNT(*) FROM \"legoset\"")
103104
Mono<Long> selectCount();
104105
}
105106

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/convert/MappingR2dbcConverterUnitTests.java

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@
1515
*/
1616
package org.springframework.data.r2dbc.convert;
1717

18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
20+
1821
import io.r2dbc.spi.R2dbcType;
1922
import io.r2dbc.spi.Row;
2023
import io.r2dbc.spi.test.MockColumnMetadata;
2124
import io.r2dbc.spi.test.MockRow;
2225
import io.r2dbc.spi.test.MockRowMetadata;
26+
27+
import java.time.Instant;
28+
import java.time.LocalDateTime;
29+
import java.util.Arrays;
30+
import java.util.Collections;
31+
import java.util.Map;
32+
2333
import org.junit.jupiter.api.BeforeEach;
2434
import org.junit.jupiter.api.Test;
2535
import org.springframework.beans.factory.annotation.Value;
@@ -37,19 +47,11 @@
3747
import org.springframework.data.relational.core.sql.SqlIdentifier;
3848
import org.springframework.r2dbc.core.Parameter;
3949

40-
import java.time.Instant;
41-
import java.time.LocalDateTime;
42-
import java.util.Arrays;
43-
import java.util.Collections;
44-
import java.util.Map;
45-
46-
import static org.assertj.core.api.Assertions.*;
47-
import static org.mockito.Mockito.*;
48-
4950
/**
5051
* Unit tests for {@link MappingR2dbcConverter}.
5152
*
5253
* @author Mark Paluch
54+
* @author Jens Schauder
5355
*/
5456
public class MappingR2dbcConverterUnitTests {
5557

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

81-
assertThat(row).containsEntry(SqlIdentifier.unquoted("id"), Parameter.fromOrEmpty("id", String.class));
82-
assertThat(row).containsEntry(SqlIdentifier.unquoted("firstname"), Parameter.fromOrEmpty("Walter", String.class));
83-
assertThat(row).containsEntry(SqlIdentifier.unquoted("lastname"), Parameter.fromOrEmpty("White", String.class));
84-
assertThat(row).containsEntry(SqlIdentifier.unquoted("instant"), Parameter.from(instant));
85-
assertThat(row).containsEntry(SqlIdentifier.unquoted("local_date_time"), Parameter.from(localDateTime));
83+
assertThat(row).containsEntry(SqlIdentifier.quoted("ID"), Parameter.fromOrEmpty("id", String.class));
84+
assertThat(row).containsEntry(SqlIdentifier.quoted("FIRSTNAME"), Parameter.fromOrEmpty("Walter", String.class));
85+
assertThat(row).containsEntry(SqlIdentifier.quoted("LASTNAME"), Parameter.fromOrEmpty("White", String.class));
86+
assertThat(row).containsEntry(SqlIdentifier.quoted("INSTANT"), Parameter.from(instant));
87+
assertThat(row).containsEntry(SqlIdentifier.quoted("LOCAL_DATE_TIME"), Parameter.from(localDateTime));
8688
}
8789

8890
@Test // gh-41
@@ -122,7 +124,7 @@ void shouldConvertMapToString() {
122124
OutboundRow row = new OutboundRow();
123125
converter.write(withMap, row);
124126

125-
assertThat(row).containsEntry(SqlIdentifier.unquoted("nested"), Parameter.from("map"));
127+
assertThat(row).containsEntry(SqlIdentifier.quoted("NESTED"), Parameter.from("map"));
126128
}
127129

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

146-
assertThat(row).containsEntry(SqlIdentifier.unquoted("condition"), Parameter.from("Mint"));
148+
assertThat(row).containsEntry(SqlIdentifier.quoted("CONDITION"), Parameter.from("Mint"));
147149
}
148150

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

156-
assertThat(row).containsEntry(SqlIdentifier.unquoted("condition"), Parameter.fromOrEmpty(null, String.class));
158+
assertThat(row).containsEntry(SqlIdentifier.quoted("CONDITION"), Parameter.fromOrEmpty(null, String.class));
157159
}
158160

159161
@Test // gh-59
@@ -215,7 +217,7 @@ void writeShouldWritePrimitiveIdIfValueIsNonZero() {
215217
OutboundRow row = new OutboundRow();
216218
converter.write(new WithPrimitiveId(1), row);
217219

218-
assertThat(row).containsEntry(SqlIdentifier.unquoted("id"), Parameter.fromOrEmpty(1L, Long.TYPE));
220+
assertThat(row).containsEntry(SqlIdentifier.quoted("ID"), Parameter.fromOrEmpty(1L, Long.TYPE));
219221
}
220222

221223
@Test // gh-59
@@ -257,12 +259,11 @@ void writeShouldObtainIdFromIdentifierAccessor() {
257259
OutboundRow row = new OutboundRow();
258260
converter.write(entity, row);
259261

260-
assertThat(row).containsEntry(SqlIdentifier.unquoted("id"), Parameter.from(42L));
262+
assertThat(row).containsEntry(SqlIdentifier.quoted("ID"), Parameter.from(42L));
261263
}
262264

263265
static class Person {
264-
@Id
265-
String id;
266+
@Id String id;
266267
String firstname, lastname;
267268
Instant instant;
268269
LocalDateTime localDateTime;
@@ -298,8 +299,7 @@ public void setLastname(String lastname) {
298299
}
299300

300301
static class WithEnum {
301-
@Id
302-
String id;
302+
@Id String id;
303303
Condition condition;
304304

305305
public WithEnum(String id, Condition condition) {
@@ -313,8 +313,7 @@ enum Condition {
313313
}
314314

315315
static class PersonWithConversions {
316-
@Id
317-
String id;
316+
@Id String id;
318317
Map<String, String> nested;
319318
NonMappableEntity unsupported;
320319

@@ -325,8 +324,7 @@ public PersonWithConversions(String id, Map<String, String> nested, NonMappableE
325324
}
326325
}
327326

328-
record WithPrimitiveId (
329-
@Id long id){
327+
record WithPrimitiveId(@Id long id) {
330328
}
331329

332330
static class CustomConversionPerson {

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/convert/MySqlMappingR2dbcConverterUnitTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* MySQL-specific unit tests for {@link MappingR2dbcConverter}.
4040
*
4141
* @author Mark Paluch
42+
* @author Jens Schauder
4243
*/
4344
class MySqlMappingR2dbcConverterUnitTests {
4445

@@ -68,8 +69,9 @@ void shouldWriteBooleanToByte() {
6869

6970
converter.write(object, row);
7071

71-
OutboundRowAssert.assertThat(row).containsColumnWithValue("flag1", (byte) 1).containsColumnWithValue("flag2",
72-
(byte) 0);
72+
OutboundRowAssert.assertThat(row) //
73+
.containsColumnWithValue("FLAG1", (byte) 1) //
74+
.containsColumnWithValue("FLAG2", (byte) 0);
7375
}
7476

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

9799
converter.write(object, row);
98100

99-
OutboundRowAssert.assertThat(row).containsColumnWithValue("state", (byte) 3);
101+
OutboundRowAssert.assertThat(row).containsColumnWithValue("STATE", (byte) 3);
100102
}
101103

102104
record BooleanMapping(
103105

104106
Integer id, boolean flag1, boolean flag2) {
105107
}
106108

107-
record WithByte (
109+
record WithByte(
108110

109-
Integer id,
110-
byte state){
111+
Integer id, byte state) {
111112
}
112113

113114
}

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/convert/PostgresMappingR2dbcConverterUnitTests.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* Postgres-specific unit tests for {@link MappingR2dbcConverter}.
5050
*
5151
* @author Mark Paluch
52+
* @author Jens Schauder
5253
*/
5354
class PostgresMappingR2dbcConverterUnitTests {
5455

@@ -79,7 +80,7 @@ void shouldPassThruJson() {
7980
OutboundRow row = new OutboundRow();
8081
converter.write(person, row);
8182

82-
assertThat(row).containsEntry(SqlIdentifier.unquoted("json_value"), Parameter.from(person.jsonValue));
83+
assertThat(row).containsEntry(SqlIdentifier.quoted("JSON_VALUE"), Parameter.from(person.jsonValue));
8384
}
8485

8586
@Test // gh-453
@@ -127,24 +128,18 @@ void shouldApplyCustomWritingConverter() {
127128
OutboundRow row = new OutboundRow();
128129
converter.write(object, row);
129130

130-
Parameter parameter = row.get(SqlIdentifier.unquoted("holder"));
131+
Parameter parameter = row.get(SqlIdentifier.quoted("HOLDER"));
131132
assertThat(parameter).isNotNull();
132133
assertThat(parameter.getValue()).isInstanceOf(Json.class);
133134
}
134135

135-
record JsonPerson(
136-
@Id Long id,
137-
Json jsonValue) {
136+
record JsonPerson(@Id Long id, Json jsonValue) {
138137
}
139138

140-
record ConvertedJson(
141-
@Id Long id,
142-
String jsonString,
143-
byte[] jsonBytes) {
139+
record ConvertedJson(@Id Long id, String jsonString, byte[] jsonBytes) {
144140
}
145141

146-
record WithJsonHolder(
147-
JsonHolder holder) {
142+
record WithJsonHolder(JsonHolder holder) {
148143
}
149144

150145
@ReadingConverter

0 commit comments

Comments
 (0)