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

changeset 75
33b6843fdf8a
parent 62
833e0385572a
child 80
27a25f32048e
equal deleted inserted replaced
74:91d1fc2a3a14 75:33b6843fdf8a
45 45
46 private final PreparedStatement insert, update, list, find; 46 private final PreparedStatement insert, update, list, find;
47 47
48 public PGVersionDao(Connection connection) throws SQLException { 48 public PGVersionDao(Connection connection) throws SQLException {
49 list = connection.prepareStatement( 49 list = connection.prepareStatement(
50 "select id, project, name, ordinal, status " + 50 "select versionid, project, name, ordinal, status " +
51 "from lpit_version " + 51 "from lpit_version " +
52 "where project = ? " + 52 "where project = ? " +
53 "order by ordinal, lower(name)"); 53 "order by ordinal, lower(name)");
54 54
55 find = connection.prepareStatement( 55 find = connection.prepareStatement(
56 "select id, project, name, ordinal, status " + 56 "select versionid, project, name, ordinal, status " +
57 "from lpit_version " + 57 "from lpit_version " +
58 "where id = ?"); 58 "where versionid = ?");
59 59
60 insert = connection.prepareStatement( 60 insert = connection.prepareStatement(
61 "insert into lpit_version (project, name, ordinal, status) values (?, ?, ?, ?::version_status)" 61 "insert into lpit_version (project, name, ordinal, status) values (?, ?, ?, ?::version_status)"
62 ); 62 );
63 update = connection.prepareStatement( 63 update = connection.prepareStatement(
64 "update lpit_version set name = ?, ordinal = ?, status = ?::version_status where id = ?" 64 "update lpit_version set name = ?, ordinal = ?, status = ?::version_status where versionid = ?"
65 ); 65 );
66 } 66 }
67 67
68 public Version mapColumns(ResultSet result) throws SQLException { 68 private Version mapColumns(ResultSet result) throws SQLException {
69 final var version = new Version(result.getInt("id"), new Project(result.getInt("project"))); 69 final var project = new Project(result.getInt("project"));
70 final var version = new Version(result.getInt("versionid"), project);
70 version.setName(result.getString("name")); 71 version.setName(result.getString("name"));
71 version.setOrdinal(result.getInt("ordinal")); 72 version.setOrdinal(result.getInt("ordinal"));
72 version.setStatus(VersionStatus.valueOf(result.getString("status"))); 73 version.setStatus(VersionStatus.valueOf(result.getString("status")));
73 return version; 74 return version;
74 } 75 }
84 insert.executeUpdate(); 85 insert.executeUpdate();
85 } 86 }
86 87
87 @Override 88 @Override
88 public boolean update(Version instance) throws SQLException { 89 public boolean update(Version instance) throws SQLException {
90 if (instance.getId() < 0) return false;
89 Objects.requireNonNull(instance.getName()); 91 Objects.requireNonNull(instance.getName());
90 update.setString(1, instance.getName()); 92 update.setString(1, instance.getName());
91 update.setInt(2, instance.getOrdinal()); 93 update.setInt(2, instance.getOrdinal());
92 update.setString(3, instance.getStatus().name()); 94 update.setString(3, instance.getStatus().name());
93 update.setInt(4, instance.getId()); 95 update.setInt(4, instance.getId());
98 public List<Version> list(Project project) throws SQLException { 100 public List<Version> list(Project project) throws SQLException {
99 list.setInt(1, project.getId()); 101 list.setInt(1, project.getId());
100 List<Version> versions = new ArrayList<>(); 102 List<Version> versions = new ArrayList<>();
101 try (var result = list.executeQuery()) { 103 try (var result = list.executeQuery()) {
102 while (result.next()) { 104 while (result.next()) {
103 versions.add(mapColumns(result)); 105 final var v = mapColumns(result);
106 v.setProject(project);
107 versions.add(v);
104 } 108 }
105 } 109 }
106 return versions; 110 return versions;
107 } 111 }
108 112

mercurial