src/main/java/de/uapcore/lightpit/dao/postgres/PGVersionDao.java

changeset 138
e2aa673dd473
parent 128
947d0f6a6a83
equal deleted inserted replaced
137:a7e543ab0c5f 138:e2aa673dd473
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 * 27 *
28 */ 28 */
29 package de.uapcore.lightpit.dao.postgres; 29 package de.uapcore.lightpit.dao.postgres;
30 30
31 import de.uapcore.lightpit.dao.Functions;
31 import de.uapcore.lightpit.dao.VersionDao; 32 import de.uapcore.lightpit.dao.VersionDao;
32 import de.uapcore.lightpit.entities.Project; 33 import de.uapcore.lightpit.entities.Project;
33 import de.uapcore.lightpit.entities.Version; 34 import de.uapcore.lightpit.entities.Version;
34 import de.uapcore.lightpit.entities.VersionStatus; 35 import de.uapcore.lightpit.entities.VersionStatus;
35 36
36 import java.sql.Connection; 37 import java.sql.Connection;
37 import java.sql.PreparedStatement; 38 import java.sql.PreparedStatement;
38 import java.sql.ResultSet; 39 import java.sql.ResultSet;
39 import java.sql.SQLException; 40 import java.sql.SQLException;
40 import java.util.ArrayList;
41 import java.util.List; 41 import java.util.List;
42 import java.util.Objects;
43 42
44 public final class PGVersionDao implements VersionDao { 43 public final class PGVersionDao implements VersionDao {
45 44
46 private final PreparedStatement insert, update, list, find; 45 private final PreparedStatement insert, update, list, find, findByNode;
47 46
48 public PGVersionDao(Connection connection) throws SQLException { 47 public PGVersionDao(Connection connection) throws SQLException {
49 list = connection.prepareStatement( 48 final var query = "select versionid, project, name, node, ordinal, status from lpit_version";
50 "select versionid, project, name, ordinal, status " + 49
51 "from lpit_version " + 50 list = connection.prepareStatement(query + " where project = ? " +
52 "where project = ? " +
53 "order by ordinal desc, lower(name) desc"); 51 "order by ordinal desc, lower(name) desc");
54 52 find = connection.prepareStatement(query + " where versionid = ?");
55 find = connection.prepareStatement( 53 findByNode = connection.prepareStatement(query + " where project = ? and node = ?");
56 "select versionid, project, name, ordinal, status " +
57 "from lpit_version " +
58 "where versionid = ?");
59 54
60 insert = connection.prepareStatement( 55 insert = connection.prepareStatement(
61 "insert into lpit_version (project, name, ordinal, status) values (?, ?, ?, ?::version_status)" 56 "insert into lpit_version (name, node, ordinal, status, project) values (?, ?, ?, ?::version_status, ?)"
62 ); 57 );
63 update = connection.prepareStatement( 58 update = connection.prepareStatement(
64 "update lpit_version set name = ?, ordinal = ?, status = ?::version_status where versionid = ?" 59 "update lpit_version set name = ?, node = ?, ordinal = ?, status = ?::version_status where versionid = ?"
65 ); 60 );
66 } 61 }
67 62
68 private Version mapColumns(ResultSet result) throws SQLException { 63 private static Version mapColumns(ResultSet result) throws SQLException {
69 final var version = new Version(result.getInt("versionid")); 64 final var version = new Version(result.getInt("versionid"));
70 version.setName(result.getString("name")); 65 version.setName(result.getString("name"));
66 version.setNode(result.getString("node"));
71 version.setOrdinal(result.getInt("ordinal")); 67 version.setOrdinal(result.getInt("ordinal"));
72 version.setStatus(VersionStatus.valueOf(result.getString("status"))); 68 version.setStatus(VersionStatus.valueOf(result.getString("status")));
73 return version; 69 return version;
74 } 70 }
75 71
72 private static int setFields(PreparedStatement stmt, Version instance) throws SQLException {
73 int column = 0;
74 stmt.setString(++column, instance.getName());
75 stmt.setString(++column, instance.getNode());
76 stmt.setInt(++column, instance.getOrdinal());
77 stmt.setString(++column, instance.getStatus().name());
78 return column;
79 }
80
76 @Override 81 @Override
77 public void save(Version instance, Project project) throws SQLException { 82 public void save(Version instance, Project project) throws SQLException {
78 Objects.requireNonNull(instance.getName()); 83 int column = setFields(insert, instance);
79 insert.setInt(1, project.getId()); 84 insert.setInt(++column, project.getId());
80 insert.setString(2, instance.getName());
81 insert.setInt(3, instance.getOrdinal());
82 insert.setString(4, instance.getStatus().name());
83 insert.executeUpdate(); 85 insert.executeUpdate();
84 } 86 }
85 87
86 @Override 88 @Override
87 public boolean update(Version instance) throws SQLException { 89 public boolean update(Version instance) throws SQLException {
88 if (instance.getId() < 0) return false; 90 if (instance.getId() < 0) return false;
89 Objects.requireNonNull(instance.getName()); 91 int column = setFields(update, instance);
90 update.setString(1, instance.getName()); 92 update.setInt(++column, instance.getId());
91 update.setInt(2, instance.getOrdinal());
92 update.setString(3, instance.getStatus().name());
93 update.setInt(4, instance.getId());
94 return update.executeUpdate() > 0; 93 return update.executeUpdate() > 0;
95 } 94 }
96 95
97 @Override 96 @Override
98 public List<Version> list(Project project) throws SQLException { 97 public List<Version> list(Project project) throws SQLException {
99 list.setInt(1, project.getId()); 98 list.setInt(1, project.getId());
100 List<Version> versions = new ArrayList<>(); 99 return Functions.list(list, PGVersionDao::mapColumns);
101 try (var result = list.executeQuery()) {
102 while (result.next()) {
103 versions.add(mapColumns(result));
104 }
105 }
106 return versions;
107 } 100 }
108 101
109 @Override 102 @Override
110 public Version find(int id) throws SQLException { 103 public Version find(int id) throws SQLException {
111 find.setInt(1, id); 104 find.setInt(1, id);
112 try (var result = find.executeQuery()) { 105 return Functions.find(find, PGVersionDao::mapColumns);
113 if (result.next()) { 106 }
114 return mapColumns(result); 107
115 } else { 108 @Override
116 return null; 109 public Version findByNode(Project project, String node) throws SQLException {
117 } 110 findByNode.setInt(1, project.getId());
118 } 111 findByNode.setString(2, node);;
112 return Functions.find(findByNode, PGVersionDao::mapColumns);
119 } 113 }
120 } 114 }

mercurial