Skip to content

Commit 0b85b6f

Browse files
committed
Polishing.
Simplified use of queryMethod in the different JdbcQuery classes. Improved method names. Added author tags. Code formatting. Corrected HTML in JavaDoc. Originial pull request #1423
1 parent 468f94b commit 0b85b6f

File tree

4 files changed

+66
-48
lines changed

4 files changed

+66
-48
lines changed

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

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @author Maciej Walkowiak
4343
* @author Mark Paluch
4444
* @author Dennis Effing
45+
* @author Mikhail Polivakha
4546
* @since 2.0
4647
*/
4748
public abstract class AbstractJdbcQuery implements RepositoryQuery {
@@ -71,26 +72,42 @@ public JdbcQueryMethod getQueryMethod() {
7172
}
7273

7374
/**
74-
* Creates a {@link JdbcQueryExecution} given {@link JdbcQueryMethod}, {@link ResultSetExtractor} an
75+
* Creates a {@link JdbcQueryExecution} given a {@link JdbcQueryMethod}, and ac{@link ResultSetExtractor} or a
7576
* {@link RowMapper}. Prefers the given {@link ResultSetExtractor} over {@link RowMapper}.
7677
*
7778
* @param queryMethod must not be {@literal null}.
7879
* @param extractor must not be {@literal null}.
7980
* @param rowMapper must not be {@literal null}.
8081
* @return a JdbcQueryExecution appropriate for {@literal queryMethod}. Guaranteed to be not {@literal null}.
82+
* @deprecated use {@link #createReadingQueryExecution(ResultSetExtractor, RowMapper)} instead.
8183
*/
84+
@Deprecated(since = "3.1", forRemoval = true)
85+
// a better name would be createReadingQueryExecution
8286
protected JdbcQueryExecution<?> getQueryExecution(JdbcQueryMethod queryMethod,
8387
@Nullable ResultSetExtractor<?> extractor, RowMapper<?> rowMapper) {
88+
return createReadingQueryExecution(extractor, rowMapper);
89+
}
90+
91+
/**
92+
* Creates a {@link JdbcQueryExecution} given a {@link ResultSetExtractor} or a {@link RowMapper}. Prefers the given
93+
* {@link ResultSetExtractor} over {@link RowMapper}.
94+
*
95+
* @param extractor must not be {@literal null}.
96+
* @param rowMapper must not be {@literal null}.
97+
* @return a JdbcQueryExecution appropriate for {@literal queryMethod}. Guaranteed to be not {@literal null}.
98+
*/
99+
protected JdbcQueryExecution<?> createReadingQueryExecution(@Nullable ResultSetExtractor<?> extractor,
100+
RowMapper<?> rowMapper) {
84101

85-
if (queryMethod.isCollectionQuery()) {
86-
return extractor != null ? getQueryExecution(extractor) : collectionQuery(rowMapper);
102+
if (getQueryMethod().isCollectionQuery()) {
103+
return extractor != null ? createSingleReadingQueryExecution(extractor) : collectionQuery(rowMapper);
87104
}
88105

89-
if (queryMethod.isStreamQuery()) {
90-
return extractor != null ? getQueryExecution(extractor) : streamQuery(rowMapper);
106+
if (getQueryMethod().isStreamQuery()) {
107+
return extractor != null ? createSingleReadingQueryExecution(extractor) : streamQuery(rowMapper);
91108
}
92109

93-
return extractor != null ? getQueryExecution(extractor) : singleObjectQuery(rowMapper);
110+
return extractor != null ? createSingleReadingQueryExecution(extractor) : singleObjectQuery(rowMapper);
94111
}
95112

96113
protected JdbcQueryExecution<Object> createModifyingQueryExecutor() {
@@ -100,7 +117,8 @@ protected JdbcQueryExecution<Object> createModifyingQueryExecutor() {
100117
int updatedCount = operations.update(query, parameters);
101118
Class<?> returnedObjectType = queryMethod.getReturnedObjectType();
102119

103-
return (returnedObjectType == boolean.class || returnedObjectType == Boolean.class) ? updatedCount != 0
120+
return (returnedObjectType == boolean.class || returnedObjectType == Boolean.class) //
121+
? updatedCount != 0 //
104122
: updatedCount;
105123
};
106124
}
@@ -117,14 +135,15 @@ JdbcQueryExecution<Object> singleObjectQuery(RowMapper<?> rowMapper) {
117135
}
118136

119137
<T> JdbcQueryExecution<List<T>> collectionQuery(RowMapper<T> rowMapper) {
120-
return getQueryExecution(new RowMapperResultSetExtractor<>(rowMapper));
138+
return createSingleReadingQueryExecution(new RowMapperResultSetExtractor<>(rowMapper));
121139
}
122140

123141
/**
124142
* Obtain the result type to read from {@link ResultProcessor}.
125143
*
126-
* @param resultProcessor the {@link ResultProcessor} used to determine the result type. Must not be {@literal null}.
127-
* @return the type that should get loaded from the database before it gets converted into the actual return type of a method. Guaranteed to be not {@literal null}.
144+
* @param resultProcessor the {@link ResultProcessor} used to determine the result type. Must not be {@literal null}.
145+
* @return the type that should get loaded from the database before it gets converted into the actual return type of a
146+
* method. Guaranteed to be not {@literal null}.
128147
*/
129148
protected Class<?> resolveTypeToRead(ResultProcessor resultProcessor) {
130149

@@ -142,7 +161,7 @@ private <T> JdbcQueryExecution<Stream<T>> streamQuery(RowMapper<T> rowMapper) {
142161
return (query, parameters) -> operations.queryForStream(query, parameters, rowMapper);
143162
}
144163

145-
private <T> JdbcQueryExecution<T> getQueryExecution(ResultSetExtractor<T> resultSetExtractor) {
164+
private <T> JdbcQueryExecution<T> createSingleReadingQueryExecution(ResultSetExtractor<T> resultSetExtractor) {
146165
return (query, parameters) -> operations.query(query, parameters, resultSetExtractor);
147166
}
148167

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* @author Mark Paluch
5353
* @author Jens Schauder
5454
* @author Diego Krupitza
55+
* @author Mikhail Polivakha
5556
* @since 2.0
5657
*/
5758
public class PartTreeJdbcQuery extends AbstractJdbcQuery {
@@ -172,27 +173,29 @@ private JdbcQueryExecution<?> getQueryExecution(ResultProcessor processor,
172173
return queryExecution;
173174
}
174175

176+
protected ParametrizedQuery createQuery(RelationalParametersParameterAccessor accessor, ReturnedType returnedType) {
177+
178+
RelationalEntityMetadata<?> entityMetadata = getQueryMethod().getEntityInformation();
179+
180+
JdbcQueryCreator queryCreator = new JdbcQueryCreator(context, tree, converter, dialect, entityMetadata, accessor,
181+
getQueryMethod().isSliceQuery(), returnedType, this.getQueryMethod().lookupLockAnnotation());
182+
return queryCreator.createQuery(getDynamicSort(accessor));
183+
}
184+
175185
private JdbcQueryExecution<?> getJdbcQueryExecution(@Nullable ResultSetExtractor<Boolean> extractor, RowMapper<Object> rowMapper) {
186+
176187
if (getQueryMethod().isPageQuery() || getQueryMethod().isSliceQuery()) {
177188
return collectionQuery(rowMapper);
178189
} else {
190+
179191
if (getQueryMethod().isModifyingQuery()) {
180192
return createModifyingQueryExecutor();
181193
} else {
182-
return getQueryExecution(getQueryMethod(), extractor, rowMapper);
194+
return createReadingQueryExecution(extractor, rowMapper);
183195
}
184196
}
185197
}
186198

187-
protected ParametrizedQuery createQuery(RelationalParametersParameterAccessor accessor, ReturnedType returnedType) {
188-
189-
RelationalEntityMetadata<?> entityMetadata = getQueryMethod().getEntityInformation();
190-
191-
JdbcQueryCreator queryCreator = new JdbcQueryCreator(context, tree, converter, dialect, entityMetadata, accessor,
192-
getQueryMethod().isSliceQuery(), returnedType, this.getQueryMethod().lookupLockAnnotation());
193-
return queryCreator.createQuery(getDynamicSort(accessor));
194-
}
195-
196199
/**
197200
* {@link JdbcQueryExecution} returning a {@link org.springframework.data.domain.Slice}.
198201
*

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@
3333
* You can also specify the way to extract data from {@link java.sql.ResultSet}. There are 4 attribute of this
3434
* annotation you can set to do that:
3535
* <p>
36-
* 1. {@link #resultSetExtractorRef()}
37-
* 2. {@link #resultSetExtractorClass()}
38-
* 3. {@link #rowMapperRef()}
39-
* 4. {@link #rowMapperClass()}
36+
* <ol>
37+
* <li> {@link #resultSetExtractorRef()}
38+
* </li><li> {@link #resultSetExtractorClass()}
39+
* </li><li> {@link #rowMapperRef()}
40+
* </li><li> {@link #rowMapperClass()}
41+
* </li>
42+
*</ol>
4043
*
4144
* The annotation attributes above are listed in their preference order, that is - the {@link #resultSetExtractorRef()},
4245
* has the highest privilege and, will suppress any other 3 attribute from above, and consequently {@link #rowMapperClass()}
@@ -45,6 +48,7 @@
4548
* @author Jens Schauder
4649
* @author Moises Cisneros
4750
* @author Hebert Coelho
51+
* @author Mikhail Polivakha
4852
*/
4953
@Retention(RetentionPolicy.RUNTIME)
5054
@Target(ElementType.METHOD)

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

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,12 @@
6161
* @author Hebert Coelho
6262
* @author Chirag Tailor
6363
* @author Christopher Klein
64+
* @author Mikhail Polivakha
6465
* @since 2.0
6566
*/
6667
public class StringBasedJdbcQuery extends AbstractJdbcQuery {
6768

6869
private static final String PARAMETER_NEEDS_TO_BE_NAMED = "For queries with named parameters you need to provide names for method parameters; Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters";
69-
70-
private final JdbcQueryMethod queryMethod;
7170
private final JdbcConverter converter;
7271
private final RowMapperFactory rowMapperFactory;
7372
private BeanFactory beanFactory;
@@ -104,7 +103,6 @@ public StringBasedJdbcQuery(JdbcQueryMethod queryMethod, NamedParameterJdbcOpera
104103

105104
Assert.notNull(rowMapperFactory, "RowMapperFactory must not be null");
106105

107-
this.queryMethod = queryMethod;
108106
this.converter = converter;
109107
this.rowMapperFactory = rowMapperFactory;
110108
this.evaluationContextProvider = evaluationContextProvider;
@@ -135,25 +133,24 @@ public Object execute(Object[] objects) {
135133
String query = determineQuery();
136134

137135
if (ObjectUtils.isEmpty(query)) {
138-
throw new IllegalStateException(String.format("No query specified on %s", queryMethod.getName()));
136+
throw new IllegalStateException(String.format("No query specified on %s", getQueryMethod().getName()));
139137
}
140138

141139
return queryExecution.execute(processSpelExpressions(objects, parameterMap, query), parameterMap);
142140
}
143141

144-
private JdbcQueryExecution<?> createJdbcQueryExecution(RelationalParameterAccessor accessor, ResultProcessor processor, ResultProcessingConverter converter) {
145-
JdbcQueryExecution<?> queryExecution;
142+
private JdbcQueryExecution<?> createJdbcQueryExecution(RelationalParameterAccessor accessor,
143+
ResultProcessor processor, ResultProcessingConverter converter) {
146144

147-
if (queryMethod.isModifyingQuery()) {
148-
queryExecution = createModifyingQueryExecutor();
145+
if (getQueryMethod().isModifyingQuery()) {
146+
return createModifyingQueryExecutor();
149147
} else {
150148

151149
RowMapper<Object> rowMapper = determineRowMapper(rowMapperFactory.create(resolveTypeToRead(processor)), converter,
152150
accessor.findDynamicProjection() != null);
153151

154-
queryExecution = getQueryExecution(queryMethod, determineResultSetExtractor(rowMapper), rowMapper);
152+
return createReadingQueryExecution(determineResultSetExtractor(rowMapper), rowMapper);
155153
}
156-
return queryExecution;
157154
}
158155

159156
private String processSpelExpressions(Object[] objects, MapSqlParameterSource parameterMap, String query) {
@@ -162,18 +159,13 @@ private String processSpelExpressions(Object[] objects, MapSqlParameterSource pa
162159
.of((counter, expression) -> String.format("__$synthetic$__%d", counter + 1), String::concat)
163160
.withEvaluationContextProvider(evaluationContextProvider);
164161

165-
SpelEvaluator spelEvaluator = queryContext.parse(query, queryMethod.getParameters());
162+
SpelEvaluator spelEvaluator = queryContext.parse(query, getQueryMethod().getParameters());
166163

167164
spelEvaluator.evaluate(objects).forEach(parameterMap::addValue);
168165

169166
return spelEvaluator.getQueryString();
170167
}
171168

172-
@Override
173-
public JdbcQueryMethod getQueryMethod() {
174-
return queryMethod;
175-
}
176-
177169
private MapSqlParameterSource bindParameters(RelationalParameterAccessor accessor) {
178170

179171
MapSqlParameterSource parameters = new MapSqlParameterSource();
@@ -191,7 +183,7 @@ private void convertAndAddParameter(MapSqlParameterSource parameters, Parameter
191183

192184
String parameterName = p.getName().orElseThrow(() -> new IllegalStateException(PARAMETER_NEEDS_TO_BE_NAMED));
193185

194-
RelationalParameters.RelationalParameter parameter = queryMethod.getParameters().getParameter(p.getIndex());
186+
RelationalParameters.RelationalParameter parameter = getQueryMethod().getParameters().getParameter(p.getIndex());
195187
ResolvableType resolvableType = parameter.getResolvableType();
196188
Class<?> type = resolvableType.resolve();
197189
Assert.notNull(type, "@Query parameter type could not be resolved");
@@ -233,10 +225,10 @@ private void convertAndAddParameter(MapSqlParameterSource parameters, Parameter
233225

234226
private String determineQuery() {
235227

236-
String query = queryMethod.getDeclaredQuery();
228+
String query = getQueryMethod().getDeclaredQuery();
237229

238230
if (ObjectUtils.isEmpty(query)) {
239-
throw new IllegalStateException(String.format("No query specified on %s", queryMethod.getName()));
231+
throw new IllegalStateException(String.format("No query specified on %s", getQueryMethod().getName()));
240232
}
241233

242234
return query;
@@ -246,7 +238,7 @@ private String determineQuery() {
246238
@SuppressWarnings({ "rawtypes", "unchecked" })
247239
ResultSetExtractor<Object> determineResultSetExtractor(@Nullable RowMapper<Object> rowMapper) {
248240

249-
String resultSetExtractorRef = queryMethod.getResultSetExtractorRef();
241+
String resultSetExtractorRef = getQueryMethod().getResultSetExtractorRef();
250242

251243
if (!ObjectUtils.isEmpty(resultSetExtractorRef)) {
252244

@@ -255,7 +247,7 @@ ResultSetExtractor<Object> determineResultSetExtractor(@Nullable RowMapper<Objec
255247
return (ResultSetExtractor<Object>) beanFactory.getBean(resultSetExtractorRef);
256248
}
257249

258-
Class<? extends ResultSetExtractor> resultSetExtractorClass = queryMethod.getResultSetExtractorClass();
250+
Class<? extends ResultSetExtractor> resultSetExtractorClass = getQueryMethod().getResultSetExtractorClass();
259251

260252
if (isUnconfigured(resultSetExtractorClass, ResultSetExtractor.class)) {
261253
return null;
@@ -288,7 +280,7 @@ RowMapper<Object> determineRowMapper(@Nullable RowMapper<?> defaultMapper,
288280
@Nullable
289281
RowMapper<Object> determineRowMapper(@Nullable RowMapper<?> defaultMapper) {
290282

291-
String rowMapperRef = queryMethod.getRowMapperRef();
283+
String rowMapperRef = getQueryMethod().getRowMapperRef();
292284

293285
if (!ObjectUtils.isEmpty(rowMapperRef)) {
294286

@@ -297,7 +289,7 @@ RowMapper<Object> determineRowMapper(@Nullable RowMapper<?> defaultMapper) {
297289
return (RowMapper<Object>) beanFactory.getBean(rowMapperRef);
298290
}
299291

300-
Class<?> rowMapperClass = queryMethod.getRowMapperClass();
292+
Class<?> rowMapperClass = getQueryMethod().getRowMapperClass();
301293

302294
if (isUnconfigured(rowMapperClass, RowMapper.class)) {
303295
return (RowMapper<Object>) defaultMapper;

0 commit comments

Comments
 (0)