|
|
@ -25,6 +25,8 @@ import com.arialyy.aria.core.download.DTaskWrapper; |
|
|
|
import com.arialyy.aria.core.download.DownloadEntity; |
|
|
|
import com.arialyy.aria.core.download.DownloadEntity; |
|
|
|
import com.arialyy.aria.util.ALog; |
|
|
|
import com.arialyy.aria.util.ALog; |
|
|
|
import java.lang.reflect.Field; |
|
|
|
import java.lang.reflect.Field; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
@ -80,23 +82,29 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
if (oldVersion < newVersion) { |
|
|
|
if (oldVersion < newVersion) { |
|
|
|
if (oldVersion < 31) { |
|
|
|
if (oldVersion < 31) { |
|
|
|
handle314AriaUpdate(db); |
|
|
|
handle314AriaUpdate(db); |
|
|
|
|
|
|
|
} else if (oldVersion < 45) { |
|
|
|
|
|
|
|
handle360AriaUpdate(db); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
handleDbUpdate(db); |
|
|
|
handleDbUpdate(db, null, null); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
|
|
|
@Override public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
|
|
|
//super.onDowngrade(db, oldVersion, newVersion);
|
|
|
|
|
|
|
|
if (oldVersion > newVersion) { |
|
|
|
if (oldVersion > newVersion) { |
|
|
|
handleDbUpdate(db); |
|
|
|
handleDbUpdate(db, null, null); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 处理数据库升级 |
|
|
|
* 处理数据库升级,该段代码无法修改表字段 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param modifyColumns 需要修改的表字段的映射,key为表名, |
|
|
|
|
|
|
|
* value{@code Map<String, String>}中的Map的key为老字段名称,value为该老字段对应的新字段名称 |
|
|
|
|
|
|
|
* @param delColumns 需要删除的表字段,key为表名,value{@code List<String>}为需要删除的字段列表 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void handleDbUpdate(SQLiteDatabase db) { |
|
|
|
private void handleDbUpdate(SQLiteDatabase db, Map<String, Map<String, String>> modifyColumns, |
|
|
|
|
|
|
|
Map<String, List<String>> delColumns) { |
|
|
|
if (db == null) { |
|
|
|
if (db == null) { |
|
|
|
ALog.e("SqlHelper", "db 为 null"); |
|
|
|
ALog.e("SqlHelper", "db 为 null"); |
|
|
|
return; |
|
|
|
return; |
|
|
@ -104,128 +112,152 @@ final class SqlHelper extends SQLiteOpenHelper { |
|
|
|
ALog.e("SqlHelper", "db已关闭"); |
|
|
|
ALog.e("SqlHelper", "db已关闭"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
Set<String> tables = DBConfig.mapping.keySet(); |
|
|
|
|
|
|
|
for (String tableName : tables) { |
|
|
|
try { |
|
|
|
Class clazz = DBConfig.mapping.get(tableName); |
|
|
|
db.beginTransaction(); |
|
|
|
if (mDelegate.tableExists(db, clazz)) { |
|
|
|
Set<String> tables = DBConfig.mapping.keySet(); |
|
|
|
String countColumnSql = "SELECT rowid FROM " + tableName; |
|
|
|
for (String tableName : tables) { |
|
|
|
Cursor cursor = db.rawQuery(countColumnSql, null); |
|
|
|
Class clazz = DBConfig.mapping.get(tableName); |
|
|
|
int dbColumnNum = cursor.getColumnCount(); |
|
|
|
if (mDelegate.tableExists(db, clazz)) { |
|
|
|
List<Field> fields = SqlUtil.getAllNotIgnoreField(clazz); |
|
|
|
|
|
|
|
int newEntityColumnNum = (fields == null || fields.isEmpty()) ? 0 : fields.size(); |
|
|
|
|
|
|
|
if (dbColumnNum != newEntityColumnNum) { |
|
|
|
|
|
|
|
db = mDelegate.checkDb(db); |
|
|
|
db = mDelegate.checkDb(db); |
|
|
|
//备份数据
|
|
|
|
|
|
|
|
List<DbEntity> list = |
|
|
|
|
|
|
|
DelegateManager.getInstance().getDelegate(DelegateFind.class).findAllData(db, clazz); |
|
|
|
|
|
|
|
//修改表名为中介表名
|
|
|
|
//修改表名为中介表名
|
|
|
|
String alertSql = "ALTER TABLE " + tableName + " RENAME TO " + tableName + "_temp"; |
|
|
|
String alertSql = String.format("ALTER TABLE %s RENAME TO %s_temp", tableName, tableName); |
|
|
|
//db.beginTransaction();
|
|
|
|
|
|
|
|
db.execSQL(alertSql); |
|
|
|
db.execSQL(alertSql); |
|
|
|
|
|
|
|
|
|
|
|
//创建一个原本的表
|
|
|
|
//创建新表
|
|
|
|
mDelegate.createTable(db, clazz); |
|
|
|
mDelegate.createTable(db, clazz); |
|
|
|
//传入原来的数据
|
|
|
|
|
|
|
|
if (list != null && list.size() > 0) { |
|
|
|
String sql = String.format("SELECT COUNT(*) FROM %s_temp", tableName); |
|
|
|
DelegateUpdate update = DelegateManager.getInstance().getDelegate(DelegateUpdate.class); |
|
|
|
Cursor cursor = db.rawQuery(sql, null); |
|
|
|
for (DbEntity entity : list) { |
|
|
|
cursor.moveToFirst(); |
|
|
|
update.insertData(db, entity); |
|
|
|
long count = cursor.getLong(0); |
|
|
|
|
|
|
|
cursor.close(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (count > 0) { |
|
|
|
|
|
|
|
// 获取所有表字段名称
|
|
|
|
|
|
|
|
Cursor columnC = |
|
|
|
|
|
|
|
db.rawQuery(String.format("PRAGMA table_info(%s_temp)", tableName), null); |
|
|
|
|
|
|
|
StringBuilder params = new StringBuilder(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (columnC.moveToNext()) { |
|
|
|
|
|
|
|
String columnName = columnC.getString(columnC.getColumnIndex("name")); |
|
|
|
|
|
|
|
if (delColumns != null && delColumns.get(tableName) != null) { |
|
|
|
|
|
|
|
List<String> delColumn = delColumns.get(tableName); |
|
|
|
|
|
|
|
if (delColumn != null && !delColumn.isEmpty()) { |
|
|
|
|
|
|
|
if (delColumn.contains(columnName)) { |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
params.append(columnName).append(","); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
columnC.close(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String oldParamStr = params.toString(); |
|
|
|
|
|
|
|
oldParamStr = oldParamStr.substring(0, oldParamStr.length() - 1); |
|
|
|
|
|
|
|
String newParamStr = oldParamStr; |
|
|
|
|
|
|
|
// 处理字段名称改变
|
|
|
|
|
|
|
|
if (modifyColumns != null) { |
|
|
|
|
|
|
|
//newParamStr = params.toString();
|
|
|
|
|
|
|
|
Map<String, String> columnMap = modifyColumns.get(tableName); |
|
|
|
|
|
|
|
if (columnMap != null && !columnMap.isEmpty()) { |
|
|
|
|
|
|
|
Set<String> keys = columnMap.keySet(); |
|
|
|
|
|
|
|
for (String key : keys) { |
|
|
|
|
|
|
|
if (newParamStr.contains(key)) { |
|
|
|
|
|
|
|
newParamStr = newParamStr.replace(key, columnMap.get(key)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//恢复数据
|
|
|
|
|
|
|
|
String insertSql = |
|
|
|
|
|
|
|
String.format("INSERT INTO %s (%s) SELECT %s FROM %s_temp", tableName, newParamStr, |
|
|
|
|
|
|
|
oldParamStr, tableName); |
|
|
|
|
|
|
|
ALog.d(TAG, "insertSql = " + insertSql); |
|
|
|
|
|
|
|
db.execSQL(insertSql); |
|
|
|
} |
|
|
|
} |
|
|
|
//删除中介表
|
|
|
|
//删除中介表
|
|
|
|
mDelegate.dropTable(db, tableName + "_temp"); |
|
|
|
mDelegate.dropTable(db, tableName + "_temp"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
db.setTransactionSuccessful(); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
ALog.e(TAG, e); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
db.endTransaction(); |
|
|
|
} |
|
|
|
} |
|
|
|
//db.setTransactionSuccessful();
|
|
|
|
|
|
|
|
//db.endTransaction();
|
|
|
|
|
|
|
|
mDelegate.close(db); |
|
|
|
mDelegate.close(db); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 处理3.6以下版本的数据库升级 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private void handle360AriaUpdate(SQLiteDatabase db) { |
|
|
|
|
|
|
|
String[] taskTables = |
|
|
|
|
|
|
|
new String[] { "UploadTaskEntity", "DownloadTaskEntity", "DownloadGroupTaskEntity" }; |
|
|
|
|
|
|
|
for (String taskTable : taskTables) { |
|
|
|
|
|
|
|
if (mDelegate.tableExists(db, taskTable)) { |
|
|
|
|
|
|
|
mDelegate.dropTable(db, taskTable); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Map<String, Map<String, String>> columnMap = new HashMap<>(); |
|
|
|
|
|
|
|
Map<String, String> map = new HashMap<>(); |
|
|
|
|
|
|
|
map.put("groupName", "groupHash"); |
|
|
|
|
|
|
|
columnMap.put("DownloadEntity", map); |
|
|
|
|
|
|
|
columnMap.put("DownloadGroupEntity", map); |
|
|
|
|
|
|
|
handleDbUpdate(db, columnMap, null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 处理3.4版本之前数据库迁移,主要是修改子表外键字段对应的值 |
|
|
|
* 处理3.4版本之前数据库迁移,主要是修改子表外键字段对应的值 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void handle314AriaUpdate(SQLiteDatabase db) { |
|
|
|
private void handle314AriaUpdate(SQLiteDatabase db) { |
|
|
|
Set<String> tables = DBConfig.mapping.keySet(); |
|
|
|
String[] taskTables = |
|
|
|
Map<String, List<DbEntity>> map = new HashMap<>(); |
|
|
|
new String[] { "UploadTaskEntity", "DownloadTaskEntity", "DownloadGroupTaskEntity" }; |
|
|
|
for (String tableName : tables) { |
|
|
|
for (String taskTable : taskTables) { |
|
|
|
Class clazz = DBConfig.mapping.get(tableName); |
|
|
|
if (mDelegate.tableExists(db, taskTable)) { |
|
|
|
|
|
|
|
mDelegate.dropTable(db, taskTable); |
|
|
|
String pColumn = SqlUtil.getPrimaryName(clazz); |
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(pColumn)) { |
|
|
|
|
|
|
|
//删除所有主键为null的数据
|
|
|
|
|
|
|
|
String nullSql = |
|
|
|
|
|
|
|
"DELETE FROM " + tableName + " WHERE " + pColumn + " = '' OR " + pColumn + " IS NULL"; |
|
|
|
|
|
|
|
ALog.d(TAG, nullSql); |
|
|
|
|
|
|
|
db.execSQL(nullSql); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//删除所有主键重复的数据
|
|
|
|
|
|
|
|
String repeatSql = "DELETE FROM " |
|
|
|
|
|
|
|
+ tableName |
|
|
|
|
|
|
|
+ " WHERE " |
|
|
|
|
|
|
|
+ pColumn |
|
|
|
|
|
|
|
+ " in (SELECT " |
|
|
|
|
|
|
|
+ pColumn |
|
|
|
|
|
|
|
+ " FROM " |
|
|
|
|
|
|
|
+ tableName |
|
|
|
|
|
|
|
+ " GROUP BY " + pColumn + " having count(" + pColumn + ") > 1)"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ALog.d(TAG, repeatSql); |
|
|
|
|
|
|
|
db.execSQL(repeatSql); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//备份数据
|
|
|
|
//删除所有主键为null和逐渐重复的数据
|
|
|
|
List<DbEntity> list = |
|
|
|
String[] tables = new String[] { "DownloadEntity", "DownloadGroupEntity" }; |
|
|
|
DelegateManager.getInstance().getDelegate(DelegateFind.class).findAllData(db, clazz); |
|
|
|
String[] keys = new String[] { "downloadPath", "groupName" }; |
|
|
|
|
|
|
|
int i = 0; |
|
|
|
map.put(tableName, list); |
|
|
|
for (String tableName : tables) { |
|
|
|
|
|
|
|
String pColumn = keys[i]; |
|
|
|
//修改表名为中介表名
|
|
|
|
String nullSql = |
|
|
|
String alertSql = "ALTER TABLE " + tableName + " RENAME TO " + tableName + "_temp"; |
|
|
|
String.format("DELETE FROM %s WHERE %s='' OR %s IS NULL", tableName, pColumn, pColumn); |
|
|
|
db.execSQL(alertSql); |
|
|
|
ALog.d(TAG, nullSql); |
|
|
|
|
|
|
|
db.execSQL(nullSql); |
|
|
|
//创建一个原本的表
|
|
|
|
|
|
|
|
mDelegate.createTable(db, clazz); |
|
|
|
//删除所有主键重复的数据
|
|
|
|
//插入数据,3.6版本后移除任务实体的保存
|
|
|
|
String repeatSql = |
|
|
|
//if (list != null && list.size() > 0) {
|
|
|
|
String.format( |
|
|
|
// DelegateUpdate update = DelegateManager.getInstance().getDelegate(DelegateUpdate.class);
|
|
|
|
"DELETE FROM %s WHERE %s IN(SELECT %s FROM %s GROUP BY %s HAVING COUNT(%s) > 1)", |
|
|
|
// try {
|
|
|
|
tableName, pColumn, pColumn, tableName, pColumn, pColumn); |
|
|
|
// for (DbEntity entity : list) {
|
|
|
|
|
|
|
|
// if (entity instanceof DTaskWrapper && ((DTaskWrapper) entity).isGroupTask()) {
|
|
|
|
ALog.d(TAG, repeatSql); |
|
|
|
// if (TextUtils.isEmpty(((DTaskWrapper) entity).getKey())) {
|
|
|
|
db.execSQL(repeatSql); |
|
|
|
// ALog.w(TAG, "DownloadTaskEntity的key为空,将忽略该条数据");
|
|
|
|
i++; |
|
|
|
// continue;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (TextUtils.isEmpty(((DTaskWrapper) entity).getUrl())) {
|
|
|
|
|
|
|
|
// List<DbEntity> temp = map.get("DownloadEntity");
|
|
|
|
|
|
|
|
// boolean isRefresh = false;
|
|
|
|
|
|
|
|
// for (DbEntity dbEntity : temp) {
|
|
|
|
|
|
|
|
// if (((DownloadEntity) dbEntity).getDownloadPath()
|
|
|
|
|
|
|
|
// .equals(((DTaskWrapper) entity).getKey())) {
|
|
|
|
|
|
|
|
// ((DTaskWrapper) entity).setUrl(((DownloadEntity) dbEntity).getUrl());
|
|
|
|
|
|
|
|
// isRefresh = true;
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (isRefresh) {
|
|
|
|
|
|
|
|
// update.insertData(db, entity);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// update.insertData(db, entity);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// } catch (Exception e) {
|
|
|
|
|
|
|
|
// ALog.e(TAG, ALog.getExceptionString(e));
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//删除中介表
|
|
|
|
|
|
|
|
mDelegate.dropTable(db, tableName + "_temp"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mDelegate.close(db); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
map.clear(); |
|
|
|
Map<String, Map<String, String>> modifyColumnMap = new HashMap<>(); |
|
|
|
|
|
|
|
Map<String, String> map = new HashMap<>(); |
|
|
|
|
|
|
|
map.put("groupName", "groupHash"); |
|
|
|
|
|
|
|
modifyColumnMap.put("DownloadEntity", map); |
|
|
|
|
|
|
|
modifyColumnMap.put("DownloadGroupEntity", map); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, List<String>> delColumnMap = new HashMap<>(); |
|
|
|
|
|
|
|
List<String> dEntityDel = new ArrayList<>(); |
|
|
|
|
|
|
|
dEntityDel.add("taskKey"); |
|
|
|
|
|
|
|
delColumnMap.put("DownloadEntity", dEntityDel); |
|
|
|
|
|
|
|
List<String> dgEntityDel = new ArrayList<>(); |
|
|
|
|
|
|
|
dgEntityDel.add("subtask"); |
|
|
|
|
|
|
|
delColumnMap.put("DownloadGroupEntity", dgEntityDel); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handleDbUpdate(db, modifyColumnMap, delColumnMap); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |