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

changeset 159
86b5d8a1662f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main/kotlin/de/uapcore/lightpit/dao/postgres/PGVersionDao.kt	Thu Nov 19 13:58:54 2020 +0100
     1.3 @@ -0,0 +1,105 @@
     1.4 +/*
     1.5 + * Copyright 2020 Mike Becker. All rights reserved.
     1.6 + *
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions are met:
     1.9 + *
    1.10 + * 1. Redistributions of source code must retain the above copyright
    1.11 + * notice, this list of conditions and the following disclaimer.
    1.12 + *
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + * notice, this list of conditions and the following disclaimer in the
    1.15 + * documentation and/or other materials provided with the distribution.
    1.16 + *
    1.17 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.18 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.19 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    1.20 + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    1.21 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.22 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.23 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    1.24 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    1.25 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.26 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.27 + *
    1.28 + */
    1.29 +
    1.30 +package de.uapcore.lightpit.dao.postgres
    1.31 +
    1.32 +import de.uapcore.lightpit.dao.AbstractVersionDao
    1.33 +import de.uapcore.lightpit.entities.Project
    1.34 +import de.uapcore.lightpit.entities.Version
    1.35 +import de.uapcore.lightpit.entities.VersionStatus
    1.36 +import java.sql.Connection
    1.37 +import java.sql.PreparedStatement
    1.38 +import java.sql.ResultSet
    1.39 +
    1.40 +class PGVersionDao(connection: Connection) : AbstractVersionDao() {
    1.41 +
    1.42 +    companion object {
    1.43 +        fun mapResult(rs: ResultSet): Version {
    1.44 +            val id = rs.getInt("versionid")
    1.45 +            return if (rs.wasNull()) {
    1.46 +                Version(-1)
    1.47 +            } else {
    1.48 +                val version = Version(id)
    1.49 +                version.name = rs.getString("name")
    1.50 +                version.node = rs.getString("node")
    1.51 +                version.ordinal = rs.getInt("ordinal")
    1.52 +                version.status = VersionStatus.valueOf(rs.getString("status"))
    1.53 +                version
    1.54 +            }
    1.55 +        }
    1.56 +    }
    1.57 +
    1.58 +    private val query = "select versionid, project, name, node, ordinal, status from lpit_version"
    1.59 +    private val listStmt = connection.prepareStatement(query + " where project = ? " +
    1.60 +            "order by ordinal desc, lower(name) desc")
    1.61 +    private val findStmt = connection.prepareStatement("$query where versionid = ?")
    1.62 +    private val findByNodeStmt = connection.prepareStatement("$query where project = ? and node = ?")
    1.63 +    private val insertStmt = connection.prepareStatement(
    1.64 +            "insert into lpit_version (name, node, ordinal, status, project) values (?, ?, ?, ?::version_status, ?)"
    1.65 +    )
    1.66 +    private val updateStmt = connection.prepareStatement(
    1.67 +            "update lpit_version set name = ?, node = ?, ordinal = ?, status = ?::version_status where versionid = ?"
    1.68 +    )
    1.69 +
    1.70 +    override fun mapResult(rs: ResultSet): Version = Companion.mapResult(rs)
    1.71 +
    1.72 +    private fun setFields(stmt: PreparedStatement, instance: Version): Int {
    1.73 +        var column = 0
    1.74 +        stmt.setString(++column, instance.name)
    1.75 +        stmt.setString(++column, instance.node)
    1.76 +        stmt.setInt(++column, instance.ordinal)
    1.77 +        stmt.setString(++column, instance.status.name)
    1.78 +        return column
    1.79 +    }
    1.80 +
    1.81 +    override fun save(instance: Version, parent: Project) {
    1.82 +        var column = setFields(insertStmt, instance)
    1.83 +        insertStmt.setInt(++column, parent.id)
    1.84 +        insertStmt.executeUpdate()
    1.85 +    }
    1.86 +
    1.87 +    override fun update(instance: Version): Boolean {
    1.88 +        var column = setFields(updateStmt, instance)
    1.89 +        updateStmt.setInt(++column, instance.id)
    1.90 +        return updateStmt.executeUpdate() > 0
    1.91 +    }
    1.92 +
    1.93 +    override fun list(parent: Project): List<Version> {
    1.94 +        listStmt.setInt(1, parent.id)
    1.95 +        return super.list(listStmt)
    1.96 +    }
    1.97 +
    1.98 +    override fun find(id: Int): Version? {
    1.99 +        findStmt.setInt(1, id)
   1.100 +        return super.find(findStmt)
   1.101 +    }
   1.102 +
   1.103 +    override fun findByNode(parent: Project, node: String): Version? {
   1.104 +        findByNodeStmt.setInt(1, parent.id)
   1.105 +        findByNodeStmt.setString(2, node)
   1.106 +        return super.find(findByNodeStmt)
   1.107 +    }
   1.108 +}
   1.109 \ No newline at end of file

mercurial