Skip to content

Commit 7ded5f8

Browse files
committed
Polishing.
Introduce factory methods to create mapping contexts that use quoting and that use plain identifiers for easier creation of the correct mapping context. See #1993 Original pull request #2066
1 parent b89e070 commit 7ded5f8

25 files changed

+178
-70
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/JdbcMappingContext.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,61 @@ public JdbcMappingContext(NamingStrategy namingStrategy) {
5454
setSimpleTypeHolder(JdbcSimpleTypes.HOLDER);
5555
}
5656

57+
/**
58+
* Create a new {@code JdbcMappingContext} using {@link #setForceQuote(boolean) plain identifiers}. Plain
59+
* {@link org.springframework.data.relational.core.sql.SqlIdentifier identifiers} (i.e. table and column names) are
60+
* typically not case-sensitive (case-sensitivity can be still enforced by specific database configurations).
61+
*
62+
* @return a new {@code JdbcMappingContext} using plain identifiers.
63+
* @since 4.0
64+
*/
65+
public static JdbcMappingContext forPlainIdentifiers() {
66+
JdbcMappingContext context = forQuotedIdentifiers();
67+
context.setForceQuote(false);
68+
return context;
69+
}
70+
71+
/**
72+
* Create a new {@code JdbcMappingContext} using {@link #setForceQuote(boolean) plain identifiers} and the given
73+
* {@link NamingStrategy}. Plain {@link org.springframework.data.relational.core.sql.SqlIdentifier identifiers} (i.e.
74+
* table and column names) are typically not case-sensitive (case-sensitivity can be still enforced by specific
75+
* database configurations).
76+
*
77+
* @param namingStrategy must not be {@literal null}.
78+
* @return a new {@code JdbcMappingContext} using plain identifiers.
79+
* @since 4.0
80+
*/
81+
public static JdbcMappingContext forPlainIdentifiers(NamingStrategy namingStrategy) {
82+
JdbcMappingContext context = forQuotedIdentifiers(namingStrategy);
83+
context.setForceQuote(false);
84+
return context;
85+
}
86+
87+
/**
88+
* Create a new {@code JdbcMappingContext} using {@link #setForceQuote(boolean) quoted identifiers} (default
89+
* behavior). Quoted {@link org.springframework.data.relational.core.sql.SqlIdentifier identifiers} (i.e. table and
90+
* column names) are typically case-sensitive.
91+
*
92+
* @return a new {@code JdbcMappingContext} using quoted identifiers.
93+
* @since 4.0
94+
*/
95+
public static JdbcMappingContext forQuotedIdentifiers() {
96+
return new JdbcMappingContext();
97+
}
98+
99+
/**
100+
* Create a new {@code JdbcMappingContext} using {@link #setForceQuote(boolean) quoted identifiers} (default behavior)
101+
* and the given {@link NamingStrategy}. Quoted {@link org.springframework.data.relational.core.sql.SqlIdentifier
102+
* identifiers} (i.e. table and column names) are typically case-sensitive.
103+
*
104+
* @param namingStrategy must not be {@literal null}.
105+
* @return a new {@code JdbcMappingContext} using quoted identifiers.
106+
* @since 4.0
107+
*/
108+
public static JdbcMappingContext forQuotedIdentifiers(NamingStrategy namingStrategy) {
109+
return new JdbcMappingContext(namingStrategy);
110+
}
111+
57112
@Override
58113
protected RelationalPersistentProperty createPersistentProperty(Property property,
59114
RelationalPersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder) {

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/AbstractJdbcConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public RelationalManagedTypes jdbcManagedTypes() throws ClassNotFoundException {
116116
public JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStrategy,
117117
JdbcCustomConversions customConversions, RelationalManagedTypes jdbcManagedTypes) {
118118

119-
JdbcMappingContext mappingContext = new JdbcMappingContext(namingStrategy.orElse(DefaultNamingStrategy.INSTANCE));
119+
JdbcMappingContext mappingContext = JdbcMappingContext
120+
.forQuotedIdentifiers(namingStrategy.orElse(DefaultNamingStrategy.INSTANCE));
120121
mappingContext.setSimpleTypeHolder(customConversions.getSimpleTypeHolder());
121122
mappingContext.setManagedTypes(jdbcManagedTypes);
122123

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/EntityRowMapperUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ private <T> EntityRowMapper<T> createRowMapper(Class<T> type) {
10321032
@SuppressWarnings("unchecked")
10331033
private <T> EntityRowMapper<T> createRowMapper(Class<T> type, NamingStrategy namingStrategy) {
10341034

1035-
RelationalMappingContext context = new JdbcMappingContext(namingStrategy);
1035+
RelationalMappingContext context = JdbcMappingContext.forQuotedIdentifiers(namingStrategy);
10361036

10371037
DataAccessStrategy accessStrategy = mock(DataAccessStrategy.class);
10381038

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/JdbcIdentifierBuilderUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
*/
4141
class JdbcIdentifierBuilderUnitTests {
4242

43-
JdbcMappingContext context = new JdbcMappingContext();
43+
JdbcMappingContext context = JdbcMappingContext.forQuotedIdentifiers();
4444
JdbcConverter converter = new MappingJdbcConverter(context, (identifier, path) -> {
4545
throw new UnsupportedOperationException();
4646
});

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/MappingJdbcConverterUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void accessesCorrectValuesForOneToOneRelationshipWithIdenticallyNamedIdPropertie
165165
@Test // GH-1750
166166
void readByteArrayToNestedUuidWithCustomConverter() {
167167

168-
JdbcMappingContext context = new JdbcMappingContext();
168+
JdbcMappingContext context = JdbcMappingContext.forQuotedIdentifiers();
169169
StubbedJdbcTypeFactory typeFactory = new StubbedJdbcTypeFactory();
170170
Converter<byte[], UUID> customConverter = new ByteArrayToUuid();
171171
MappingJdbcConverter converter = new MappingJdbcConverter( //

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/RowDocumentResultSetExtractorUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
*/
4949
public class RowDocumentResultSetExtractorUnitTests {
5050

51-
RelationalMappingContext context = new JdbcMappingContext(new DefaultNamingStrategy());
51+
RelationalMappingContext context = JdbcMappingContext.forQuotedIdentifiers(new DefaultNamingStrategy());
5252

5353
private final PathToColumnMapping column = new PathToColumnMapping() {
5454
@Override

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorContextBasedNamingStrategyUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private void threadedTest(String user, CountDownLatch latch, Consumer<String> te
212212
*/
213213
private SqlGenerator configureSqlGenerator(NamingStrategy namingStrategy) {
214214

215-
RelationalMappingContext context = new JdbcMappingContext(namingStrategy);
215+
RelationalMappingContext context = JdbcMappingContext.forQuotedIdentifiers(namingStrategy);
216216
JdbcConverter converter = new MappingJdbcConverter(context, (identifier, path) -> {
217217
throw new UnsupportedOperationException();
218218
});

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorEmbeddedUnitTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@
4646
*/
4747
class SqlGeneratorEmbeddedUnitTests {
4848

49-
private RelationalMappingContext context = new JdbcMappingContext();
49+
private RelationalMappingContext context = JdbcMappingContext.forPlainIdentifiers();
5050
private JdbcConverter converter = new MappingJdbcConverter(context, (identifier, path) -> {
5151
throw new UnsupportedOperationException();
5252
});
5353
private SqlGenerator sqlGenerator;
5454

5555
@BeforeEach
5656
void setUp() {
57-
this.context.setForceQuote(false);
5857
this.sqlGenerator = createSqlGenerator(DummyEntity.class);
5958
}
6059

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorFixedNamingStrategyUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private PersistentPropertyPath<RelationalPersistentProperty> getPath(String path
196196
*/
197197
private SqlGenerator configureSqlGenerator(NamingStrategy namingStrategy) {
198198

199-
context = new JdbcMappingContext(namingStrategy);
199+
context = JdbcMappingContext.forQuotedIdentifiers(namingStrategy);
200200
JdbcConverter converter = new MappingJdbcConverter(context, (identifier, path) -> {
201201
throw new UnsupportedOperationException();
202202
});

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,11 @@
1515
*/
1616
package org.springframework.data.jdbc.core.convert;
1717

18-
import static java.util.Collections.emptySet;
19-
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
21-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
22-
import static org.assertj.core.api.Assertions.entry;
23-
import static org.assertj.core.api.SoftAssertions.assertSoftly;
24-
import static org.springframework.data.relational.core.mapping.ForeignKeyNaming.APPLY_RENAMING;
25-
import static org.springframework.data.relational.core.mapping.ForeignKeyNaming.IGNORE_RENAMING;
26-
import static org.springframework.data.relational.core.sql.SqlIdentifier.EMPTY;
27-
import static org.springframework.data.relational.core.sql.SqlIdentifier.quoted;
28-
import static org.springframework.data.relational.core.sql.SqlIdentifier.unquoted;
18+
import static java.util.Collections.*;
19+
import static org.assertj.core.api.Assertions.*;
20+
import static org.assertj.core.api.SoftAssertions.*;
21+
import static org.springframework.data.relational.core.mapping.ForeignKeyNaming.*;
22+
import static org.springframework.data.relational.core.sql.SqlIdentifier.*;
2923

3024
import java.util.Map;
3125
import java.util.Set;
@@ -87,7 +81,7 @@ class SqlGeneratorUnitTests {
8781
private static final Identifier BACKREF = Identifier.of(unquoted("backref"), "some-value", String.class);
8882

8983
private final PrefixingNamingStrategy namingStrategy = new PrefixingNamingStrategy();
90-
private RelationalMappingContext context = new JdbcMappingContext(namingStrategy);
84+
private RelationalMappingContext context = JdbcMappingContext.forQuotedIdentifiers(namingStrategy);
9185
private final JdbcConverter converter = new MappingJdbcConverter(context, (identifier, path) -> {
9286
throw new UnsupportedOperationException();
9387
});
@@ -977,7 +971,7 @@ void selectByQueryPaginationValidTest() {
977971
void backReferenceShouldConsiderRenamedParent() {
978972

979973
namingStrategy.setForeignKeyNaming(APPLY_RENAMING);
980-
context = new JdbcMappingContext(namingStrategy);
974+
context = JdbcMappingContext.forQuotedIdentifiers(namingStrategy);
981975

982976
String sql = sqlGenerator.createDeleteInByPath(getPath("ref", RenamedDummy.class));
983977

@@ -988,7 +982,7 @@ void backReferenceShouldConsiderRenamedParent() {
988982
void backReferenceShouldIgnoreRenamedParent() {
989983

990984
namingStrategy.setForeignKeyNaming(IGNORE_RENAMING);
991-
context = new JdbcMappingContext(namingStrategy);
985+
context = JdbcMappingContext.forQuotedIdentifiers(namingStrategy);
992986

993987
String sql = sqlGenerator.createDeleteInByPath(getPath("ref", RenamedDummy.class));
994988

@@ -999,7 +993,7 @@ void backReferenceShouldIgnoreRenamedParent() {
999993
void keyColumnShouldConsiderRenamedParent() {
1000994

1001995
namingStrategy.setForeignKeyNaming(APPLY_RENAMING);
1002-
context = new JdbcMappingContext(namingStrategy);
996+
context = JdbcMappingContext.forQuotedIdentifiers(namingStrategy);
1003997

1004998
SqlGenerator sqlGenerator = createSqlGenerator(ReferencedEntity.class);
1005999
String sql = sqlGenerator.getFindAllByProperty(Identifier.of(unquoted("parentId"), 23, RenamedDummy.class),
@@ -1012,7 +1006,7 @@ void keyColumnShouldConsiderRenamedParent() {
10121006
void keyColumnShouldIgnoreRenamedParent() {
10131007

10141008
namingStrategy.setForeignKeyNaming(IGNORE_RENAMING);
1015-
context = new JdbcMappingContext(namingStrategy);
1009+
context = JdbcMappingContext.forQuotedIdentifiers(namingStrategy);
10161010

10171011
SqlGenerator sqlGenerator = createSqlGenerator(ReferencedEntity.class);
10181012
String sql = sqlGenerator.getFindAllByProperty(Identifier.of(unquoted("parentId"), 23, RenamedDummy.class),

0 commit comments

Comments
 (0)