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

Fri, 23 Oct 2020 20:34:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 23 Oct 2020 20:34:57 +0200
changeset 150
822b7e3d064d
parent 138
e2aa673dd473
permissions
-rw-r--r--

migrate entities package

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

mercurial