Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | package org.mozilla.gecko.background.helpers; |
michael@0 | 5 | |
michael@0 | 6 | import android.database.Cursor; |
michael@0 | 7 | import android.database.sqlite.SQLiteDatabase; |
michael@0 | 8 | import junit.framework.Assert; |
michael@0 | 9 | |
michael@0 | 10 | public class DBHelpers { |
michael@0 | 11 | |
michael@0 | 12 | /* |
michael@0 | 13 | * Works for strings and int-ish values. |
michael@0 | 14 | */ |
michael@0 | 15 | public static void assertCursorContains(Object[][] expected, Cursor actual) { |
michael@0 | 16 | Assert.assertEquals(expected.length, actual.getCount()); |
michael@0 | 17 | int i = 0, j = 0; |
michael@0 | 18 | Object[] row; |
michael@0 | 19 | |
michael@0 | 20 | do { |
michael@0 | 21 | row = expected[i]; |
michael@0 | 22 | for (j = 0; j < row.length; ++j) { |
michael@0 | 23 | Object atIndex = row[j]; |
michael@0 | 24 | if (atIndex == null) { |
michael@0 | 25 | continue; |
michael@0 | 26 | } |
michael@0 | 27 | if (atIndex instanceof String) { |
michael@0 | 28 | Assert.assertEquals(atIndex, actual.getString(j)); |
michael@0 | 29 | } else { |
michael@0 | 30 | Assert.assertEquals(atIndex, actual.getInt(j)); |
michael@0 | 31 | } |
michael@0 | 32 | } |
michael@0 | 33 | ++i; |
michael@0 | 34 | } while (actual.moveToPosition(i)); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | public static int getRowCount(SQLiteDatabase db, String table) { |
michael@0 | 38 | return getRowCount(db, table, null, null); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | public static int getRowCount(SQLiteDatabase db, String table, String selection, String[] selectionArgs) { |
michael@0 | 42 | final Cursor c = db.query(table, null, selection, selectionArgs, null, null, null); |
michael@0 | 43 | try { |
michael@0 | 44 | return c.getCount(); |
michael@0 | 45 | } finally { |
michael@0 | 46 | c.close(); |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * Returns an ID that is non-existent in the given sqlite table. Assumes that a column named |
michael@0 | 52 | * "id" exists. |
michael@0 | 53 | */ |
michael@0 | 54 | public static int getNonExistentID(SQLiteDatabase db, String table) { |
michael@0 | 55 | // XXX: We should use selectionArgs to concatenate table, but sqlite throws a syntax error on |
michael@0 | 56 | // "?" because it wants to ensure id is a valid column in table. |
michael@0 | 57 | final Cursor c = db.rawQuery("SELECT MAX(id) + 1 FROM " + table, null); |
michael@0 | 58 | try { |
michael@0 | 59 | if (!c.moveToNext()) { |
michael@0 | 60 | return 0; |
michael@0 | 61 | } |
michael@0 | 62 | return c.getInt(0); |
michael@0 | 63 | } finally { |
michael@0 | 64 | c.close(); |
michael@0 | 65 | } |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * Returns an ID that exists in the given sqlite table. Assumes that a column named * "id" |
michael@0 | 70 | * exists. |
michael@0 | 71 | */ |
michael@0 | 72 | public static long getExistentID(SQLiteDatabase db, String table) { |
michael@0 | 73 | final Cursor c = db.query(table, new String[] {"id"}, null, null, null, null, null, "1"); |
michael@0 | 74 | try { |
michael@0 | 75 | if (!c.moveToNext()) { |
michael@0 | 76 | throw new IllegalStateException("Given table does not contain any entries."); |
michael@0 | 77 | } |
michael@0 | 78 | return c.getInt(0); |
michael@0 | 79 | } finally { |
michael@0 | 80 | c.close(); |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | } |