-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityManager.java
335 lines (283 loc) · 11 KB
/
EntityManager.java
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
package com.covenant;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Pair;
public class EntityManager {
private static EntityManager _instance;
private DbManager _dbManager;
private Map<Class<?>, String> tableNameCache;
private Map<Class<?>, List<Pair<Field, Column>>> fieldColumnPairCache;
private Map<Class<?>, Field> pkColumnCache;
private EntityManager() {
tableNameCache = new HashMap<Class<?>, String>();
fieldColumnPairCache = new HashMap<Class<?>, List<Pair<Field, Column>>>();
pkColumnCache = new HashMap<Class<?>, Field>();
}
public static void init(DbManager dbManager) {
EntityManager em = EntityManager.getInstance();
em._setDbManager(dbManager);
}
public static EntityManager getInstance() {
if (_instance == null) {
_instance = new EntityManager();
}
return _instance;
}
private void _setDbManager(DbManager dbManager) {
this._dbManager = dbManager;
}
public boolean save(Object entity) {
// Get the class of the entity and verify that it is persistable
Class<?> entityClass = entity.getClass();
_verifyIsEntity(entityClass);
String tableName = tableNameCache.get(entityClass);
if (tableName == null) {
tableName = entityClass.getAnnotation(Entity.class).table();
tableNameCache.put(entityClass, tableName);
// We will only need to do this the first time as well
if (this._dbManager.getMode() == DbManager.AUTO) {
createTable(tableName, entity);
}
}
List<Pair<Field, Column>> fieldColumnPairs = this._getColumns(entityClass);
Field pkColumn = this._getPK(entityClass, fieldColumnPairs);
String pkColumnName = pkColumn.getAnnotation(Column.class).name().toString();
ContentValues values;
values = new ContentValues();
// Get all of the fields that are persistable EntityFields
Field field;
Column column;
for (Pair<Field, Column> fieldColumnPair : fieldColumnPairs) {
field = fieldColumnPair.first;
column = fieldColumnPair.second;
try {
if (field.getType() == String.class) {
values.put(column.name(), field.get(entity).toString());
} else if (field.getType() == int.class) {
values.put(column.name(), field.getInt(entity));
} else if (field.getType() == long.class) {
values.put(column.name(), field.getLong(entity));
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
// If the primary key is 0, insert
if (values.getAsLong(pkColumnName) == 0) {
values.remove(pkColumnName);
long pkValue = _dbManager.getDb().insert(tableName, null, values);
try {
pkColumn.setLong(entity, pkValue);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return pkValue > 0;
}
// Else, update
else {
long pkValue = values.getAsLong(pkColumnName);
values.remove(pkColumnName);
return _dbManager.getDb().update(tableName, values, pkColumnName + "=" + pkValue, null) > 0;
}
}
private void createTable(String table, Object entity) {
Cursor c = _dbManager.getDb().rawQuery("SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name = '" + table + "'", null);
if (c.getCount() == 0) {
createTables(false, entity.getClass());
}
}
public void createTables(boolean dropIfExists, Class<?>... entityClasses) {
for (Class<?> entityClass : entityClasses) {
// Get the class of the entity and verify that it is persistable
_verifyIsEntity(entityClass);
String table = entityClass.getAnnotation(Entity.class).table();
List<Pair<Field, Column>> fieldColumnPairs = this._getColumns(entityClass);
StringBuilder createQueryBuilder = new StringBuilder();
Column column = null;
Field field = null;
int count = fieldColumnPairs.size();
int index = 0;
if (dropIfExists) {
this._dbManager.getDb().execSQL("DROP TABLE IF EXISTS " + table);
}
createQueryBuilder.append("CREATE TABLE " + table + " ( ");
for (Pair<Field, Column> fieldColumnPair : fieldColumnPairs) {
field = fieldColumnPair.first;
column = fieldColumnPair.second;
if (column.PK() == true) {
createQueryBuilder.append("_id" + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL");
} else {
if (field.getType() == String.class) {
createQueryBuilder.append(column.name() + " VARCHAR");
}
else if (field.getType() == int.class) {
createQueryBuilder.append(column.name() + " INTEGER");
}
else {
createQueryBuilder.append(column.name() + " VARCHAR");
}
}
if (++index < count) {
createQueryBuilder.append(",");
}
}
createQueryBuilder.append(")");
String createQuery = createQueryBuilder.toString();
this._dbManager.getDb().execSQL(createQuery);
}
}
public <T> int delete(Object entity) {
Class<?> entityClass = entity.getClass();
_verifyIsEntity(entityClass);
List<Pair<Field, Column>> fieldColumnPairs = this._getColumns(entityClass);
Field pkColumn = this._getPK(entityClass, fieldColumnPairs);
try {
return this.deleteByPk(entityClass, pkColumn.getLong(entity));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return -1;
}
public void execSQL(String sql) {
this._dbManager.getDb().execSQL(sql);
}
public <T> T fetchOneBy(Class<T> entityClass, String whereClause) {
List<T> entityList = this.fetchBy(entityClass, whereClause);
T firstEntity = null;
if (entityList.size() > 0) {
firstEntity = entityList.get(0);
}
return firstEntity;
}
public <T> T fetchOneByPK(Class<T> entityClass, long pkValue) {
List<Pair<Field, Column>> fieldColumnPairs = this._getColumns(entityClass);
Field pkColumn = this._getPK(entityClass, fieldColumnPairs);
String pkColumnName = pkColumn.getAnnotation(Column.class).name().toString();
return this.fetchOneBy(entityClass, pkColumnName + " = " + pkValue);
}
public <T> List<T> fetchAll(Class<T> entityClass) {
_verifyIsEntity(entityClass);
String table = entityClass.getAnnotation(Entity.class).table();
Cursor c = this._dbManager.getDb().query(table, new String[] { "*" }, null, null, null, null, null);
return inflateFromCursor(entityClass, c, c.getCount());
}
public <T> List<T> fetchBy(Class<T> entityClass, String whereClause) {
_verifyIsEntity(entityClass);
String table = entityClass.getAnnotation(Entity.class).table();
Cursor c = this._dbManager.getDb().query(table, new String[] { "*" }, whereClause, null, null, null, null);
return inflateFromCursor(entityClass, c, c.getCount());
}
public <T> List<T> fetchBy(Class<T> entityClass, CovenantQuery covenantQuery) {
_verifyIsEntity(entityClass);
String table = entityClass.getAnnotation(Entity.class).table();
Cursor c = this._dbManager.getDb().query(covenantQuery.getDistinct(), table, null, covenantQuery.getWhere(), null,
covenantQuery.getGroupBy(), covenantQuery.getHaving(), covenantQuery.getOrderBy(), covenantQuery.getLimit());
return inflateFromCursor(entityClass, c, c.getCount());
}
public <T> int deleteByPk(Class<?> entityClass, long pkValue) {
_verifyIsEntity(entityClass);
String table = entityClass.getAnnotation(Entity.class).table();
List<Pair<Field, Column>> fieldColumnPairs = this._getColumns(entityClass.getClass());
Field pkColumn = this._getPK(entityClass, fieldColumnPairs);
String pkColumnName = pkColumn.getAnnotation(Column.class).name().toString();
return this._dbManager.getDb().delete(table, pkColumnName + " =?", new String[] { Long.toString(pkValue) });
}
public <T> int deleteWhere(Class<?> entityClass, String whereClause) {
_verifyIsEntity(entityClass);
String table = entityClass.getAnnotation(Entity.class).table();
return this._dbManager.getDb().delete(table, whereClause, null);
}
public void startTransaction() {
this._dbManager.getDb().beginTransaction();
}
public void endTransaction() {
this._dbManager.getDb().setTransactionSuccessful();
this._dbManager.getDb().endTransaction();
}
private Field _getPK(Class<?> entityClass, List<Pair<Field, Column>> fieldColumnPairs) {
Field pkColumn = pkColumnCache.get(entityClass);
if (pkColumn == null) {
for (Pair<Field, Column> fieldColumnPair : fieldColumnPairs) {
fieldColumnPair.first.setAccessible(true);
Column column = fieldColumnPair.first.getAnnotation(Column.class);
if (column != null) {
if (column.PK()) {
pkColumn = fieldColumnPair.first;
break;
}
}
}
pkColumnCache.put(entityClass, pkColumn);
}
return pkColumn;
}
private boolean _verifyIsEntity(Class<?> entityClass) {
return entityClass.isAnnotationPresent(Entity.class);
}
private static <T> ArrayList<T> inflateFromCursor(Class<T> entityClass, Cursor c, int limit) {
ArrayList<T> returnList = new ArrayList<T>();
int i = 0;
try {
if (c != null) {
if (c.moveToFirst()) {
do {
T current = entityClass.newInstance();
Field[] fields = entityClass.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Column column = field.getAnnotation(Column.class);
if (column != null && (c.getColumnIndex(column.name()) >= 0)) {
if (field.getType() == String.class) {
field.set(current, c.getString(c.getColumnIndex(column.name())));
} else if (field.getType() == int.class) {
field.setInt(current, c.getInt(c.getColumnIndex(column.name())));
} else if (field.getType() == double.class) {
field.setDouble(current, c.getDouble(c.getColumnIndex(column.name())));
} else if (field.getType() == long.class) {
field.setLong(current, c.getLong(c.getColumnIndex(column.name())));
}
}
}
returnList.add(current);
} while (c.moveToNext() && ++i < limit);
c.close();
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} finally {
c.close();
}
return returnList;
}
private List<Pair<Field, Column>> _getColumns(Class<?> entityClass) {
List<Pair<Field, Column>> fieldCoumnPairs = fieldColumnPairCache.get(entityClass);
if (fieldCoumnPairs == null) {
fieldCoumnPairs = new ArrayList<Pair<Field, Column>>();
Field[] fields = entityClass.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Column column = field.getAnnotation(Column.class);
if (column != null) {
field.setAccessible(true);
fieldCoumnPairs.add(new Pair<Field, Column>(field, column));
}
}
fieldColumnPairCache.put(entityClass, fieldCoumnPairs);
}
return fieldCoumnPairs;
}
}