mobile/android/base/db/DBUtils.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/db/DBUtils.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,99 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.db;
     1.9 +
    1.10 +import org.mozilla.gecko.GeckoAppShell;
    1.11 +
    1.12 +import android.content.ContentValues;
    1.13 +import android.database.sqlite.SQLiteOpenHelper;
    1.14 +import android.text.TextUtils;
    1.15 +import android.util.Log;
    1.16 +
    1.17 +public class DBUtils {
    1.18 +    private static final String LOGTAG = "GeckoDBUtils";
    1.19 +
    1.20 +    public static final String qualifyColumn(String table, String column) {
    1.21 +        return table + "." + column;
    1.22 +    }
    1.23 +
    1.24 +    // This is available in Android >= 11. Implemented locally to be
    1.25 +    // compatible with older versions.
    1.26 +    public static String concatenateWhere(String a, String b) {
    1.27 +        if (TextUtils.isEmpty(a)) {
    1.28 +            return b;
    1.29 +        }
    1.30 +
    1.31 +        if (TextUtils.isEmpty(b)) {
    1.32 +            return a;
    1.33 +        }
    1.34 +
    1.35 +        return "(" + a + ") AND (" + b + ")";
    1.36 +    }
    1.37 +
    1.38 +    // This is available in Android >= 11. Implemented locally to be
    1.39 +    // compatible with older versions.
    1.40 +    public static String[] appendSelectionArgs(String[] originalValues, String[] newValues) {
    1.41 +        if (originalValues == null || originalValues.length == 0) {
    1.42 +            return newValues;
    1.43 +        }
    1.44 +
    1.45 +        if (newValues == null || newValues.length == 0) {
    1.46 +            return originalValues;
    1.47 +        }
    1.48 +
    1.49 +        String[] result = new String[originalValues.length + newValues.length];
    1.50 +        System.arraycopy(originalValues, 0, result, 0, originalValues.length);
    1.51 +        System.arraycopy(newValues, 0, result, originalValues.length, newValues.length);
    1.52 +
    1.53 +        return result;
    1.54 +    }
    1.55 +
    1.56 +    public static void replaceKey(ContentValues aValues, String aOriginalKey,
    1.57 +                                  String aNewKey, String aDefault) {
    1.58 +        String value = aDefault;
    1.59 +        if (aOriginalKey != null && aValues.containsKey(aOriginalKey)) {
    1.60 +            value = aValues.get(aOriginalKey).toString();
    1.61 +            aValues.remove(aOriginalKey);
    1.62 +        }
    1.63 +
    1.64 +        if (!aValues.containsKey(aNewKey)) {
    1.65 +            aValues.put(aNewKey, value);
    1.66 +        }
    1.67 +    }
    1.68 +
    1.69 +    public static void ensureDatabaseIsNotLocked(SQLiteOpenHelper dbHelper, String databasePath) {
    1.70 +        for (int retries = 0; retries < 5; retries++) {
    1.71 +            try {
    1.72 +                // Try a simple test and exit the loop
    1.73 +                dbHelper.getWritableDatabase();
    1.74 +                return;
    1.75 +            } catch (Exception e) {
    1.76 +                // Things could get very bad if we don't find a way to unlock the DB
    1.77 +                Log.d(LOGTAG, "Database is locked, trying to kill any zombie processes: " + databasePath);
    1.78 +                GeckoAppShell.killAnyZombies();
    1.79 +                try {
    1.80 +                    Thread.sleep(retries * 100);
    1.81 +                } catch (InterruptedException ie) { }
    1.82 +            }
    1.83 +        }
    1.84 +        Log.d(LOGTAG, "Failed to unlock database");
    1.85 +        GeckoAppShell.listOfOpenFiles();
    1.86 +    }
    1.87 +
    1.88 +    /**
    1.89 +     * Verifies that 0-byte arrays aren't added as favicon or thumbnail data.
    1.90 +     * @param values        ContentValues of query
    1.91 +     * @param columnName    Name of data column to verify
    1.92 +     */
    1.93 +    public static void stripEmptyByteArray(ContentValues values, String columnName) {
    1.94 +        if (values.containsKey(columnName)) {
    1.95 +            byte[] data = values.getAsByteArray(columnName);
    1.96 +            if (data == null || data.length == 0) {
    1.97 +                Log.w(LOGTAG, "Tried to insert an empty or non-byte-array image. Ignoring.");
    1.98 +                values.putNull(columnName);
    1.99 +            }
   1.100 +        }
   1.101 +    }
   1.102 +}

mercurial