michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: package org.mozilla.gecko.background.helpers; michael@0: michael@0: import android.database.Cursor; michael@0: import android.database.sqlite.SQLiteDatabase; michael@0: import junit.framework.Assert; michael@0: michael@0: public class DBHelpers { michael@0: michael@0: /* michael@0: * Works for strings and int-ish values. michael@0: */ michael@0: public static void assertCursorContains(Object[][] expected, Cursor actual) { michael@0: Assert.assertEquals(expected.length, actual.getCount()); michael@0: int i = 0, j = 0; michael@0: Object[] row; michael@0: michael@0: do { michael@0: row = expected[i]; michael@0: for (j = 0; j < row.length; ++j) { michael@0: Object atIndex = row[j]; michael@0: if (atIndex == null) { michael@0: continue; michael@0: } michael@0: if (atIndex instanceof String) { michael@0: Assert.assertEquals(atIndex, actual.getString(j)); michael@0: } else { michael@0: Assert.assertEquals(atIndex, actual.getInt(j)); michael@0: } michael@0: } michael@0: ++i; michael@0: } while (actual.moveToPosition(i)); michael@0: } michael@0: michael@0: public static int getRowCount(SQLiteDatabase db, String table) { michael@0: return getRowCount(db, table, null, null); michael@0: } michael@0: michael@0: public static int getRowCount(SQLiteDatabase db, String table, String selection, String[] selectionArgs) { michael@0: final Cursor c = db.query(table, null, selection, selectionArgs, null, null, null); michael@0: try { michael@0: return c.getCount(); michael@0: } finally { michael@0: c.close(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Returns an ID that is non-existent in the given sqlite table. Assumes that a column named michael@0: * "id" exists. michael@0: */ michael@0: public static int getNonExistentID(SQLiteDatabase db, String table) { michael@0: // XXX: We should use selectionArgs to concatenate table, but sqlite throws a syntax error on michael@0: // "?" because it wants to ensure id is a valid column in table. michael@0: final Cursor c = db.rawQuery("SELECT MAX(id) + 1 FROM " + table, null); michael@0: try { michael@0: if (!c.moveToNext()) { michael@0: return 0; michael@0: } michael@0: return c.getInt(0); michael@0: } finally { michael@0: c.close(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Returns an ID that exists in the given sqlite table. Assumes that a column named * "id" michael@0: * exists. michael@0: */ michael@0: public static long getExistentID(SQLiteDatabase db, String table) { michael@0: final Cursor c = db.query(table, new String[] {"id"}, null, null, null, null, null, "1"); michael@0: try { michael@0: if (!c.moveToNext()) { michael@0: throw new IllegalStateException("Given table does not contain any entries."); michael@0: } michael@0: return c.getInt(0); michael@0: } finally { michael@0: c.close(); michael@0: } michael@0: } michael@0: michael@0: }