mobile/android/base/db/DBUtils.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.db;
michael@0 6
michael@0 7 import org.mozilla.gecko.GeckoAppShell;
michael@0 8
michael@0 9 import android.content.ContentValues;
michael@0 10 import android.database.sqlite.SQLiteOpenHelper;
michael@0 11 import android.text.TextUtils;
michael@0 12 import android.util.Log;
michael@0 13
michael@0 14 public class DBUtils {
michael@0 15 private static final String LOGTAG = "GeckoDBUtils";
michael@0 16
michael@0 17 public static final String qualifyColumn(String table, String column) {
michael@0 18 return table + "." + column;
michael@0 19 }
michael@0 20
michael@0 21 // This is available in Android >= 11. Implemented locally to be
michael@0 22 // compatible with older versions.
michael@0 23 public static String concatenateWhere(String a, String b) {
michael@0 24 if (TextUtils.isEmpty(a)) {
michael@0 25 return b;
michael@0 26 }
michael@0 27
michael@0 28 if (TextUtils.isEmpty(b)) {
michael@0 29 return a;
michael@0 30 }
michael@0 31
michael@0 32 return "(" + a + ") AND (" + b + ")";
michael@0 33 }
michael@0 34
michael@0 35 // This is available in Android >= 11. Implemented locally to be
michael@0 36 // compatible with older versions.
michael@0 37 public static String[] appendSelectionArgs(String[] originalValues, String[] newValues) {
michael@0 38 if (originalValues == null || originalValues.length == 0) {
michael@0 39 return newValues;
michael@0 40 }
michael@0 41
michael@0 42 if (newValues == null || newValues.length == 0) {
michael@0 43 return originalValues;
michael@0 44 }
michael@0 45
michael@0 46 String[] result = new String[originalValues.length + newValues.length];
michael@0 47 System.arraycopy(originalValues, 0, result, 0, originalValues.length);
michael@0 48 System.arraycopy(newValues, 0, result, originalValues.length, newValues.length);
michael@0 49
michael@0 50 return result;
michael@0 51 }
michael@0 52
michael@0 53 public static void replaceKey(ContentValues aValues, String aOriginalKey,
michael@0 54 String aNewKey, String aDefault) {
michael@0 55 String value = aDefault;
michael@0 56 if (aOriginalKey != null && aValues.containsKey(aOriginalKey)) {
michael@0 57 value = aValues.get(aOriginalKey).toString();
michael@0 58 aValues.remove(aOriginalKey);
michael@0 59 }
michael@0 60
michael@0 61 if (!aValues.containsKey(aNewKey)) {
michael@0 62 aValues.put(aNewKey, value);
michael@0 63 }
michael@0 64 }
michael@0 65
michael@0 66 public static void ensureDatabaseIsNotLocked(SQLiteOpenHelper dbHelper, String databasePath) {
michael@0 67 for (int retries = 0; retries < 5; retries++) {
michael@0 68 try {
michael@0 69 // Try a simple test and exit the loop
michael@0 70 dbHelper.getWritableDatabase();
michael@0 71 return;
michael@0 72 } catch (Exception e) {
michael@0 73 // Things could get very bad if we don't find a way to unlock the DB
michael@0 74 Log.d(LOGTAG, "Database is locked, trying to kill any zombie processes: " + databasePath);
michael@0 75 GeckoAppShell.killAnyZombies();
michael@0 76 try {
michael@0 77 Thread.sleep(retries * 100);
michael@0 78 } catch (InterruptedException ie) { }
michael@0 79 }
michael@0 80 }
michael@0 81 Log.d(LOGTAG, "Failed to unlock database");
michael@0 82 GeckoAppShell.listOfOpenFiles();
michael@0 83 }
michael@0 84
michael@0 85 /**
michael@0 86 * Verifies that 0-byte arrays aren't added as favicon or thumbnail data.
michael@0 87 * @param values ContentValues of query
michael@0 88 * @param columnName Name of data column to verify
michael@0 89 */
michael@0 90 public static void stripEmptyByteArray(ContentValues values, String columnName) {
michael@0 91 if (values.containsKey(columnName)) {
michael@0 92 byte[] data = values.getAsByteArray(columnName);
michael@0 93 if (data == null || data.length == 0) {
michael@0 94 Log.w(LOGTAG, "Tried to insert an empty or non-byte-array image. Ignoring.");
michael@0 95 values.putNull(columnName);
michael@0 96 }
michael@0 97 }
michael@0 98 }
michael@0 99 }

mercurial