Browse Source

Polishing.

Tweak naming. Refine methods to static ones and collapse into a single method.

See #1615
pull/1621/head
Mark Paluch 2 years ago
parent
commit
1e20a32352
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 41
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/RowDocumentResultSetExtractor.java

41
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/RowDocumentResultSetExtractor.java

@ -155,11 +155,9 @@ class RowDocumentResultSetExtractor {
private final AggregateContext<ResultSet> aggregateContext; private final AggregateContext<ResultSet> aggregateContext;
/** /**
* Answers the question if the internal {@link ResultSet} points at an actual row. Since when not currently * Answers the question if the internal {@link ResultSet} points at an actual row.
* extracting a document the {@link ResultSet} points at the next row to be read (or behind all rows), this is
* equivalent to {@literal hasNext()} from the outside.
*/ */
private boolean pointsAtRow; private boolean hasNext;
RowDocumentIterator(RelationalPersistentEntity<?> entity, ResultSet resultSet) throws SQLException { RowDocumentIterator(RelationalPersistentEntity<?> entity, ResultSet resultSet) throws SQLException {
@ -174,11 +172,10 @@ class RowDocumentResultSetExtractor {
this.resultSet = resultSet; this.resultSet = resultSet;
this.identifierIndex = columns.get(idColumn); this.identifierIndex = columns.get(idColumn);
this.hasNext = hasRow(resultSet);
pointsAtRow = pointAtInitialRow();
} }
private boolean pointAtInitialRow() throws SQLException { private static boolean hasRow(ResultSet resultSet) {
// If we are before the first row we need to advance to the first row. // If we are before the first row we need to advance to the first row.
try { try {
@ -202,13 +199,11 @@ class RowDocumentResultSetExtractor {
// maybe isBeforeFirst or isBeforeLast aren't implemented // maybe isBeforeFirst or isBeforeLast aren't implemented
// or the ResultSet is empty. // or the ResultSet is empty.
try {
boolean peek = peek(resultSet); resultSet.getObject(1);
if (peek) {
// we can see actual data, so we are looking at a current row. // we can see actual data, so we are looking at a current row.
return true; return true;
} } catch (SQLException ignored) {}
try { try {
return resultSet.next(); return resultSet.next();
@ -219,25 +214,9 @@ class RowDocumentResultSetExtractor {
} }
} }
/**
* Tries ot access values of the passed in {@link ResultSet} in order to check if it is pointing at an actual row.
*
* @param resultSet to check.
* @return true if values of the {@literal ResultSet} can be accessed and it therefore points to an actual row.
*/
private boolean peek(ResultSet resultSet) {
try {
resultSet.getObject(1);
return true;
} catch (SQLException e) {
return false;
}
}
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return pointsAtRow; return hasNext;
} }
@Override @Override
@ -257,8 +236,8 @@ class RowDocumentResultSetExtractor {
} }
reader.accept(resultSet); reader.accept(resultSet);
pointsAtRow = resultSet.next(); hasNext = resultSet.next();
} while (pointsAtRow); } while (hasNext);
} catch (SQLException e) { } catch (SQLException e) {
throw new DataRetrievalFailureException("Cannot advance ResultSet", e); throw new DataRetrievalFailureException("Cannot advance ResultSet", e);
} }

Loading…
Cancel
Save