2011-12-16 15:11:09 -08:00
|
|
|
/* 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/. */
|
2012-01-19 12:19:56 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko.sqlite;
|
|
|
|
|
2011-12-16 15:11:09 -08:00
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.database.Cursor;
|
2012-04-09 10:08:37 -07:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteException;
|
2011-12-16 15:11:09 -08:00
|
|
|
import android.text.TextUtils;
|
2012-07-15 13:20:43 -07:00
|
|
|
import android.util.Log;
|
2012-01-19 12:19:56 -08:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2011-12-16 15:11:09 -08:00
|
|
|
import java.util.Map.Entry;
|
2012-01-19 12:19:56 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This class allows using the mozsqlite3 library included with Firefox
|
|
|
|
* to read SQLite databases, instead of the Android SQLiteDataBase API,
|
|
|
|
* which might use whatever outdated DB is present on the Android system.
|
|
|
|
*/
|
2012-07-17 17:54:54 -07:00
|
|
|
public class SQLiteBridge {
|
2012-01-19 12:19:56 -08:00
|
|
|
private static final String LOGTAG = "SQLiteBridge";
|
2012-02-27 03:28:21 -08:00
|
|
|
|
2012-04-09 10:08:37 -07:00
|
|
|
// Path to the database. If this database was not opened with openDatabase, we reopen it every query.
|
2012-01-19 12:19:56 -08:00
|
|
|
private String mDb;
|
2012-04-09 10:08:37 -07:00
|
|
|
// pointer to the database if it was opened with openDatabase
|
|
|
|
protected long mDbPointer = 0;
|
2012-02-27 03:28:21 -08:00
|
|
|
|
|
|
|
// Values remembered after a query.
|
2012-02-27 03:28:22 -08:00
|
|
|
private long[] mQueryResults;
|
|
|
|
|
2012-04-09 10:08:37 -07:00
|
|
|
private boolean mTransactionSuccess = false;
|
|
|
|
private boolean mInTransaction = false;
|
|
|
|
|
2012-02-27 03:28:22 -08:00
|
|
|
private static final int RESULT_INSERT_ROW_ID = 0;
|
|
|
|
private static final int RESULT_ROWS_CHANGED = 1;
|
2012-01-19 12:19:56 -08:00
|
|
|
|
2012-01-19 12:37:29 -08:00
|
|
|
// JNI code in $(topdir)/mozglue/android/..
|
2012-02-27 03:28:22 -08:00
|
|
|
private static native MatrixBlobCursor sqliteCall(String aDb, String aQuery,
|
|
|
|
String[] aParams,
|
|
|
|
long[] aUpdateResult)
|
2012-01-19 12:19:56 -08:00
|
|
|
throws SQLiteBridgeException;
|
2012-04-09 10:08:37 -07:00
|
|
|
private static native MatrixBlobCursor sqliteCallWithDb(long aDb, String aQuery,
|
|
|
|
String[] aParams,
|
|
|
|
long[] aUpdateResult)
|
|
|
|
throws SQLiteBridgeException;
|
|
|
|
private static native long openDatabase(String aDb)
|
|
|
|
throws SQLiteBridgeException;
|
|
|
|
private static native void closeDatabase(long aDb);
|
2012-01-19 12:19:56 -08:00
|
|
|
|
|
|
|
// Takes the path to the database we want to access.
|
|
|
|
public SQLiteBridge(String aDb) throws SQLiteBridgeException {
|
|
|
|
mDb = aDb;
|
|
|
|
}
|
|
|
|
|
2011-12-16 15:11:09 -08:00
|
|
|
// Executes a simple line of sql.
|
|
|
|
public void execSQL(String sql)
|
|
|
|
throws SQLiteBridgeException {
|
2012-02-27 03:28:22 -08:00
|
|
|
internalQuery(sql, null);
|
2011-12-16 15:11:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Executes a simple line of sql. Allow you to bind arguments
|
|
|
|
public void execSQL(String sql, String[] bindArgs)
|
|
|
|
throws SQLiteBridgeException {
|
2012-02-27 03:28:22 -08:00
|
|
|
internalQuery(sql, bindArgs);
|
2011-12-16 15:11:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Executes a DELETE statement on the database
|
|
|
|
public int delete(String table, String whereClause, String[] whereArgs)
|
|
|
|
throws SQLiteBridgeException {
|
|
|
|
StringBuilder sb = new StringBuilder("DELETE from ");
|
|
|
|
sb.append(table);
|
|
|
|
if (whereClause != null) {
|
|
|
|
sb.append(" WHERE " + whereClause);
|
|
|
|
}
|
|
|
|
|
2012-02-27 03:28:22 -08:00
|
|
|
internalQuery(sb.toString(), whereArgs);
|
2012-02-27 03:28:22 -08:00
|
|
|
return (int)mQueryResults[RESULT_ROWS_CHANGED];
|
2011-12-16 15:11:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public Cursor query(String table,
|
|
|
|
String[] columns,
|
|
|
|
String selection,
|
|
|
|
String[] selectionArgs,
|
|
|
|
String groupBy,
|
|
|
|
String having,
|
|
|
|
String orderBy,
|
|
|
|
String limit)
|
|
|
|
throws SQLiteBridgeException {
|
|
|
|
StringBuilder sb = new StringBuilder("SELECT ");
|
|
|
|
if (columns != null)
|
|
|
|
sb.append(TextUtils.join(", ", columns));
|
|
|
|
else
|
|
|
|
sb.append(" * ");
|
|
|
|
|
|
|
|
sb.append(" FROM ");
|
|
|
|
sb.append(table);
|
|
|
|
|
|
|
|
if (selection != null) {
|
|
|
|
sb.append(" WHERE " + selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupBy != null) {
|
|
|
|
sb.append(" GROUP BY " + groupBy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (having != null) {
|
|
|
|
sb.append(" HAVING " + having);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (orderBy != null) {
|
|
|
|
sb.append(" ORDER BY " + orderBy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limit != null) {
|
|
|
|
sb.append(" " + limit);
|
|
|
|
}
|
|
|
|
|
2012-02-27 03:28:22 -08:00
|
|
|
return rawQuery(sb.toString(), selectionArgs);
|
|
|
|
}
|
|
|
|
|
2012-03-22 10:35:19 -07:00
|
|
|
/* This method is referenced by Robocop via reflection. */
|
2012-02-27 03:28:22 -08:00
|
|
|
public Cursor rawQuery(String sql, String[] selectionArgs)
|
|
|
|
throws SQLiteBridgeException {
|
2012-02-27 03:28:22 -08:00
|
|
|
return internalQuery(sql, selectionArgs);
|
2011-12-16 15:11:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public long insert(String table, String nullColumnHack, ContentValues values)
|
|
|
|
throws SQLiteBridgeException {
|
2012-04-09 10:48:51 -07:00
|
|
|
if (values == null)
|
|
|
|
return 0;
|
|
|
|
|
2011-12-16 15:11:09 -08:00
|
|
|
ArrayList<String> valueNames = new ArrayList<String>();
|
|
|
|
ArrayList<String> valueBinds = new ArrayList<String>();
|
|
|
|
ArrayList<String> keyNames = new ArrayList<String>();
|
|
|
|
|
2012-04-09 10:48:51 -07:00
|
|
|
for (Entry<String, Object> value : values.valueSet()) {
|
2011-12-16 15:11:09 -08:00
|
|
|
keyNames.add(value.getKey());
|
2012-04-09 10:48:51 -07:00
|
|
|
|
|
|
|
Object val = value.getValue();
|
|
|
|
if (val == null) {
|
|
|
|
valueNames.add("NULL");
|
|
|
|
} else {
|
|
|
|
valueNames.add("?");
|
|
|
|
valueBinds.add(val.toString());
|
|
|
|
}
|
2011-12-16 15:11:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder("INSERT into ");
|
|
|
|
sb.append(table);
|
|
|
|
|
|
|
|
sb.append(" (");
|
|
|
|
sb.append(TextUtils.join(", ", keyNames));
|
|
|
|
sb.append(")");
|
|
|
|
|
|
|
|
// XXX - Do we need to bind these values?
|
|
|
|
sb.append(" VALUES (");
|
|
|
|
sb.append(TextUtils.join(", ", valueNames));
|
|
|
|
sb.append(") ");
|
|
|
|
|
|
|
|
String[] binds = new String[valueBinds.size()];
|
|
|
|
valueBinds.toArray(binds);
|
2012-02-27 03:28:22 -08:00
|
|
|
internalQuery(sb.toString(), binds);
|
2012-02-27 03:28:22 -08:00
|
|
|
return mQueryResults[RESULT_INSERT_ROW_ID];
|
2011-12-16 15:11:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public int update(String table, ContentValues values, String whereClause, String[] whereArgs)
|
|
|
|
throws SQLiteBridgeException {
|
2012-04-09 10:48:51 -07:00
|
|
|
if (values == null)
|
|
|
|
return 0;
|
|
|
|
|
2011-12-16 15:11:09 -08:00
|
|
|
ArrayList<String> valueNames = new ArrayList<String>();
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder("UPDATE ");
|
|
|
|
sb.append(table);
|
|
|
|
sb.append(" SET ");
|
2012-04-09 10:48:51 -07:00
|
|
|
|
|
|
|
boolean isFirst = true;
|
|
|
|
|
|
|
|
for (Entry<String, Object> value : values.valueSet()) {
|
|
|
|
if (isFirst)
|
|
|
|
isFirst = false;
|
2011-12-16 15:11:09 -08:00
|
|
|
else
|
2012-04-09 10:48:51 -07:00
|
|
|
sb.append(", ");
|
|
|
|
|
|
|
|
sb.append(value.getKey());
|
|
|
|
|
|
|
|
Object val = value.getValue();
|
|
|
|
if (val == null) {
|
|
|
|
sb.append(" = NULL");
|
|
|
|
} else {
|
|
|
|
sb.append(" = ?");
|
|
|
|
valueNames.add(val.toString());
|
|
|
|
}
|
2011-12-16 15:11:09 -08:00
|
|
|
}
|
|
|
|
|
2012-04-09 10:48:51 -07:00
|
|
|
if (!TextUtils.isEmpty(whereClause)) {
|
2011-12-16 15:11:09 -08:00
|
|
|
sb.append(" WHERE ");
|
|
|
|
sb.append(whereClause);
|
|
|
|
for (int i = 0; i < whereArgs.length; i++) {
|
|
|
|
valueNames.add(whereArgs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String[] binds = new String[valueNames.size()];
|
|
|
|
valueNames.toArray(binds);
|
|
|
|
|
2012-02-27 03:28:22 -08:00
|
|
|
internalQuery(sb.toString(), binds);
|
2012-02-27 03:28:22 -08:00
|
|
|
return (int)mQueryResults[RESULT_ROWS_CHANGED];
|
2011-12-16 15:11:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getVersion()
|
|
|
|
throws SQLiteBridgeException {
|
2012-02-27 03:28:22 -08:00
|
|
|
Cursor cursor = internalQuery("PRAGMA user_version", null);
|
2011-12-16 15:11:09 -08:00
|
|
|
int ret = -1;
|
2012-02-27 03:28:22 -08:00
|
|
|
if (cursor != null) {
|
|
|
|
cursor.moveToFirst();
|
|
|
|
String version = cursor.getString(0);
|
|
|
|
ret = Integer.parseInt(version);
|
2011-12-16 15:11:09 -08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-19 12:19:56 -08:00
|
|
|
// Do an SQL query, substituting the parameters in the query with the passed
|
|
|
|
// parameters. The parameters are subsituded in order, so named parameters
|
|
|
|
// are not supported.
|
2012-02-27 03:28:22 -08:00
|
|
|
private Cursor internalQuery(String aQuery, String[] aParams)
|
2012-01-19 12:19:56 -08:00
|
|
|
throws SQLiteBridgeException {
|
2012-04-09 10:08:37 -07:00
|
|
|
|
2012-02-27 03:28:22 -08:00
|
|
|
mQueryResults = new long[2];
|
2012-04-09 10:08:37 -07:00
|
|
|
if (isOpen()) {
|
|
|
|
return sqliteCallWithDb(mDbPointer, aQuery, aParams, mQueryResults);
|
|
|
|
}
|
2012-02-27 03:28:22 -08:00
|
|
|
return sqliteCall(mDb, aQuery, aParams, mQueryResults);
|
2012-01-19 12:19:56 -08:00
|
|
|
}
|
|
|
|
|
2012-04-09 10:08:37 -07:00
|
|
|
/*
|
|
|
|
* The second two parameters here are just provided for compatbility with SQLiteDatabase
|
|
|
|
* Support for them is not currently implemented
|
|
|
|
*/
|
|
|
|
public static SQLiteBridge openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags)
|
|
|
|
throws SQLiteException {
|
|
|
|
SQLiteBridge bridge = null;
|
|
|
|
try {
|
|
|
|
bridge = new SQLiteBridge(path);
|
2012-04-11 11:27:25 -07:00
|
|
|
bridge.mDbPointer = SQLiteBridge.openDatabase(path);
|
2012-04-09 10:08:37 -07:00
|
|
|
} catch(SQLiteBridgeException ex) {
|
|
|
|
// catch and rethrow as a SQLiteException to match SQLiteDatabase
|
|
|
|
throw new SQLiteException(ex.getMessage());
|
|
|
|
}
|
|
|
|
return bridge;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void close() {
|
|
|
|
if (isOpen()) {
|
|
|
|
closeDatabase(mDbPointer);
|
|
|
|
}
|
|
|
|
mDbPointer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isOpen() {
|
|
|
|
return mDbPointer > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void beginTransaction() throws SQLiteBridgeException {
|
|
|
|
if (inTransaction()) {
|
|
|
|
throw new SQLiteBridgeException("Nested transactions are not supported");
|
|
|
|
}
|
|
|
|
execSQL("BEGIN EXCLUSIVE");
|
|
|
|
mTransactionSuccess = false;
|
|
|
|
mInTransaction = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void beginTransactionNonExclusive() throws SQLiteBridgeException {
|
|
|
|
if (inTransaction()) {
|
|
|
|
throw new SQLiteBridgeException("Nested transactions are not supported");
|
|
|
|
}
|
|
|
|
execSQL("BEGIN IMMEDIATE");
|
|
|
|
mTransactionSuccess = false;
|
|
|
|
mInTransaction = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void endTransaction() {
|
|
|
|
if (!inTransaction())
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (mTransactionSuccess) {
|
|
|
|
execSQL("COMMIT TRANSACTION");
|
|
|
|
} else {
|
|
|
|
execSQL("ROLLBACK TRANSACTION");
|
|
|
|
}
|
|
|
|
} catch(SQLiteBridgeException ex) {
|
|
|
|
Log.e(LOGTAG, "Error ending transaction", ex);
|
|
|
|
}
|
|
|
|
mInTransaction = false;
|
|
|
|
mTransactionSuccess = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTransactionSuccessful() throws SQLiteBridgeException {
|
|
|
|
if (!inTransaction()) {
|
|
|
|
throw new SQLiteBridgeException("setTransactionSuccessful called outside a transaction");
|
|
|
|
}
|
|
|
|
mTransactionSuccess = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean inTransaction() {
|
|
|
|
return mInTransaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void finalize() {
|
|
|
|
if (isOpen()) {
|
|
|
|
Log.e(LOGTAG, "Bridge finalized without closing the database");
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
}
|
2012-02-27 03:28:22 -08:00
|
|
|
}
|