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.
This commit is contained in:
Peter Serwylo 2016-10-19 06:44:08 +11:00
parent 63a609fbab
commit d65e72638b

View File

@ -940,11 +940,20 @@ class DBHelper extends SQLiteOpenHelper {
+ Repo.PUSH_REQUEST_IGNORE); + Repo.PUSH_REQUEST_IGNORE);
} }
private static boolean columnExists(SQLiteDatabase db, String table, String column) { private static boolean columnExists(SQLiteDatabase db, String table, String field) {
Cursor cursor = db.rawQuery("select * from " + table + " limit 0,1", null); boolean found = false;
boolean exists = cursor.getColumnIndex(column) != -1; 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(); cursor.close();
return exists; return found;
} }
private static boolean tableExists(SQLiteDatabase db, String table) { private static boolean tableExists(SQLiteDatabase db, String table) {