diff -r a7e543ab0c5f -r e2aa673dd473 src/main/java/de/uapcore/lightpit/dao/postgres/PGProjectDao.java --- a/src/main/java/de/uapcore/lightpit/dao/postgres/PGProjectDao.java Thu Oct 22 12:00:34 2020 +0200 +++ b/src/main/java/de/uapcore/lightpit/dao/postgres/PGProjectDao.java Thu Oct 22 13:03:26 2020 +0200 @@ -28,6 +28,7 @@ */ package de.uapcore.lightpit.dao.postgres; +import de.uapcore.lightpit.dao.Functions; import de.uapcore.lightpit.dao.ProjectDao; import de.uapcore.lightpit.entities.IssueSummary; import de.uapcore.lightpit.entities.Project; @@ -37,32 +38,26 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; -import java.util.Objects; import static de.uapcore.lightpit.dao.Functions.setForeignKeyOrNull; import static de.uapcore.lightpit.dao.Functions.setStringOrNull; public final class PGProjectDao implements ProjectDao { - private final PreparedStatement insert, update, list, find; + private final PreparedStatement insert, update, list, find, findByNode; private final PreparedStatement issue_summary; public PGProjectDao(Connection connection) throws SQLException { - list = connection.prepareStatement( - "select projectid, name, description, repourl, " + - "userid, username, lastname, givenname, mail " + - "from lpit_project " + - "left join lpit_user owner on lpit_project.owner = owner.userid " + - "order by name"); + final var query = "select projectid, name, node, description, repourl, " + + "userid, username, lastname, givenname, mail " + + "from lpit_project " + + "left join lpit_user owner on lpit_project.owner = owner.userid "; - find = connection.prepareStatement( - "select projectid, name, description, repourl, " + - "userid, username, lastname, givenname, mail " + - "from lpit_project " + - "left join lpit_user owner on lpit_project.owner = owner.userid " + - "where projectid = ?"); + list = connection.prepareStatement(query + " order by name"); + + find = connection.prepareStatement(query + " where projectid = ?"); + findByNode = connection.prepareStatement(query + " where node = ?"); issue_summary = connection.prepareStatement( "select phase, count(*) as total "+ @@ -73,16 +68,17 @@ ); insert = connection.prepareStatement( - "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)" + "insert into lpit_project (name, node, description, repourl, owner) values (?, ?, ?, ?, ?)" ); update = connection.prepareStatement( - "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where projectid = ?" + "update lpit_project set name = ?, node = ?, description = ?, repourl = ?, owner = ? where projectid = ?" ); } - public Project mapColumns(ResultSet result) throws SQLException { + private static Project mapColumns(ResultSet result) throws SQLException { final var proj = new Project(result.getInt("projectid")); proj.setName(result.getString("name")); + proj.setNode(result.getString("node")); proj.setDescription(result.getString("description")); proj.setRepoUrl(result.getString("repourl")); proj.setOwner(PGUserDao.mapColumns(result)); @@ -112,50 +108,44 @@ return summary; } + private static int setColumns(PreparedStatement stmt, Project instance) throws SQLException { + int column = 0; + stmt.setString(++column, instance.getName()); + stmt.setString(++column, instance.getNode()); + setStringOrNull(stmt, ++column, instance.getDescription()); + setStringOrNull(stmt, ++column, instance.getRepoUrl()); + setForeignKeyOrNull(stmt, ++column, instance.getOwner(), User::getId); + return column; + } + @Override public void save(Project instance) throws SQLException { - Objects.requireNonNull(instance.getName()); - insert.setString(1, instance.getName()); - setStringOrNull(insert, 2, instance.getDescription()); - setStringOrNull(insert, 3, instance.getRepoUrl()); - setForeignKeyOrNull(insert, 4, instance.getOwner(), User::getId); + setColumns(insert, instance); insert.executeUpdate(); } @Override public boolean update(Project instance) throws SQLException { if (instance.getId() < 0) return false; - Objects.requireNonNull(instance.getName()); - update.setString(1, instance.getName()); - setStringOrNull(update, 2, instance.getDescription()); - setStringOrNull(update, 3, instance.getRepoUrl()); - setForeignKeyOrNull(update, 4, instance.getOwner(), User::getId); - update.setInt(5, instance.getId()); + int column = setColumns(update, instance); + update.setInt(++column, instance.getId()); return update.executeUpdate() > 0; } @Override public List list() throws SQLException { - List projects = new ArrayList<>(); - try (var result = list.executeQuery()) { - while (result.next()) { - final var project = mapColumns(result); - projects.add(project); - } - } - return projects; + return Functions.list(list, PGProjectDao::mapColumns); } @Override public Project find(int id) throws SQLException { find.setInt(1, id); - try (var result = find.executeQuery()) { - if (result.next()) { - final var project = mapColumns(result); - return project; - } else { - return null; - } - } + return Functions.find(find, PGProjectDao::mapColumns); + } + + @Override + public Project findByNode(String node) throws SQLException { + findByNode.setString(1, node); + return Functions.find(findByNode, PGProjectDao::mapColumns); } }