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

changeset 159
86b5d8a1662f
parent 158
4f912cd42876
child 160
e2d09cf3fb96
     1.1 --- a/src/main/java/de/uapcore/lightpit/dao/postgres/PGVersionDao.java	Fri Nov 06 10:50:32 2020 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,114 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2018 Mike Becker. All rights reserved.
     1.8 - *
     1.9 - * Redistribution and use in source and binary forms, with or without
    1.10 - * modification, are permitted provided that the following conditions are met:
    1.11 - *
    1.12 - *   1. Redistributions of source code must retain the above copyright
    1.13 - *      notice, this list of conditions and the following disclaimer.
    1.14 - *
    1.15 - *   2. Redistributions in binary form must reproduce the above copyright
    1.16 - *      notice, this list of conditions and the following disclaimer in the
    1.17 - *      documentation and/or other materials provided with the distribution.
    1.18 - *
    1.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 - * POSSIBILITY OF SUCH DAMAGE.
    1.30 - *
    1.31 - */
    1.32 -package de.uapcore.lightpit.dao.postgres;
    1.33 -
    1.34 -import de.uapcore.lightpit.dao.Functions;
    1.35 -import de.uapcore.lightpit.dao.VersionDao;
    1.36 -import de.uapcore.lightpit.entities.Project;
    1.37 -import de.uapcore.lightpit.entities.Version;
    1.38 -import de.uapcore.lightpit.entities.VersionStatus;
    1.39 -
    1.40 -import java.sql.Connection;
    1.41 -import java.sql.PreparedStatement;
    1.42 -import java.sql.ResultSet;
    1.43 -import java.sql.SQLException;
    1.44 -import java.util.List;
    1.45 -
    1.46 -public final class PGVersionDao implements VersionDao {
    1.47 -
    1.48 -    private final PreparedStatement insert, update, list, find, findByNode;
    1.49 -
    1.50 -    public PGVersionDao(Connection connection) throws SQLException {
    1.51 -        final var query = "select versionid, project, name, node, ordinal, status from lpit_version";
    1.52 -
    1.53 -        list = connection.prepareStatement(query + " where project = ? " +
    1.54 -                        "order by ordinal desc, lower(name) desc");
    1.55 -        find = connection.prepareStatement(query + " where versionid = ?");
    1.56 -        findByNode = connection.prepareStatement(query + " where project = ? and node = ?");
    1.57 -
    1.58 -        insert = connection.prepareStatement(
    1.59 -                "insert into lpit_version (name, node, ordinal, status, project) values (?, ?, ?, ?::version_status, ?)"
    1.60 -        );
    1.61 -        update = connection.prepareStatement(
    1.62 -                "update lpit_version set name = ?, node = ?, ordinal = ?, status = ?::version_status where versionid = ?"
    1.63 -        );
    1.64 -    }
    1.65 -
    1.66 -    private static Version mapColumns(ResultSet result) throws SQLException {
    1.67 -        final var version = new Version(result.getInt("versionid"));
    1.68 -        version.setName(result.getString("name"));
    1.69 -        version.setNode(result.getString("node"));
    1.70 -        version.setOrdinal(result.getInt("ordinal"));
    1.71 -        version.setStatus(VersionStatus.valueOf(result.getString("status")));
    1.72 -        return version;
    1.73 -    }
    1.74 -
    1.75 -    private static int setFields(PreparedStatement stmt, Version instance) throws SQLException {
    1.76 -        int column = 0;
    1.77 -        stmt.setString(++column, instance.getName());
    1.78 -        stmt.setString(++column, instance.getNode());
    1.79 -        stmt.setInt(++column, instance.getOrdinal());
    1.80 -        stmt.setString(++column, instance.getStatus().name());
    1.81 -        return column;
    1.82 -    }
    1.83 -
    1.84 -    @Override
    1.85 -    public void save(Version instance, Project project) throws SQLException {
    1.86 -        int column = setFields(insert, instance);
    1.87 -        insert.setInt(++column, project.getId());
    1.88 -        insert.executeUpdate();
    1.89 -    }
    1.90 -
    1.91 -    @Override
    1.92 -    public boolean update(Version instance) throws SQLException {
    1.93 -        if (instance.getId() < 0) return false;
    1.94 -        int column = setFields(update, instance);
    1.95 -        update.setInt(++column, instance.getId());
    1.96 -        return update.executeUpdate() > 0;
    1.97 -    }
    1.98 -
    1.99 -    @Override
   1.100 -    public List<Version> list(Project project) throws SQLException {
   1.101 -        list.setInt(1, project.getId());
   1.102 -        return Functions.list(list, PGVersionDao::mapColumns);
   1.103 -    }
   1.104 -
   1.105 -    @Override
   1.106 -    public Version find(int id) throws SQLException {
   1.107 -        find.setInt(1, id);
   1.108 -        return Functions.find(find, PGVersionDao::mapColumns);
   1.109 -    }
   1.110 -
   1.111 -    @Override
   1.112 -    public Version findByNode(Project project, String node) throws SQLException {
   1.113 -        findByNode.setInt(1, project.getId());
   1.114 -        findByNode.setString(2, node);;
   1.115 -        return Functions.find(findByNode, PGVersionDao::mapColumns);
   1.116 -    }
   1.117 -}

mercurial