package io.trygvis.persistence.sql; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public abstract class SqlDao implements FromResultSet { protected final Connection c; protected SqlDao(Connection c) { this.c = c; } public abstract void insert(T o) throws SQLException; public abstract void delete(T o) throws SQLException; public abstract void deleteById(Id id) throws SQLException; public abstract T selectById(Id id) throws SQLException; public abstract void update(T entity) throws SQLException; public abstract List selectWhere(String where) throws SQLException; // ----------------------------------------------------------------------- // Protected // ----------------------------------------------------------------------- protected static List runQuery(Connection c, String sql, FromResultSet f) throws SQLException { try (PreparedStatement stmt = c.prepareStatement(sql)) { ResultSet rs = stmt.executeQuery(); List list = new ArrayList<>(); while (rs.next()) { list.add(f.fromResultSet(rs)); } return list; } } }