26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 * |
27 * |
28 */ |
28 */ |
29 package de.uapcore.lightpit.dao; |
29 package de.uapcore.lightpit.dao; |
30 |
30 |
31 import java.sql.Connection; |
|
32 import java.sql.PreparedStatement; |
31 import java.sql.PreparedStatement; |
33 import java.sql.ResultSet; |
32 import java.sql.ResultSet; |
34 import java.sql.SQLException; |
33 import java.sql.SQLException; |
|
34 import java.sql.Types; |
35 import java.util.ArrayList; |
35 import java.util.ArrayList; |
36 import java.util.List; |
36 import java.util.List; |
|
37 import java.util.Optional; |
|
38 import java.util.function.Function; |
37 |
39 |
38 public abstract class AbstractDao<T> implements GenericDao<T> { |
40 public abstract class AbstractDao<T> implements GenericDao<T> { |
39 |
41 |
40 protected abstract PreparedStatement listQuery(Connection connection) throws SQLException; |
42 private final PreparedStatement listQuery; |
41 |
43 |
42 protected abstract T mapColumns(ResultSet result) throws SQLException; |
44 protected AbstractDao(PreparedStatement listQuery) { |
|
45 this.listQuery = listQuery; |
|
46 } |
|
47 |
|
48 public final T mapColumns(ResultSet result) throws SQLException { |
|
49 return mapColumns(result, ""); |
|
50 } |
|
51 |
|
52 public abstract T mapColumns(ResultSet result, String qualifier) throws SQLException; |
|
53 |
|
54 /** |
|
55 * Qualifies a column label if an qualifier is specified. |
|
56 * |
|
57 * @param qualifier an optional qualifier |
|
58 * @param label the column label |
|
59 * @return the label, qualified if necessary |
|
60 */ |
|
61 protected final String qual(String qualifier, String label) { |
|
62 if (qualifier == null || qualifier.isBlank()) { |
|
63 return label; |
|
64 } else { |
|
65 return qualifier + "." + label; |
|
66 } |
|
67 } |
|
68 |
|
69 protected final void setStringOrNull(PreparedStatement stmt, int index, String str) throws SQLException { |
|
70 if (str == null || str.isBlank()) { |
|
71 stmt.setNull(index, Types.VARCHAR); |
|
72 } else { |
|
73 stmt.setString(index, str); |
|
74 } |
|
75 } |
|
76 |
|
77 protected final <T> void setForeignKeyOrNull(PreparedStatement stmt, int index, T instance, Function<T, Integer> keyGetter) throws SQLException { |
|
78 Integer key = Optional.ofNullable(instance).map(keyGetter).orElse(null); |
|
79 if (key == null) { |
|
80 stmt.setNull(index, Types.INTEGER); |
|
81 } else { |
|
82 stmt.setInt(index, key); |
|
83 } |
|
84 } |
43 |
85 |
44 @Override |
86 @Override |
45 public List<T> list(Connection conn) throws SQLException { |
87 public List<T> list() throws SQLException { |
46 List<T> list = new ArrayList<>(); |
88 List<T> list = new ArrayList<>(); |
47 try (PreparedStatement stmt = listQuery(conn); |
89 try (ResultSet result = listQuery.executeQuery()) { |
48 ResultSet result = stmt.executeQuery()) { |
|
49 while (result.next()) { |
90 while (result.next()) { |
50 list.add(mapColumns(result)); |
91 list.add(mapColumns(result)); |
51 } |
92 } |
52 } |
93 } |
53 return list; |
94 return list; |