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

changeset 38
cf85ef18f231
child 47
57cfb94ab99f
equal deleted inserted replaced
37:fecda0f466e6 38:cf85ef18f231
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.AbstractDao;
32 import de.uapcore.lightpit.dao.ProjectDao;
33 import de.uapcore.lightpit.entities.Project;
34 import de.uapcore.lightpit.entities.User;
35
36 import java.sql.Connection;
37 import java.sql.PreparedStatement;
38 import java.sql.ResultSet;
39 import java.sql.SQLException;
40 import java.util.Objects;
41
42 public final class PGProjectDao extends AbstractDao<Project> implements ProjectDao {
43
44 private final PGUserDao userDao;
45
46 private final PreparedStatement insert;
47 private final PreparedStatement update;
48
49 public PGProjectDao(Connection connection, PGUserDao userDao) throws SQLException {
50 super(connection.prepareStatement(
51 "select * from lpit_project join lpit_user owner on lpit_project.owner = owner.userid"));
52
53 insert = connection.prepareStatement(
54 "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)"
55 );
56 update = connection.prepareStatement(
57 "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where id = ?"
58 );
59
60 this.userDao = userDao;
61 }
62
63 @Override
64 public Project mapColumns(ResultSet result, String q) throws SQLException {
65 final var proj = new Project(result.getInt(qual(q, "id")));
66 proj.setName(result.getString(qual(q, "name")));
67 proj.setDescription(result.getString(qual(q, "description")));
68 proj.setRepoUrl(result.getString(qual(q, "repourl")));
69 proj.setOwner(userDao.mapColumns(result, "owner"));
70 return proj;
71 }
72
73 @Override
74 public void save(Project instance) throws SQLException {
75 Objects.requireNonNull(instance.getName());
76 insert.setString(1, instance.getName());
77 setStringOrNull(insert, 2, instance.getDescription());
78 setStringOrNull(insert, 3, instance.getRepoUrl());
79 setForeignKeyOrNull(insert, 4, instance.getOwner(), User::getUserID);
80 insert.executeUpdate();
81 }
82
83 @Override
84 public boolean update(Project instance) throws SQLException {
85 Objects.requireNonNull(instance.getName());
86 update.setString(1, instance.getName());
87 setStringOrNull(update, 2, instance.getDescription());
88 setStringOrNull(update, 3, instance.getRepoUrl());
89 setForeignKeyOrNull(update, 4, instance.getOwner(), User::getUserID);
90 return update.executeUpdate() > 0;
91 }
92 }

mercurial