-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathAbstractQueryBuilder.java
More file actions
70 lines (59 loc) · 2.25 KB
/
Copy pathAbstractQueryBuilder.java
File metadata and controls
70 lines (59 loc) · 2.25 KB
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
package persistence.sql;
import jakarta.persistence.Column;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Stream;
import persistence.exception.UnsupportedClassException;
public abstract class AbstractQueryBuilder {
protected AbstractQueryBuilder() {
}
/**
* Get the stream of fields that are not annotated with <code>@Transient</code> from the entity class
* @param entityClass Entity class
* @return Stream of fields that are annotated with <code>@Column</code> or <code>@Id</code>
* @see Column
* @see Id
* @see Transient
*/
protected Stream<Field> getColumnFieldStream(Class<?> entityClass) {
return Arrays.stream(entityClass.getDeclaredFields())
.filter(field -> !field.isAnnotationPresent(Transient.class))
.sorted(Comparator.comparing(field -> field.isAnnotationPresent(Id.class) ? 0 : 1));
}
/**
* Get the column name from the field
* @param field Field
* @return Column name from the field
*/
protected String getColumnNameFrom(Field field) {
if (!field.isAnnotationPresent(Column.class)) {
return field.getName();
}
Column column = field.getAnnotation(Column.class);
if (column.name().isEmpty()) {
return field.getName();
}
return column.name();
}
/**
* Get the column value from the object
* @param columnValue Column value object
* @return Column value from the object
*/
protected String getColumnValueFromObject(Object columnValue) {
// TODO: remove this else-if statement
if (columnValue.getClass().equals(Boolean.class)) {
return columnValue == Boolean.TRUE ? "1" : "0";
} else if (columnValue.getClass().equals(String.class)) {
return String.format("'%s'", columnValue);
} else if (columnValue.getClass().equals(Integer.class)) {
return columnValue.toString();
} else if (columnValue.getClass().equals(Long.class)) {
return columnValue.toString();
}
throw new UnsupportedClassException(columnValue.getClass());
}
}