DBHelper: don't wrap entire func bodies in ifs

This removes tons of unnecessary indenting. Do it in smaller functions
too for consistency.
This commit is contained in:
Daniel Martí 2016-04-23 15:40:09 +01:00
parent 7f2a811541
commit 6a0eec1262

View File

@ -116,7 +116,9 @@ class DBHelper extends SQLiteOpenHelper {
} }
private void populateRepoNames(SQLiteDatabase db, int oldVersion) { private void populateRepoNames(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 37) { if (oldVersion >= 37) {
return;
}
Utils.debugLog(TAG, "Populating repo names from the url"); Utils.debugLog(TAG, "Populating repo names from the url");
final String[] columns = {"address", "_id"}; final String[] columns = {"address", "_id"};
Cursor cursor = db.query(TABLE_REPO, columns, Cursor cursor = db.query(TABLE_REPO, columns,
@ -139,10 +141,11 @@ class DBHelper extends SQLiteOpenHelper {
cursor.close(); cursor.close();
} }
} }
}
private void renameRepoId(SQLiteDatabase db, int oldVersion) { private void renameRepoId(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 36 && !columnExists(db, TABLE_REPO, "_id")) { if (oldVersion >= 36 || columnExists(db, TABLE_REPO, "_id")) {
return;
}
Utils.debugLog(TAG, "Renaming " + TABLE_REPO + ".id to _id"); Utils.debugLog(TAG, "Renaming " + TABLE_REPO + ".id to _id");
db.beginTransaction(); db.beginTransaction();
@ -190,7 +193,6 @@ class DBHelper extends SQLiteOpenHelper {
} }
db.endTransaction(); db.endTransaction();
} }
}
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
@ -299,7 +301,9 @@ class DBHelper extends SQLiteOpenHelper {
* key in sqlite - table must be recreated). * key in sqlite - table must be recreated).
*/ */
private void migrateRepoTable(SQLiteDatabase db, int oldVersion) { private void migrateRepoTable(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 20) { if (oldVersion >= 20) {
return;
}
List<Repo> oldrepos = new ArrayList<>(); List<Repo> oldrepos = new ArrayList<>();
Cursor cursor = db.query(TABLE_REPO, Cursor cursor = db.query(TABLE_REPO,
new String[] {"address", "inuse", "pubkey"}, new String[] {"address", "inuse", "pubkey"},
@ -330,7 +334,6 @@ class DBHelper extends SQLiteOpenHelper {
db.insert(TABLE_REPO, null, values); db.insert(TABLE_REPO, null, values);
} }
} }
}
private void insertNameAndDescription(SQLiteDatabase db, private void insertNameAndDescription(SQLiteDatabase db,
int addressResId, int nameResId, int descriptionResId) { int addressResId, int nameResId, int descriptionResId) {
@ -350,7 +353,9 @@ class DBHelper extends SQLiteOpenHelper {
private void addNameAndDescriptionToRepo(SQLiteDatabase db, int oldVersion) { private void addNameAndDescriptionToRepo(SQLiteDatabase db, int oldVersion) {
boolean nameExists = columnExists(db, TABLE_REPO, "name"); boolean nameExists = columnExists(db, TABLE_REPO, "name");
boolean descriptionExists = columnExists(db, TABLE_REPO, "description"); boolean descriptionExists = columnExists(db, TABLE_REPO, "description");
if (oldVersion < 21 && !(nameExists && descriptionExists)) { if (oldVersion >= 21 || (nameExists && descriptionExists)) {
return;
}
if (!nameExists) { if (!nameExists) {
db.execSQL("alter table " + TABLE_REPO + " add column name text"); db.execSQL("alter table " + TABLE_REPO + " add column name text");
} }
@ -365,7 +370,6 @@ class DBHelper extends SQLiteOpenHelper {
R.string.guardianproject_repo_name, R.string.guardianproject_repo_description); R.string.guardianproject_repo_name, R.string.guardianproject_repo_description);
insertNameAndDescription(db, R.string.guardianproject_archive_address, insertNameAndDescription(db, R.string.guardianproject_archive_address,
R.string.guardianproject_archive_name, R.string.guardianproject_archive_description); R.string.guardianproject_archive_name, R.string.guardianproject_archive_description);
}
} }
@ -374,7 +378,9 @@ class DBHelper extends SQLiteOpenHelper {
* calculate its fingerprint and save it to the database. * calculate its fingerprint and save it to the database.
*/ */
private void addFingerprintToRepo(SQLiteDatabase db, int oldVersion) { private void addFingerprintToRepo(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 44) { if (oldVersion >= 44) {
return;
}
if (!columnExists(db, TABLE_REPO, "fingerprint")) { if (!columnExists(db, TABLE_REPO, "fingerprint")) {
db.execSQL("alter table " + TABLE_REPO + " add column fingerprint text"); db.execSQL("alter table " + TABLE_REPO + " add column fingerprint text");
} }
@ -401,36 +407,41 @@ class DBHelper extends SQLiteOpenHelper {
db.update(TABLE_REPO, values, "address = ?", new String[] {repo.address}); db.update(TABLE_REPO, values, "address = ?", new String[] {repo.address});
} }
} }
}
private void addMaxAgeToRepo(SQLiteDatabase db, int oldVersion) { private void addMaxAgeToRepo(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 30 && !columnExists(db, TABLE_REPO, "maxage")) { if (oldVersion >= 30 || columnExists(db, TABLE_REPO, "maxage")) {
db.execSQL("alter table " + TABLE_REPO + " add column maxage integer not null default 0"); return;
} }
db.execSQL("alter table " + TABLE_REPO + " add column maxage integer not null default 0");
} }
private void addVersionToRepo(SQLiteDatabase db, int oldVersion) { private void addVersionToRepo(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 33 && !columnExists(db, TABLE_REPO, "version")) { if (oldVersion >= 33 || columnExists(db, TABLE_REPO, "version")) {
db.execSQL("alter table " + TABLE_REPO + " add column version integer not null default 0"); return;
} }
db.execSQL("alter table " + TABLE_REPO + " add column version integer not null default 0");
} }
private void addLastUpdatedToRepo(SQLiteDatabase db, int oldVersion) { private void addLastUpdatedToRepo(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 35 && !columnExists(db, TABLE_REPO, "lastUpdated")) { if (oldVersion >= 35 || columnExists(db, TABLE_REPO, "lastUpdated")) {
return;
}
Utils.debugLog(TAG, "Adding lastUpdated column to " + TABLE_REPO); Utils.debugLog(TAG, "Adding lastUpdated column to " + TABLE_REPO);
db.execSQL("Alter table " + TABLE_REPO + " add column lastUpdated string"); db.execSQL("Alter table " + TABLE_REPO + " add column lastUpdated string");
} }
}
private void addIsSwapToRepo(SQLiteDatabase db, int oldVersion) { private void addIsSwapToRepo(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 47 && !columnExists(db, TABLE_REPO, "isSwap")) { if (oldVersion >= 47 || columnExists(db, TABLE_REPO, "isSwap")) {
return;
}
Utils.debugLog(TAG, "Adding isSwap field to " + TABLE_REPO + " table in db."); Utils.debugLog(TAG, "Adding isSwap field to " + TABLE_REPO + " table in db.");
db.execSQL("alter table " + TABLE_REPO + " add column isSwap boolean default 0;"); db.execSQL("alter table " + TABLE_REPO + " add column isSwap boolean default 0;");
} }
}
private void addCredentialsToRepo(SQLiteDatabase db, int oldVersion) { private void addCredentialsToRepo(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 52) { if (oldVersion >= 52) {
return;
}
if (!columnExists(db, TABLE_REPO, "username")) { if (!columnExists(db, TABLE_REPO, "username")) {
Utils.debugLog(TAG, "Adding username field to " + TABLE_REPO + " table in db."); Utils.debugLog(TAG, "Adding username field to " + TABLE_REPO + " table in db.");
db.execSQL("alter table " + TABLE_REPO + " add column username string;"); db.execSQL("alter table " + TABLE_REPO + " add column username string;");
@ -441,49 +452,55 @@ class DBHelper extends SQLiteOpenHelper {
db.execSQL("alter table " + TABLE_REPO + " add column password string;"); db.execSQL("alter table " + TABLE_REPO + " add column password string;");
} }
} }
}
private void addChangelogToApp(SQLiteDatabase db, int oldVersion) { private void addChangelogToApp(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 48 && !columnExists(db, TABLE_APP, "changelogURL")) { if (oldVersion >= 48 || columnExists(db, TABLE_APP, "changelogURL")) {
return;
}
Utils.debugLog(TAG, "Adding changelogURL column to " + TABLE_APP); Utils.debugLog(TAG, "Adding changelogURL column to " + TABLE_APP);
db.execSQL("alter table " + TABLE_APP + " add column changelogURL text"); db.execSQL("alter table " + TABLE_APP + " add column changelogURL text");
} }
}
private void addIconUrlLargeToApp(SQLiteDatabase db, int oldVersion) { private void addIconUrlLargeToApp(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 49 && !columnExists(db, TABLE_APP, "iconUrlLarge")) { if (oldVersion >= 49 || columnExists(db, TABLE_APP, "iconUrlLarge")) {
return;
}
Utils.debugLog(TAG, "Adding iconUrlLarge columns to " + TABLE_APP); Utils.debugLog(TAG, "Adding iconUrlLarge columns to " + TABLE_APP);
db.execSQL("alter table " + TABLE_APP + " add column iconUrlLarge text"); db.execSQL("alter table " + TABLE_APP + " add column iconUrlLarge text");
} }
}
private void updateIconUrlLarge(SQLiteDatabase db, int oldVersion) { private void updateIconUrlLarge(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 50) { if (oldVersion >= 50) {
return;
}
Utils.debugLog(TAG, "Recalculating app icon URLs so that the newly added large icons will get updated."); Utils.debugLog(TAG, "Recalculating app icon URLs so that the newly added large icons will get updated.");
AppProvider.UpgradeHelper.updateIconUrls(context, db); AppProvider.UpgradeHelper.updateIconUrls(context, db);
clearRepoEtags(db); clearRepoEtags(db);
} }
}
private void addAuthorToApp(SQLiteDatabase db, int oldVersion) { private void addAuthorToApp(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 53 && !columnExists(db, TABLE_APP, "author")) { if (oldVersion >= 53) {
return;
}
if (!columnExists(db, TABLE_APP, "author")) {
Utils.debugLog(TAG, "Adding author column to " + TABLE_APP); Utils.debugLog(TAG, "Adding author column to " + TABLE_APP);
db.execSQL("alter table " + TABLE_APP + " add column author text"); db.execSQL("alter table " + TABLE_APP + " add column author text");
} }
if (oldVersion < 53 && !columnExists(db, TABLE_APP, "email")) { if (!columnExists(db, TABLE_APP, "email")) {
Utils.debugLog(TAG, "Adding email column to " + TABLE_APP); Utils.debugLog(TAG, "Adding email column to " + TABLE_APP);
db.execSQL("alter table " + TABLE_APP + " add column email text"); db.execSQL("alter table " + TABLE_APP + " add column email text");
} }
} }
private void useMaxValueInMaxSdkVersion(SQLiteDatabase db, int oldVersion) { private void useMaxValueInMaxSdkVersion(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 54) { if (oldVersion >= 54) {
return;
}
Utils.debugLog(TAG, "Converting maxSdkVersion value 0 to " + Byte.MAX_VALUE); Utils.debugLog(TAG, "Converting maxSdkVersion value 0 to " + Byte.MAX_VALUE);
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(ApkProvider.DataColumns.MAX_SDK_VERSION, Byte.MAX_VALUE); values.put(ApkProvider.DataColumns.MAX_SDK_VERSION, Byte.MAX_VALUE);
db.update(TABLE_APK, values, ApkProvider.DataColumns.MAX_SDK_VERSION + " < 1", null); db.update(TABLE_APK, values, ApkProvider.DataColumns.MAX_SDK_VERSION + " < 1", null);
} }
}
/** /**
* By clearing the etags stored in the repo table, it means that next time the user updates * By clearing the etags stored in the repo table, it means that next time the user updates
@ -501,7 +518,9 @@ class DBHelper extends SQLiteOpenHelper {
// was is specified by the user. We don't want to weely-neely nuke that data. // was is specified by the user. We don't want to weely-neely nuke that data.
// and the new way to deal with changes to the table structure is to add a // and the new way to deal with changes to the table structure is to add a
// if (oldVersion < x && !columnExists(...) and then alter the table as required. // if (oldVersion < x && !columnExists(...) and then alter the table as required.
if (oldVersion < 42) { if (oldVersion >= 42) {
return;
}
context.getSharedPreferences("FDroid", Context.MODE_PRIVATE).edit() context.getSharedPreferences("FDroid", Context.MODE_PRIVATE).edit()
.putBoolean("triedEmptyUpdate", false).commit(); .putBoolean("triedEmptyUpdate", false).commit();
db.execSQL("drop table " + TABLE_APP); db.execSQL("drop table " + TABLE_APP);
@ -509,7 +528,6 @@ class DBHelper extends SQLiteOpenHelper {
clearRepoEtags(db); clearRepoEtags(db);
createAppApk(db); createAppApk(db);
} }
}
private static void createAppApk(SQLiteDatabase db) { private static void createAppApk(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_APP); db.execSQL(CREATE_TABLE_APP);
@ -527,11 +545,12 @@ class DBHelper extends SQLiteOpenHelper {
// If any column was added or removed, just drop the table, create it // If any column was added or removed, just drop the table, create it
// again and let the cache be filled from scratch again. // again and let the cache be filled from scratch again.
private void recreateInstalledCache(SQLiteDatabase db, int oldVersion) { private void recreateInstalledCache(SQLiteDatabase db, int oldVersion) {
if (oldVersion < 51) { if (oldVersion >= 51) {
return;
}
db.execSQL(DROP_TABLE_INSTALLED_APP); db.execSQL(DROP_TABLE_INSTALLED_APP);
createInstalledApp(db); createInstalledApp(db);
} }
}
private static boolean columnExists(SQLiteDatabase db, private static boolean columnExists(SQLiteDatabase db,
String table, String column) { String table, String column) {