Skip to content

Commit 6a62dd2

Browse files
committed
GH-2020 rebased
1 parent 9bc242d commit 6a62dd2

File tree

17 files changed

+225
-92
lines changed

17 files changed

+225
-92
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/dialect/JdbcDialect.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package org.springframework.data.jdbc.core.dialect;
1717

1818
import org.springframework.data.relational.core.dialect.Dialect;
19+
import org.springframework.data.jdbc.core.dialect.SqlTypeResolver;
20+
import org.springframework.data.jdbc.core.dialect.DefaultSqlTypeResolver;
1921

2022
/**
2123
* {@link org.springframework.data.relational.core.dialect.ArrayColumns} that offer JDBC specific functionality.
@@ -37,4 +39,12 @@ default JdbcArrayColumns getArraySupport() {
3739
return JdbcArrayColumns.Unsupported.INSTANCE;
3840
}
3941

42+
/**
43+
* Returns a {@link SqlTypeResolver} of this dialect.
44+
*
45+
* @since 4.0
46+
*/
47+
default SqlTypeResolver getSqlTypeResolver() {
48+
return DefaultSqlTypeResolver.INSTANCE;
49+
}
4050
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/JdbcParameters.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import org.springframework.core.MethodParameter;
2222
import org.springframework.data.jdbc.core.convert.JdbcColumnTypes;
2323
import org.springframework.data.jdbc.support.JdbcUtil;
24-
import org.springframework.data.relational.core.dialect.DefaultSqlTypeResolver;
25-
import org.springframework.data.relational.core.dialect.SqlTypeResolver;
24+
import org.springframework.data.jdbc.core.dialect.DefaultSqlTypeResolver;
25+
import org.springframework.data.jdbc.core.dialect.SqlTypeResolver;
2626
import org.springframework.data.relational.repository.query.RelationalParameters;
2727
import org.springframework.data.repository.query.Parameter;
2828
import org.springframework.data.repository.query.ParametersSource;
@@ -47,7 +47,7 @@ public class JdbcParameters extends RelationalParameters {
4747
public JdbcParameters(ParametersSource parametersSource) {
4848
super(parametersSource,
4949
methodParameter -> new JdbcParameter(methodParameter, parametersSource.getDomainTypeInformation(),
50-
Lazy.of(DefaultSqlTypeResolver.INSTANCE)));
50+
DefaultSqlTypeResolver.INSTANCE));
5151
}
5252

5353
/**
@@ -56,7 +56,7 @@ public JdbcParameters(ParametersSource parametersSource) {
5656
* @param parametersSource must not be {@literal null}.
5757
* @param sqlTypeResolver must not be {@literal null}.
5858
*/
59-
public JdbcParameters(ParametersSource parametersSource, Lazy<SqlTypeResolver> sqlTypeResolver) {
59+
public JdbcParameters(ParametersSource parametersSource, SqlTypeResolver sqlTypeResolver) {
6060
super(parametersSource,
6161
methodParameter -> new JdbcParameter(methodParameter, parametersSource.getDomainTypeInformation(), sqlTypeResolver));
6262

@@ -96,13 +96,13 @@ public static class JdbcParameter extends RelationalParameter {
9696
*
9797
* @param parameter must not be {@literal null}.
9898
*/
99-
JdbcParameter(MethodParameter parameter, TypeInformation<?> domainType, Lazy<SqlTypeResolver> sqlTypeResolver) {
99+
JdbcParameter(MethodParameter parameter, TypeInformation<?> domainType, SqlTypeResolver sqlTypeResolver) {
100100
super(parameter, domainType);
101101

102102
TypeInformation<?> typeInformation = getTypeInformation();
103103

104104
sqlType = Lazy.of(() -> {
105-
SQLType resolvedSqlType = sqlTypeResolver.get().resolveSqlType(this);
105+
SQLType resolvedSqlType = sqlTypeResolver.resolveSqlType(this);
106106

107107
if (resolvedSqlType == null) {
108108
return JdbcUtil.targetSqlTypeFor(JdbcColumnTypes.INSTANCE.resolvePrimitiveType(typeInformation.getType()));
@@ -112,7 +112,7 @@ public static class JdbcParameter extends RelationalParameter {
112112
});
113113

114114
actualSqlType = Lazy.of(() -> {
115-
SQLType resolvedActualSqlType = sqlTypeResolver.get().resolveActualSqlType(this);
115+
SQLType resolvedActualSqlType = sqlTypeResolver.resolveActualSqlType(this);
116116

117117
if (resolvedActualSqlType == null) {
118118
return JdbcUtil.targetSqlTypeFor(JdbcColumnTypes.INSTANCE.resolvePrimitiveType(typeInformation.getActualType().getType()));

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/JdbcQueryMethod.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,22 @@
1717

1818
import java.lang.annotation.Annotation;
1919
import java.lang.reflect.Method;
20-
import java.util.List;
2120
import java.util.Map;
2221
import java.util.Optional;
2322

2423
import org.springframework.core.annotation.AnnotatedElementUtils;
2524
import org.springframework.core.annotation.AnnotationUtils;
2625
import org.springframework.data.mapping.context.MappingContext;
2726
import org.springframework.data.projection.ProjectionFactory;
28-
import org.springframework.data.relational.core.dialect.DefaultSqlTypeResolver;
29-
import org.springframework.data.relational.core.dialect.SqlTypeResolver;
27+
import org.springframework.data.jdbc.core.dialect.DefaultSqlTypeResolver;
28+
import org.springframework.data.jdbc.core.dialect.SqlTypeResolver;
3029
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
3130
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
3231
import org.springframework.data.relational.repository.Lock;
3332
import org.springframework.data.relational.repository.query.RelationalEntityMetadata;
3433
import org.springframework.data.relational.repository.query.SimpleRelationalEntityMetadata;
3534
import org.springframework.data.repository.core.NamedQueries;
3635
import org.springframework.data.repository.core.RepositoryMetadata;
37-
import org.springframework.data.repository.query.Parameter;
3836
import org.springframework.data.repository.query.Parameters;
3937
import org.springframework.data.repository.query.ParametersSource;
4038
import org.springframework.data.repository.query.QueryMethod;
@@ -59,7 +57,6 @@
5957
* @author Mark Paluch
6058
* @author Daeho Kwon
6159
* @author Mikhail Polivakha
62-
>>>>>>> d92a7298 (GH-2020 Added SqlTypeResolver abstraction)
6360
*/
6461
public class JdbcQueryMethod extends QueryMethod {
6562

@@ -70,8 +67,6 @@ public class JdbcQueryMethod extends QueryMethod {
7067
private @Nullable RelationalEntityMetadata<?> metadata;
7168
private final boolean modifyingQuery;
7269

73-
private final SqlTypeResolver sqlTypeResolver;
74-
7570
// TODO: Remove NamedQueries and put it into JdbcQueryLookupStrategy
7671
public JdbcQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
7772
NamedQueries namedQueries,
@@ -84,19 +79,12 @@ public JdbcQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFac
8479
MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> mappingContext,
8580
SqlTypeResolver sqlTypeResolver) {
8681

87-
super(method, metadata, factory, JdbcParameters::new);
82+
super(method, metadata, factory, parametersSource -> new JdbcParameters(parametersSource, sqlTypeResolver));
8883
this.namedQueries = namedQueries;
8984
this.method = method;
9085
this.mappingContext = mappingContext;
9186
this.annotationCache = new ConcurrentReferenceHashMap<>();
9287
this.modifyingQuery = AnnotationUtils.findAnnotation(method, Modifying.class) != null;
93-
this.sqlTypeResolver = sqlTypeResolver;
94-
}
95-
96-
// SqlTypeResolver has to be wrapped, because the createParameters() is invoked in the parents constructor before child initialization
97-
@Override
98-
protected Parameters<?, ?> createParameters(ParametersSource parametersSource) {
99-
return new JdbcParameters(parametersSource, Lazy.of(() -> this.sqlTypeResolver));
10088
}
10189

10290
@Override

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.context.ApplicationEventPublisher;
2424
import org.springframework.data.jdbc.core.convert.JdbcConverter;
2525
import org.springframework.data.jdbc.core.convert.QueryMappingConfiguration;
26+
import org.springframework.data.jdbc.core.dialect.JdbcDialect;
2627
import org.springframework.data.jdbc.repository.query.DefaultRowMapperFactory;
2728
import org.springframework.data.jdbc.repository.query.JdbcQueryMethod;
2829
import org.springframework.data.jdbc.repository.query.PartTreeJdbcQuery;
@@ -69,7 +70,7 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
6970
protected final ValueExpressionDelegate delegate;
7071

7172
JdbcQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
72-
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
73+
RelationalMappingContext context, JdbcConverter converter, JdbcDialect dialect,
7374
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
7475
ValueExpressionDelegate delegate) {
7576

@@ -105,7 +106,7 @@ static class CreateQueryLookupStrategy extends JdbcQueryLookupStrategy {
105106
private final RowMapperFactory rowMapperFactory;
106107

107108
CreateQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
108-
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
109+
RelationalMappingContext context, JdbcConverter converter, JdbcDialect dialect,
109110
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
110111
ValueExpressionDelegate delegate) {
111112

@@ -138,7 +139,7 @@ static class DeclaredQueryLookupStrategy extends JdbcQueryLookupStrategy {
138139
private final RowMapperFactory rowMapperFactory;
139140

140141
DeclaredQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
141-
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
142+
RelationalMappingContext context, JdbcConverter converter, JdbcDialect dialect,
142143
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
143144
@Nullable BeanFactory beanfactory, ValueExpressionDelegate delegate) {
144145
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations, delegate);
@@ -191,7 +192,7 @@ static class CreateIfNotFoundQueryLookupStrategy extends JdbcQueryLookupStrategy
191192
* @param lookupStrategy must not be {@literal null}.
192193
*/
193194
CreateIfNotFoundQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
194-
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
195+
RelationalMappingContext context, JdbcConverter converter, JdbcDialect dialect,
195196
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
196197
CreateQueryLookupStrategy createStrategy, DeclaredQueryLookupStrategy lookupStrategy,
197198
ValueExpressionDelegate delegate) {
@@ -225,6 +226,11 @@ JdbcQueryMethod getJdbcQueryMethod(Method method, RepositoryMetadata repositoryM
225226
return new JdbcQueryMethod(method, repositoryMetadata, projectionFactory, namedQueries, getMappingContext(), getDialect().getSqlTypeResolver());
226227
}
227228

229+
@Override
230+
public JdbcDialect getDialect() {
231+
return (JdbcDialect) super.getDialect();
232+
}
233+
228234
/**
229235
* Creates a {@link QueryLookupStrategy} based on the provided
230236
* {@link org.springframework.data.repository.query.QueryLookupStrategy.Key}.
@@ -238,7 +244,9 @@ JdbcQueryMethod getJdbcQueryMethod(Method method, RepositoryMetadata repositoryM
238244
* @param queryMappingConfiguration must not be {@literal null}
239245
* @param operations must not be {@literal null}
240246
* @param beanFactory may be {@literal null}
247+
* @deprecated please, use {@link #create(Key, ApplicationEventPublisher, EntityCallbacks, RelationalMappingContext, JdbcConverter, JdbcDialect, QueryMappingConfiguration, NamedParameterJdbcOperations, BeanFactory, ValueExpressionDelegate)}
241248
*/
249+
@Deprecated(forRemoval = true, since = "4.0")
242250
public static QueryLookupStrategy create(@Nullable Key key, ApplicationEventPublisher publisher,
243251
@Nullable EntityCallbacks callbacks, RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
244252
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
@@ -250,6 +258,52 @@ public static QueryLookupStrategy create(@Nullable Key key, ApplicationEventPubl
250258
Assert.notNull(queryMappingConfiguration, "QueryMappingConfiguration must not be null");
251259
Assert.notNull(operations, "NamedParameterJdbcOperations must not be null");
252260
Assert.notNull(delegate, "ValueExpressionDelegate must not be null");
261+
Assert.state(dialect instanceof JdbcDialect, "Dialect must be an instance of the JdbcDialect");
262+
263+
CreateQueryLookupStrategy createQueryLookupStrategy = new CreateQueryLookupStrategy(publisher, callbacks, context,
264+
converter, (JdbcDialect) dialect, queryMappingConfiguration, operations, delegate);
265+
266+
DeclaredQueryLookupStrategy declaredQueryLookupStrategy = new DeclaredQueryLookupStrategy(publisher, callbacks,
267+
context, converter, (JdbcDialect) dialect, queryMappingConfiguration, operations, beanFactory, delegate);
268+
269+
Key keyToUse = key != null ? key : Key.CREATE_IF_NOT_FOUND;
270+
271+
LOG.debug(String.format("Using the queryLookupStrategy %s", keyToUse));
272+
273+
return switch (keyToUse) {
274+
case CREATE -> createQueryLookupStrategy;
275+
case USE_DECLARED_QUERY -> declaredQueryLookupStrategy;
276+
case CREATE_IF_NOT_FOUND ->
277+
new CreateIfNotFoundQueryLookupStrategy(publisher, callbacks, context, converter, (JdbcDialect) dialect,
278+
queryMappingConfiguration, operations, createQueryLookupStrategy, declaredQueryLookupStrategy, delegate);
279+
};
280+
}
281+
282+
/**
283+
* Creates a {@link QueryLookupStrategy} based on the provided
284+
* {@link org.springframework.data.repository.query.QueryLookupStrategy.Key}.
285+
*
286+
* @param key the key that decides what {@link QueryLookupStrategy} shozld be used.
287+
* @param publisher must not be {@literal null}
288+
* @param callbacks may be {@literal null}
289+
* @param context must not be {@literal null}
290+
* @param converter must not be {@literal null}
291+
* @param dialect must not be {@literal null}
292+
* @param queryMappingConfiguration must not be {@literal null}
293+
* @param operations must not be {@literal null}
294+
* @param beanFactory may be {@literal null}
295+
*/
296+
public static QueryLookupStrategy create(@Nullable Key key, ApplicationEventPublisher publisher,
297+
@Nullable EntityCallbacks callbacks, RelationalMappingContext context, JdbcConverter converter, JdbcDialect dialect,
298+
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
299+
@Nullable BeanFactory beanFactory, ValueExpressionDelegate delegate) {
300+
Assert.notNull(publisher, "ApplicationEventPublisher must not be null");
301+
Assert.notNull(context, "RelationalMappingContextPublisher must not be null");
302+
Assert.notNull(converter, "JdbcConverter must not be null");
303+
Assert.notNull(dialect, "Dialect must not be null");
304+
Assert.notNull(queryMappingConfiguration, "QueryMappingConfiguration must not be null");
305+
Assert.notNull(operations, "NamedParameterJdbcOperations must not be null");
306+
Assert.notNull(delegate, "ValueExpressionDelegate must not be null");
253307

254308
CreateQueryLookupStrategy createQueryLookupStrategy = new CreateQueryLookupStrategy(publisher, callbacks, context,
255309
converter, dialect, queryMappingConfiguration, operations, delegate);

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
2424
import org.springframework.data.jdbc.core.convert.JdbcConverter;
2525
import org.springframework.data.jdbc.core.convert.QueryMappingConfiguration;
26+
import org.springframework.data.jdbc.core.dialect.JdbcDialect;
2627
import org.springframework.data.mapping.callback.EntityCallbacks;
2728
import org.springframework.data.relational.core.dialect.Dialect;
2829
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
@@ -58,7 +59,7 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
5859
private final ApplicationEventPublisher publisher;
5960
private final DataAccessStrategy accessStrategy;
6061
private final NamedParameterJdbcOperations operations;
61-
private final Dialect dialect;
62+
private final JdbcDialect dialect;
6263
private @Nullable BeanFactory beanFactory;
6364

6465
private QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
@@ -75,10 +76,41 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
7576
* @param publisher must not be {@literal null}.
7677
* @param operations must not be {@literal null}.
7778
*/
79+
@Deprecated(forRemoval = true, since = "4.0")
7880
public JdbcRepositoryFactory(DataAccessStrategy dataAccessStrategy, RelationalMappingContext context,
7981
JdbcConverter converter, Dialect dialect, ApplicationEventPublisher publisher,
8082
NamedParameterJdbcOperations operations) {
8183

84+
Assert.notNull(dataAccessStrategy, "DataAccessStrategy must not be null");
85+
Assert.notNull(context, "RelationalMappingContext must not be null");
86+
Assert.notNull(converter, "RelationalConverter must not be null");
87+
Assert.notNull(dialect, "Dialect must not be null");
88+
Assert.notNull(publisher, "ApplicationEventPublisher must not be null");
89+
Assert.state(dialect instanceof JdbcDialect, "Dialect must be an instance of the JdbcDialect");
90+
91+
this.publisher = publisher;
92+
this.context = context;
93+
this.converter = converter;
94+
this.dialect = (JdbcDialect) dialect;
95+
this.accessStrategy = dataAccessStrategy;
96+
this.operations = operations;
97+
}
98+
99+
/**
100+
* Creates a new {@link JdbcRepositoryFactory} for the given {@link DataAccessStrategy},
101+
* {@link RelationalMappingContext} and {@link ApplicationEventPublisher}.
102+
*
103+
* @param dataAccessStrategy must not be {@literal null}.
104+
* @param context must not be {@literal null}.
105+
* @param converter must not be {@literal null}.
106+
* @param dialect must not be {@literal null}.
107+
* @param publisher must not be {@literal null}.
108+
* @param operations must not be {@literal null}.
109+
*/
110+
public JdbcRepositoryFactory(DataAccessStrategy dataAccessStrategy, RelationalMappingContext context,
111+
JdbcConverter converter, JdbcDialect dialect, ApplicationEventPublisher publisher,
112+
NamedParameterJdbcOperations operations) {
113+
82114
Assert.notNull(dataAccessStrategy, "DataAccessStrategy must not be null");
83115
Assert.notNull(context, "RelationalMappingContext must not be null");
84116
Assert.notNull(converter, "RelationalConverter must not be null");

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.Serializable;
1919

2020
import org.springframework.beans.factory.BeanFactory;
21+
import org.springframework.beans.factory.FactoryBean;
2122
import org.springframework.beans.factory.annotation.Autowired;
2223
import org.springframework.context.ApplicationEventPublisher;
2324
import org.springframework.context.ApplicationEventPublisherAware;
@@ -28,6 +29,7 @@
2829
import org.springframework.data.jdbc.core.convert.QueryMappingConfiguration;
2930
import org.springframework.data.jdbc.core.convert.SqlGeneratorSource;
3031
import org.springframework.data.jdbc.core.convert.SqlParametersFactory;
32+
import org.springframework.data.jdbc.core.dialect.JdbcDialect;
3133
import org.springframework.data.mapping.callback.EntityCallbacks;
3234
import org.springframework.data.relational.core.dialect.Dialect;
3335
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
@@ -38,7 +40,7 @@
3840
import org.springframework.util.Assert;
3941

4042
/**
41-
* Special adapter for Springs {@link org.springframework.beans.factory.FactoryBean} interface to allow easy setup of
43+
* Special adapter for Springs {@link FactoryBean} interface to allow easy setup of
4244
* repository factories via Spring configuration.
4345
*
4446
* @author Jens Schauder
@@ -61,7 +63,7 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
6163
private QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
6264
private NamedParameterJdbcOperations operations;
6365
private EntityCallbacks entityCallbacks;
64-
private Dialect dialect;
66+
private JdbcDialect dialect;
6567

6668
/**
6769
* Creates a new {@link JdbcRepositoryFactoryBean} for the given repository interface.
@@ -103,8 +105,20 @@ public void setMappingContext(RelationalMappingContext mappingContext) {
103105
this.mappingContext = mappingContext;
104106
}
105107

108+
/**
109+
* @deprecated please, use {@link #setDialect(JdbcDialect)} instead.
110+
*/
111+
@Deprecated(forRemoval = true, since = "4.0")
106112
public void setDialect(Dialect dialect) {
107113

114+
Assert.notNull(dialect, "Dialect must not be null");
115+
Assert.state(dialect instanceof JdbcDialect, "Dialect must be an instance of the JdbcDialect");
116+
117+
this.setDialect(((JdbcDialect) dialect));
118+
}
119+
120+
public void setDialect(JdbcDialect dialect) {
121+
108122
Assert.notNull(dialect, "Dialect must not be null");
109123

110124
this.dialect = dialect;

0 commit comments

Comments
 (0)