Skip to content

Commit fb896a8

Browse files
committed
Move to enum representation of dialect values.
1 parent fd8a8c9 commit fb896a8

File tree

4 files changed

+96
-15
lines changed

4 files changed

+96
-15
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcDataProperties.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ public class JdbcDataProperties {
3131
/**
3232
* Dialect to use. By default, the dialect is determined by inspecting the database connection.
3333
*/
34-
private Class<? extends Dialect> dialect;
34+
private JdbcDatabaseDialect dialect;
3535

36-
public Class<? extends Dialect> getDialect() {
36+
public JdbcDatabaseDialect getDialect() {
3737
return this.dialect;
3838
}
3939

40-
public void setDialect(Class<? extends Dialect> dialect) {
40+
public void setDialect(JdbcDatabaseDialect dialect) {
4141
this.dialect = dialect;
4242
}
43-
4443
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.data.jdbc;
18+
19+
import java.util.function.Supplier;
20+
21+
import org.springframework.data.jdbc.core.dialect.JdbcDb2Dialect;
22+
import org.springframework.data.jdbc.core.dialect.JdbcMySqlDialect;
23+
import org.springframework.data.jdbc.core.dialect.JdbcPostgresDialect;
24+
import org.springframework.data.jdbc.core.dialect.JdbcSqlServerDialect;
25+
import org.springframework.data.relational.core.dialect.Db2Dialect;
26+
import org.springframework.data.relational.core.dialect.Dialect;
27+
import org.springframework.data.relational.core.dialect.H2Dialect;
28+
import org.springframework.data.relational.core.dialect.HsqlDbDialect;
29+
import org.springframework.data.relational.core.dialect.MariaDbDialect;
30+
import org.springframework.data.relational.core.dialect.OracleDialect;
31+
32+
/**
33+
* List of database dialects that can be configured in Boot for use with Spring Data JDBC.
34+
*
35+
* @author Jens Schauder
36+
* @since 3.3
37+
*/
38+
public enum JdbcDatabaseDialect implements Supplier<Dialect> {
39+
40+
DB2 {
41+
@Override
42+
public Dialect get() {
43+
return JdbcDb2Dialect.INSTANCE;
44+
}
45+
},
46+
H2{
47+
@Override
48+
public Dialect get() {
49+
return H2Dialect.INSTANCE;
50+
}
51+
},
52+
HSQL{
53+
@Override
54+
public Dialect get() {
55+
return HsqlDbDialect.INSTANCE;
56+
}
57+
},
58+
MARIA{
59+
@Override
60+
public Dialect get() {
61+
return MariaDbDialect.INSTANCE;
62+
}
63+
},
64+
MYSQL{
65+
@Override
66+
public Dialect get() {
67+
return JdbcMySqlDialect.INSTANCE;
68+
}
69+
},
70+
ORACLE{
71+
@Override
72+
public Dialect get() {
73+
return OracleDialect.INSTANCE;
74+
75+
}
76+
},
77+
POSTGRESQL{
78+
@Override
79+
public Dialect get() {
80+
return JdbcPostgresDialect.INSTANCE;
81+
}
82+
},
83+
SQL_SERVER{
84+
@Override
85+
public Dialect get() {
86+
return JdbcSqlServerDialect.INSTANCE;
87+
}
88+
}
89+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfiguration.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,9 @@ public DataAccessStrategy dataAccessStrategyBean(NamedParameterJdbcOperations op
149149
@Bean
150150
@ConditionalOnMissingBean
151151
public Dialect jdbcDialect(NamedParameterJdbcOperations operations) {
152-
if (this.properties.getDialect() != null
153-
) {
154-
Class<?> dialectType = this.properties.getDialect();
155-
try {
156-
return (Dialect)dialectType.getDeclaredConstructor().newInstance();
157-
}
158-
catch (InstantiationException | IllegalAccessException |
159-
InvocationTargetException | NoSuchMethodException e) {
160-
throw new BeanCreationException("Couldn't create instance of type " + dialectType, e);
161-
}
152+
JdbcDatabaseDialect dialectEnum = this.properties.getDialect();
153+
if (dialectEnum != null) {
154+
return dialectEnum.get();
162155
}
163156
return super.jdbcDialect(operations);
164157
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfigurationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void allowsUserToDefineCustomDialect() {
186186
@Test
187187
void allowsConfigurationOfDialectByProperty() {
188188
this.contextRunner.with(database())
189-
.withPropertyValues("spring.data.jdbc.dialect:" + JdbcPostgresDialect.class.getName())
189+
.withPropertyValues("spring.data.jdbc.dialect=postgresql" )
190190
.withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class,
191191
DataSourceTransactionManagerAutoConfiguration.class))
192192
.withUserConfiguration(TestConfiguration.class)

0 commit comments

Comments
 (0)