DbManager基类, 包含表结构的基本操作. Created by wyouflf on 16/1/22.
| 14 | * Created by wyouflf on 16/1/22. |
| 15 | */ |
| 16 | public 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected