-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTableInfo.java
More file actions
295 lines (248 loc) · 10 KB
/
Copy pathTableInfo.java
File metadata and controls
295 lines (248 loc) · 10 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
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
package org.javawebstack.orm;
import org.atteo.evo.inflector.English;
import org.javawebstack.orm.annotation.*;
import org.javawebstack.orm.exception.ORMConfigurationException;
import org.javawebstack.orm.util.Helper;
import org.javawebstack.orm.util.KeyType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
public class TableInfo {
private String idField = "id";
private String tableName;
private final List<String> fieldNames = new ArrayList<>();
private final Map<String, Field> fields = new HashMap<>();
private final Map<String, String> fieldToColumn = new HashMap<>();
private final Map<String, Column> fieldConfigs = new HashMap<>();
private final Map<String, SQLType> sqlTypes = new HashMap<>();
private final Map<String, String> sqlTypeParameters = new HashMap<>();
private final ORMConfig config;
private SoftDelete softDelete;
private Dates dates;
private String morphType;
private final Class<? extends Model> modelClass;
private String primaryKey;
private final List<String> uniqueKeys = new ArrayList<>();
private Constructor<?> constructor;
private String relationField;
private final Map<String, String> filterable = new HashMap<>();
private final List<String> searchable = new ArrayList<>();
private static final Class<?>[] appliesDefaultSize = {
String.class,
char[].class,
};
public TableInfo(Class<? extends Model> model, ORMConfig config) throws ORMConfigurationException {
this.config = config;
this.modelClass = model;
Stack<Class<?>> superClasses = Helper.getSuperClassesTill(model, Model.class);
while (!superClasses.isEmpty()) {
Class<? extends Model> superClass = (Class<? extends Model>) superClasses.pop();
if (Modifier.isAbstract(superClass.getModifiers())) {
analyzeColumns(superClass);
} else {
throw new ORMConfigurationException("The parent model has to be abstract!");
}
}
constructInfo(model);
}
private void constructInfo (Class<? extends Model> model) throws ORMConfigurationException {
analyzeColumns(model);
analyzeTable(model);
if (!fields.containsKey(idField))
idField = "uuid";
if (!fields.containsKey(idField))
throw new ORMConfigurationException("No id field found!");
if (config.isIdPrimaryKey()) {
if (primaryKey == null)
primaryKey = idField;
}
}
private void analyzeTable(Class<? extends Model> model) throws ORMConfigurationException {
if (model.isAnnotationPresent(Table.class)) {
Table table = model.getDeclaredAnnotationsByType(Table.class)[0];
tableName = table.value();
} else {
tableName = Helper.toSnakeCase(English.plural(model.getSimpleName()));
}
if (model.isAnnotationPresent(MorphType.class)) {
morphType = model.getDeclaredAnnotationsByType(MorphType.class)[0].value();
} else {
morphType = Helper.toSnakeCase(model.getSimpleName());
}
try {
constructor = model.getConstructor();
constructor.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new ORMConfigurationException("The model class has no empty constructor!");
}
if (model.isAnnotationPresent(RelationField.class)) {
relationField = model.getDeclaredAnnotationsByType(RelationField.class)[0].value();
} else {
relationField = Helper.pascalToCamelCase(model.getSimpleName()) + ((getIdType().equals(UUID.class) && !idField.equalsIgnoreCase("id")) ? "UUID" : "Id");
}
if (model.isAnnotationPresent(SoftDelete.class)) {
softDelete = model.getDeclaredAnnotationsByType(SoftDelete.class)[0];
if (!fields.containsKey(softDelete.value()))
throw new ORMConfigurationException("Missing soft-delete field '" + softDelete.value() + "'");
}
if (model.isAnnotationPresent(Dates.class)) {
dates = model.getDeclaredAnnotationsByType(Dates.class)[0];
if (!fields.containsKey(dates.create()))
throw new ORMConfigurationException("Missing dates field '" + dates.create() + "'");
if (!fields.containsKey(dates.update()))
throw new ORMConfigurationException("Missing dates field '" + dates.update() + "'");
}
}
private void analyzeColumns(Class<? extends Model> model) throws ORMConfigurationException {
for (Field field : model.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()))
continue;
if (!field.isAnnotationPresent(Column.class))
continue;
field.setAccessible(true);
String fieldName = field.getName();
fieldNames.add(fieldName);
Column fieldConfig = field.getDeclaredAnnotationsByType(Column.class)[0];
if (fieldConfig.name().length() > 0) {
fieldToColumn.put(fieldName, fieldConfig.name());
} else {
fieldToColumn.put(fieldName, config.isCamelToSnakeCase() ? Helper.toSnakeCase(fieldName) : fieldName);
}
fields.put(fieldName, field);
fieldConfigs.put(fieldName, fieldConfig);
int fieldSize;
if (Arrays.stream(appliesDefaultSize).anyMatch(type -> type.equals(field.getType())) && fieldConfig.size() == -1)
fieldSize = config.getDefaultSize();
else
fieldSize = fieldConfig.size();
SQLType sqlType = config.getType(field.getType(), fieldSize);
if (sqlType != null) {
sqlTypes.put(fieldName, sqlType);
sqlTypeParameters.put(fieldName, config.getTypeParameters(field.getType(), fieldSize));
}
if (!sqlTypes.containsKey(fieldName))
throw new ORMConfigurationException("Couldn't find type-mapper for '" + fieldName + "'!");
if (fieldConfig.id()) {
idField = fieldName;
}
if (fieldConfig.key() == KeyType.PRIMARY) {
if (primaryKey != null && !primaryKey.equals(fieldName))
throw new ORMConfigurationException("Multiple primary key fields!");
primaryKey = fieldName;
}
if (fieldConfig.key() == KeyType.UNIQUE)
uniqueKeys.add(fieldName);
if (field.isAnnotationPresent(Filterable.class)) {
String name = field.getAnnotationsByType(Filterable.class)[0].value();
this.filterable.put(name.length() > 0 ? name : fieldName, fieldName);
}
if (field.isAnnotationPresent(Searchable.class))
this.searchable.add(fieldName);
}
}
public boolean isSoftDelete() {
return softDelete != null;
}
public SoftDelete getSoftDelete() {
return softDelete;
}
public boolean hasDates() {
return dates != null;
}
public boolean hasCreated() {
return hasDates() && getFields().contains(getCreatedField());
}
public boolean hasUpdated() {
return hasDates() && getFields().contains(getUpdatedField());
}
public boolean isAutoIncrement() {
return (
getField(getIdField()).getType().equals(Integer.class) ||
getField(getIdField()).getType().equals(int.class) ||
getField(getIdField()).getType().equals(Long.class) ||
getField(getIdField()).getType().equals(long.class)
) && (fieldConfigs.get(idField).ai() || config.isIdAutoIncrement());
}
public String getSoftDeleteField() {
return softDelete.value();
}
public String getCreatedField() {
return dates.create();
}
public String getUpdatedField() {
return dates.update();
}
public List<String> getFields() {
return fieldNames;
}
public Field getField(String fieldName) {
return fields.get(fieldName);
}
public String getMorphType() {
return morphType;
}
public Map<String, String> getFilterable() {
return filterable;
}
public List<String> getSearchable() {
return searchable;
}
public String getColumnName(String fieldName) {
String[] spl = fieldName.split("\\.");
fieldName = spl[spl.length - 1];
if (fieldToColumn.containsKey(fieldName))
spl[spl.length - 1] = fieldToColumn.get(fieldName);
return String.join(".", spl);
}
public SQLType getType(String fieldName) {
return sqlTypes.get(fieldName);
}
public String getTypeParameters(String fieldName) {
return sqlTypeParameters.get(fieldName);
}
public String getRawTableName() {
return tableName;
}
public String getTablePrefix() {
return config.getTablePrefix();
}
public String getTableName() {
return config.getTablePrefix() + tableName;
}
public Class<? extends Model> getModelClass() {
return modelClass;
}
public ORMConfig getConfig() {
return config;
}
public String getPrimaryKey() {
return primaryKey;
}
public List<String> getUniqueKeys() {
return uniqueKeys;
}
public String getIdField() {
return idField;
}
public String getIdColumn() {
return getColumnName(getIdField());
}
public Class<?> getIdType() {
return getField(getIdField()).getType();
}
public Object getDefault(String fieldName) {
return null;
}
public boolean isNotNull(String fieldName) {
if (idField.equals(fieldName))
return true;
return false;
}
public Constructor<?> getModelConstructor() {
return constructor;
}
public String getRelationField() {
return relationField;
}
}