src/main/kotlin/de/uapcore/lightpit/dao/Extensions.kt

changeset 167
3f30adba1c63
child 180
009700915269
equal deleted inserted replaced
166:6eede6088d41 167:3f30adba1c63
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
26 package de.uapcore.lightpit.dao
27
28 import java.sql.Date
29 import java.sql.PreparedStatement
30 import java.sql.ResultSet
31 import java.sql.Types
32
33 fun PreparedStatement.setStringSafe(idx: Int, str: String) {
34 setString(idx, str)
35 }
36
37 fun PreparedStatement.setStringOrNull(idx: Int, str: String?) {
38 when (str) {
39 null -> setNull(idx, Types.VARCHAR)
40 else -> setString(idx, str)
41 }
42 }
43
44 fun PreparedStatement.setIntOrNull(idx: Int, value: Int?) {
45 when (value) {
46 null -> setNull(idx, Types.INTEGER)
47 else -> setInt(idx, value)
48 }
49 }
50
51 fun PreparedStatement.setDateOrNull(idx: Int, value: Date?) {
52 when (value) {
53 null -> setNull(idx, Types.DATE)
54 else -> setDate(idx, value)
55 }
56 }
57
58 fun <T : Enum<T>> PreparedStatement.setEnum(idx: Int, e: Enum<T>) {
59 setString(idx, e.name)
60 }
61
62 inline fun <reified T : Enum<T>> ResultSet.getEnum(col: String): T {
63 return enumValueOf(getString(col))
64 }

mercurial