From d65e72638bb26fd47c3921c5dfb5a704a2e5363b Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Wed, 19 Oct 2016 06:44:08 +1100 Subject: [PATCH] Reimplement `columnExists` using `PRAGMA table_info`. For some reason, the existing approach of "select * and then see if the column of interest is in the results set" didn't work as expected under tests. Perhaps SQLite is caching the list of columns for the purpose of `select *` even after running an `alter table add column` query? Either way, I couldn't figure out why it wasn't working as expected. This left us with two options: * Try to `select columnToCheck` and see if it throws an exception * Query columns using `PRAGMA table_info. The exception thrown when a column doesn't exist is not specific enough for our code to check that this is the exact exception that occured. It is not possible to say: `try { ... } catch (SQLiteColumnNotFound e) { ...}` unfotunately. Also, there is a cost associated with unwinding the stack to process an exception, which means exceptions probably shouldn't be used in unexceptional circumstances such as this. This change instead uses `PRAGMA table_info(tableOfInterest)` and then iterates over the cursor looking for the relevant column. Even if the performance is worse than the stack unwinding of an exception, it is more concise and less hacky. --- .../java/org/fdroid/fdroid/data/DBHelper.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 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 000b98b1e..5de7f6aea 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java +++ b/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java @@ -940,11 +940,20 @@ class DBHelper extends SQLiteOpenHelper { + Repo.PUSH_REQUEST_IGNORE); } - private static boolean columnExists(SQLiteDatabase db, String table, String column) { - Cursor cursor = db.rawQuery("select * from " + table + " limit 0,1", null); - boolean exists = cursor.getColumnIndex(column) != -1; + private static boolean columnExists(SQLiteDatabase db, String table, String field) { + boolean found = false; + Cursor cursor = db.rawQuery("PRAGMA table_info(" + table + ")", null); + cursor.moveToFirst(); + while (!cursor.isAfterLast()) { + String name = cursor.getString(cursor.getColumnIndex("name")); + if (name.equalsIgnoreCase(field)) { + found = true; + break; + } + cursor.moveToNext(); + } cursor.close(); - return exists; + return found; } private static boolean tableExists(SQLiteDatabase db, String table) {