Skip to content

Commit

Permalink
Close result set using try with resources (#4929)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlesarnal authored Jul 25, 2024
1 parent b626efa commit 4e20ef0
Showing 1 changed file with 78 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class MappedQueryImpl<T> implements MappedQuery<T>, Closeable {

final PreparedStatement statement;
final RowMapper<T> mapper;
final ResultSet resultSet;

/**
* Constructor.
Expand All @@ -47,7 +46,6 @@ public class MappedQueryImpl<T> implements MappedQuery<T>, Closeable {
public MappedQueryImpl(PreparedStatement statement, RowMapper<T> mapper) throws SQLException {
this.statement = statement;
this.mapper = mapper;
this.resultSet = statement.executeQuery();
}

/**
Expand All @@ -56,18 +54,21 @@ public MappedQueryImpl(PreparedStatement statement, RowMapper<T> mapper) throws
@Override
public T one() {
T rval = null;
try {
if (this.resultSet.next()) {
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
rval = this.mapper.map(resultSet);
if (this.resultSet.next()) {
if (resultSet.next()) {
throw new RuntimeSqlException("SQL error: Expected only one result but got multiple.");
}
} else {
}
else {
throw new RuntimeSqlException("SQL error: Expected only one result row but got none.");
}
} catch (SQLException e) {
}
catch (SQLException e) {
throw new RuntimeSqlException(e);
} finally {
}
finally {
close();
}
return rval;
Expand All @@ -79,15 +80,18 @@ public T one() {
@Override
public T first() {
T rval = null;
try {
if (this.resultSet.next()) {
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
rval = this.mapper.map(resultSet);
} else {
}
else {
throw new RuntimeSqlException("SQL error: Expected AT LEAST one result row but got none.");
}
} catch (SQLException e) {
}
catch (SQLException e) {
throw new RuntimeSqlException(e);
} finally {
}
finally {
close();
}
return rval;
Expand All @@ -99,18 +103,21 @@ public T first() {
@Override
public Optional<T> findOne() {
Optional<T> rval;
try {
if (this.resultSet.next()) {
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
rval = Optional.of(this.mapper.map(resultSet));
if (this.resultSet.next()) {
if (resultSet.next()) {
throw new RuntimeSqlException("SQL error: Expected only one result but got multiple.");
}
} else {
}
else {
rval = Optional.empty();
}
} catch (SQLException e) {
}
catch (SQLException e) {
throw new RuntimeSqlException(e);
} finally {
}
finally {
close();
}
return rval;
Expand All @@ -122,15 +129,18 @@ public Optional<T> findOne() {
@Override
public Optional<T> findFirst() {
Optional<T> rval = null;
try {
if (this.resultSet.next()) {
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
rval = Optional.of(this.mapper.map(resultSet));
} else {
}
else {
rval = Optional.empty();
}
} catch (SQLException e) {
}
catch (SQLException e) {
throw new RuntimeSqlException(e);
} finally {
}
finally {
close();
}
return rval;
Expand All @@ -142,16 +152,18 @@ public Optional<T> findFirst() {
@Override
public Optional<T> findLast() {
Optional<T> rval = null;
try {
while (this.resultSet.next()) {
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
rval = Optional.of(this.mapper.map(resultSet));
}
if (rval == null) {
rval = Optional.empty();
}
} catch (SQLException e) {
}
catch (SQLException e) {
throw new RuntimeSqlException(e);
} finally {
}
finally {
close();
}
return rval;
Expand All @@ -163,14 +175,16 @@ public Optional<T> findLast() {
@Override
public List<T> list() {
List<T> rval = new LinkedList<>();
try {
while (this.resultSet.next()) {
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
T t = this.mapper.map(resultSet);
rval.add(t);
}
} catch (SQLException e) {
}
catch (SQLException e) {
throw new RuntimeSqlException(e);
} finally {
}
finally {
close();
}
return rval;
Expand All @@ -181,22 +195,38 @@ public List<T> list() {
*/
@Override
public Stream<T> stream() {
return StreamSupport.stream(new Spliterators.AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.NONNULL) {
@Override
public boolean tryAdvance(Consumer<? super T> action) {
try {
ResultSet resultSet = statement.executeQuery();
return StreamSupport.stream(
new Spliterators.AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.NONNULL) {
@Override
public boolean tryAdvance(Consumer<? super T> action) {
try {
if (!resultSet.next()) {
return false;
}
T t = mapper.map(resultSet);
action.accept(t);
return true;
}
catch (SQLException e) {
throw new RuntimeSqlException(e);
}
}

}, false).onClose(() -> {
try {
if (!resultSet.next()) {
return false;
}
T t = mapper.map(resultSet);
action.accept(t);
return true;
} catch (SQLException e) {
throw new RuntimeSqlException(e);
resultSet.close();
close();
}
}

}, false).onClose(this::close);
catch (SQLException e) {
throw new RuntimeException(e);
}
});
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}

/**
Expand All @@ -206,7 +236,8 @@ public boolean tryAdvance(Consumer<? super T> action) {
public void close() {
try {
this.statement.close();
} catch (SQLException e) {
}
catch (SQLException e) {
throw new RuntimeSqlException(e);
}
}
Expand Down

0 comments on commit 4e20ef0

Please sign in to comment.