Sun, 20 Dec 2020 11:06:25 +0100
minimize footprint of flexmark - fixes #116
/* * 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.AbstractComponentDao import de.uapcore.lightpit.dao.Functions import de.uapcore.lightpit.entities.Component import de.uapcore.lightpit.entities.Project import de.uapcore.lightpit.entities.User import de.uapcore.lightpit.types.WebColor import java.sql.Connection import java.sql.PreparedStatement import java.sql.ResultSet class PGComponentDao(connection: Connection) : AbstractComponentDao() { private val query = "select id, name, node, color, ordinal, description, " + "userid, username, givenname, lastname, mail " + "from lpit_component " + "left join lpit_user on lead = userid" private val listStmt = connection.prepareStatement("$query where project = ? order by ordinal, lower(name)") private val findStmt = connection.prepareStatement("$query where id = ? ") private val findByNodeStmt = connection.prepareStatement("$query where project = ? and node = ?") private val insertStmt = connection.prepareStatement( "insert into lpit_component (name, node, color, ordinal, description, lead, project) values (?, ?, ?, ?, ?, ?, ?)" ) private val updateStmt = connection.prepareStatement( "update lpit_component set name = ?, node = ?, color = ?, ordinal = ?, description = ?, lead = ? where id = ?" ) override fun mapResult(rs: ResultSet): Component { val component = Component(rs.getInt("id")) component.name = rs.getString("name") component.node = rs.getString("node") component.color = try { WebColor(rs.getString("color")) } catch (ex: IllegalArgumentException) { WebColor("000000") } component.ordinal = rs.getInt("ordinal") component.description = rs.getString("description") component.lead = PGUserDao.mapResult(rs).takeUnless { rs.wasNull() } return component } private fun setColumns(stmt: PreparedStatement, instance: Component): Int { var column = 0 stmt.setString(++column, instance.name) stmt.setString(++column, instance.node) stmt.setString(++column, instance.color.hex) stmt.setInt(++column, instance.ordinal) Functions.setStringOrNull(stmt, ++column, instance.description) setForeignKeyOrNull(stmt, ++column, instance.lead, User::id) return column } override fun save(instance: Component, parent: Project) { var column = setColumns(insertStmt, instance) insertStmt.setInt(++column, parent.id) insertStmt.executeUpdate() } override fun update(instance: Component): Boolean { var column = setColumns(updateStmt, instance) updateStmt.setInt(++column, instance.id) return updateStmt.executeUpdate() > 0 } override fun list(parent: Project): List<Component> { listStmt.setInt(1, parent.id) return super.list(listStmt) } override fun find(id: Int): Component? { findStmt.setInt(1, id) return super.find(findStmt) } override fun findByNode(parent: Project, node: String): Component? { findByNodeStmt.setInt(1, parent.id) findByNodeStmt.setString(2, node) return super.find(findByNodeStmt) } }