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

Thu, 19 Nov 2020 13:58:54 +0100

author
mike@uapl01.localdomain
date
Thu, 19 Nov 2020 13:58:54 +0100
changeset 159
86b5d8a1662f
permissions
-rw-r--r--

migrates DAO classes

     1 /*
     2  * Copyright 2020 Mike Becker. All rights reserved.
     3  *
     4  * Redistribution and use in source and binary forms, with or without
     5  * modification, are permitted provided that the following conditions are met:
     6  *
     7  * 1. Redistributions of source code must retain the above copyright
     8  * notice, this list of conditions and the following disclaimer.
     9  *
    10  * 2. Redistributions in binary form must reproduce the above copyright
    11  * notice, this list of conditions and the following disclaimer in the
    12  * documentation and/or other materials provided with the distribution.
    13  *
    14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    21  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    24  *
    25  */
    27 package de.uapcore.lightpit.dao.postgres
    29 import de.uapcore.lightpit.dao.AbstractVersionDao
    30 import de.uapcore.lightpit.entities.Project
    31 import de.uapcore.lightpit.entities.Version
    32 import de.uapcore.lightpit.entities.VersionStatus
    33 import java.sql.Connection
    34 import java.sql.PreparedStatement
    35 import java.sql.ResultSet
    37 class PGVersionDao(connection: Connection) : AbstractVersionDao() {
    39     companion object {
    40         fun mapResult(rs: ResultSet): Version {
    41             val id = rs.getInt("versionid")
    42             return if (rs.wasNull()) {
    43                 Version(-1)
    44             } else {
    45                 val version = Version(id)
    46                 version.name = rs.getString("name")
    47                 version.node = rs.getString("node")
    48                 version.ordinal = rs.getInt("ordinal")
    49                 version.status = VersionStatus.valueOf(rs.getString("status"))
    50                 version
    51             }
    52         }
    53     }
    55     private val query = "select versionid, project, name, node, ordinal, status from lpit_version"
    56     private val listStmt = connection.prepareStatement(query + " where project = ? " +
    57             "order by ordinal desc, lower(name) desc")
    58     private val findStmt = connection.prepareStatement("$query where versionid = ?")
    59     private val findByNodeStmt = connection.prepareStatement("$query where project = ? and node = ?")
    60     private val insertStmt = connection.prepareStatement(
    61             "insert into lpit_version (name, node, ordinal, status, project) values (?, ?, ?, ?::version_status, ?)"
    62     )
    63     private val updateStmt = connection.prepareStatement(
    64             "update lpit_version set name = ?, node = ?, ordinal = ?, status = ?::version_status where versionid = ?"
    65     )
    67     override fun mapResult(rs: ResultSet): Version = Companion.mapResult(rs)
    69     private fun setFields(stmt: PreparedStatement, instance: Version): Int {
    70         var column = 0
    71         stmt.setString(++column, instance.name)
    72         stmt.setString(++column, instance.node)
    73         stmt.setInt(++column, instance.ordinal)
    74         stmt.setString(++column, instance.status.name)
    75         return column
    76     }
    78     override fun save(instance: Version, parent: Project) {
    79         var column = setFields(insertStmt, instance)
    80         insertStmt.setInt(++column, parent.id)
    81         insertStmt.executeUpdate()
    82     }
    84     override fun update(instance: Version): Boolean {
    85         var column = setFields(updateStmt, instance)
    86         updateStmt.setInt(++column, instance.id)
    87         return updateStmt.executeUpdate() > 0
    88     }
    90     override fun list(parent: Project): List<Version> {
    91         listStmt.setInt(1, parent.id)
    92         return super.list(listStmt)
    93     }
    95     override fun find(id: Int): Version? {
    96         findStmt.setInt(1, id)
    97         return super.find(findStmt)
    98     }
   100     override fun findByNode(parent: Project, node: String): Version? {
   101         findByNodeStmt.setInt(1, parent.id)
   102         findByNodeStmt.setString(2, node)
   103         return super.find(findByNodeStmt)
   104     }
   105 }

mercurial