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