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

changeset 159
86b5d8a1662f
equal deleted inserted replaced
158:4f912cd42876 159:86b5d8a1662f
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
27 package de.uapcore.lightpit.dao.postgres
28
29 import de.uapcore.lightpit.dao.AbstractUserDao
30 import de.uapcore.lightpit.dao.Functions
31 import de.uapcore.lightpit.entities.User
32 import java.sql.Connection
33 import java.sql.ResultSet
34
35 class PGUserDao(connection: Connection) : AbstractUserDao() {
36
37 companion object {
38 fun mapResult(rs: ResultSet): User {
39 val id = rs.getInt("userid")
40 return if (rs.wasNull()) {
41 User(-1)
42 } else {
43 val user = User(id)
44 user.username = rs.getString("username")
45 user.givenname = Functions.getSafeString(rs, "givenname")
46 user.lastname = Functions.getSafeString(rs, "lastname")
47 user.mail = Functions.getSafeString(rs, "mail")
48 user
49 }
50 }
51 }
52
53 private val listStmt = connection.prepareStatement(
54 "select userid, username, lastname, givenname, mail " +
55 "from lpit_user where userid >= 0 " +
56 "order by username")
57 private val findStmt = connection.prepareStatement(
58 "select userid, username, lastname, givenname, mail " +
59 "from lpit_user where userid = ? ")
60 private val findByUsernameStmt = connection.prepareStatement(
61 "select userid, username, lastname, givenname, mail " +
62 "from lpit_user where lower(username) = lower(?) ")
63 private val insertStmt = connection.prepareStatement("insert into lpit_user (username, lastname, givenname, mail) values (?, ?, ?, ?)")
64 private val updateStmt = connection.prepareStatement("update lpit_user set lastname = ?, givenname = ?, mail = ? where userid = ?")
65
66 override fun mapResult(rs: ResultSet): User = Companion.mapResult(rs)
67
68 override fun save(instance: User) {
69 insertStmt.setString(1, instance.username)
70 Functions.setStringOrNull(insertStmt, 2, instance.lastname)
71 Functions.setStringOrNull(insertStmt, 3, instance.givenname)
72 Functions.setStringOrNull(insertStmt, 4, instance.mail)
73 insertStmt.executeUpdate()
74 }
75
76 override fun update(instance: User): Boolean {
77 Functions.setStringOrNull(updateStmt, 1, instance.lastname)
78 Functions.setStringOrNull(updateStmt, 2, instance.givenname)
79 Functions.setStringOrNull(updateStmt, 3, instance.mail)
80 updateStmt.setInt(4, instance.id)
81 return updateStmt.executeUpdate() > 0
82 }
83
84 override fun list(): List<User> = super.list(listStmt)
85
86 override fun find(id: Int): User? {
87 findStmt.setInt(1, id)
88 return super.find(findStmt)
89 }
90
91 override fun findByUsername(username: String): User? {
92 findByUsernameStmt.setString(1, username)
93 return super.find(findByUsernameStmt)
94 }
95 }

mercurial