MCPcopy Create free account
hub / github.com/wyouflf/xUtils3 / DbBase

Class DbBase

xutils/src/main/java/org/xutils/db/table/DbBase.java:16–99  ·  view source on GitHub ↗

DbManager基类, 包含表结构的基本操作. Created by wyouflf on 16/1/22.

Source from the content-addressed store, hash-verified

14 * Created by wyouflf on 16/1/22.
15 */
16public abstract class DbBase implements DbManager {
17
18 private final HashMap<Class<?>, TableEntity<?>> tableMap = new HashMap<Class<?>, TableEntity<?>>();
19
20 @Override
21 @SuppressWarnings("unchecked")
22 public <T> TableEntity<T> getTable(Class<T> entityType) throws DbException {
23 synchronized (tableMap) {
24 TableEntity<T> table = (TableEntity<T>) tableMap.get(entityType);
25 if (table == null) {
26 try {
27 table = new TableEntity<T>(this, entityType);
28 } catch (DbException ex) {
29 throw ex;
30 } catch (Throwable ex) {
31 throw new DbException(ex);
32 }
33 tableMap.put(entityType, table);
34 }
35
36 return table;
37 }
38 }
39
40 @Override
41 public void dropTable(Class<?> entityType) throws DbException {
42 TableEntity<?> table = this.getTable(entityType);
43 if (!table.tableIsExists()) return;
44 execNonQuery("DROP TABLE \"" + table.getName() + "\"");
45 table.setTableCheckedStatus(false);
46 this.removeTable(entityType);
47 }
48
49 @Override
50 public void dropDb() throws DbException {
51 Cursor cursor = execQuery("SELECT name FROM sqlite_master WHERE type='table' AND name<>'sqlite_sequence'");
52 if (cursor != null) {
53 try {
54 while (cursor.moveToNext()) {
55 try {
56 String tableName = cursor.getString(0);
57 execNonQuery("DROP TABLE " + tableName);
58 } catch (Throwable e) {
59 LogUtil.e(e.getMessage(), e);
60 }
61 }
62
63 synchronized (tableMap) {
64 for (TableEntity<?> table : tableMap.values()) {
65 table.setTableCheckedStatus(false);
66 }
67 tableMap.clear();
68 }
69 } catch (Throwable e) {
70 throw new DbException(e);
71 } finally {
72 IOUtil.closeQuietly(cursor);
73 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected