Sat, 24 Oct 2020 12:09:08 +0200
migrate DataSourceProvider
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@38 | 32 | import de.uapcore.lightpit.dao.ProjectDao; |
universe@86 | 33 | import de.uapcore.lightpit.entities.IssueSummary; |
universe@38 | 34 | import de.uapcore.lightpit.entities.Project; |
universe@38 | 35 | import de.uapcore.lightpit.entities.User; |
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@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@138 | 48 | private final PreparedStatement insert, update, list, find, findByNode; |
universe@81 | 49 | private final PreparedStatement issue_summary; |
universe@38 | 50 | |
universe@47 | 51 | public PGProjectDao(Connection connection) throws SQLException { |
universe@138 | 52 | final var query = "select projectid, name, node, description, repourl, " + |
universe@138 | 53 | "userid, username, lastname, givenname, mail " + |
universe@138 | 54 | "from lpit_project " + |
universe@138 | 55 | "left join lpit_user owner on lpit_project.owner = owner.userid "; |
universe@47 | 56 | |
universe@138 | 57 | list = connection.prepareStatement(query + " order by name"); |
universe@138 | 58 | |
universe@138 | 59 | find = connection.prepareStatement(query + " where projectid = ?"); |
universe@138 | 60 | findByNode = connection.prepareStatement(query + " where node = ?"); |
universe@38 | 61 | |
universe@81 | 62 | issue_summary = connection.prepareStatement( |
universe@81 | 63 | "select phase, count(*) as total "+ |
universe@81 | 64 | "from lpit_issue " + |
universe@81 | 65 | "join lpit_issue_phases using(status) " + |
universe@81 | 66 | "where project = ? "+ |
universe@81 | 67 | "group by phase " |
universe@81 | 68 | ); |
universe@81 | 69 | |
universe@38 | 70 | insert = connection.prepareStatement( |
universe@138 | 71 | "insert into lpit_project (name, node, description, repourl, owner) values (?, ?, ?, ?, ?)" |
universe@38 | 72 | ); |
universe@38 | 73 | update = connection.prepareStatement( |
universe@138 | 74 | "update lpit_project set name = ?, node = ?, description = ?, repourl = ?, owner = ? where projectid = ?" |
universe@38 | 75 | ); |
universe@38 | 76 | } |
universe@38 | 77 | |
universe@138 | 78 | private static Project mapColumns(ResultSet result) throws SQLException { |
universe@75 | 79 | final var proj = new Project(result.getInt("projectid")); |
universe@47 | 80 | proj.setName(result.getString("name")); |
universe@138 | 81 | proj.setNode(result.getString("node")); |
universe@47 | 82 | proj.setDescription(result.getString("description")); |
universe@47 | 83 | proj.setRepoUrl(result.getString("repourl")); |
universe@128 | 84 | proj.setOwner(PGUserDao.mapColumns(result)); |
universe@47 | 85 | |
universe@38 | 86 | return proj; |
universe@38 | 87 | } |
universe@38 | 88 | |
universe@86 | 89 | public IssueSummary getIssueSummary(Project project) throws SQLException { |
universe@86 | 90 | issue_summary.setInt(1, project.getId()); |
universe@81 | 91 | final var result = issue_summary.executeQuery(); |
universe@86 | 92 | final var summary = new IssueSummary(); |
universe@81 | 93 | while (result.next()) { |
universe@81 | 94 | final var phase = result.getInt("phase"); |
universe@81 | 95 | final var total = result.getInt("total"); |
universe@81 | 96 | switch(phase) { |
universe@81 | 97 | case 0: |
universe@86 | 98 | summary.setOpen(total); |
universe@81 | 99 | break; |
universe@81 | 100 | case 1: |
universe@86 | 101 | summary.setActive(total); |
universe@81 | 102 | break; |
universe@81 | 103 | case 2: |
universe@86 | 104 | summary.setDone(total); |
universe@81 | 105 | break; |
universe@81 | 106 | } |
universe@81 | 107 | } |
universe@86 | 108 | return summary; |
universe@81 | 109 | } |
universe@81 | 110 | |
universe@138 | 111 | private static int setColumns(PreparedStatement stmt, Project instance) throws SQLException { |
universe@138 | 112 | int column = 0; |
universe@138 | 113 | stmt.setString(++column, instance.getName()); |
universe@138 | 114 | stmt.setString(++column, instance.getNode()); |
universe@138 | 115 | setStringOrNull(stmt, ++column, instance.getDescription()); |
universe@138 | 116 | setStringOrNull(stmt, ++column, instance.getRepoUrl()); |
universe@138 | 117 | setForeignKeyOrNull(stmt, ++column, instance.getOwner(), User::getId); |
universe@138 | 118 | return column; |
universe@138 | 119 | } |
universe@138 | 120 | |
universe@38 | 121 | @Override |
universe@38 | 122 | public void save(Project instance) throws SQLException { |
universe@138 | 123 | setColumns(insert, instance); |
universe@38 | 124 | insert.executeUpdate(); |
universe@38 | 125 | } |
universe@38 | 126 | |
universe@38 | 127 | @Override |
universe@38 | 128 | public boolean update(Project instance) throws SQLException { |
universe@75 | 129 | if (instance.getId() < 0) return false; |
universe@138 | 130 | int column = setColumns(update, instance); |
universe@138 | 131 | update.setInt(++column, instance.getId()); |
universe@38 | 132 | return update.executeUpdate() > 0; |
universe@38 | 133 | } |
universe@47 | 134 | |
universe@47 | 135 | @Override |
universe@47 | 136 | public List<Project> list() throws SQLException { |
universe@138 | 137 | return Functions.list(list, PGProjectDao::mapColumns); |
universe@47 | 138 | } |
universe@47 | 139 | |
universe@47 | 140 | @Override |
universe@47 | 141 | public Project find(int id) throws SQLException { |
universe@47 | 142 | find.setInt(1, id); |
universe@138 | 143 | return Functions.find(find, PGProjectDao::mapColumns); |
universe@138 | 144 | } |
universe@138 | 145 | |
universe@138 | 146 | @Override |
universe@138 | 147 | public Project findByNode(String node) throws SQLException { |
universe@138 | 148 | findByNode.setString(1, node); |
universe@138 | 149 | return Functions.find(findByNode, PGProjectDao::mapColumns); |
universe@47 | 150 | } |
universe@38 | 151 | } |