diff -r a7e543ab0c5f -r e2aa673dd473 src/main/java/de/uapcore/lightpit/dao/postgres/PGVersionDao.java --- a/src/main/java/de/uapcore/lightpit/dao/postgres/PGVersionDao.java Thu Oct 22 12:00:34 2020 +0200 +++ b/src/main/java/de/uapcore/lightpit/dao/postgres/PGVersionDao.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.VersionDao; import de.uapcore.lightpit.entities.Project; import de.uapcore.lightpit.entities.Version; @@ -37,84 +38,77 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; -import java.util.Objects; public final class PGVersionDao implements VersionDao { - private final PreparedStatement insert, update, list, find; + private final PreparedStatement insert, update, list, find, findByNode; public PGVersionDao(Connection connection) throws SQLException { - list = connection.prepareStatement( - "select versionid, project, name, ordinal, status " + - "from lpit_version " + - "where project = ? " + + final var query = "select versionid, project, name, node, ordinal, status from lpit_version"; + + list = connection.prepareStatement(query + " where project = ? " + "order by ordinal desc, lower(name) desc"); - - find = connection.prepareStatement( - "select versionid, project, name, ordinal, status " + - "from lpit_version " + - "where versionid = ?"); + find = connection.prepareStatement(query + " where versionid = ?"); + findByNode = connection.prepareStatement(query + " where project = ? and node = ?"); insert = connection.prepareStatement( - "insert into lpit_version (project, name, ordinal, status) values (?, ?, ?, ?::version_status)" + "insert into lpit_version (name, node, ordinal, status, project) values (?, ?, ?, ?::version_status, ?)" ); update = connection.prepareStatement( - "update lpit_version set name = ?, ordinal = ?, status = ?::version_status where versionid = ?" + "update lpit_version set name = ?, node = ?, ordinal = ?, status = ?::version_status where versionid = ?" ); } - private Version mapColumns(ResultSet result) throws SQLException { + private static Version mapColumns(ResultSet result) throws SQLException { final var version = new Version(result.getInt("versionid")); version.setName(result.getString("name")); + version.setNode(result.getString("node")); version.setOrdinal(result.getInt("ordinal")); version.setStatus(VersionStatus.valueOf(result.getString("status"))); return version; } + private static int setFields(PreparedStatement stmt, Version instance) throws SQLException { + int column = 0; + stmt.setString(++column, instance.getName()); + stmt.setString(++column, instance.getNode()); + stmt.setInt(++column, instance.getOrdinal()); + stmt.setString(++column, instance.getStatus().name()); + return column; + } + @Override public void save(Version instance, Project project) throws SQLException { - Objects.requireNonNull(instance.getName()); - insert.setInt(1, project.getId()); - insert.setString(2, instance.getName()); - insert.setInt(3, instance.getOrdinal()); - insert.setString(4, instance.getStatus().name()); + int column = setFields(insert, instance); + insert.setInt(++column, project.getId()); insert.executeUpdate(); } @Override public boolean update(Version instance) throws SQLException { if (instance.getId() < 0) return false; - Objects.requireNonNull(instance.getName()); - update.setString(1, instance.getName()); - update.setInt(2, instance.getOrdinal()); - update.setString(3, instance.getStatus().name()); - update.setInt(4, instance.getId()); + int column = setFields(update, instance); + update.setInt(++column, instance.getId()); return update.executeUpdate() > 0; } @Override public List list(Project project) throws SQLException { list.setInt(1, project.getId()); - List versions = new ArrayList<>(); - try (var result = list.executeQuery()) { - while (result.next()) { - versions.add(mapColumns(result)); - } - } - return versions; + return Functions.list(list, PGVersionDao::mapColumns); } @Override public Version find(int id) throws SQLException { find.setInt(1, id); - try (var result = find.executeQuery()) { - if (result.next()) { - return mapColumns(result); - } else { - return null; - } - } + return Functions.find(find, PGVersionDao::mapColumns); + } + + @Override + public Version findByNode(Project project, String node) throws SQLException { + findByNode.setInt(1, project.getId()); + findByNode.setString(2, node);; + return Functions.find(findByNode, PGVersionDao::mapColumns); } }