Skip to content
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

Refactor ResultSetEnumerable to avoid try-catch in nested lambdas #2198

Merged
merged 1 commit into from
Oct 8, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,32 @@ public class ResultSetEnumerable<T> extends AbstractEnumerable<T> {
}
};
} else {
//noinspection unchecked
return (Function0) () -> {
try {
final List<Object> list = new ArrayList<>();
for (int i = 0; i < columnCount; i++) {
if (metaData.getColumnType(i + 1) == Types.TIMESTAMP) {
long v = resultSet.getLong(i + 1);
if (v == 0 && resultSet.wasNull()) {
list.add(null);
} else {
list.add(v);
}
} else {
list.add(resultSet.getObject(i + 1));
}
}
return list.toArray();
} catch (SQLException e) {
throw new RuntimeException(e);
}
};
return () -> convertColumns(resultSet, metaData, columnCount);
}
};

private static Object[] convertColumns(ResultSet resultSet, ResultSetMetaData metaData,
int columnCount) {
final List<Object> list = new ArrayList<>(columnCount);
try {
for (int i = 0; i < columnCount; i++) {
if (metaData.getColumnType(i + 1) == Types.TIMESTAMP) {
long v = resultSet.getLong(i + 1);
if (v == 0 && resultSet.wasNull()) {
list.add(null);
} else {
list.add(v);
}
} else {
list.add(resultSet.getObject(i + 1));
}
}
return list.toArray();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

private ResultSetEnumerable(
DataSource dataSource,
String sql,
Expand Down Expand Up @@ -423,21 +425,23 @@ private static class ResultSetEnumerator<T> implements Enumerator<T> {
}
};
}
//noinspection unchecked
return (Function0) () -> {
try {
final List<Object> list = new ArrayList<>();
for (int i = 0; i < columnCount; i++) {
list.add(primitives[i].jdbcGet(resultSet, i + 1));
}
return list.toArray();
} catch (SQLException e) {
throw new RuntimeException(e);
}
};
return () -> convertPrimitiveColumns(primitives, resultSet, columnCount);
};
}

private static Object[] convertPrimitiveColumns(Primitive[] primitives,
ResultSet resultSet, int columnCount) {
final List<Object> list = new ArrayList<>(columnCount);
try {
for (int i = 0; i < columnCount; i++) {
list.add(primitives[i].jdbcGet(resultSet, i + 1));
}
return list.toArray();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

/**
* Consumer for decorating a {@link PreparedStatement}, that is, setting
* its parameters.
Expand Down