src/main/kotlin/de/uapcore/lightpit/dao/PostgresDataAccessObject.kt

Sun, 08 Jan 2023 17:07:26 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 08 Jan 2023 17:07:26 +0100
changeset 268
ca5501d851fa
parent 263
aa22103809cd
child 284
671c1c8fbf1c
permissions
-rw-r--r--

#15 add issue filters

universe@167 1 /*
universe@180 2 * Copyright 2021 Mike Becker. All rights reserved.
universe@167 3 *
universe@167 4 * Redistribution and use in source and binary forms, with or without
universe@167 5 * modification, are permitted provided that the following conditions are met:
universe@167 6 *
universe@167 7 * 1. Redistributions of source code must retain the above copyright
universe@167 8 * notice, this list of conditions and the following disclaimer.
universe@167 9 *
universe@167 10 * 2. Redistributions in binary form must reproduce the above copyright
universe@167 11 * notice, this list of conditions and the following disclaimer in the
universe@167 12 * documentation and/or other materials provided with the distribution.
universe@167 13 *
universe@167 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@167 15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@167 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
universe@167 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
universe@167 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
universe@167 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
universe@167 20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
universe@167 21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
universe@167 22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
universe@167 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
universe@167 24 */
universe@167 25
universe@167 26 package de.uapcore.lightpit.dao
universe@167 27
universe@167 28 import de.uapcore.lightpit.entities.*
universe@232 29 import de.uapcore.lightpit.types.IssueHistoryType
universe@268 30 import de.uapcore.lightpit.types.RelationType
universe@167 31 import de.uapcore.lightpit.types.WebColor
universe@184 32 import de.uapcore.lightpit.viewmodel.ComponentSummary
universe@184 33 import de.uapcore.lightpit.viewmodel.IssueSummary
universe@184 34 import de.uapcore.lightpit.viewmodel.VersionSummary
universe@189 35 import org.intellij.lang.annotations.Language
universe@167 36 import java.sql.Connection
universe@167 37 import java.sql.PreparedStatement
universe@167 38 import java.sql.ResultSet
universe@167 39
universe@167 40 class PostgresDataAccessObject(private val connection: Connection) : DataAccessObject {
universe@167 41
universe@189 42 /**
universe@189 43 * Prepares the given [sql] statement and executes the [block] function with the prepared statement as receiver.
universe@189 44 * The statement is then closed properly.
universe@189 45 */
universe@189 46 private fun <R> withStatement(@Language("SQL") sql: String, block: PreparedStatement.() -> R) =
universe@189 47 connection.prepareStatement(sql).use(block)
universe@189 48
universe@189 49 /**
universe@189 50 * Prepares the given [sql] statement and executes the [block] function on that statement.
universe@189 51 * The statement is then closed properly.
universe@189 52 */
universe@189 53 private fun <R> useStatement(@Language("SQL") sql: String, block: (PreparedStatement) -> R) =
universe@189 54 connection.prepareStatement(sql).use(block)
universe@189 55
universe@189 56 /**
universe@189 57 * Executes the statement and iterates the whole result set extracting the rows with the given [extractor] function.
universe@189 58 */
universe@189 59 private fun <T> PreparedStatement.queryAll(extractor: (ResultSet) -> T): List<T> = executeQuery().use {
universe@189 60 sequence {
universe@189 61 while (it.next()) {
universe@189 62 yield(extractor(it))
universe@189 63 }
universe@189 64 }.toList()
universe@189 65 }
universe@189 66
universe@189 67 /**
universe@189 68 * Executes the statement and extracts a single row with the given [extractor] function.
universe@189 69 * If the result set is empty, null is returned.
universe@189 70 */
universe@189 71 private fun <T> PreparedStatement.querySingle(extractor: (ResultSet) -> T): T? = executeQuery().use {
universe@189 72 return if (it.next()) extractor(it) else null
universe@189 73 }
universe@189 74
universe@167 75 //<editor-fold desc="User">
universe@189 76 //language=SQL
universe@189 77 private val userQuery = "select userid, username, lastname, givenname, mail from lpit_user"
universe@189 78
universe@189 79 private fun ResultSet.extractUser() = User(getInt("userid")).apply {
universe@189 80 username = getString("username")
universe@189 81 givenname = getString("givenname")
universe@189 82 lastname = getString("lastname")
universe@189 83 mail = getString("mail")
universe@189 84 }
universe@189 85
universe@189 86 private fun ResultSet.containsUserInfo(): Boolean {
universe@189 87 getInt("userid")
universe@189 88 return !wasNull()
universe@189 89 }
universe@189 90
universe@189 91 private fun ResultSet.extractOptionalUser() = if (containsUserInfo()) extractUser() else null
universe@189 92
universe@189 93 override fun listUsers() =
universe@189 94 withStatement("$userQuery where userid > 0 order by username") {
universe@189 95 queryAll { it.extractUser() }
universe@189 96 }
universe@189 97
universe@189 98 override fun findUser(id: Int): User? =
universe@189 99 withStatement("$userQuery where userid = ?") {
universe@189 100 setInt(1, id)
universe@189 101 querySingle { it.extractUser() }
universe@189 102 }
universe@189 103
universe@189 104 override fun findUserByName(username: String): User? =
universe@189 105 withStatement("$userQuery where lower(username) = lower(?)") {
universe@189 106 setString(1, username)
universe@189 107 querySingle { it.extractUser() }
universe@189 108 }
universe@189 109
universe@189 110 override fun insertUser(user: User) {
universe@189 111 withStatement("insert into lpit_user (username, lastname, givenname, mail) values (?, ?, ?, ?)") {
universe@189 112 with(user) {
universe@189 113 setStringSafe(1, username)
universe@189 114 setStringOrNull(2, lastname)
universe@189 115 setStringOrNull(3, givenname)
universe@189 116 setStringOrNull(4, mail)
universe@167 117 }
universe@189 118 executeUpdate()
universe@167 119 }
universe@167 120 }
universe@167 121
universe@189 122 override fun updateUser(user: User) {
universe@189 123 withStatement("update lpit_user set lastname = ?, givenname = ?, mail = ? where userid = ?") {
universe@189 124 with(user) {
universe@189 125 setStringOrNull(1, lastname)
universe@189 126 setStringOrNull(2, givenname)
universe@189 127 setStringOrNull(3, mail)
universe@189 128 setInt(4, id)
universe@189 129 }
universe@189 130 executeUpdate()
universe@167 131 }
universe@167 132 }
universe@225 133 //</editor-fold>
universe@167 134
universe@167 135 //<editor-fold desc="Version">
universe@167 136 //language=SQL
universe@225 137 private val versionQuery = "select versionid, project, name, node, ordinal, status, release, eol from lpit_version"
universe@167 138
universe@189 139 private fun ResultSet.extractVersion() =
universe@189 140 Version(getInt("versionid"), getInt("project")).apply {
universe@189 141 name = getString("name")
universe@189 142 node = getString("node")
universe@189 143 ordinal = getInt("ordinal")
universe@225 144 release = getDate("release")
universe@225 145 eol = getDate("eol")
universe@189 146 status = getEnum("status")
universe@189 147 }
universe@189 148
universe@189 149 override fun listVersions(project: Project): List<Version> =
universe@189 150 withStatement("$versionQuery where project = ? order by ordinal desc, lower(name) desc") {
universe@189 151 setInt(1, project.id)
universe@189 152 queryAll { it.extractVersion() }
universe@189 153 }
universe@189 154
universe@189 155 override fun listVersionSummaries(project: Project): List<VersionSummary> =
universe@189 156 withStatement(
universe@231 157 """with
universe@231 158 version_map as (
universe@231 159 select issueid, status, resolved as versionid, true as isresolved from lpit_issue
universe@231 160 union all
universe@231 161 select issueid, status, affected as versionid, false as isresolved from lpit_issue
universe@231 162 ), issues as (
universe@231 163 select versionid, phase, isresolved, count(issueid) as total from version_map
universe@184 164 join lpit_issue_phases using (status)
universe@184 165 group by versionid, phase, isresolved
universe@184 166 ),
universe@184 167 summary as (
universe@184 168 select versionid, phase, isresolved, total
universe@184 169 from lpit_version v
universe@184 170 left join issues using (versionid)
universe@184 171 )
universe@225 172 select v.versionid, project, name, node, ordinal, status, release, eol,
universe@190 173 ro.total as resolved_open, ra.total as resolved_active, rd.total as resolved_done,
universe@190 174 ao.total as affected_open, aa.total as affected_active, ad.total as affected_done
universe@190 175 from lpit_version v
universe@190 176 left join summary ro on ro.versionid = v.versionid and ro.phase = 0 and ro.isresolved
universe@190 177 left join summary ra on ra.versionid = v.versionid and ra.phase = 1 and ra.isresolved
universe@190 178 left join summary rd on rd.versionid = v.versionid and rd.phase = 2 and rd.isresolved
universe@190 179 left join summary ao on ao.versionid = v.versionid and ao.phase = 0 and not ao.isresolved
universe@190 180 left join summary aa on aa.versionid = v.versionid and aa.phase = 1 and not aa.isresolved
universe@190 181 left join summary ad on ad.versionid = v.versionid and ad.phase = 2 and not ad.isresolved
universe@190 182 where v.project = ?
universe@203 183 order by ordinal desc, lower(name) desc
universe@189 184 """.trimIndent()
universe@189 185 ) {
universe@189 186 setInt(1, project.id)
universe@190 187 queryAll { rs ->
universe@190 188 VersionSummary(rs.extractVersion()).apply {
universe@190 189 reportedTotal.open = rs.getInt("affected_open")
universe@190 190 reportedTotal.active = rs.getInt("affected_active")
universe@190 191 reportedTotal.done = rs.getInt("affected_done")
universe@190 192 resolvedTotal.open = rs.getInt("resolved_open")
universe@190 193 resolvedTotal.active = rs.getInt("resolved_active")
universe@190 194 resolvedTotal.done = rs.getInt("resolved_done")
universe@190 195 }
universe@184 196 }
universe@189 197 }
universe@184 198
universe@189 199 override fun findVersion(id: Int): Version? =
universe@189 200 withStatement("$versionQuery where versionid = ?") {
universe@189 201 setInt(1, id)
universe@189 202 querySingle { it.extractVersion() }
universe@189 203 }
universe@167 204
universe@189 205 override fun findVersionByNode(project: Project, node: String): Version? =
universe@189 206 withStatement("$versionQuery where project = ? and node = ?") {
universe@189 207 setInt(1, project.id)
universe@189 208 setString(2, node)
universe@189 209 querySingle { it.extractVersion() }
universe@189 210 }
universe@167 211
universe@167 212 override fun insertVersion(version: Version) {
universe@225 213 withStatement("insert into lpit_version (name, node, ordinal, status, project, release, eol) values (?, ?, ?, ?::version_status, ?, ?, ?)") {
universe@189 214 with(version) {
universe@189 215 setStringSafe(1, name)
universe@189 216 setStringSafe(2, node)
universe@189 217 setInt(3, ordinal)
universe@189 218 setEnum(4, status)
universe@260 219 setInt(5, projectid)
universe@260 220 setDateOrNull(6, release)
universe@260 221 setDateOrNull(7, eol)
universe@189 222 }
universe@189 223 executeUpdate()
universe@189 224 }
universe@189 225
universe@167 226 }
universe@167 227
universe@167 228 override fun updateVersion(version: Version) {
universe@225 229 withStatement("update lpit_version set name = ?, node = ?, ordinal = ?, status = ?::version_status, release=?,eol=? where versionid = ?") {
universe@189 230 with(version) {
universe@189 231 setStringSafe(1, name)
universe@189 232 setStringSafe(2, node)
universe@189 233 setInt(3, ordinal)
universe@189 234 setEnum(4, status)
universe@225 235 setDateOrNull(5, version.release)
universe@225 236 setDateOrNull(6, version.eol)
universe@225 237 setInt(7, id)
universe@189 238 }
universe@189 239 executeUpdate()
universe@189 240 }
universe@167 241 }
universe@225 242 //</editor-fold>
universe@167 243
universe@167 244 //<editor-fold desc="Component">
universe@167 245 //language=SQL
universe@167 246 private val componentQuery =
universe@167 247 """
universe@227 248 select id, project, name, node, color, ordinal, description, active,
universe@167 249 userid, username, givenname, lastname, mail
universe@167 250 from lpit_component
universe@167 251 left join lpit_user on lead = userid
universe@189 252 """.trimIndent()
universe@167 253
universe@189 254 private fun ResultSet.extractComponent(): Component =
universe@189 255 Component(getInt("id"), getInt("project")).apply {
universe@189 256 name = getString("name")
universe@189 257 node = getString("node")
universe@189 258 color = try {
universe@189 259 WebColor(getString("color"))
universe@189 260 } catch (ex: IllegalArgumentException) {
universe@189 261 WebColor("000000")
universe@189 262 }
universe@189 263 ordinal = getInt("ordinal")
universe@189 264 description = getString("description")
universe@227 265 active = getBoolean("active")
universe@189 266 lead = extractOptionalUser()
universe@189 267 }
universe@189 268
universe@189 269 private fun PreparedStatement.setComponent(index: Int, component: Component): Int {
universe@189 270 with(component) {
universe@189 271 var i = index
universe@189 272 setStringSafe(i++, name)
universe@189 273 setStringSafe(i++, node)
universe@189 274 setStringSafe(i++, color.hex)
universe@189 275 setInt(i++, ordinal)
universe@189 276 setStringOrNull(i++, description)
universe@227 277 setBoolean(i++, active)
universe@189 278 setIntOrNull(i++, lead?.id)
universe@189 279 return i
universe@189 280 }
universe@167 281 }
universe@189 282
universe@189 283 override fun listComponents(project: Project): List<Component> =
universe@189 284 withStatement("$componentQuery where project = ? order by ordinal, lower(name)") {
universe@189 285 setInt(1, project.id)
universe@189 286 queryAll { it.extractComponent() }
universe@189 287 }
universe@189 288
universe@189 289 override fun listComponentSummaries(project: Project): List<ComponentSummary> =
universe@189 290 withStatement(
universe@184 291 """
universe@184 292 with issues as (
universe@184 293 select component, phase, count(issueid) as total
universe@184 294 from lpit_issue
universe@184 295 join lpit_issue_phases using (status)
universe@184 296 group by component, phase
universe@184 297 ),
universe@184 298 summary as (
universe@184 299 select c.id, phase, total
universe@184 300 from lpit_component c
universe@184 301 left join issues i on c.id = i.component
universe@184 302 )
universe@227 303 select c.id, project, name, node, color, ordinal, description, active,
universe@190 304 userid, username, givenname, lastname, mail,
universe@227 305 open.total as open, wip.total as wip, done.total as done
universe@184 306 from lpit_component c
universe@184 307 left join lpit_user on lead = userid
universe@190 308 left join summary open on c.id = open.id and open.phase = 0
universe@227 309 left join summary wip on c.id = wip.id and wip.phase = 1
universe@190 310 left join summary done on c.id = done.id and done.phase = 2
universe@190 311 where c.project = ?
universe@184 312 order by ordinal, name
universe@189 313 """.trimIndent()
universe@189 314 ) {
universe@189 315 setInt(1, project.id)
universe@190 316 queryAll { rs ->
universe@190 317 ComponentSummary(rs.extractComponent()).apply {
universe@190 318 issueSummary.open = rs.getInt("open")
universe@227 319 issueSummary.active = rs.getInt("wip")
universe@190 320 issueSummary.done = rs.getInt("done")
universe@190 321 }
universe@184 322 }
universe@189 323 }
universe@184 324
universe@189 325 override fun findComponent(id: Int): Component? =
universe@189 326 withStatement("$componentQuery where id = ?") {
universe@189 327 setInt(1, id)
universe@189 328 querySingle { it.extractComponent() }
universe@189 329 }
universe@167 330
universe@189 331 override fun findComponentByNode(project: Project, node: String): Component? =
universe@189 332 withStatement("$componentQuery where project = ? and node = ?") {
universe@189 333 setInt(1, project.id)
universe@189 334 setString(2, node)
universe@189 335 querySingle { it.extractComponent() }
universe@189 336 }
universe@167 337
universe@167 338 override fun insertComponent(component: Component) {
universe@227 339 withStatement("insert into lpit_component (name, node, color, ordinal, description, active, lead, project) values (?, ?, ?, ?, ?, ?, ?, ?)") {
universe@189 340 val col = setComponent(1, component)
universe@189 341 setInt(col, component.projectid)
universe@189 342 executeUpdate()
universe@189 343 }
universe@167 344 }
universe@167 345
universe@167 346 override fun updateComponent(component: Component) {
universe@227 347 withStatement("update lpit_component set name = ?, node = ?, color = ?, ordinal = ?, description = ?, active = ?, lead = ? where id = ?") {
universe@189 348 val col = setComponent(1, component)
universe@189 349 setInt(col, component.id)
universe@189 350 executeUpdate()
universe@167 351 }
universe@167 352 }
universe@167 353
universe@225 354 //</editor-fold>
universe@189 355
universe@225 356 //<editor-fold desc="Project">
universe@167 357
universe@167 358 //language=SQL
universe@167 359 private val projectQuery =
universe@167 360 """
universe@175 361 select projectid, name, node, ordinal, description, repourl,
universe@167 362 userid, username, lastname, givenname, mail
universe@167 363 from lpit_project
universe@167 364 left join lpit_user owner on lpit_project.owner = owner.userid
universe@189 365 """.trimIndent()
universe@167 366
universe@189 367 private fun ResultSet.extractProject() =
universe@189 368 Project(getInt("projectid")).apply {
universe@189 369 name = getString("name")
universe@189 370 node = getString("node")
universe@189 371 ordinal = getInt("ordinal")
universe@189 372 description = getString("description")
universe@189 373 repoUrl = getString("repourl")
universe@189 374 owner = extractOptionalUser()
universe@189 375 }
universe@189 376
universe@189 377 private fun PreparedStatement.setProject(index: Int, project: Project): Int {
universe@189 378 var i = index
universe@189 379 with(project) {
universe@189 380 setStringSafe(i++, name)
universe@189 381 setStringSafe(i++, node)
universe@189 382 setInt(i++, ordinal)
universe@189 383 setStringOrNull(i++, description)
universe@189 384 setStringOrNull(i++, repoUrl)
universe@189 385 setIntOrNull(i++, owner?.id)
universe@189 386 }
universe@189 387 return i
universe@167 388 }
universe@189 389
universe@189 390 override fun listProjects(): List<Project> =
universe@189 391 withStatement("$projectQuery order by ordinal, lower(name)") {
universe@189 392 queryAll { it.extractProject() }
universe@189 393 }
universe@189 394
universe@189 395 override fun findProject(id: Int): Project? =
universe@189 396 withStatement("$projectQuery where projectid = ?") {
universe@189 397 setInt(1, id)
universe@189 398 querySingle { it.extractProject() }
universe@189 399 }
universe@189 400
universe@189 401 override fun findProjectByNode(node: String): Project? =
universe@189 402 withStatement("$projectQuery where node = ?") {
universe@189 403 setString(1, node)
universe@189 404 querySingle { it.extractProject() }
universe@189 405 }
universe@189 406
universe@189 407 override fun insertProject(project: Project) {
universe@189 408 withStatement("insert into lpit_project (name, node, ordinal, description, repourl, owner) values (?, ?, ?, ?, ?, ?)") {
universe@189 409 setProject(1, project)
universe@189 410 executeUpdate()
universe@189 411 }
universe@167 412 }
universe@189 413
universe@189 414 override fun updateProject(project: Project) {
universe@189 415 withStatement("update lpit_project set name = ?, node = ?, ordinal = ?, description = ?, repourl = ?, owner = ? where projectid = ?") {
universe@189 416 val col = setProject(1, project)
universe@189 417 setInt(col, project.id)
universe@189 418 executeUpdate()
universe@189 419 }
universe@167 420 }
universe@189 421
universe@189 422 override fun collectIssueSummary(project: Project): IssueSummary =
universe@189 423 withStatement(
universe@167 424 """
universe@167 425 select phase, count(*) as total
universe@167 426 from lpit_issue
universe@167 427 join lpit_issue_phases using(status)
universe@167 428 where project = ?
universe@167 429 group by phase
universe@189 430 """.trimIndent()
universe@189 431 ) {
universe@189 432 setInt(1, project.id)
universe@189 433 executeQuery().use {
universe@189 434 val summary = IssueSummary()
universe@189 435 while (it.next()) {
universe@189 436 val phase = it.getInt("phase")
universe@189 437 val total = it.getInt("total")
universe@189 438 when (phase) {
universe@189 439 0 -> summary.open = total
universe@189 440 1 -> summary.active = total
universe@189 441 2 -> summary.done = total
universe@189 442 }
universe@167 443 }
universe@189 444 summary
universe@167 445 }
universe@167 446 }
universe@167 447
universe@257 448 override fun collectIssueSummary(assignee: User): IssueSummary =
universe@257 449 withStatement(
universe@257 450 """
universe@257 451 select phase, count(*) as total
universe@257 452 from lpit_issue
universe@257 453 join lpit_issue_phases using(status)
universe@257 454 where assignee = ?
universe@257 455 group by phase
universe@257 456 """.trimIndent()
universe@257 457 ) {
universe@257 458 setInt(1, assignee.id)
universe@257 459 executeQuery().use {
universe@257 460 val summary = IssueSummary()
universe@257 461 while (it.next()) {
universe@257 462 val phase = it.getInt("phase")
universe@257 463 val total = it.getInt("total")
universe@257 464 when (phase) {
universe@257 465 0 -> summary.open = total
universe@257 466 1 -> summary.active = total
universe@257 467 2 -> summary.done = total
universe@257 468 }
universe@257 469 }
universe@257 470 summary
universe@257 471 }
universe@257 472 }
universe@257 473
universe@225 474 //</editor-fold>
universe@189 475
universe@225 476 //<editor-fold desc="Issue">
universe@167 477
universe@167 478 //language=SQL
universe@167 479 private val issueQuery =
universe@167 480 """
universe@167 481 select issueid,
universe@167 482 i.project, p.name as projectname, p.node as projectnode,
universe@167 483 component, c.name as componentname, c.node as componentnode,
universe@268 484 status, phase, category, subject, i.description,
universe@167 485 userid, username, givenname, lastname, mail,
universe@231 486 created, updated, eta, affected, resolved
universe@167 487 from lpit_issue i
universe@167 488 join lpit_project p on i.project = projectid
universe@268 489 join lpit_issue_phases using (status)
universe@167 490 left join lpit_component c on component = c.id
universe@167 491 left join lpit_user on userid = assignee
universe@189 492 """.trimIndent()
universe@167 493
universe@189 494 private fun ResultSet.extractIssue(): Issue {
universe@189 495 val proj = Project(getInt("project")).apply {
universe@189 496 name = getString("projectname")
universe@189 497 node = getString("projectnode")
universe@189 498 }
universe@189 499 val comp = getInt("component").let {
universe@189 500 if (wasNull()) null else
universe@189 501 Component(it, proj.id).apply {
universe@189 502 name = getString("componentname")
universe@189 503 node = getString("componentnode")
universe@189 504 }
universe@189 505 }
universe@189 506 val issue = Issue(getInt("issueid"), proj).apply {
universe@189 507 component = comp
universe@189 508 status = getEnum("status")
universe@189 509 category = getEnum("category")
universe@189 510 subject = getString("subject")
universe@189 511 description = getString("description")
universe@189 512 assignee = extractOptionalUser()
universe@189 513 created = getTimestamp("created")
universe@189 514 updated = getTimestamp("updated")
universe@189 515 eta = getDate("eta")
universe@231 516 affected = getInt("affected").takeIf { it > 0 }?.let { findVersion(it) }
universe@231 517 resolved = getInt("resolved").takeIf { it > 0 }?.let { findVersion(it) }
universe@189 518 }
universe@189 519
universe@189 520 return issue
universe@167 521 }
universe@167 522
universe@189 523 private fun PreparedStatement.setIssue(index: Int, issue: Issue): Int {
universe@189 524 var i = index
universe@189 525 with(issue) {
universe@189 526 setIntOrNull(i++, component?.id)
universe@189 527 setEnum(i++, status)
universe@189 528 setEnum(i++, category)
universe@189 529 setStringSafe(i++, subject)
universe@189 530 setStringOrNull(i++, description)
universe@189 531 setIntOrNull(i++, assignee?.id)
universe@189 532 setDateOrNull(i++, eta)
universe@231 533 setIntOrNull(i++, affected?.id)
universe@231 534 setIntOrNull(i++, resolved?.id)
universe@189 535 }
universe@189 536 return i
universe@167 537 }
universe@167 538
universe@268 539 override fun listIssues(project: Project, includeDone: Boolean): List<Issue> =
universe@268 540 withStatement("$issueQuery where i.project = ? and (? or phase < 2)") {
universe@263 541 setInt(1, project.id)
universe@268 542 setBoolean(2, includeDone)
universe@263 543 queryAll { it.extractIssue() }
universe@263 544 }
universe@263 545
universe@268 546 override fun listIssues(project: Project, includeDone: Boolean, version: Version?, component: Component?): List<Issue> =
universe@189 547 withStatement(
universe@268 548 """$issueQuery where i.project = ? and
universe@268 549 (? or phase < 2) and
universe@231 550 (not ? or ? in (resolved, affected)) and (not ? or (resolved is null and affected is null)) and
universe@183 551 (not ? or component = ?) and (not ? or component is null)
universe@189 552 """.trimIndent()
universe@189 553 ) {
universe@248 554 fun <T : Entity> applyFilter(search: T?, fflag: Int, nflag: Int, idcol: Int) {
universe@248 555 if (search == null) {
universe@248 556 setBoolean(fflag, false)
universe@248 557 setBoolean(nflag, false)
universe@248 558 setInt(idcol, 0)
universe@248 559 } else {
universe@248 560 setBoolean(fflag, true)
universe@248 561 setBoolean(nflag, false)
universe@248 562 setInt(idcol, search.id)
universe@189 563 }
universe@189 564 }
universe@263 565 setInt(1, project.id)
universe@268 566 setBoolean(2, includeDone)
universe@268 567 applyFilter(version, 3, 5, 4)
universe@268 568 applyFilter(component, 6, 8, 7)
universe@167 569
universe@189 570 queryAll { it.extractIssue() }
universe@189 571 }
universe@167 572
universe@189 573 override fun findIssue(id: Int): Issue? =
universe@189 574 withStatement("$issueQuery where issueid = ?") {
universe@189 575 setInt(1, id)
universe@189 576 querySingle { it.extractIssue() }
universe@189 577 }
universe@189 578
universe@189 579 override fun insertIssue(issue: Issue): Int {
universe@189 580 val id = withStatement(
universe@167 581 """
universe@231 582 insert into lpit_issue (component, status, category, subject, description, assignee, eta, affected, resolved, project)
universe@232 583 values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?, ?, ?, ?)
universe@167 584 returning issueid
universe@189 585 """.trimIndent()
universe@189 586 ) {
universe@189 587 val col = setIssue(1, issue)
universe@189 588 setInt(col, issue.project.id)
universe@189 589 querySingle { it.getInt(1) }!!
universe@167 590 }
universe@184 591 return id
universe@167 592 }
universe@167 593
universe@167 594 override fun updateIssue(issue: Issue) {
universe@189 595 withStatement(
universe@189 596 """
universe@189 597 update lpit_issue set updated = now(),
universe@189 598 component = ?, status = ?::issue_status, category = ?::issue_category, subject = ?,
universe@231 599 description = ?, assignee = ?, eta = ?, affected = ?, resolved = ?
universe@189 600 where issueid = ?
universe@189 601 """.trimIndent()
universe@189 602 ) {
universe@189 603 val col = setIssue(1, issue)
universe@189 604 setInt(col, issue.id)
universe@189 605 executeUpdate()
universe@189 606 }
universe@167 607 }
universe@167 608
universe@232 609 override fun insertHistoryEvent(issue: Issue, newId: Int) {
universe@232 610 val type = if (newId > 0) IssueHistoryType.New else IssueHistoryType.Update
universe@232 611 val issueid = if (newId > 0) newId else issue.id
universe@232 612
universe@232 613 val eventid =
universe@242 614 withStatement("insert into lpit_issue_history_event(issueid, subject, type) values (?,?,?::issue_history_event) returning eventid") {
universe@232 615 setInt(1, issueid)
universe@242 616 setString(2, issue.subject)
universe@242 617 setEnum(3, type)
universe@232 618 querySingle { it.getInt(1) }!!
universe@232 619 }
universe@232 620 withStatement(
universe@232 621 """
universe@242 622 insert into lpit_issue_history_data (component, status, category, description, assignee, eta, affected, resolved, eventid)
universe@242 623 values (?, ?::issue_status, ?::issue_category, ?, ?, ?, ?, ?, ?)
universe@232 624 """.trimIndent()
universe@232 625 ) {
universe@232 626 setStringOrNull(1, issue.component?.name)
universe@232 627 setEnum(2, issue.status)
universe@232 628 setEnum(3, issue.category)
universe@242 629 setStringOrNull(4, issue.description)
universe@242 630 setStringOrNull(5, issue.assignee?.shortDisplayname)
universe@242 631 setDateOrNull(6, issue.eta)
universe@242 632 setStringOrNull(7, issue.affected?.name)
universe@242 633 setStringOrNull(8, issue.resolved?.name)
universe@242 634 setInt(9, eventid)
universe@232 635 executeUpdate()
universe@232 636 }
universe@232 637 }
universe@232 638
universe@225 639 //</editor-fold>
universe@167 640
universe@263 641 //<editor-fold desc="Issue Relations">
universe@263 642 override fun insertIssueRelation(rel: IssueRelation) {
universe@263 643 withStatement(
universe@263 644 """
universe@263 645 insert into lpit_issue_relation (from_issue, to_issue, type)
universe@263 646 values (?, ?, ?::relation_type)
universe@263 647 on conflict do nothing
universe@263 648 """.trimIndent()
universe@263 649 ) {
universe@263 650 if (rel.reverse) {
universe@263 651 setInt(2, rel.from.id)
universe@263 652 setInt(1, rel.to.id)
universe@263 653 } else {
universe@263 654 setInt(1, rel.from.id)
universe@263 655 setInt(2, rel.to.id)
universe@263 656 }
universe@263 657 setEnum(3, rel.type)
universe@263 658 executeUpdate()
universe@263 659 }
universe@263 660 }
universe@263 661
universe@263 662 override fun deleteIssueRelation(rel: IssueRelation) {
universe@263 663 withStatement("delete from lpit_issue_relation where from_issue = ? and to_issue = ? and type=?::relation_type") {
universe@263 664 if (rel.reverse) {
universe@263 665 setInt(2, rel.from.id)
universe@263 666 setInt(1, rel.to.id)
universe@263 667 } else {
universe@263 668 setInt(1, rel.from.id)
universe@263 669 setInt(2, rel.to.id)
universe@263 670 }
universe@263 671 setEnum(3, rel.type)
universe@263 672 executeUpdate()
universe@263 673 }
universe@263 674 }
universe@263 675
universe@263 676 override fun listIssueRelations(issue: Issue): List<IssueRelation> = buildList {
universe@263 677 withStatement("select to_issue, type from lpit_issue_relation where from_issue = ?") {
universe@263 678 setInt(1, issue.id)
universe@263 679 queryAll { IssueRelation(issue, findIssue(it.getInt("to_issue"))!!, it.getEnum("type"), false) }
universe@263 680 }.forEach(this::add)
universe@263 681 withStatement("select from_issue, type from lpit_issue_relation where to_issue = ?") {
universe@263 682 setInt(1, issue.id)
universe@263 683 queryAll { IssueRelation(issue, findIssue(it.getInt("from_issue"))!!, it.getEnum("type"), true) }
universe@263 684 }.forEach(this::add)
universe@263 685 }
universe@268 686
universe@268 687 override fun getIssueRelationMap(project: Project, includeDone: Boolean): IssueRelationMap =
universe@268 688 withStatement(
universe@268 689 """
universe@268 690 select r.from_issue, r.to_issue, r.type
universe@268 691 from lpit_issue_relation r
universe@268 692 join lpit_issue i on i.issueid = r.from_issue
universe@268 693 join lpit_issue_phases p on i.status = p.status
universe@268 694 where i.project = ? and (? or p.phase < 2)
universe@268 695 """.trimIndent()
universe@268 696 ) {
universe@268 697 setInt(1, project.id)
universe@268 698 setBoolean(2, includeDone)
universe@268 699 queryAll { Pair(it.getInt("from_issue"), Pair(it.getInt("to_issue"), it.getEnum<RelationType>("type"))) }
universe@268 700 }.groupBy({it.first},{it.second})
universe@263 701 //</editor-fold>
universe@263 702
universe@225 703 //<editor-fold desc="IssueComment">
universe@167 704
universe@189 705 private fun ResultSet.extractIssueComment() =
universe@189 706 IssueComment(getInt("commentid"), getInt("issueid")).apply {
universe@189 707 created = getTimestamp("created")
universe@189 708 updated = getTimestamp("updated")
universe@189 709 updateCount = getInt("updatecount")
universe@189 710 comment = getString("comment")
universe@189 711 author = extractOptionalUser()
universe@189 712 }
universe@189 713
universe@189 714 override fun listComments(issue: Issue): List<IssueComment> =
universe@189 715 withStatement("select * from lpit_issue_comment left join lpit_user using (userid) where issueid = ? order by created") {
universe@189 716 setInt(1, issue.id)
universe@189 717 queryAll { it.extractIssueComment() }
universe@189 718 }
universe@189 719
universe@207 720 override fun findComment(id: Int): IssueComment? =
universe@207 721 withStatement("select * from lpit_issue_comment left join lpit_user using (userid) where commentid = ?") {
universe@207 722 setInt(1, id)
universe@207 723 querySingle { it.extractIssueComment() }
universe@207 724 }
universe@207 725
universe@232 726 override fun insertComment(issueComment: IssueComment): Int =
universe@189 727 useStatement("update lpit_issue set updated = now() where issueid = ?") { updateIssueDate ->
universe@232 728 withStatement("insert into lpit_issue_comment (issueid, comment, userid) values (?, ? ,?) returning commentid") {
universe@189 729 with(issueComment) {
universe@189 730 updateIssueDate.setInt(1, issueid)
universe@189 731 setInt(1, issueid)
universe@189 732 setStringSafe(2, comment)
universe@189 733 setIntOrNull(3, author?.id)
universe@189 734 }
universe@232 735 val commentid = querySingle { it.getInt(1) }!!
universe@189 736 updateIssueDate.executeUpdate()
universe@232 737 commentid
universe@167 738 }
universe@167 739 }
universe@207 740
universe@207 741 override fun updateComment(issueComment: IssueComment) {
universe@207 742 useStatement("update lpit_issue set updated = now() where issueid = ?") { updateIssueDate ->
universe@207 743 withStatement("update lpit_issue_comment set comment = ?, updatecount = updatecount + 1, updated = now() where commentid = ?") {
universe@207 744 with(issueComment) {
universe@207 745 updateIssueDate.setInt(1, issueid)
universe@207 746 setStringSafe(1, comment)
universe@207 747 setInt(2, id)
universe@207 748 }
universe@207 749 executeUpdate()
universe@207 750 updateIssueDate.executeUpdate()
universe@207 751 }
universe@207 752 }
universe@207 753 }
universe@232 754
universe@232 755
universe@242 756 override fun insertHistoryEvent(issue: Issue, issueComment: IssueComment, newId: Int) {
universe@232 757 val type = if (newId > 0) IssueHistoryType.NewComment else IssueHistoryType.UpdateComment
universe@232 758 val commentid = if (newId > 0) newId else issueComment.id
universe@232 759
universe@232 760 val eventid =
universe@242 761 withStatement("insert into lpit_issue_history_event(issueid, subject, type) values (?,?,?::issue_history_event) returning eventid") {
universe@232 762 setInt(1, issueComment.issueid)
universe@244 763 setString(2, issue.subject)
universe@242 764 setEnum(3, type)
universe@232 765 querySingle { it.getInt(1) }!!
universe@232 766 }
universe@245 767 withStatement("insert into lpit_issue_comment_history (commentid, eventid, comment) values (?,?,?)") {
universe@232 768 setInt(1, commentid)
universe@232 769 setInt(2, eventid)
universe@232 770 setString(3, issueComment.comment)
universe@232 771 executeUpdate()
universe@232 772 }
universe@232 773 }
universe@232 774
universe@225 775 //</editor-fold>
universe@235 776
universe@235 777 //<editor-fold desc="Issue History">
universe@235 778
universe@235 779 override fun listIssueHistory(projectId: Int, days: Int) =
universe@235 780 withStatement(
universe@235 781 """
universe@241 782 select u.username as current_assignee, evt.*, evtdata.*
universe@235 783 from lpit_issue_history_event evt
universe@241 784 join lpit_issue issue using (issueid)
universe@241 785 left join lpit_user u on u.userid = issue.assignee
universe@235 786 join lpit_issue_history_data evtdata using (eventid)
universe@235 787 where project = ?
universe@235 788 and time > now() - (? * interval '1' day)
universe@235 789 order by time desc
universe@235 790 """.trimIndent()
universe@235 791 ) {
universe@235 792 setInt(1, projectId)
universe@235 793 setInt(2, days)
universe@235 794 queryAll { rs->
universe@235 795 with(rs) {
universe@235 796 IssueHistoryEntry(
universe@242 797 subject = getString("subject"),
universe@242 798 time = getTimestamp("time"),
universe@242 799 type = getEnum("type"),
universe@242 800 currentAssignee = getString("current_assignee"),
universe@242 801 issueid = getInt("issueid"),
universe@242 802 component = getString("component") ?: "",
universe@242 803 status = getEnum("status"),
universe@242 804 category = getEnum("category"),
universe@242 805 description = getString("description") ?: "",
universe@242 806 assignee = getString("assignee") ?: "",
universe@242 807 eta = getDate("eta"),
universe@242 808 affected = getString("affected") ?: "",
universe@242 809 resolved = getString("resolved") ?: ""
universe@235 810 )
universe@235 811 }
universe@235 812 }
universe@235 813 }
universe@235 814
universe@241 815 override fun listIssueCommentHistory(projectId: Int, days: Int) =
universe@241 816 withStatement(
universe@241 817 """
universe@241 818 select u.username as current_assignee, evt.*, evtdata.*
universe@241 819 from lpit_issue_history_event evt
universe@241 820 join lpit_issue issue using (issueid)
universe@241 821 left join lpit_user u on u.userid = issue.assignee
universe@241 822 join lpit_issue_comment_history evtdata using (eventid)
universe@241 823 where project = ?
universe@241 824 and time > now() - (? * interval '1' day)
universe@241 825 order by time desc
universe@241 826 """.trimIndent()
universe@241 827 ) {
universe@241 828 setInt(1, projectId)
universe@241 829 setInt(2, days)
universe@241 830 queryAll { rs->
universe@241 831 with(rs) {
universe@241 832 IssueCommentHistoryEntry(
universe@242 833 subject = getString("subject"),
universe@242 834 time = getTimestamp("time"),
universe@242 835 type = getEnum("type"),
universe@242 836 currentAssignee = getString("current_assignee"),
universe@242 837 issueid = getInt("issueid"),
universe@242 838 commentid = getInt("commentid"),
universe@242 839 comment = getString("comment")
universe@241 840 )
universe@241 841 }
universe@241 842 }
universe@241 843 }
universe@241 844
universe@235 845 //</editor-fold>
universe@167 846 }

mercurial