49 private final PreparedStatement insertAffected, insertResolved; |
47 private final PreparedStatement insertAffected, insertResolved; |
50 private final PreparedStatement insertComment, updateComment, listComments; |
48 private final PreparedStatement insertComment, updateComment, listComments; |
51 |
49 |
52 public PGIssueDao(Connection connection) throws SQLException { |
50 public PGIssueDao(Connection connection) throws SQLException { |
53 list = connection.prepareStatement( |
51 list = connection.prepareStatement( |
54 "select issueid, project, p.name as projectname, status, category, subject, i.description, " + |
52 "select issueid, i.project, p.name as projectname, component, c.name as componentname, " + |
|
53 "status, category, subject, i.description, " + |
55 "userid, username, givenname, lastname, mail, " + |
54 "userid, username, givenname, lastname, mail, " + |
56 "created, updated, eta " + |
55 "created, updated, eta " + |
57 "from lpit_issue i " + |
56 "from lpit_issue i " + |
58 "join lpit_project p on project = projectid " + |
57 "join lpit_project p on i.project = projectid " + |
|
58 "left join lpit_component c on component = c.id " + |
59 "left join lpit_user on userid = assignee " + |
59 "left join lpit_user on userid = assignee " + |
60 "where project = ? "); |
60 "where i.project = ? and coalesce(component, -1) = coalesce(?, component, -1)"); |
61 |
61 |
62 listForVersion = connection.prepareStatement( |
62 listForVersion = connection.prepareStatement( |
63 "with issue_version as ( "+ |
63 "with issue_version as ( "+ |
64 "select issueid, versionid from lpit_issue_affected_version union "+ |
64 "select issueid, versionid from lpit_issue_affected_version union "+ |
65 "select issueid, versionid from lpit_issue_resolved_version) "+ |
65 "select issueid, versionid from lpit_issue_resolved_version) "+ |
66 "select issueid, project, p.name as projectname, status, category, subject, i.description, " + |
66 "select issueid, i.project, p.name as projectname, component, c.name as componentname, " + |
|
67 "status, category, subject, i.description, " + |
67 "userid, username, givenname, lastname, mail, " + |
68 "userid, username, givenname, lastname, mail, " + |
68 "created, updated, eta " + |
69 "created, updated, eta " + |
69 "from lpit_issue i " + |
70 "from lpit_issue i " + |
70 "join lpit_project p on project = projectid " + |
71 "join lpit_project p on i.project = projectid " + |
|
72 "left join lpit_component c on component = c.id " + |
71 "left join issue_version using (issueid) "+ |
73 "left join issue_version using (issueid) "+ |
72 "left join lpit_user on userid = assignee " + |
74 "left join lpit_user on userid = assignee " + |
73 "where coalesce(versionid,-1) = ? " |
75 "where coalesce(versionid,-1) = ? and coalesce(component, -1) = coalesce(?, component, -1)" |
74 ); |
76 ); |
75 |
77 |
76 find = connection.prepareStatement( |
78 find = connection.prepareStatement( |
77 "select issueid, project, p.name as projectname, status, category, subject, i.description, " + |
79 "select issueid, i.project, p.name as projectname, component, c.name as componentname, " + |
|
80 "status, category, subject, i.description, " + |
78 "userid, username, givenname, lastname, mail, " + |
81 "userid, username, givenname, lastname, mail, " + |
79 "created, updated, eta " + |
82 "created, updated, eta " + |
80 "from lpit_issue i " + |
83 "from lpit_issue i " + |
81 "left join lpit_project p on project = projectid " + |
84 "join lpit_project p on i.project = projectid " + |
|
85 "left join lpit_component c on component = c.id " + |
82 "left join lpit_user on userid = assignee " + |
86 "left join lpit_user on userid = assignee " + |
83 "where issueid = ? "); |
87 "where issueid = ? "); |
84 |
88 |
85 insert = connection.prepareStatement( |
89 insert = connection.prepareStatement( |
86 "insert into lpit_issue (project, status, category, subject, description, assignee, eta) " + |
90 "insert into lpit_issue (project, component, status, category, subject, description, assignee, eta) " + |
87 "values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?) returning issueid" |
91 "values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?) returning issueid" |
88 ); |
92 ); |
89 update = connection.prepareStatement( |
93 update = connection.prepareStatement( |
90 "update lpit_issue set updated = now(), status = ?::issue_status, category = ?::issue_category, " + |
94 "update lpit_issue set " + |
|
95 "updated = now(), component = ?, status = ?::issue_status, category = ?::issue_category, " + |
91 "subject = ?, description = ?, assignee = ?, eta = ? where issueid = ?" |
96 "subject = ?, description = ?, assignee = ?, eta = ? where issueid = ?" |
92 ); |
97 ); |
93 |
98 |
94 affectedVersions = connection.prepareStatement( |
99 affectedVersions = connection.prepareStatement( |
95 "select versionid, name, status, ordinal " + |
100 "select versionid, name, status, ordinal " + |
159 insertResolved.setInt(2, v.getId()); |
171 insertResolved.setInt(2, v.getId()); |
160 insertResolved.executeUpdate(); |
172 insertResolved.executeUpdate(); |
161 } |
173 } |
162 } |
174 } |
163 |
175 |
|
176 private int setData(PreparedStatement stmt, int column, Issue instance) throws SQLException { |
|
177 setForeignKeyOrNull(stmt, ++column, instance.getComponent(), Component::getId); |
|
178 stmt.setString(++column, instance.getStatus().name()); |
|
179 stmt.setString(++column, instance.getCategory().name()); |
|
180 stmt.setString(++column, instance.getSubject()); |
|
181 setStringOrNull(stmt, ++column, instance.getDescription()); |
|
182 setForeignKeyOrNull(stmt, ++column, instance.getAssignee(), User::getId); |
|
183 setDateOrNull(stmt, ++column, instance.getEta()); |
|
184 return column; |
|
185 } |
|
186 |
164 @Override |
187 @Override |
165 public void save(Issue instance, Project project) throws SQLException { |
188 public void save(Issue instance, Project project) throws SQLException { |
166 Objects.requireNonNull(instance.getSubject()); |
189 Objects.requireNonNull(instance.getSubject()); |
167 instance.setProject(project); |
190 instance.setProject(project); |
168 insert.setInt(1, instance.getProject().getId()); |
191 int column = 0; |
169 insert.setString(2, instance.getStatus().name()); |
192 insert.setInt(++column, instance.getProject().getId()); |
170 insert.setString(3, instance.getCategory().name()); |
193 setData(insert, column, instance); |
171 insert.setString(4, instance.getSubject()); |
|
172 setStringOrNull(insert, 5, instance.getDescription()); |
|
173 setForeignKeyOrNull(insert, 6, instance.getAssignee(), User::getId); |
|
174 setDateOrNull(insert, 7, instance.getEta()); |
|
175 // insert and retrieve the ID |
194 // insert and retrieve the ID |
176 final var rs = insert.executeQuery(); |
195 final var rs = insert.executeQuery(); |
177 rs.next(); |
196 rs.next(); |
178 instance.setId(rs.getInt(1)); |
197 instance.setId(rs.getInt(1)); |
179 updateVersionLists(instance); |
198 updateVersionLists(instance); |
181 |
200 |
182 @Override |
201 @Override |
183 public boolean update(Issue instance) throws SQLException { |
202 public boolean update(Issue instance) throws SQLException { |
184 if (instance.getId() < 0) return false; |
203 if (instance.getId() < 0) return false; |
185 Objects.requireNonNull(instance.getSubject()); |
204 Objects.requireNonNull(instance.getSubject()); |
186 update.setString(1, instance.getStatus().name()); |
205 int column = setData(update, 0, instance); |
187 update.setString(2, instance.getCategory().name()); |
206 update.setInt(++column, instance.getId()); |
188 update.setString(3, instance.getSubject()); |
|
189 setStringOrNull(update, 4, instance.getDescription()); |
|
190 setForeignKeyOrNull(update, 5, instance.getAssignee(), User::getId); |
|
191 setDateOrNull(update, 6, instance.getEta()); |
|
192 update.setInt(7, instance.getId()); |
|
193 boolean success = update.executeUpdate() > 0; |
207 boolean success = update.executeUpdate() > 0; |
194 if (success) { |
208 if (success) { |
195 updateVersionLists(instance); |
209 updateVersionLists(instance); |
196 return true; |
210 return true; |
197 } else { |
211 } else { |
198 return false; |
212 return false; |
199 } |
213 } |
200 } |
214 } |
201 |
215 |
202 private List<Issue> list(PreparedStatement query, int arg) throws SQLException { |
216 private List<Issue> executeQuery(PreparedStatement query) throws SQLException { |
203 query.setInt(1, arg); |
|
204 List<Issue> issues = new ArrayList<>(); |
217 List<Issue> issues = new ArrayList<>(); |
205 try (var result = query.executeQuery()) { |
218 try (var result = query.executeQuery()) { |
206 while (result.next()) { |
219 while (result.next()) { |
207 issues.add(mapColumns(result)); |
220 issues.add(mapColumns(result)); |
208 } |
221 } |