diff -r a7e543ab0c5f -r e2aa673dd473 src/main/java/de/uapcore/lightpit/dao/Functions.java --- a/src/main/java/de/uapcore/lightpit/dao/Functions.java Thu Oct 22 12:00:34 2020 +0200 +++ b/src/main/java/de/uapcore/lightpit/dao/Functions.java Thu Oct 22 13:03:26 2020 +0200 @@ -28,10 +28,9 @@ */ package de.uapcore.lightpit.dao; -import java.sql.Date; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.sql.Types; +import java.sql.*; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; import java.util.function.Function; @@ -65,6 +64,33 @@ } } + @FunctionalInterface + public interface ResultSetMapper { + T apply(ResultSet rs) throws SQLException; + } + + public static List list(PreparedStatement stmt, ResultSetMapper mapper) throws SQLException { + List results = new ArrayList<>(); + try (var result = stmt.executeQuery()) { + while (result.next()) { + final var project = mapper.apply(result); + results.add(project); + } + } + return results; + } + + public static T find(PreparedStatement stmt, ResultSetMapper mapper) throws SQLException { + try (var result = stmt.executeQuery()) { + if (result.next()) { + final var ent = mapper.apply(result); + return ent; + } else { + return null; + } + } + } + private Functions() { }