Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package com.trimaral.orm;
import com.google.common.base.CharMatcher;
import com.google.common.collect.ObjectArrays;
import com.google.common.primitives.Primitives;
import com.google.i18n.phonenumbers.NumberParseException;
import com.trimaral.orm.annotations.Column;
import com.trimaral.orm.annotations.Table;
import com.trimaral.orm.exceptions.AnnotationNotFoundException;
import com.trimaral.orm.exceptions.DAOException;
import com.trimaral.orm.util.ClassUtil;
import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.sql.*;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
/**
* @param <T> An entity class
* @author Lorenzo Ferron
* @version 2019.09.28
*/
public class ResultSetMapper<T> {
public static final String SET_CLAUSE = " SET ";
public static final String WHERE_CLAUSE = " WHERE ";
public static final String VALUES_CLAUSE = " VALUES ";
public static final String FROM_CLAUSE = " FROM ";
public static final String SELECT_SQL = "SELECT *" + FROM_CLAUSE;
public static final String INSERT_SQL = "INSERT ";
public static final String IGNORE_SQL = "IGNORE ";
public static final String INTO_SQL = "INTO ";
public static final String UPDATE_SQL = "UPDATE ";
public static final String DELETE_SQL = "DELETE";
public static final String DELIMITER = ", ";
private static final String REPLACE_SQL = " ON DUPLICATE KEY UPDATE ";
private final DataSource dataSource;
private final Class<T> clazz;
private Table table;
private Field[] fields;
private List<Field> primaryKeyField = new ArrayList<>(0);
public ResultSetMapper(DataSource dataSource, Class<T> clazz) {
this.dataSource = dataSource;
this.clazz = clazz;
if (clazz != null) {
if (!clazz.isAnnotationPresent(Table.class))
throw new AnnotationNotFoundException();
table = clazz.getAnnotation(Table.class);
fields = ClassUtil.getAnnotatedDeclaredFields(clazz, Column.class);
for (Field field : fields)
if (field.getAnnotation(Column.class).isPrimaryKey())
primaryKeyField.add(field);
/*if (primaryKeyField.isEmpty()) {
primaryKeyFromParent(clazz.getSuperclass());
fields = ObjectArrays.concat(fields, primaryKeyField.toArray(new Field[0]), Field.class);
}*/
}
}
private static void questionMarksParamsCount(String criteria, Object[] params) {
int criteriaCount = criteria == null ? 0 : CharMatcher.is('?').countIn(criteria);
int paramsCount = params != null ? params.length : 0;
if (criteriaCount != paramsCount)
throw new DAOException("?: criteria = " + criteriaCount + "; params = " + paramsCount);
}
/*private void primaryKeyFromParent(Class<? super T> parent) {
if (parent.equals(Object.class)) // caso base
return;
Field[] parentFields = ClassUtil.getAnnotatedDeclaredFields(parent, Column.class);
for (Field field : parentFields)
if (field.getAnnotation(Column.class).isPrimaryKey())
primaryKeyField.add(field);
primaryKeyFromParent(parent.getSuperclass());
}*/
@SuppressWarnings({"unchecked"})
public <T> List<T> findByCriteria(String criteria, Object... params) throws SQLException, IllegalArgumentException, NullPointerException, NumberParseException {
questionMarksParamsCount(criteria, params);
String sqlStatement = SELECT_SQL + table.name();
if (criteria != null)
sqlStatement += WHERE_CLAUSE + criteria.trim();
List<T> result = new ArrayList<>(0);
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(sqlStatement)) {
if (criteria != null) for (int i = 0; i < params.length; i++) stmt.setObject(i + 1, params[i]);
System.out.println(stmt.toString()); // For debug purpose
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
T item = (T) clazz.newInstance();
for (Field field : fields) {
Object value;
Class<?> type = field.getType();
value = type.equals(YearMonth.class) ? YearMonth.parse(rs.getString(field.getAnnotation(Column.class).name()), DateTimeFormatter.ofPattern("yyyy-MM-00")) : rs.getObject(field.getAnnotation(Column.class).name());
if (type.isPrimitive()) {
Class<?> boxed = Primitives.wrap(type);
value = boxed.cast(value);
}
field.setAccessible(true);
field.set(item, value);
field.setAccessible(false);
}
result.add(item);
}
}
} catch (IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
return result;
}
public String findById() {
if (primaryKeyField.isEmpty())
throw new NullPointerException("Primary Key is not found");
else if (primaryKeyField.size() > 1)
throw new UnsupportedOperationException("Only one primary key");
return primaryKeyField.get(0).getAnnotation(Column.class).name() + " = ?";
}
public void deleteById(Long id) throws SQLException, IllegalArgumentException, NullPointerException, NumberParseException {
if (primaryKeyField.isEmpty())
throw new NullPointerException("Primary Key is not found");
else if (primaryKeyField.size() > 1)
throw new UnsupportedOperationException("Only one primary key");
deleteByCriteria(primaryKeyField.get(0).getAnnotation(Column.class).name() + " = ?", id);
}
public void getByCriteria(String criteria, Object... params) {
questionMarksParamsCount(criteria, params);
}
public void update(T item) throws SQLException, IllegalArgumentException, NullPointerException, NumberParseException {
if (primaryKeyField.isEmpty())
throw new NullPointerException("Primary Key is not found");
StringJoiner joiner = new StringJoiner(" AND ");
Object[] params = criteriaJoiner(item, joiner);
updateByCriteria(item, joiner.toString(), params);
}
public void updateByCriteria(T item, String criteria, Object... params) throws SQLException, IllegalArgumentException, NullPointerException, NumberParseException {
questionMarksParamsCount(criteria, params);
StringJoiner joiner = new StringJoiner(DELIMITER);
List<Field> filteredFields = new ArrayList<>(0);
Column column;
for (Field field : fields) {
field.setAccessible(true);
column = field.getAnnotation(Column.class);
if ((field.getDeclaringClass().equals(clazz) || column.isPrimaryKey()) && !column.hold()) {
joiner.add(field.getAnnotation(Column.class).name() + " = ?");
filteredFields.add(field);
}
}
String sqlStatement = UPDATE_SQL + table.name() + SET_CLAUSE + joiner.toString() +
WHERE_CLAUSE + criteria.trim();
executeStatement(sqlStatement, item, filteredFields, params);
}
private void executeStatement(String sqlStatement, T item, List<Field> filteredFields, Object... params) throws SQLException, IllegalArgumentException, NullPointerException, NumberParseException {
try {
boolean keysGeneration = primaryKeyField.stream().allMatch(x -> {
try {
return x.get(item) == null;
} catch (IllegalAccessException e) {
e.printStackTrace();
return false;
}
});
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = keysGeneration ? conn.prepareStatement(sqlStatement, Statement.RETURN_GENERATED_KEYS) : conn.prepareStatement(sqlStatement)) {
int counter = 1;
for (Field field : filteredFields) {
if (field.getType().equals(YearMonth.class)) {
String date = ((YearMonth) field.get(item)).format(DateTimeFormatter.ofPattern("yyyy-MM-00"));
stmt.setString(counter++, field.getAnnotation(Column.class).isNullable() && "".equals(date) ? null : date);
} else
stmt.setObject(counter++, field.getAnnotation(Column.class).isNullable() && "".equals(field.get(item)) ? null : field.get(item));
}
for (Object param : params)
stmt.setObject(counter++, param);
System.out.println(stmt.toString()); // For debug purpose
int affectedRows = stmt.executeUpdate();
if (keysGeneration) {
if (affectedRows == 0)
throw new SQLException("No rows affected.");
try (ResultSet generatedKeys = stmt.getGeneratedKeys()) {
if (generatedKeys.next())
Arrays.stream(fields).filter(p -> p.getAnnotation(Column.class).isPrimaryKey()).findFirst().get().set(item, generatedKeys.getLong(1));
else
throw new SQLException("No ID obtained.");
}
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} finally {
filteredFields.clear();
for (Field field : fields)
field.setAccessible(false);
}
}
public void save(T item, boolean replaceMode, boolean ignore) throws SQLException, IllegalArgumentException, NullPointerException, NumberParseException {
try {
StringJoiner joiner = new StringJoiner(DELIMITER, " ( ", " ) ");
List<Field> filteredFields = new ArrayList<>(0);
Column column;
for (Field field : fields) {
field.setAccessible(true);
column = field.getAnnotation(Column.class);
if ((field.getDeclaringClass().equals(clazz) || column.isPrimaryKey()) && ((replaceMode || ignore) && column.isPrimaryKey() && field.get(item) != null || !(field.get(item) == null && (column.isPrimaryKey() || column.hasDefaultValue())))) {
joiner.add(field.getAnnotation(Column.class).name());
filteredFields.add(field);
}
}
String sqlStatement = INSERT_SQL;
if (ignore)
sqlStatement += IGNORE_SQL;
sqlStatement += INTO_SQL + table.name() + joiner.toString() + VALUES_CLAUSE;
joiner = new StringJoiner(DELIMITER, " (", ")");
for (int i = 0; i < filteredFields.size(); i++)
joiner.add("?");
sqlStatement += joiner.toString();
if (replaceMode) {
sqlStatement += REPLACE_SQL;
joiner = new StringJoiner(DELIMITER + " ");
for (Field field : filteredFields) {
String nameColumn = field.getAnnotation(Column.class).name();
joiner.add(nameColumn + "=" + VALUES_CLAUSE + "(" + nameColumn + ")");
}
sqlStatement += joiner.toString();
}
executeStatement(sqlStatement, item, filteredFields);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private Object[] criteriaJoiner(T item, StringJoiner joiner) {
Object[] params = new Object[0];
for (Field field : primaryKeyField) {
joiner.add(field.getAnnotation(Column.class).name() + " = ?");
field.setAccessible(true);
try {
params = ObjectArrays.concat(params, field.get(item));
} catch (IllegalAccessException e) {
e.printStackTrace();
} finally {
field.setAccessible(false);
}
}
return params;
}
public void delete(T item) throws SQLException, IllegalArgumentException, NullPointerException, NumberParseException {
if (primaryKeyField.isEmpty())
throw new NullPointerException("Primary Key is not found");
StringJoiner joiner = new StringJoiner(" AND ");
Object[] params = criteriaJoiner(item, joiner);
deleteByCriteria(joiner.toString(), params);
}
public void deleteByCriteria(String criteria, Object... params) throws SQLException, IllegalArgumentException, NullPointerException, NumberParseException {
questionMarksParamsCount(criteria, params);
String sqlStatement = DELETE_SQL + FROM_CLAUSE + table.name() + WHERE_CLAUSE + criteria.trim();
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(sqlStatement)) {
for (int i = 0; i < params.length; i++) stmt.setObject(i + 1, params[i]);
System.out.println(stmt.toString()); // For debug purpose
stmt.executeUpdate();
}
}
public Object customQuery(String query, Object... params) throws SQLException, IllegalArgumentException, NullPointerException, NumberParseException {
questionMarksParamsCount(query, params);
try (Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) {
for (int i = 0; i < params.length; i++) stmt.setObject(i + 1, params[i]);
System.out.println(stmt.toString()); // For debug purpose
if (stmt.execute()) {
try (ResultSet rs = stmt.getResultSet()) {
List<T> items = new ArrayList<>(0);
while (rs.next()) {
T item = clazz.newInstance();
for (Field field : fields) {
Object value;
Class<?> type = field.getType();
value = type.equals(YearMonth.class) ? YearMonth.parse(rs.getString(field.getAnnotation(Column.class).name()), DateTimeFormatter.ofPattern("yyyy-MM-00")) : rs.getObject(field.getAnnotation(Column.class).name());
if (type.isPrimitive()) {
Class<?> boxed = Primitives.wrap(type);
value = boxed.cast(value);
}
field.setAccessible(true);
field.set(item, value);
field.setAccessible(false);
}
items.add(item);
}
return items;
}
} else {
try (ResultSet rs = stmt.getGeneratedKeys()) {
if (rs.next())
return rs.getLong(1);
}
}
} catch (IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
return null;
}
}