src/main/kotlin/de/uapcore/lightpit/dao/postgres/PGVersionDao.kt

changeset 159
86b5d8a1662f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/kotlin/de/uapcore/lightpit/dao/postgres/PGVersionDao.kt	Thu Nov 19 13:58:54 2020 +0100
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2020 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+package de.uapcore.lightpit.dao.postgres
+
+import de.uapcore.lightpit.dao.AbstractVersionDao
+import de.uapcore.lightpit.entities.Project
+import de.uapcore.lightpit.entities.Version
+import de.uapcore.lightpit.entities.VersionStatus
+import java.sql.Connection
+import java.sql.PreparedStatement
+import java.sql.ResultSet
+
+class PGVersionDao(connection: Connection) : AbstractVersionDao() {
+
+    companion object {
+        fun mapResult(rs: ResultSet): Version {
+            val id = rs.getInt("versionid")
+            return if (rs.wasNull()) {
+                Version(-1)
+            } else {
+                val version = Version(id)
+                version.name = rs.getString("name")
+                version.node = rs.getString("node")
+                version.ordinal = rs.getInt("ordinal")
+                version.status = VersionStatus.valueOf(rs.getString("status"))
+                version
+            }
+        }
+    }
+
+    private val query = "select versionid, project, name, node, ordinal, status from lpit_version"
+    private val listStmt = connection.prepareStatement(query + " where project = ? " +
+            "order by ordinal desc, lower(name) desc")
+    private val findStmt = connection.prepareStatement("$query where versionid = ?")
+    private val findByNodeStmt = connection.prepareStatement("$query where project = ? and node = ?")
+    private val insertStmt = connection.prepareStatement(
+            "insert into lpit_version (name, node, ordinal, status, project) values (?, ?, ?, ?::version_status, ?)"
+    )
+    private val updateStmt = connection.prepareStatement(
+            "update lpit_version set name = ?, node = ?, ordinal = ?, status = ?::version_status where versionid = ?"
+    )
+
+    override fun mapResult(rs: ResultSet): Version = Companion.mapResult(rs)
+
+    private fun setFields(stmt: PreparedStatement, instance: Version): Int {
+        var column = 0
+        stmt.setString(++column, instance.name)
+        stmt.setString(++column, instance.node)
+        stmt.setInt(++column, instance.ordinal)
+        stmt.setString(++column, instance.status.name)
+        return column
+    }
+
+    override fun save(instance: Version, parent: Project) {
+        var column = setFields(insertStmt, instance)
+        insertStmt.setInt(++column, parent.id)
+        insertStmt.executeUpdate()
+    }
+
+    override fun update(instance: Version): Boolean {
+        var column = setFields(updateStmt, instance)
+        updateStmt.setInt(++column, instance.id)
+        return updateStmt.executeUpdate() > 0
+    }
+
+    override fun list(parent: Project): List<Version> {
+        listStmt.setInt(1, parent.id)
+        return super.list(listStmt)
+    }
+
+    override fun find(id: Int): Version? {
+        findStmt.setInt(1, id)
+        return super.find(findStmt)
+    }
+
+    override fun findByNode(parent: Project, node: String): Version? {
+        findByNodeStmt.setInt(1, parent.id)
+        findByNodeStmt.setString(2, node)
+        return super.find(findByNodeStmt)
+    }
+}
\ No newline at end of file

mercurial