Skip to content

Commit 676ffc1

Browse files
committed
Polishing.
Reformat keys extraction code. See #2149 Signed-off-by: Artemiy Chereshnevvv <chereshnevvv4real@gmail.com> Polishing. Reformat 'applyScrollOrderBy'. See #2149 Signed-off-by: Artemiy Chereshnevvv <chereshnevvv4real@gmail.com>
1 parent 93f3529 commit 676ffc1

File tree

2 files changed

+26
-42
lines changed

2 files changed

+26
-42
lines changed

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

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717

1818
import static org.springframework.data.jdbc.repository.query.JdbcQueryExecution.*;
1919

20-
import java.lang.reflect.Field;
2120
import java.sql.ResultSet;
2221
import java.util.ArrayList;
23-
import java.util.Arrays;
2422
import java.util.Collection;
2523
import java.util.LinkedHashMap;
2624
import java.util.List;
@@ -29,7 +27,6 @@
2927
import java.util.function.IntFunction;
3028
import java.util.function.LongSupplier;
3129
import java.util.function.Supplier;
32-
import java.util.stream.Collectors;
3330

3431
import org.jspecify.annotations.Nullable;
3532
import org.springframework.core.convert.converter.Converter;
@@ -44,6 +41,7 @@
4441
import org.springframework.data.domain.Window;
4542
import org.springframework.data.jdbc.core.JdbcAggregateOperations;
4643
import org.springframework.data.jdbc.core.convert.JdbcConverter;
44+
import org.springframework.data.mapping.PersistentPropertyAccessor;
4745
import org.springframework.data.relational.core.conversion.RelationalConverter;
4846
import org.springframework.data.relational.core.dialect.Dialect;
4947
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
@@ -64,7 +62,6 @@
6462
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
6563
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
6664
import org.springframework.util.Assert;
67-
import org.springframework.util.ReflectionUtils;
6865

6966
/**
7067
* An {@link AbstractJdbcQuery} implementation based on a {@link PartTree}.
@@ -318,19 +315,17 @@ static class ScrollQueryExecution<T> implements JdbcQueryExecution<Window<T>> {
318315
if (orders.isEmpty())
319316
orders = sort.get().map(Sort.Order::getProperty).toList();
320317

321-
orders = orders.stream().map(it -> {
322-
RelationalPersistentProperty prop = tableEntity.getPersistentProperty(it);
323-
318+
List<RelationalPersistentProperty> properties = new ArrayList<>();
319+
for (String propertyName : orders) {
320+
RelationalPersistentProperty prop = tableEntity.getPersistentProperty(propertyName);
324321
if (prop == null)
325-
return it;
326-
327-
return prop.getName();
328-
}).toList();
322+
continue;
329323

330-
keys = extractKeys(resultList, orders);
324+
properties.add(prop);
325+
}
331326

332-
Map<String, Object> finalKeys = keys;
333-
positionFunction = (ignoredI) -> ScrollPosition.of(finalKeys, ((KeysetScrollPosition) position).getDirection());
327+
final Map<String, Object> resultKeys = extractKeys(resultList, properties);
328+
positionFunction = (ignoredI) -> ScrollPosition.of(resultKeys, ((KeysetScrollPosition) position).getDirection());
334329
}
335330

336331
if (positionFunction == null)
@@ -347,26 +342,26 @@ else if (limit.isLimited())
347342
return Window.from(resultList, positionFunction, hasNext);
348343
}
349344

350-
private Map<String, Object> extractKeys(List<T> resultList, List<String> orders) {
345+
private Map<String, Object> extractKeys(List<T> resultList, List<RelationalPersistentProperty> properties) {
351346
if (resultList.isEmpty())
352347
return Map.of();
353348

349+
Map<String, Object> result = new LinkedHashMap<>();
350+
354351
T last = resultList.get(resultList.size() - 1);
352+
PersistentPropertyAccessor<T> accessor = tableEntity.getPropertyAccessor(last);
355353

356-
Field[] fields = last.getClass().getDeclaredFields();
354+
for (RelationalPersistentProperty property : properties) {
355+
String propertyName = property.getName();
356+
Object propertyValue = accessor.getProperty(property);
357357

358-
// noinspection DataFlowIssue
359-
return Arrays.stream(fields).filter(it -> {
360-
String name = it.getName();
358+
if (propertyValue == null)
359+
continue;
361360

362-
RelationalPersistentProperty prop = tableEntity.getPersistentProperty(name);
363-
if (prop != null)
364-
name = prop.getName();
361+
result.put(propertyName, propertyValue);
362+
}
365363

366-
String finalName = name;
367-
return orders.stream().anyMatch(order -> order.equalsIgnoreCase(finalName));
368-
}).peek(ReflectionUtils::makeAccessible).collect(Collectors.toMap(Field::getName,
369-
it -> ReflectionUtils.getField(it, last), (e1, e2) -> e1, LinkedHashMap::new));
364+
return result;
370365
}
371366
}
372367

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,30 +237,19 @@ Sort applyScrollOrderBy(Sort sort, @Nullable ScrollPosition scrollPosition) {
237237
if (!(scrollPosition instanceof KeysetScrollPosition) || scrollPosition.isInitial())
238238
return sort;
239239

240-
Set<String> orders = sort.get().map(Sort.Order::getProperty).map(it -> {
241-
RelationalPersistentProperty prop = entity.getPersistentProperty(it);
242-
if (prop == null)
243-
return it;
244-
245-
return prop.getName();
246-
}).collect(Collectors.toSet());
240+
Set<String> sortedProperties = sort.get().map(Sort.Order::getProperty).collect(Collectors.toSet());
247241

248242
Set<String> keys = ((KeysetScrollPosition) scrollPosition).getKeys().keySet();
249243

250-
Set<String> notSorted = keys.stream().map(it -> {
251-
RelationalPersistentProperty prop = entity.getPersistentProperty(it);
252-
if (prop == null)
253-
return it;
254-
255-
return prop.getName();
256-
}).filter(it -> orders.stream().noneMatch(order -> order.equalsIgnoreCase(it))).collect(Collectors.toSet());
244+
Set<String> notSortedProperties
245+
= keys.stream().filter(it -> !sortedProperties.contains(it)).collect(Collectors.toSet());
257246

258-
if (notSorted.isEmpty())
247+
if (notSortedProperties.isEmpty())
259248
return sort;
260249

261250
Sort.Direction defaultSort = sort.get().map(Sort.Order::getDirection).findAny().orElse(Sort.DEFAULT_DIRECTION);
262251

263-
return sort.and(Sort.by(defaultSort, notSorted.toArray(new String[0])));
252+
return sort.and(Sort.by(defaultSort, notSortedProperties.toArray(new String[0])));
264253
}
265254

266255
Criteria applyScrollCriteria(@Nullable ScrollPosition position, Sort sort) {

0 commit comments

Comments
 (0)