Sun, 17 May 2020 16:23:39 +0200
adds version management
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@38 | 31 | import de.uapcore.lightpit.dao.ProjectDao; |
universe@38 | 32 | import de.uapcore.lightpit.entities.Project; |
universe@38 | 33 | import de.uapcore.lightpit.entities.User; |
universe@38 | 34 | |
universe@38 | 35 | import java.sql.Connection; |
universe@38 | 36 | import java.sql.PreparedStatement; |
universe@38 | 37 | import java.sql.ResultSet; |
universe@38 | 38 | import java.sql.SQLException; |
universe@47 | 39 | import java.util.ArrayList; |
universe@47 | 40 | import java.util.List; |
universe@38 | 41 | import java.util.Objects; |
universe@38 | 42 | |
universe@47 | 43 | import static de.uapcore.lightpit.dao.Functions.setForeignKeyOrNull; |
universe@47 | 44 | import static de.uapcore.lightpit.dao.Functions.setStringOrNull; |
universe@38 | 45 | |
universe@59 | 46 | public final class PGProjectDao implements ProjectDao { |
universe@38 | 47 | |
universe@47 | 48 | private final PreparedStatement insert, update, list, find; |
universe@38 | 49 | |
universe@47 | 50 | public PGProjectDao(Connection connection) throws SQLException { |
universe@47 | 51 | list = connection.prepareStatement( |
universe@47 | 52 | "select id, name, description, repourl, " + |
universe@47 | 53 | "userid, username, lastname, givenname, mail " + |
universe@47 | 54 | "from lpit_project " + |
universe@47 | 55 | "left join lpit_user owner on lpit_project.owner = owner.userid " + |
universe@47 | 56 | "order by name"); |
universe@47 | 57 | |
universe@47 | 58 | find = connection.prepareStatement( |
universe@47 | 59 | "select id, name, description, repourl, " + |
universe@47 | 60 | "userid, username, lastname, givenname, mail " + |
universe@47 | 61 | "from lpit_project " + |
universe@47 | 62 | "left join lpit_user owner on lpit_project.owner = owner.userid " + |
universe@47 | 63 | "where id = ?"); |
universe@38 | 64 | |
universe@38 | 65 | insert = connection.prepareStatement( |
universe@38 | 66 | "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)" |
universe@38 | 67 | ); |
universe@38 | 68 | update = connection.prepareStatement( |
universe@38 | 69 | "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where id = ?" |
universe@38 | 70 | ); |
universe@38 | 71 | } |
universe@38 | 72 | |
universe@47 | 73 | public Project mapColumns(ResultSet result) throws SQLException { |
universe@47 | 74 | final var proj = new Project(result.getInt("id")); |
universe@47 | 75 | proj.setName(result.getString("name")); |
universe@47 | 76 | proj.setDescription(result.getString("description")); |
universe@47 | 77 | proj.setRepoUrl(result.getString("repourl")); |
universe@47 | 78 | |
universe@47 | 79 | final int id = result.getInt("userid"); |
universe@47 | 80 | if (id != 0) { |
universe@47 | 81 | final var user = new User(id); |
universe@47 | 82 | user.setUsername(result.getString("username")); |
universe@47 | 83 | user.setGivenname(result.getString("givenname")); |
universe@47 | 84 | user.setLastname(result.getString("lastname")); |
universe@47 | 85 | user.setMail(result.getString("mail")); |
universe@47 | 86 | proj.setOwner(user); |
universe@47 | 87 | } |
universe@47 | 88 | |
universe@38 | 89 | return proj; |
universe@38 | 90 | } |
universe@38 | 91 | |
universe@38 | 92 | @Override |
universe@38 | 93 | public void save(Project instance) throws SQLException { |
universe@38 | 94 | Objects.requireNonNull(instance.getName()); |
universe@38 | 95 | insert.setString(1, instance.getName()); |
universe@38 | 96 | setStringOrNull(insert, 2, instance.getDescription()); |
universe@38 | 97 | setStringOrNull(insert, 3, instance.getRepoUrl()); |
universe@51 | 98 | setForeignKeyOrNull(insert, 4, instance.getOwner(), User::getId); |
universe@38 | 99 | insert.executeUpdate(); |
universe@38 | 100 | } |
universe@38 | 101 | |
universe@38 | 102 | @Override |
universe@38 | 103 | public boolean update(Project instance) throws SQLException { |
universe@38 | 104 | Objects.requireNonNull(instance.getName()); |
universe@38 | 105 | update.setString(1, instance.getName()); |
universe@38 | 106 | setStringOrNull(update, 2, instance.getDescription()); |
universe@38 | 107 | setStringOrNull(update, 3, instance.getRepoUrl()); |
universe@51 | 108 | setForeignKeyOrNull(update, 4, instance.getOwner(), User::getId); |
universe@47 | 109 | update.setInt(5, instance.getId()); |
universe@38 | 110 | return update.executeUpdate() > 0; |
universe@38 | 111 | } |
universe@47 | 112 | |
universe@47 | 113 | @Override |
universe@47 | 114 | public List<Project> list() throws SQLException { |
universe@47 | 115 | List<Project> projects = new ArrayList<>(); |
universe@47 | 116 | try (var result = list.executeQuery()) { |
universe@47 | 117 | while (result.next()) { |
universe@47 | 118 | projects.add(mapColumns(result)); |
universe@47 | 119 | } |
universe@47 | 120 | } |
universe@47 | 121 | return projects; |
universe@47 | 122 | } |
universe@47 | 123 | |
universe@47 | 124 | @Override |
universe@47 | 125 | public Project find(int id) throws SQLException { |
universe@47 | 126 | find.setInt(1, id); |
universe@47 | 127 | try (var result = find.executeQuery()) { |
universe@47 | 128 | if (result.next()) { |
universe@47 | 129 | return mapColumns(result); |
universe@47 | 130 | } else { |
universe@47 | 131 | return null; |
universe@47 | 132 | } |
universe@47 | 133 | } |
universe@47 | 134 | } |
universe@38 | 135 | } |