Skip to content

Split Single Query Conversion into two-step process #1604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-GH-1586-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-GH-1586-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-GH-1586-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-GH-1586-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.jdbc.core.convert;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand All @@ -27,73 +29,87 @@
import org.springframework.data.relational.core.sqlgeneration.AliasFactory;
import org.springframework.data.relational.core.sqlgeneration.SingleQuerySqlGenerator;
import org.springframework.data.relational.core.sqlgeneration.SqlGenerator;
import org.springframework.data.relational.domain.RowDocument;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
* Reads complete Aggregates from the database, by generating appropriate SQL using a {@link SingleQuerySqlGenerator}
* and a matching {@link AggregateResultSetExtractor} and invoking a
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}
* through {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}. Results are converterd into an
* intermediate {@link ResultSetRowDocumentExtractor RowDocument} and mapped via
* {@link org.springframework.data.relational.core.conversion.RelationalConverter#read(Class, RowDocument)}.
*
* @param <T> the type of aggregate produced by this reader.
* @author Jens Schauder
* @author Mark Paluch
* @since 3.2
*/
class AggregateReader<T> {

private final RelationalPersistentEntity<T> aggregate;
private final RelationalPersistentEntity<T> entity;
private final org.springframework.data.relational.core.sqlgeneration.SqlGenerator sqlGenerator;
private final JdbcConverter converter;
private final NamedParameterJdbcOperations jdbcTemplate;
private final AggregateResultSetExtractor<T> extractor;
private final ResultSetRowDocumentExtractor extractor;

AggregateReader(Dialect dialect, JdbcConverter converter, AliasFactory aliasFactory,
NamedParameterJdbcOperations jdbcTemplate, RelationalPersistentEntity<T> aggregate) {
NamedParameterJdbcOperations jdbcTemplate, RelationalPersistentEntity<T> entity) {

this.converter = converter;
this.aggregate = aggregate;
this.entity = entity;
this.jdbcTemplate = jdbcTemplate;

this.sqlGenerator = new CachingSqlGenerator(
new SingleQuerySqlGenerator(converter.getMappingContext(), aliasFactory, dialect, aggregate));
new SingleQuerySqlGenerator(converter.getMappingContext(), aliasFactory, dialect, entity));

this.extractor = new AggregateResultSetExtractor<>(aggregate, converter, createPathToColumnMapping(aliasFactory));
this.extractor = new ResultSetRowDocumentExtractor(converter.getMappingContext(),
createPathToColumnMapping(aliasFactory));
}

public List<T> findAll() {

Iterable<T> result = jdbcTemplate.query(sqlGenerator.findAll(), extractor);

Assert.state(result != null, "result is null");

return (List<T>) result;
return jdbcTemplate.query(sqlGenerator.findAll(), this::extractAll);
}

@Nullable
public T findById(Object id) {

id = converter.writeValue(id, aggregate.getRequiredIdProperty().getTypeInformation());
id = converter.writeValue(id, entity.getRequiredIdProperty().getTypeInformation());

Iterator<T> result = jdbcTemplate.query(sqlGenerator.findById(), Map.of("id", id), extractor).iterator();
return jdbcTemplate.query(sqlGenerator.findById(), Map.of("id", id), rs -> {

T returnValue = result.hasNext() ? result.next() : null;
Iterator<RowDocument> iterate = extractor.iterate(entity, rs);
if (iterate.hasNext()) {

if (result.hasNext()) {
throw new IncorrectResultSizeDataAccessException(1);
}

return returnValue;
RowDocument object = iterate.next();
if (iterate.hasNext()) {
throw new IncorrectResultSizeDataAccessException(1);
}
return converter.read(entity.getType(), object);
}
return null;
});
}

public Iterable<T> findAllById(Iterable<?> ids) {

List<Object> convertedIds = new ArrayList<>();
for (Object id : ids) {
convertedIds.add(converter.writeValue(id, aggregate.getRequiredIdProperty().getTypeInformation()));
convertedIds.add(converter.writeValue(id, entity.getRequiredIdProperty().getTypeInformation()));
}

return jdbcTemplate.query(sqlGenerator.findAllById(), Map.of("ids", convertedIds), extractor);
return jdbcTemplate.query(sqlGenerator.findAllById(), Map.of("ids", convertedIds), this::extractAll);
}

private List<T> extractAll(ResultSet rs) throws SQLException {

Iterator<RowDocument> iterate = extractor.iterate(entity, rs);
List<T> resultList = new ArrayList<>();
while (iterate.hasNext()) {
resultList.add(converter.read(entity.getType(), iterate.next()));
}

return resultList;
}

private PathToColumnMapping createPathToColumnMapping(AliasFactory aliasFactory) {
Expand All @@ -117,8 +133,8 @@ public String keyColumn(AggregatePath path) {
* A wrapper for the {@link org.springframework.data.relational.core.sqlgeneration.SqlGenerator} that caches the
* generated statements.
*
* @since 3.2
* @author Jens Schauder
* @since 3.2
*/
static class CachingSqlGenerator implements org.springframework.data.relational.core.sqlgeneration.SqlGenerator {

Expand Down
Loading