src/main/java/de/uapcore/lightpit/dao/postgres/PGUserDao.java

changeset 47
57cfb94ab99f
parent 38
cf85ef18f231
child 51
dd0a45ae25d7
     1.1 --- a/src/main/java/de/uapcore/lightpit/dao/postgres/PGUserDao.java	Wed May 13 21:46:26 2020 +0200
     1.2 +++ b/src/main/java/de/uapcore/lightpit/dao/postgres/PGUserDao.java	Thu May 14 22:48:01 2020 +0200
     1.3 @@ -28,7 +28,7 @@
     1.4   */
     1.5  package de.uapcore.lightpit.dao.postgres;
     1.6  
     1.7 -import de.uapcore.lightpit.dao.AbstractDao;
     1.8 +import de.uapcore.lightpit.dao.GenericDao;
     1.9  import de.uapcore.lightpit.dao.UserDao;
    1.10  import de.uapcore.lightpit.entities.User;
    1.11  
    1.12 @@ -36,26 +36,41 @@
    1.13  import java.sql.PreparedStatement;
    1.14  import java.sql.ResultSet;
    1.15  import java.sql.SQLException;
    1.16 +import java.util.ArrayList;
    1.17 +import java.util.List;
    1.18  import java.util.Objects;
    1.19  
    1.20 -public final class PGUserDao extends AbstractDao<User> implements UserDao {
    1.21 +import static de.uapcore.lightpit.dao.Functions.setStringOrNull;
    1.22  
    1.23 -    private final PreparedStatement insert;
    1.24 -    private final PreparedStatement update;
    1.25 +public final class PGUserDao implements UserDao, GenericDao<User> {
    1.26 +
    1.27 +    public static final String[] COLUMNS = {
    1.28 +            "id", "username", "lastname", "givenname", "mail"
    1.29 +    };
    1.30 +
    1.31 +    private final PreparedStatement insert, update, list, find;
    1.32  
    1.33      public PGUserDao(Connection connection) throws SQLException {
    1.34 -        super(connection.prepareStatement("select * from lpit_user where userid >= 0 order by username"));
    1.35 +        list = connection.prepareStatement(
    1.36 +                "select userid, username, lastname, givenname, mail " +
    1.37 +                        "from lpit_user where userid >= 0 " +
    1.38 +                        "order by username");
    1.39 +        find = connection.prepareStatement(
    1.40 +                "select userid, username, lastname, givenname, mail " +
    1.41 +                        "from lpit_user where userid = ? ");
    1.42  
    1.43          insert = connection.prepareStatement("insert into lpit_user (username, lastname, givenname, mail) values (?, ?, ?, ?)");
    1.44          update = connection.prepareStatement("update lpit_user set lastname = ?, givenname = ?, mail = ? where userid = ?");
    1.45      }
    1.46  
    1.47 -    @Override
    1.48 -    public User mapColumns(ResultSet result, String q) throws SQLException {
    1.49 -        final var user = new User(result.getInt(qual(q, "userid")));
    1.50 -        user.setUsername(result.getString(qual(q, "username")));
    1.51 -        user.setGivenname(result.getString(qual(q, "givenname")));
    1.52 -        user.setLastname(result.getString(qual(q, "lastname")));
    1.53 +    public User mapColumns(ResultSet result) throws SQLException {
    1.54 +        final int id = result.getInt("userid");
    1.55 +        if (id == 0) return null;
    1.56 +        final var user = new User(id);
    1.57 +        user.setUsername(result.getString("username"));
    1.58 +        user.setGivenname(result.getString("givenname"));
    1.59 +        user.setLastname(result.getString("lastname"));
    1.60 +        user.setMail(result.getString("mail"));
    1.61          return user;
    1.62      }
    1.63  
    1.64 @@ -77,4 +92,27 @@
    1.65          update.setInt(4, instance.getUserID());
    1.66          return update.executeUpdate() > 0;
    1.67      }
    1.68 +
    1.69 +    @Override
    1.70 +    public List<User> list() throws SQLException {
    1.71 +        List<User> users = new ArrayList<>();
    1.72 +        try (var result = list.executeQuery()) {
    1.73 +            while (result.next()) {
    1.74 +                users.add(mapColumns(result));
    1.75 +            }
    1.76 +        }
    1.77 +        return users;
    1.78 +    }
    1.79 +
    1.80 +    @Override
    1.81 +    public User find(int id) throws SQLException {
    1.82 +        find.setInt(1, id);
    1.83 +        try (var result = find.executeQuery()) {
    1.84 +            if (result.next()) {
    1.85 +                return mapColumns(result);
    1.86 +            } else {
    1.87 +                return null;
    1.88 +            }
    1.89 +        }
    1.90 +    }
    1.91  }

mercurial