Skip to content

Commit 3f56f75

Browse files
committed
Allowed for extension of JdbcRepositoryFactory and Query resolution api
Signed-off-by: mipo256 <[email protected]>
1 parent 8032c0f commit 3f56f75

File tree

6 files changed

+122
-78
lines changed

6 files changed

+122
-78
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2020-2025 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.data.jdbc.repository.query;
18+
19+
import java.sql.SQLType;
20+
21+
import org.springframework.core.MethodParameter;
22+
import org.springframework.data.jdbc.core.convert.JdbcColumnTypes;
23+
import org.springframework.data.jdbc.support.JdbcUtil;
24+
import org.springframework.data.relational.repository.query.RelationalParameters;
25+
import org.springframework.data.repository.query.Parameter;
26+
import org.springframework.data.util.Lazy;
27+
import org.springframework.data.util.TypeInformation;
28+
29+
/**
30+
* Custom {@link Parameter} implementation.
31+
*
32+
* @author Mark Paluch
33+
* @author Chirag Tailor
34+
*/
35+
public class JdbcParameter extends RelationalParameters.RelationalParameter {
36+
37+
private final SQLType sqlType;
38+
private final Lazy<SQLType> actualSqlType;
39+
40+
/**
41+
* Creates a new {@link RelationalParameters.RelationalParameter}.
42+
*
43+
* @param parameter must not be {@literal null}.
44+
*/
45+
public JdbcParameter(MethodParameter parameter, TypeInformation<?> domainType) {
46+
super(parameter, domainType);
47+
48+
TypeInformation<?> typeInformation = getTypeInformation();
49+
50+
sqlType = JdbcUtil.targetSqlTypeFor(JdbcColumnTypes.INSTANCE.resolvePrimitiveType(typeInformation.getType()));
51+
52+
actualSqlType = Lazy.of(() -> JdbcUtil.targetSqlTypeFor(
53+
JdbcColumnTypes.INSTANCE.resolvePrimitiveType(typeInformation.getActualType().getType())));
54+
}
55+
56+
public JdbcParameter(MethodParameter parameter, TypeInformation<?> domainType, SQLType sqlType,
57+
Lazy<SQLType> actualSqlType) {
58+
super(parameter, domainType);
59+
60+
this.sqlType = sqlType;
61+
this.actualSqlType = actualSqlType;
62+
}
63+
64+
public SQLType getSqlType() {
65+
return sqlType;
66+
}
67+
68+
public SQLType getActualSqlType() {
69+
return actualSqlType.get();
70+
}
71+
}

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

+14-43
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,19 @@
1515
*/
1616
package org.springframework.data.jdbc.repository.query;
1717

18-
import java.sql.SQLType;
1918
import java.util.List;
19+
import java.util.function.Function;
2020

2121
import org.springframework.core.MethodParameter;
22-
import org.springframework.data.jdbc.core.convert.JdbcColumnTypes;
23-
import org.springframework.data.jdbc.support.JdbcUtil;
2422
import org.springframework.data.relational.repository.query.RelationalParameters;
25-
import org.springframework.data.repository.query.Parameter;
23+
import org.springframework.data.relational.repository.query.RelationalParameters.RelationalParameter;
2624
import org.springframework.data.repository.query.ParametersSource;
27-
import org.springframework.data.util.Lazy;
28-
import org.springframework.data.util.TypeInformation;
2925

3026
/**
3127
* Custom extension of {@link RelationalParameters}.
3228
*
3329
* @author Mark Paluch
30+
* @author Mikhail Polivakha
3431
* @since 3.2.6
3532
*/
3633
public class JdbcParameters extends RelationalParameters {
@@ -41,8 +38,17 @@ public class JdbcParameters extends RelationalParameters {
4138
* @param parametersSource must not be {@literal null}.
4239
*/
4340
public JdbcParameters(ParametersSource parametersSource) {
44-
super(parametersSource,
45-
methodParameter -> new JdbcParameter(methodParameter, parametersSource.getDomainTypeInformation()));
41+
this(parametersSource, methodParameter -> new JdbcParameter(methodParameter, parametersSource.getDomainTypeInformation()));
42+
}
43+
44+
/**
45+
* Creates a new {@link JdbcParameters} instance from the given {@link ParametersSource} and function
46+
* of turning the {@link MethodParameter} into {@link RelationalParameter}.
47+
*
48+
* @param parametersSource must not be {@literal null}.
49+
*/
50+
public JdbcParameters(ParametersSource parametersSource, Function<MethodParameter, RelationalParameter> parameterFactory) {
51+
super(parametersSource, parameterFactory);
4652
}
4753

4854
@SuppressWarnings({ "rawtypes", "unchecked" })
@@ -61,39 +67,4 @@ protected JdbcParameters createFrom(List<RelationalParameter> parameters) {
6167
return new JdbcParameters((List) parameters);
6268
}
6369

64-
/**
65-
* Custom {@link Parameter} implementation.
66-
*
67-
* @author Mark Paluch
68-
* @author Chirag Tailor
69-
*/
70-
public static class JdbcParameter extends RelationalParameter {
71-
72-
private final SQLType sqlType;
73-
private final Lazy<SQLType> actualSqlType;
74-
75-
/**
76-
* Creates a new {@link RelationalParameter}.
77-
*
78-
* @param parameter must not be {@literal null}.
79-
*/
80-
JdbcParameter(MethodParameter parameter, TypeInformation<?> domainType) {
81-
super(parameter, domainType);
82-
83-
TypeInformation<?> typeInformation = getTypeInformation();
84-
85-
sqlType = JdbcUtil.targetSqlTypeFor(JdbcColumnTypes.INSTANCE.resolvePrimitiveType(typeInformation.getType()));
86-
87-
actualSqlType = Lazy.of(() -> JdbcUtil
88-
.targetSqlTypeFor(JdbcColumnTypes.INSTANCE.resolvePrimitiveType(typeInformation.getActualType().getType())));
89-
}
90-
91-
public SQLType getSqlType() {
92-
return sqlType;
93-
}
94-
95-
public SQLType getActualSqlType() {
96-
return actualSqlType.get();
97-
}
98-
}
9970
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ private MapSqlParameterSource bindParameters(RelationalParameterAccessor accesso
275275
Object value = accessor.getBindableValue(bindableParameter.getIndex());
276276
String parameterName = bindableParameter.getName()
277277
.orElseThrow(() -> new IllegalStateException(PARAMETER_NEEDS_TO_BE_NAMED));
278-
JdbcParameters.JdbcParameter parameter = getQueryMethod().getParameters()
278+
JdbcParameter parameter = getQueryMethod().getParameters()
279279
.getParameter(bindableParameter.getIndex());
280280

281281
JdbcValue jdbcValue = writeValue(value, parameter.getTypeInformation(), parameter);
@@ -292,7 +292,7 @@ private MapSqlParameterSource bindParameters(RelationalParameterAccessor accesso
292292
}
293293

294294
private JdbcValue writeValue(@Nullable Object value, TypeInformation<?> typeInformation,
295-
JdbcParameters.JdbcParameter parameter) {
295+
JdbcParameter parameter) {
296296

297297
if (value == null) {
298298
return JdbcValue.of(value, parameter.getSqlType());
@@ -349,7 +349,7 @@ private JdbcValue writeCollection(Collection<?> value, SQLType defaultType, Func
349349
return jdbcValue;
350350
}
351351

352-
private JdbcValue writeArrayValue(JdbcParameters.JdbcParameter parameter, Object array,
352+
private JdbcValue writeArrayValue(JdbcParameter parameter, Object array,
353353
TypeInformation<?> nestedElementType) {
354354

355355
int length = Array.getLength(array);

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

+16-16
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
* @author Christopher Klein
5757
* @author Mikhail Polivakha
5858
*/
59-
abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
59+
public abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
6060

6161
private static final Log LOG = LogFactory.getLog(JdbcQueryLookupStrategy.class);
6262

@@ -68,7 +68,7 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
6868
private final NamedParameterJdbcOperations operations;
6969
protected final ValueExpressionDelegate delegate;
7070

71-
JdbcQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
71+
protected JdbcQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
7272
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
7373
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
7474
ValueExpressionDelegate delegate) {
@@ -109,10 +109,10 @@ static class CreateQueryLookupStrategy extends JdbcQueryLookupStrategy {
109109
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
110110
ValueExpressionDelegate delegate) {
111111

112-
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations,
113-
delegate);
112+
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations, delegate);
114113

115-
this.rowMapperFactory = new DefaultRowMapperFactory(getMappingContext(), getConverter(), getQueryMappingConfiguration(), getCallbacks(), getPublisher());
114+
this.rowMapperFactory = new DefaultRowMapperFactory(getMappingContext(), getConverter(),
115+
getQueryMappingConfiguration(), getCallbacks(), getPublisher());
116116
}
117117

118118
@Override
@@ -270,23 +270,23 @@ public static QueryLookupStrategy create(@Nullable Key key, ApplicationEventPubl
270270
};
271271
}
272272

273-
JdbcConverter getConverter() {
273+
protected JdbcConverter getConverter() {
274274
return converter;
275275
}
276276

277-
NamedParameterJdbcOperations getOperations() {
277+
protected NamedParameterJdbcOperations getOperations() {
278278
return operations;
279279
}
280280

281-
QueryMappingConfiguration getQueryMappingConfiguration() {
282-
return queryMappingConfiguration;
283-
}
281+
QueryMappingConfiguration getQueryMappingConfiguration() {
282+
return queryMappingConfiguration;
283+
}
284284

285-
EntityCallbacks getCallbacks() {
286-
return callbacks;
287-
}
285+
EntityCallbacks getCallbacks() {
286+
return callbacks;
287+
}
288288

289-
ApplicationEventPublisher getPublisher() {
290-
return publisher;
291-
}
289+
ApplicationEventPublisher getPublisher() {
290+
return publisher;
291+
}
292292
}

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,17 @@
5050
* @author Diego Krupitza
5151
* @author Christopher Klein
5252
* @author Marcin Grzejszczak
53+
* @author Mikhail Polivakha
5354
*/
5455
public class JdbcRepositoryFactory extends RepositoryFactorySupport {
5556

56-
private final RelationalMappingContext context;
57-
private final JdbcConverter converter;
58-
private final ApplicationEventPublisher publisher;
59-
private final DataAccessStrategy accessStrategy;
60-
private final NamedParameterJdbcOperations operations;
61-
private final Dialect dialect;
62-
private @Nullable BeanFactory beanFactory;
57+
protected final RelationalMappingContext context;
58+
protected final JdbcConverter converter;
59+
protected final ApplicationEventPublisher publisher;
60+
protected final DataAccessStrategy accessStrategy;
61+
protected final NamedParameterJdbcOperations operations;
62+
protected final Dialect dialect;
63+
protected @Nullable BeanFactory beanFactory;
6364

6465
private QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
6566
private EntityCallbacks entityCallbacks;

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,20 @@
4848
* @author Mark Paluch
4949
* @author Hebert Coelho
5050
* @author Chirag Tailor
51+
* @author Mikhail Polivakha
5152
*/
5253
public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
5354
extends TransactionalRepositoryFactoryBeanSupport<T, S, ID> implements ApplicationEventPublisherAware {
5455

55-
private ApplicationEventPublisher publisher;
56-
private BeanFactory beanFactory;
57-
private RelationalMappingContext mappingContext;
58-
private JdbcConverter converter;
59-
private DataAccessStrategy dataAccessStrategy;
60-
private QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
61-
private NamedParameterJdbcOperations operations;
62-
private EntityCallbacks entityCallbacks;
63-
private Dialect dialect;
56+
protected ApplicationEventPublisher publisher;
57+
protected BeanFactory beanFactory;
58+
protected RelationalMappingContext mappingContext;
59+
protected JdbcConverter converter;
60+
protected DataAccessStrategy dataAccessStrategy;
61+
protected QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
62+
protected NamedParameterJdbcOperations operations;
63+
protected EntityCallbacks entityCallbacks;
64+
protected Dialect dialect;
6465

6566
/**
6667
* Creates a new {@link JdbcRepositoryFactoryBean} for the given repository interface.

0 commit comments

Comments
 (0)