Skip to content

Commit

Permalink
Refactor ResultSetEnumerable to avoid nested lambdas
Browse files Browse the repository at this point in the history
This reduces the likelihood of javac issues.

See policeman-tools/forbidden-apis#173
  • Loading branch information
vlsi committed Oct 1, 2020
1 parent 663bbad commit bd014f5
Showing 1 changed file with 37 additions and 33 deletions.
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

0 comments on commit bd014f5

Please sign in to comment.