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

changeset 159
86b5d8a1662f
parent 138
e2aa673dd473
equal deleted inserted replaced
158:4f912cd42876 159:86b5d8a1662f
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2018 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 package de.uapcore.lightpit.dao.postgres;
30
31 import de.uapcore.lightpit.dao.Functions;
32 import de.uapcore.lightpit.dao.VersionDao;
33 import de.uapcore.lightpit.entities.Project;
34 import de.uapcore.lightpit.entities.Version;
35 import de.uapcore.lightpit.entities.VersionStatus;
36
37 import java.sql.Connection;
38 import java.sql.PreparedStatement;
39 import java.sql.ResultSet;
40 import java.sql.SQLException;
41 import java.util.List;
42
43 public final class PGVersionDao implements VersionDao {
44
45 private final PreparedStatement insert, update, list, find, findByNode;
46
47 public PGVersionDao(Connection connection) throws SQLException {
48 final var query = "select versionid, project, name, node, ordinal, status from lpit_version";
49
50 list = connection.prepareStatement(query + " where project = ? " +
51 "order by ordinal desc, lower(name) desc");
52 find = connection.prepareStatement(query + " where versionid = ?");
53 findByNode = connection.prepareStatement(query + " where project = ? and node = ?");
54
55 insert = connection.prepareStatement(
56 "insert into lpit_version (name, node, ordinal, status, project) values (?, ?, ?, ?::version_status, ?)"
57 );
58 update = connection.prepareStatement(
59 "update lpit_version set name = ?, node = ?, ordinal = ?, status = ?::version_status where versionid = ?"
60 );
61 }
62
63 private static Version mapColumns(ResultSet result) throws SQLException {
64 final var version = new Version(result.getInt("versionid"));
65 version.setName(result.getString("name"));
66 version.setNode(result.getString("node"));
67 version.setOrdinal(result.getInt("ordinal"));
68 version.setStatus(VersionStatus.valueOf(result.getString("status")));
69 return version;
70 }
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
81 @Override
82 public void save(Version instance, Project project) throws SQLException {
83 int column = setFields(insert, instance);
84 insert.setInt(++column, project.getId());
85 insert.executeUpdate();
86 }
87
88 @Override
89 public boolean update(Version instance) throws SQLException {
90 if (instance.getId() < 0) return false;
91 int column = setFields(update, instance);
92 update.setInt(++column, instance.getId());
93 return update.executeUpdate() > 0;
94 }
95
96 @Override
97 public List<Version> list(Project project) throws SQLException {
98 list.setInt(1, project.getId());
99 return Functions.list(list, PGVersionDao::mapColumns);
100 }
101
102 @Override
103 public Version find(int id) throws SQLException {
104 find.setInt(1, id);
105 return Functions.find(find, PGVersionDao::mapColumns);
106 }
107
108 @Override
109 public Version findByNode(Project project, String node) throws SQLException {
110 findByNode.setInt(1, project.getId());
111 findByNode.setString(2, node);;
112 return Functions.find(findByNode, PGVersionDao::mapColumns);
113 }
114 }

mercurial