Skip to content

Commit c90d2b4

Browse files
committed
Polishing.
Reformat keys extraction code. See spring-projects#2149 Signed-off-by: Artemiy Chereshnevvv <[email protected]>
1 parent 93f3529 commit c90d2b4

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.data.domain.Window;
4545
import org.springframework.data.jdbc.core.JdbcAggregateOperations;
4646
import org.springframework.data.jdbc.core.convert.JdbcConverter;
47+
import org.springframework.data.mapping.PersistentPropertyAccessor;
4748
import org.springframework.data.relational.core.conversion.RelationalConverter;
4849
import org.springframework.data.relational.core.dialect.Dialect;
4950
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
@@ -318,19 +319,17 @@ static class ScrollQueryExecution<T> implements JdbcQueryExecution<Window<T>> {
318319
if (orders.isEmpty())
319320
orders = sort.get().map(Sort.Order::getProperty).toList();
320321

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

330-
keys = extractKeys(resultList, orders);
328+
properties.add(prop);
329+
}
331330

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

336335
if (positionFunction == null)
@@ -347,26 +346,26 @@ else if (limit.isLimited())
347346
return Window.from(resultList, positionFunction, hasNext);
348347
}
349348

350-
private Map<String, Object> extractKeys(List<T> resultList, List<String> orders) {
349+
private Map<String, Object> extractKeys(List<T> resultList, List<RelationalPersistentProperty> properties) {
351350
if (resultList.isEmpty())
352351
return Map.of();
353352

353+
Map<String, Object> result = new LinkedHashMap<>();
354+
354355
T last = resultList.get(resultList.size() - 1);
356+
PersistentPropertyAccessor<T> accessor = tableEntity.getPropertyAccessor(last);
355357

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

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

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

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));
368+
return result;
370369
}
371370
}
372371

0 commit comments

Comments
 (0)