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

changeset 47
57cfb94ab99f
parent 38
cf85ef18f231
child 51
dd0a45ae25d7
equal deleted inserted replaced
46:1574965c7dc7 47:57cfb94ab99f
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 * 27 *
28 */ 28 */
29 package de.uapcore.lightpit.dao.postgres; 29 package de.uapcore.lightpit.dao.postgres;
30 30
31 import de.uapcore.lightpit.dao.AbstractDao; 31 import de.uapcore.lightpit.dao.GenericDao;
32 import de.uapcore.lightpit.dao.ProjectDao; 32 import de.uapcore.lightpit.dao.ProjectDao;
33 import de.uapcore.lightpit.entities.Project; 33 import de.uapcore.lightpit.entities.Project;
34 import de.uapcore.lightpit.entities.User; 34 import de.uapcore.lightpit.entities.User;
35 35
36 import java.sql.Connection; 36 import java.sql.Connection;
37 import java.sql.PreparedStatement; 37 import java.sql.PreparedStatement;
38 import java.sql.ResultSet; 38 import java.sql.ResultSet;
39 import java.sql.SQLException; 39 import java.sql.SQLException;
40 import java.util.ArrayList;
41 import java.util.List;
40 import java.util.Objects; 42 import java.util.Objects;
41 43
42 public final class PGProjectDao extends AbstractDao<Project> implements ProjectDao { 44 import static de.uapcore.lightpit.dao.Functions.setForeignKeyOrNull;
45 import static de.uapcore.lightpit.dao.Functions.setStringOrNull;
43 46
44 private final PGUserDao userDao; 47 public final class PGProjectDao implements ProjectDao, GenericDao<Project> {
45 48
46 private final PreparedStatement insert; 49 private final PreparedStatement insert, update, list, find;
47 private final PreparedStatement update;
48 50
49 public PGProjectDao(Connection connection, PGUserDao userDao) throws SQLException { 51 public PGProjectDao(Connection connection) throws SQLException {
50 super(connection.prepareStatement( 52 list = connection.prepareStatement(
51 "select * from lpit_project join lpit_user owner on lpit_project.owner = owner.userid")); 53 "select id, name, description, repourl, " +
54 "userid, username, lastname, givenname, mail " +
55 "from lpit_project " +
56 "left join lpit_user owner on lpit_project.owner = owner.userid " +
57 "order by name");
58
59 find = connection.prepareStatement(
60 "select id, name, description, repourl, " +
61 "userid, username, lastname, givenname, mail " +
62 "from lpit_project " +
63 "left join lpit_user owner on lpit_project.owner = owner.userid " +
64 "where id = ?");
52 65
53 insert = connection.prepareStatement( 66 insert = connection.prepareStatement(
54 "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)" 67 "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)"
55 ); 68 );
56 update = connection.prepareStatement( 69 update = connection.prepareStatement(
57 "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where id = ?" 70 "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where id = ?"
58 ); 71 );
59
60 this.userDao = userDao;
61 } 72 }
62 73
63 @Override 74 public Project mapColumns(ResultSet result) throws SQLException {
64 public Project mapColumns(ResultSet result, String q) throws SQLException { 75 final var proj = new Project(result.getInt("id"));
65 final var proj = new Project(result.getInt(qual(q, "id"))); 76 proj.setName(result.getString("name"));
66 proj.setName(result.getString(qual(q, "name"))); 77 proj.setDescription(result.getString("description"));
67 proj.setDescription(result.getString(qual(q, "description"))); 78 proj.setRepoUrl(result.getString("repourl"));
68 proj.setRepoUrl(result.getString(qual(q, "repourl"))); 79
69 proj.setOwner(userDao.mapColumns(result, "owner")); 80 final int id = result.getInt("userid");
81 if (id != 0) {
82 final var user = new User(id);
83 user.setUsername(result.getString("username"));
84 user.setGivenname(result.getString("givenname"));
85 user.setLastname(result.getString("lastname"));
86 user.setMail(result.getString("mail"));
87 proj.setOwner(user);
88 }
89
70 return proj; 90 return proj;
71 } 91 }
72 92
73 @Override 93 @Override
74 public void save(Project instance) throws SQLException { 94 public void save(Project instance) throws SQLException {
85 Objects.requireNonNull(instance.getName()); 105 Objects.requireNonNull(instance.getName());
86 update.setString(1, instance.getName()); 106 update.setString(1, instance.getName());
87 setStringOrNull(update, 2, instance.getDescription()); 107 setStringOrNull(update, 2, instance.getDescription());
88 setStringOrNull(update, 3, instance.getRepoUrl()); 108 setStringOrNull(update, 3, instance.getRepoUrl());
89 setForeignKeyOrNull(update, 4, instance.getOwner(), User::getUserID); 109 setForeignKeyOrNull(update, 4, instance.getOwner(), User::getUserID);
110 update.setInt(5, instance.getId());
90 return update.executeUpdate() > 0; 111 return update.executeUpdate() > 0;
91 } 112 }
113
114 @Override
115 public List<Project> list() throws SQLException {
116 List<Project> projects = new ArrayList<>();
117 try (var result = list.executeQuery()) {
118 while (result.next()) {
119 projects.add(mapColumns(result));
120 }
121 }
122 return projects;
123 }
124
125 @Override
126 public Project find(int id) throws SQLException {
127 find.setInt(1, id);
128 try (var result = find.executeQuery()) {
129 if (result.next()) {
130 return mapColumns(result);
131 } else {
132 return null;
133 }
134 }
135 }
92 } 136 }

mercurial