/** * 检查表是否存在,存在的话检查是否需要改动,添加列字段。 注:sqlite仅仅支持表改名、表添加列两种alter方式。表中修改、刪除列是不被直接支持的。 不能新加主键:The column * may not have a PRIMARY KEY or UNIQUE constraint. * * <p>http://www.sqlite.org/lang_altertable.html */ private boolean checkExistAndColumns(SQLiteDatabase db, EntityTable entityTable) { if (!Checker.isEmpty(mSqlTableList)) { for (SQLiteTable sqlTable : mSqlTableList) { if (entityTable.name.equals(sqlTable.name)) { if (Log.isPrint) Log.d(TAG, "Table [" + entityTable.name + "] Exist"); if (!sqlTable.isTableChecked) { // 表仅进行一次检查,检验是否有新字段加入。 sqlTable.isTableChecked = true; if (Log.isPrint) Log.d(TAG, "Table [" + entityTable.name + "] check column now."); ArrayList<String> newColumns = null; if (entityTable.pmap != null) { for (String col : entityTable.pmap.keySet()) { if (!sqlTable.columns.contains(col)) { if (newColumns == null) newColumns = new ArrayList<String>(); newColumns.add(col); } } } if (!Checker.isEmpty(newColumns)) { sqlTable.columns.addAll(newColumns); int sum = insertNewColunms(db, entityTable.name, newColumns); if (Log.isPrint) Log.i(TAG, "Table [" + entityTable.name + "] add " + sum + " new column"); } } return true; } } } if (Log.isPrint) Log.d(TAG, "Table [" + entityTable.name + "] Not Exist"); return false; }
/** * 将Sql Table放入存储集合 * * @param table */ private void putSqlTableIntoList(EntityTable table) { if (Log.isPrint) Log.i(TAG, "Table [" + table.name + "] Create Success"); SQLiteTable sqlTable = new SQLiteTable(); sqlTable.name = table.name; sqlTable.columns = new ArrayList<String>(); if (table.key != null) sqlTable.columns.add(table.key.column); if (table.pmap != null) { for (String col : table.pmap.keySet()) { sqlTable.columns.add(col); } } if (mSqlTableList != null) mSqlTableList.add(sqlTable); }
/** * 将Sql Table放入存储集合 * * @param table */ private void putNewSqlTableIntoMap(EntityTable table) { if (Log.isPrint) { Log.i(TAG, "Table [" + table.name + "] Create Success"); } SQLiteTable sqlTable = new SQLiteTable(); sqlTable.name = table.name; sqlTable.columns = new HashMap<String, Integer>(); if (table.key != null) { sqlTable.columns.put(table.key.column, 1); } if (table.pmap != null) { for (String col : table.pmap.keySet()) { sqlTable.columns.put(col, 1); } } // 第一次建表,不用检查 sqlTable.isTableChecked = true; mSqlTableMap.put(sqlTable.name, sqlTable); }
/** * 检查表是否存在,存在的话检查是否需要改动,添加列字段。 注:sqlite仅仅支持表改名、表添加列两种alter方式。表中修改、刪除列是不被直接支持的。 不能新加主键:The column * may not have a PRIMARY KEY or UNIQUE constraint. * * <p>http://www.sqlite.org/lang_altertable.html */ private boolean checkExistAndColumns(SQLiteDatabase db, EntityTable entityTable) { if (!Checker.isEmpty(mSqlTableMap)) { SQLiteTable sqlTable = mSqlTableMap.get(entityTable.name); if (sqlTable != null) { if (Log.isPrint) { Log.d(TAG, "Table [" + entityTable.name + "] Exist"); } if (!sqlTable.isTableChecked) { // 表仅进行一次检查,检验是否有新字段加入。 sqlTable.isTableChecked = true; if (Log.isPrint) { Log.i(TAG, "Table [" + entityTable.name + "] check column now."); } if (entityTable.key != null) { if (sqlTable.columns.get(entityTable.key.column) == null) { SQLStatement stmt = SQLBuilder.buildDropTable(sqlTable.name); stmt.execute(db); if (Log.isPrint) { Log.i( TAG, "Table [" + entityTable.name + "] Primary Key has changed, " + "so drop and recreate it later."); } return false; } } if (entityTable.pmap != null) { ArrayList<String> newColumns = new ArrayList<String>(); for (String col : entityTable.pmap.keySet()) { if (sqlTable.columns.get(col) == null) { newColumns.add(col); } } if (!Checker.isEmpty(newColumns)) { for (String col : newColumns) { sqlTable.columns.put(col, 1); } int sum = insertNewColunms(db, entityTable.name, newColumns); if (Log.isPrint) { if (sum > 0) { Log.i( TAG, "Table [" + entityTable.name + "] add " + sum + " new column : " + newColumns); } else { Log.e( TAG, "Table [" + entityTable.name + "] add " + sum + " new column error : " + newColumns); } } } } } return true; } } if (Log.isPrint) { Log.d(TAG, "Table [" + entityTable.name + "] Not Exist"); } return false; }