summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/db/BaseTable.java
blob: 418d547ed46c9242657d8fb1ff0f5aedc7117209 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

// BaseTable provides a basic implementation of a Table for tables that don't require advanced operations during
// insert, delete, update, or query operations. Implementors must still provide onCreate and onUpgrade operations.
public abstract class BaseTable implements Table {
    private static final String LOGTAG = "GeckoBaseTable";

    private static final boolean DEBUG = false;

    protected static void log(String msg) {
        if (DEBUG) {
            Log.i(LOGTAG, msg);
        }
    }

    // Table implementation
    @Override
    public Table.ContentProviderInfo[] getContentProviderInfo() {
        return new Table.ContentProviderInfo[0];
    }

    // Returns the name of the table to modify/query
    protected abstract String getTable();

    // Table implementation
    @Override
    public Cursor query(SQLiteDatabase db, Uri uri, int dbId, String[] columns, String selection, String[] selectionArgs, String sortOrder, String groupBy, String limit) {
        Cursor c = db.query(getTable(), columns, selection, selectionArgs, groupBy, null, sortOrder, limit);
        log("query " + columns + " in " + selection + " = " + c);
        return c;
    }

    @Override
    public int update(SQLiteDatabase db, Uri uri, int dbId, ContentValues values, String selection, String[] selectionArgs) {
        int updated = db.updateWithOnConflict(getTable(), values, selection, selectionArgs, SQLiteDatabase.CONFLICT_REPLACE);
        log("update " + values + " in " + selection + " = " + updated);
        return updated;
    }

    @Override
    public long insert(SQLiteDatabase db, Uri uri, int dbId, ContentValues values) {
        long inserted = db.insertOrThrow(getTable(), null, values);
        log("insert " + values + " = " + inserted);
        return inserted;
    }

    @Override
    public int delete(SQLiteDatabase db, Uri uri, int dbId, String selection, String[] selectionArgs) {
        int deleted = db.delete(getTable(), selection, selectionArgs);
        log("delete " + selection + " = " + deleted);
        return deleted;
    }
};