From 63a609fbabc273f4fac72002f7e22551f12bfe94 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Tue, 18 Oct 2016 18:00:10 +1100 Subject: [PATCH] Moved methods away from top of `DBHelper` class. The fact there are arbitrary migrations at the top of the file (between `onCreate()` and `onUpdate()` makes it harder to scan this file. This changeset moves these methods verbatim, without changing any of the method bodies or signatures. --- .../java/org/fdroid/fdroid/data/DBHelper.java | 248 +++++++++--------- 1 file changed, 124 insertions(+), 124 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java b/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java index 0cd82c8b2..000b98b1e 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java +++ b/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java @@ -166,96 +166,6 @@ class DBHelper extends SQLiteOpenHelper { this.context = context; } - private void populateRepoNames(SQLiteDatabase db, int oldVersion) { - if (oldVersion >= 37) { - return; - } - Utils.debugLog(TAG, "Populating repo names from the url"); - final String[] columns = {RepoTable.Cols.ADDRESS, RepoTable.Cols._ID}; - Cursor cursor = db.query(RepoTable.NAME, columns, - RepoTable.Cols.NAME + " IS NULL OR " + RepoTable.Cols.NAME + " = ''", null, null, null, null); - if (cursor != null) { - if (cursor.getCount() > 0) { - cursor.moveToFirst(); - while (!cursor.isAfterLast()) { - String address = cursor.getString(0); - long id = cursor.getInt(1); - ContentValues values = new ContentValues(1); - String name = Repo.addressToName(address); - values.put(RepoTable.Cols.NAME, name); - final String[] args = {Long.toString(id)}; - Utils.debugLog(TAG, "Setting repo name to '" + name + "' for repo " + address); - db.update(RepoTable.NAME, values, RepoTable.Cols._ID + " = ?", args); - cursor.moveToNext(); - } - } - cursor.close(); - } - } - - private void renameRepoId(SQLiteDatabase db, int oldVersion) { - if (oldVersion >= 36 || columnExists(db, RepoTable.NAME, RepoTable.Cols._ID)) { - return; - } - - Utils.debugLog(TAG, "Renaming " + RepoTable.NAME + ".id to " + RepoTable.Cols._ID); - db.beginTransaction(); - - try { - // http://stackoverflow.com/questions/805363/how-do-i-rename-a-column-in-a-sqlite-database-table#805508 - String tempTableName = RepoTable.NAME + "__temp__"; - db.execSQL("ALTER TABLE " + RepoTable.NAME + " RENAME TO " + tempTableName + ";"); - - // I realise this is available in the CREATE_TABLE_REPO above, - // however I have a feeling that it will need to be the same as the - // current structure of the table as of DBVersion 36, or else we may - // get into strife. For example, if there was a field that - // got removed, then it will break the "insert select" - // statement. Therefore, I've put a copy of CREATE_TABLE_REPO - // here that is the same as it was at DBVersion 36. - String createTableDdl = "create table " + RepoTable.NAME + " (" - + RepoTable.Cols._ID + " integer not null primary key, " - + RepoTable.Cols.ADDRESS + " text not null, " - + RepoTable.Cols.NAME + " text, " - + RepoTable.Cols.DESCRIPTION + " text, " - + RepoTable.Cols.IN_USE + " integer not null, " - + RepoTable.Cols.PRIORITY + " integer not null, " - + RepoTable.Cols.SIGNING_CERT + " text, " - + RepoTable.Cols.FINGERPRINT + " text, " - + RepoTable.Cols.MAX_AGE + " integer not null default 0, " - + RepoTable.Cols.VERSION + " integer not null default 0, " - + RepoTable.Cols.LAST_ETAG + " text, " - + RepoTable.Cols.LAST_UPDATED + " string);"; - - db.execSQL(createTableDdl); - - String nonIdFields = TextUtils.join(", ", new String[] { - RepoTable.Cols.ADDRESS, - RepoTable.Cols.NAME, - RepoTable.Cols.DESCRIPTION, - RepoTable.Cols.IN_USE, - RepoTable.Cols.PRIORITY, - RepoTable.Cols.SIGNING_CERT, - RepoTable.Cols.FINGERPRINT, - RepoTable.Cols.MAX_AGE, - RepoTable.Cols.VERSION, - RepoTable.Cols.LAST_ETAG, - RepoTable.Cols.LAST_UPDATED, - }); - - String insertSql = "INSERT INTO " + RepoTable.NAME + - "(" + RepoTable.Cols._ID + ", " + nonIdFields + " ) " + - "SELECT id, " + nonIdFields + " FROM " + tempTableName + ";"; - - db.execSQL(insertSql); - db.execSQL("DROP TABLE " + tempTableName + ";"); - db.setTransactionSuccessful(); - } catch (Exception e) { - Log.e(TAG, "Error renaming id to " + RepoTable.Cols._ID, e); - } - db.endTransaction(); - } - @Override public void onCreate(SQLiteDatabase db) { @@ -288,40 +198,6 @@ class DBHelper extends SQLiteOpenHelper { } } - private void insertRepo(SQLiteDatabase db, String name, String address, - String description, String version, String enabled, - String priority, String pushRequests, String pubKey) { - ContentValues values = new ContentValues(); - values.put(RepoTable.Cols.ADDRESS, address); - values.put(RepoTable.Cols.NAME, name); - values.put(RepoTable.Cols.DESCRIPTION, description); - values.put(RepoTable.Cols.SIGNING_CERT, pubKey); - values.put(RepoTable.Cols.FINGERPRINT, Utils.calcFingerprint(pubKey)); - values.put(RepoTable.Cols.MAX_AGE, 0); - values.put(RepoTable.Cols.VERSION, Utils.parseInt(version, 0)); - values.put(RepoTable.Cols.IN_USE, Utils.parseInt(enabled, 0)); - values.put(RepoTable.Cols.PRIORITY, Utils.parseInt(priority, Integer.MAX_VALUE)); - values.put(RepoTable.Cols.LAST_ETAG, (String) null); - values.put(RepoTable.Cols.TIMESTAMP, 0); - - switch (pushRequests) { - case "ignore": - values.put(RepoTable.Cols.PUSH_REQUESTS, Repo.PUSH_REQUEST_IGNORE); - break; - case "prompt": - values.put(RepoTable.Cols.PUSH_REQUESTS, Repo.PUSH_REQUEST_PROMPT); - break; - case "always": - values.put(RepoTable.Cols.PUSH_REQUESTS, Repo.PUSH_REQUEST_ACCEPT_ALWAYS); - break; - default: - throw new IllegalArgumentException(pushRequests + " is not a supported option!"); - } - - Utils.debugLog(TAG, "Adding repository " + name + " with push requests as " + pushRequests); - db.insert(RepoTable.NAME, null, values); - } - @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { @@ -726,6 +602,96 @@ class DBHelper extends SQLiteOpenHelper { db.execSQL("Alter table " + RepoTable.NAME + " add column " + RepoTable.Cols.LAST_UPDATED + " string"); } + private void populateRepoNames(SQLiteDatabase db, int oldVersion) { + if (oldVersion >= 37) { + return; + } + Utils.debugLog(TAG, "Populating repo names from the url"); + final String[] columns = {RepoTable.Cols.ADDRESS, RepoTable.Cols._ID}; + Cursor cursor = db.query(RepoTable.NAME, columns, + RepoTable.Cols.NAME + " IS NULL OR " + RepoTable.Cols.NAME + " = ''", null, null, null, null); + if (cursor != null) { + if (cursor.getCount() > 0) { + cursor.moveToFirst(); + while (!cursor.isAfterLast()) { + String address = cursor.getString(0); + long id = cursor.getInt(1); + ContentValues values = new ContentValues(1); + String name = Repo.addressToName(address); + values.put(RepoTable.Cols.NAME, name); + final String[] args = {Long.toString(id)}; + Utils.debugLog(TAG, "Setting repo name to '" + name + "' for repo " + address); + db.update(RepoTable.NAME, values, RepoTable.Cols._ID + " = ?", args); + cursor.moveToNext(); + } + } + cursor.close(); + } + } + + private void renameRepoId(SQLiteDatabase db, int oldVersion) { + if (oldVersion >= 36 || columnExists(db, RepoTable.NAME, RepoTable.Cols._ID)) { + return; + } + + Utils.debugLog(TAG, "Renaming " + RepoTable.NAME + ".id to " + RepoTable.Cols._ID); + db.beginTransaction(); + + try { + // http://stackoverflow.com/questions/805363/how-do-i-rename-a-column-in-a-sqlite-database-table#805508 + String tempTableName = RepoTable.NAME + "__temp__"; + db.execSQL("ALTER TABLE " + RepoTable.NAME + " RENAME TO " + tempTableName + ";"); + + // I realise this is available in the CREATE_TABLE_REPO above, + // however I have a feeling that it will need to be the same as the + // current structure of the table as of DBVersion 36, or else we may + // get into strife. For example, if there was a field that + // got removed, then it will break the "insert select" + // statement. Therefore, I've put a copy of CREATE_TABLE_REPO + // here that is the same as it was at DBVersion 36. + String createTableDdl = "create table " + RepoTable.NAME + " (" + + RepoTable.Cols._ID + " integer not null primary key, " + + RepoTable.Cols.ADDRESS + " text not null, " + + RepoTable.Cols.NAME + " text, " + + RepoTable.Cols.DESCRIPTION + " text, " + + RepoTable.Cols.IN_USE + " integer not null, " + + RepoTable.Cols.PRIORITY + " integer not null, " + + RepoTable.Cols.SIGNING_CERT + " text, " + + RepoTable.Cols.FINGERPRINT + " text, " + + RepoTable.Cols.MAX_AGE + " integer not null default 0, " + + RepoTable.Cols.VERSION + " integer not null default 0, " + + RepoTable.Cols.LAST_ETAG + " text, " + + RepoTable.Cols.LAST_UPDATED + " string);"; + + db.execSQL(createTableDdl); + + String nonIdFields = TextUtils.join(", ", new String[] { + RepoTable.Cols.ADDRESS, + RepoTable.Cols.NAME, + RepoTable.Cols.DESCRIPTION, + RepoTable.Cols.IN_USE, + RepoTable.Cols.PRIORITY, + RepoTable.Cols.SIGNING_CERT, + RepoTable.Cols.FINGERPRINT, + RepoTable.Cols.MAX_AGE, + RepoTable.Cols.VERSION, + RepoTable.Cols.LAST_ETAG, + RepoTable.Cols.LAST_UPDATED, + }); + + String insertSql = "INSERT INTO " + RepoTable.NAME + + "(" + RepoTable.Cols._ID + ", " + nonIdFields + " ) " + + "SELECT id, " + nonIdFields + " FROM " + tempTableName + ";"; + + db.execSQL(insertSql); + db.execSQL("DROP TABLE " + tempTableName + ";"); + db.setTransactionSuccessful(); + } catch (Exception e) { + Log.e(TAG, "Error renaming id to " + RepoTable.Cols._ID, e); + } + db.endTransaction(); + } + private void addIsSwapToRepo(SQLiteDatabase db, int oldVersion) { if (oldVersion >= 47 || columnExists(db, RepoTable.NAME, RepoTable.Cols.IS_SWAP)) { return; @@ -990,4 +956,38 @@ class DBHelper extends SQLiteOpenHelper { return exists; } + private void insertRepo(SQLiteDatabase db, String name, String address, + String description, String version, String enabled, + String priority, String pushRequests, String pubKey) { + ContentValues values = new ContentValues(); + values.put(RepoTable.Cols.ADDRESS, address); + values.put(RepoTable.Cols.NAME, name); + values.put(RepoTable.Cols.DESCRIPTION, description); + values.put(RepoTable.Cols.SIGNING_CERT, pubKey); + values.put(RepoTable.Cols.FINGERPRINT, Utils.calcFingerprint(pubKey)); + values.put(RepoTable.Cols.MAX_AGE, 0); + values.put(RepoTable.Cols.VERSION, Utils.parseInt(version, 0)); + values.put(RepoTable.Cols.IN_USE, Utils.parseInt(enabled, 0)); + values.put(RepoTable.Cols.PRIORITY, Utils.parseInt(priority, Integer.MAX_VALUE)); + values.put(RepoTable.Cols.LAST_ETAG, (String) null); + values.put(RepoTable.Cols.TIMESTAMP, 0); + + switch (pushRequests) { + case "ignore": + values.put(RepoTable.Cols.PUSH_REQUESTS, Repo.PUSH_REQUEST_IGNORE); + break; + case "prompt": + values.put(RepoTable.Cols.PUSH_REQUESTS, Repo.PUSH_REQUEST_PROMPT); + break; + case "always": + values.put(RepoTable.Cols.PUSH_REQUESTS, Repo.PUSH_REQUEST_ACCEPT_ALWAYS); + break; + default: + throw new IllegalArgumentException(pushRequests + " is not a supported option!"); + } + + Utils.debugLog(TAG, "Adding repository " + name + " with push requests as " + pushRequests); + db.insert(RepoTable.NAME, null, values); + } + }