| | |
| | | import android.util.Pair; |
| | | |
| | | import com.basic.security.base.BaseApplication; |
| | | import com.basic.security.model.Log; |
| | | import com.basic.security.model.Setting; |
| | | import com.basic.security.utils.Constants; |
| | | |
| | | import java.io.File; |
| | | import java.lang.reflect.Field; |
| | | import java.lang.reflect.Method; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | |
| | | public static final Lock databaseLock = new ReentrantLock(); |
| | | public static final Lock databaseExecSqlLock = new ReentrantLock(); |
| | | public static Map<String, SQLiteDatabase> databaseMap = new HashMap<>(); |
| | | |
| | | public static SQLiteDatabase getDatabase() { |
| | | public static Class[] tableClasses = new Class[] { |
| | | Setting.class, |
| | | Log.class |
| | | }; |
| | | public static SQLiteDatabase getDatabase() {// |
| | | return getDatabase(Constants.databaseName); |
| | | } |
| | | |
| | | public static void intiTables() { |
| | | public static void intiTables() {// |
| | | for (Class tableClass : tableClasses) { |
| | | String table = tableClass.getSimpleName(); |
| | | Map<String, String> columns = new HashMap<>(); |
| | | Field[] declaredFields = tableClass.getDeclaredFields(); |
| | | for (Field field : declaredFields) { |
| | | try { |
| | | String fieldName = field.getName(); |
| | | String value = (String)field.get(null); |
| | | if (value != null) { |
| | | if ("table".equals(fieldName)) { |
| | | table = value; |
| | | } else { |
| | | columns.put(value, value); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | System.out.println("reflect "+e.getMessage()); |
| | | } |
| | | } |
| | | columns.put("id", "id"); |
| | | |
| | | String createTable = "CREATE TABLE IF NOT EXISTS " + table + " ("; |
| | | for (String column : columns.keySet()) { |
| | | createTable += column + " TEXT, "; |
| | | } |
| | | createTable += " PRIMARY KEY(id))"; |
| | | if (table.equals("RealTimeMetrics")) { |
| | | createTable = createTable.replace("TEXT", "INTEGER"); |
| | | createTable = createTable.replace("id INTEGER", "id TEXT"); |
| | | // System.out.println("createTable="+createTable); |
| | | } |
| | | DatabaseManager.execSQL(createTable); |
| | | String createIndex = "CREATE UNIQUE INDEX IF NOT EXISTS idx_"+table+" ON "+table+"(id)"; |
| | | DatabaseManager.execSQL(createIndex); |
| | | } |
| | | |
| | | |
| | | DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS alarm (\"matchPersonListStr\" TEXT, property TEXT, \"alarmLargePicture\" TEXT,\"alarmPerson\" TEXT,\"alarmPicture\" TEXT,\"alarmVideo\" TEXT,\"alarmTime\" TEXT,\"alarmAddress\" TEXT,\"alarmType\" TEXT,\"id\" TEXT, \"createTime\" TEXT, \"closeAlarm\" TEXT, \"mute\" TEXT, PRIMARY KEY(id))"); |
| | | try { |
| | | DatabaseManager.execSQL("ALTER TABLE alarm ADD COLUMN imgKey text"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | try { |
| | | DatabaseManager.execSQL("ALTER TABLE alarm ADD COLUMN indeviceid text"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | try { |
| | | DatabaseManager.execSQL("ALTER TABLE alarm ADD COLUMN videoReqNum text"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | try { |
| | | DatabaseManager.execSQL("ALTER TABLE alarm ADD COLUMN picDate text"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | try { |
| | | DatabaseManager.execSQL("ALTER TABLE alarm ADD COLUMN alarmLevel text"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_alarm ON alarm(id)"); |
| | | DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS setting (\"id\" TEXT, \"name\" TEXT, PRIMARY KEY(id))"); |
| | | DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_setting ON setting (id)"); |
| | | // DatabaseManager.execSQL("CREATE TABLE IF NOT EXISTS setting (\"id\" TEXT, \"name\" TEXT, PRIMARY KEY(id))"); |
| | | // DatabaseManager.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS idx_setting ON setting (id)"); |
| | | } |
| | | |
| | | public static SQLiteDatabase getDatabase(String databaseName) { |
| | | public static SQLiteDatabase getDatabase(String databaseName) {// |
| | | SQLiteDatabase database = databaseMap.get(databaseName); |
| | | try { |
| | | databaseLock.lock(); |
| | | if (database == null) { |
| | | database = BaseApplication.getApplication().openOrCreateDatabase(Constants.databaseName, MODE_PRIVATE, null); |
| | | // database = BaseApplication.getApplication().openOrCreateDatabase(Constants.databaseName, MODE_PRIVATE, null); |
| | | database = SQLiteDatabase.openOrCreateDatabase(databaseName, null); |
| | | databaseMap.put(databaseName, database); |
| | | intiTables(); |
| | | setCustomDatabaseFiles(BaseApplication.getApplication()); |
| | |
| | | } |
| | | } |
| | | |
| | | private static void initTableColumnNames() { |
| | | private static void initTableColumnNames() {// |
| | | SQLiteDatabase mDataBase = DatabaseManager.getDatabase(); |
| | | Cursor c = mDataBase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null); |
| | | List<String> tableNameList = new ArrayList<>(); |