47 |
47 |
48 private final PreparedStatement insert, update, list, find; |
48 private final PreparedStatement insert, update, list, find; |
49 |
49 |
50 public PGProjectDao(Connection connection) throws SQLException { |
50 public PGProjectDao(Connection connection) throws SQLException { |
51 list = connection.prepareStatement( |
51 list = connection.prepareStatement( |
52 "select id, name, description, repourl, " + |
52 "select projectid, name, description, repourl, " + |
53 "userid, username, lastname, givenname, mail " + |
53 "userid, username, lastname, givenname, mail " + |
54 "from lpit_project " + |
54 "from lpit_project " + |
55 "left join lpit_user owner on lpit_project.owner = owner.userid " + |
55 "left join lpit_user owner on lpit_project.owner = owner.userid " + |
56 "order by name"); |
56 "order by name"); |
57 |
57 |
58 find = connection.prepareStatement( |
58 find = connection.prepareStatement( |
59 "select id, name, description, repourl, " + |
59 "select projectid, name, description, repourl, " + |
60 "userid, username, lastname, givenname, mail " + |
60 "userid, username, lastname, givenname, mail " + |
61 "from lpit_project " + |
61 "from lpit_project " + |
62 "left join lpit_user owner on lpit_project.owner = owner.userid " + |
62 "left join lpit_user owner on lpit_project.owner = owner.userid " + |
63 "where id = ?"); |
63 "where projectid = ?"); |
64 |
64 |
65 insert = connection.prepareStatement( |
65 insert = connection.prepareStatement( |
66 "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)" |
66 "insert into lpit_project (name, description, repourl, owner) values (?, ?, ?, ?)" |
67 ); |
67 ); |
68 update = connection.prepareStatement( |
68 update = connection.prepareStatement( |
69 "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where id = ?" |
69 "update lpit_project set name = ?, description = ?, repourl = ?, owner = ? where projectid = ?" |
70 ); |
70 ); |
71 } |
71 } |
72 |
72 |
73 public Project mapColumns(ResultSet result) throws SQLException { |
73 public Project mapColumns(ResultSet result) throws SQLException { |
74 final var proj = new Project(result.getInt("id")); |
74 final var proj = new Project(result.getInt("projectid")); |
75 proj.setName(result.getString("name")); |
75 proj.setName(result.getString("name")); |
76 proj.setDescription(result.getString("description")); |
76 proj.setDescription(result.getString("description")); |
77 proj.setRepoUrl(result.getString("repourl")); |
77 proj.setRepoUrl(result.getString("repourl")); |
78 |
78 |
79 final int id = result.getInt("userid"); |
79 final int id = result.getInt("userid"); |
99 insert.executeUpdate(); |
99 insert.executeUpdate(); |
100 } |
100 } |
101 |
101 |
102 @Override |
102 @Override |
103 public boolean update(Project instance) throws SQLException { |
103 public boolean update(Project instance) throws SQLException { |
|
104 if (instance.getId() < 0) return false; |
104 Objects.requireNonNull(instance.getName()); |
105 Objects.requireNonNull(instance.getName()); |
105 update.setString(1, instance.getName()); |
106 update.setString(1, instance.getName()); |
106 setStringOrNull(update, 2, instance.getDescription()); |
107 setStringOrNull(update, 2, instance.getDescription()); |
107 setStringOrNull(update, 3, instance.getRepoUrl()); |
108 setStringOrNull(update, 3, instance.getRepoUrl()); |
108 setForeignKeyOrNull(update, 4, instance.getOwner(), User::getId); |
109 setForeignKeyOrNull(update, 4, instance.getOwner(), User::getId); |