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 Sep 5, 2020
1 parent c058899 commit 55fe658
Showing 1 changed file with 37 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,30 +94,32 @@ public class ResultSetEnumerable<T> extends AbstractEnumerable<T> {
}
};
} else {
//noinspection unchecked
return (Function0<@Nullable Object>) () -> {
try {
final List<@Nullable 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 @Nullable Object[] convertColumns(ResultSet resultSet, ResultSetMetaData metaData,
int columnCount) {
final List<@Nullable 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 @@ -433,21 +435,23 @@ public void close() {
}
};
}
//noinspection unchecked
return (Function0<@Nullable Object>) () -> {
try {
final List<@Nullable 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 @Nullable Object [] convertPrimitiveColumns(Primitive[] primitives,
ResultSet resultSet, int columnCount) {
final List<@Nullable 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 55fe658

Please sign in to comment.