universe@167: /* universe@180: * Copyright 2021 Mike Becker. All rights reserved. universe@167: * universe@167: * Redistribution and use in source and binary forms, with or without universe@167: * modification, are permitted provided that the following conditions are met: universe@167: * universe@167: * 1. Redistributions of source code must retain the above copyright universe@167: * notice, this list of conditions and the following disclaimer. universe@167: * universe@167: * 2. Redistributions in binary form must reproduce the above copyright universe@167: * notice, this list of conditions and the following disclaimer in the universe@167: * documentation and/or other materials provided with the distribution. universe@167: * universe@167: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@167: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@167: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE universe@167: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE universe@167: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL universe@167: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR universe@167: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER universe@167: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, universe@167: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE universe@167: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. universe@167: */ universe@167: universe@167: package de.uapcore.lightpit.dao universe@167: universe@167: import java.sql.Date universe@167: import java.sql.PreparedStatement universe@167: import java.sql.ResultSet universe@167: import java.sql.Types universe@167: universe@167: fun PreparedStatement.setStringSafe(idx: Int, str: String) { universe@167: setString(idx, str) universe@167: } universe@167: universe@167: fun PreparedStatement.setStringOrNull(idx: Int, str: String?) { universe@167: when (str) { universe@167: null -> setNull(idx, Types.VARCHAR) universe@167: else -> setString(idx, str) universe@167: } universe@167: } universe@167: universe@167: fun PreparedStatement.setIntOrNull(idx: Int, value: Int?) { universe@167: when (value) { universe@167: null -> setNull(idx, Types.INTEGER) universe@167: else -> setInt(idx, value) universe@167: } universe@167: } universe@167: universe@167: fun PreparedStatement.setDateOrNull(idx: Int, value: Date?) { universe@167: when (value) { universe@167: null -> setNull(idx, Types.DATE) universe@167: else -> setDate(idx, value) universe@167: } universe@167: } universe@167: universe@167: fun > PreparedStatement.setEnum(idx: Int, e: Enum) { universe@167: setString(idx, e.name) universe@167: } universe@167: universe@167: inline fun > ResultSet.getEnum(col: String): T { universe@167: return enumValueOf(getString(col)) universe@167: }