26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 * |
27 * |
28 */ |
28 */ |
29 package de.uapcore.lightpit.dao.postgres; |
29 package de.uapcore.lightpit.dao.postgres; |
30 |
30 |
31 import de.uapcore.lightpit.dao.AbstractDao; |
31 import de.uapcore.lightpit.dao.GenericDao; |
32 import de.uapcore.lightpit.dao.UserDao; |
32 import de.uapcore.lightpit.dao.UserDao; |
33 import de.uapcore.lightpit.entities.User; |
33 import de.uapcore.lightpit.entities.User; |
34 |
34 |
35 import java.sql.Connection; |
35 import java.sql.Connection; |
36 import java.sql.PreparedStatement; |
36 import java.sql.PreparedStatement; |
37 import java.sql.ResultSet; |
37 import java.sql.ResultSet; |
38 import java.sql.SQLException; |
38 import java.sql.SQLException; |
|
39 import java.util.ArrayList; |
|
40 import java.util.List; |
39 import java.util.Objects; |
41 import java.util.Objects; |
40 |
42 |
41 public final class PGUserDao extends AbstractDao<User> implements UserDao { |
43 import static de.uapcore.lightpit.dao.Functions.setStringOrNull; |
42 |
44 |
43 private final PreparedStatement insert; |
45 public final class PGUserDao implements UserDao, GenericDao<User> { |
44 private final PreparedStatement update; |
46 |
|
47 public static final String[] COLUMNS = { |
|
48 "id", "username", "lastname", "givenname", "mail" |
|
49 }; |
|
50 |
|
51 private final PreparedStatement insert, update, list, find; |
45 |
52 |
46 public PGUserDao(Connection connection) throws SQLException { |
53 public PGUserDao(Connection connection) throws SQLException { |
47 super(connection.prepareStatement("select * from lpit_user where userid >= 0 order by username")); |
54 list = connection.prepareStatement( |
|
55 "select userid, username, lastname, givenname, mail " + |
|
56 "from lpit_user where userid >= 0 " + |
|
57 "order by username"); |
|
58 find = connection.prepareStatement( |
|
59 "select userid, username, lastname, givenname, mail " + |
|
60 "from lpit_user where userid = ? "); |
48 |
61 |
49 insert = connection.prepareStatement("insert into lpit_user (username, lastname, givenname, mail) values (?, ?, ?, ?)"); |
62 insert = connection.prepareStatement("insert into lpit_user (username, lastname, givenname, mail) values (?, ?, ?, ?)"); |
50 update = connection.prepareStatement("update lpit_user set lastname = ?, givenname = ?, mail = ? where userid = ?"); |
63 update = connection.prepareStatement("update lpit_user set lastname = ?, givenname = ?, mail = ? where userid = ?"); |
51 } |
64 } |
52 |
65 |
53 @Override |
66 public User mapColumns(ResultSet result) throws SQLException { |
54 public User mapColumns(ResultSet result, String q) throws SQLException { |
67 final int id = result.getInt("userid"); |
55 final var user = new User(result.getInt(qual(q, "userid"))); |
68 if (id == 0) return null; |
56 user.setUsername(result.getString(qual(q, "username"))); |
69 final var user = new User(id); |
57 user.setGivenname(result.getString(qual(q, "givenname"))); |
70 user.setUsername(result.getString("username")); |
58 user.setLastname(result.getString(qual(q, "lastname"))); |
71 user.setGivenname(result.getString("givenname")); |
|
72 user.setLastname(result.getString("lastname")); |
|
73 user.setMail(result.getString("mail")); |
59 return user; |
74 return user; |
60 } |
75 } |
61 |
76 |
62 @Override |
77 @Override |
63 public void save(User instance) throws SQLException { |
78 public void save(User instance) throws SQLException { |
75 setStringOrNull(update, 2, instance.getGivenname()); |
90 setStringOrNull(update, 2, instance.getGivenname()); |
76 setStringOrNull(update, 3, instance.getMail()); |
91 setStringOrNull(update, 3, instance.getMail()); |
77 update.setInt(4, instance.getUserID()); |
92 update.setInt(4, instance.getUserID()); |
78 return update.executeUpdate() > 0; |
93 return update.executeUpdate() > 0; |
79 } |
94 } |
|
95 |
|
96 @Override |
|
97 public List<User> list() throws SQLException { |
|
98 List<User> users = new ArrayList<>(); |
|
99 try (var result = list.executeQuery()) { |
|
100 while (result.next()) { |
|
101 users.add(mapColumns(result)); |
|
102 } |
|
103 } |
|
104 return users; |
|
105 } |
|
106 |
|
107 @Override |
|
108 public User find(int id) throws SQLException { |
|
109 find.setInt(1, id); |
|
110 try (var result = find.executeQuery()) { |
|
111 if (result.next()) { |
|
112 return mapColumns(result); |
|
113 } else { |
|
114 return null; |
|
115 } |
|
116 } |
|
117 } |
80 } |
118 } |