Loading mobile/android/base/sqlite/SQLiteBridge.java +23 −61 Original line number Diff line number Diff line Loading @@ -26,15 +26,23 @@ import java.util.Set; */ public class SQLiteBridge { private static final String LOGTAG = "SQLiteBridge"; // Path to the database. We reopen it every query. private String mDb; // Remember column names from last query result. private ArrayList<String> mColumns; private Long[] mQueryResults; // Values remembered after a query. private int kResultInsertRowId = 0; private int kResultRowsChanged = 1; // JNI code in $(topdir)/mozglue/android/.. private static native void sqliteCall(String aDb, String aQuery, String[] aParams, ArrayList<String> aColumns, Long[] aUpdateResult, ArrayList<Object[]> aRes) throws SQLiteBridgeException; Loading @@ -46,21 +54,13 @@ public class SQLiteBridge { // Executes a simple line of sql. public void execSQL(String sql) throws SQLiteBridgeException { try { query(sql, null); } catch(SQLiteBridgeException ex) { throw ex; } } // Executes a simple line of sql. Allow you to bind arguments public void execSQL(String sql, String[] bindArgs) throws SQLiteBridgeException { try { query(sql, bindArgs); } catch(SQLiteBridgeException ex) { throw ex; } } // Executes a DELETE statement on the database Loading @@ -72,11 +72,8 @@ public class SQLiteBridge { sb.append(" WHERE " + whereClause); } try { return getIntResult(sb.toString(), whereArgs, 1); } catch(SQLiteBridgeException ex) { throw ex; } query(sb.toString(), whereArgs); return mQueryResults[kResultRowsChanged].intValue(); } public Cursor query(String table, Loading Loading @@ -118,15 +115,8 @@ public class SQLiteBridge { } ArrayList<Object[]> results; try { mColumns = null; results = query(sb.toString(), selectionArgs); } catch(SQLiteBridgeException ex) { throw ex; } MatrixCursor cursor = new MatrixCursor(mColumns.toArray(new String[0])); try { for (Object resultRow: results) { Loading Loading @@ -170,11 +160,8 @@ public class SQLiteBridge { String[] binds = new String[valueBinds.size()]; valueBinds.toArray(binds); try { return getIntResult(sb.toString(), binds, 0); } catch (SQLiteBridgeException ex) { throw ex; } query(sb.toString(), binds); return mQueryResults[kResultInsertRowId]; } public int update(String table, ContentValues values, String whereClause, String[] whereArgs) Loading Loading @@ -207,41 +194,15 @@ public class SQLiteBridge { String[] binds = new String[valueNames.size()]; valueNames.toArray(binds); try { return getIntResult(sb.toString(), binds, 1); } catch (SQLiteBridgeException ex) { throw ex; } } private int getIntResult(String query, String[] params, int resultIndex) throws SQLiteBridgeException { ArrayList<Object[]> results = null; try { mColumns = null; results = query(query, params); } catch(SQLiteBridgeException ex) { throw ex; } if (results != null) { for (Object resultRow: results) { Object[] resultColumns = (Object[])resultRow; return ((Number)resultColumns[resultIndex]).intValue(); } } return -1; query(sb.toString(), binds); return mQueryResults[kResultRowsChanged].intValue(); } public int getVersion() throws SQLiteBridgeException { ArrayList<Object[]> results = null; try { mColumns = null; results = query("PRAGMA user_version"); } catch(SQLiteBridgeException ex) { throw ex; } int ret = -1; if (results != null) { for (Object resultRow: results) { Loading @@ -253,11 +214,9 @@ public class SQLiteBridge { return ret; } // Do an SQL query without parameters public ArrayList<Object[]> query(String aQuery) throws SQLiteBridgeException { String[] params = new String[0]; return query(aQuery, params); return query(aQuery, null); } // Do an SQL query, substituting the parameters in the query with the passed Loading @@ -270,8 +229,11 @@ public class SQLiteBridge { public ArrayList<Object[]> query(String aQuery, String[] aParams) throws SQLiteBridgeException { ArrayList<Object[]> result = new ArrayList<Object[]>(); mQueryResults = new Long[2]; mColumns = new ArrayList<String>(); sqliteCall(mDb, aQuery, aParams, mColumns, result); sqliteCall(mDb, aQuery, aParams, mColumns, mQueryResults, result); return result; } Loading mozglue/android/SQLiteBridge.cpp +20 −22 Original line number Diff line number Diff line Loading @@ -95,10 +95,12 @@ void setup_sqlite_functions(void *sqlite_handle) static bool initialized = false; static jclass stringClass; static jclass objectClass; static jclass longClass; static jclass byteBufferClass; static jclass arrayListClass; static jmethodID jByteBufferAllocateDirect; static jmethodID jArrayListAdd; static jmethodID jLongConstructor; static jobject jNull; static void Loading @@ -123,12 +125,14 @@ JNI_Setup(JNIEnv* jenv) objectClass = jenv->FindClass("java/lang/Object"); stringClass = jenv->FindClass("java/lang/String"); longClass = jenv->FindClass("java/lang/Long"); byteBufferClass = jenv->FindClass("java/nio/ByteBuffer"); arrayListClass = jenv->FindClass("java/util/ArrayList"); jNull = jenv->NewGlobalRef(NULL); if (stringClass == NULL || objectClass == NULL || byteBufferClass == NULL || arrayListClass == NULL) { || byteBufferClass == NULL || arrayListClass == NULL || longClass == NULL) { LOG("Error finding classes"); JNI_Throw(jenv, "org/mozilla/gecko/sqlite/SQLiteBridgeException", "FindClass error"); Loading @@ -141,8 +145,13 @@ JNI_Setup(JNIEnv* jenv) // boolean add(Object o) jArrayListAdd = jenv->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z"); // new Long(long i) jLongConstructor = jenv->GetMethodID(longClass, "<init>", "(J)V"); if (jByteBufferAllocateDirect == NULL || jArrayListAdd == NULL) { if (jByteBufferAllocateDirect == NULL || jArrayListAdd == NULL || jLongConstructor == NULL) { LOG("Error finding methods"); JNI_Throw(jenv, "org/mozilla/gecko/sqlite/SQLiteBridgeException", "GetMethodId error"); Loading @@ -158,6 +167,7 @@ Java_org_mozilla_gecko_sqlite_SQLiteBridge_sqliteCall(JNIEnv* jenv, jclass, jstring jQuery, jobjectArray jParams, jobject jColumns, jobjectArray jQueryRes, jobject jArrayList) { JNI_Setup(jenv); Loading Loading @@ -244,29 +254,17 @@ Java_org_mozilla_gecko_sqlite_SQLiteBridge_sqliteCall(JNIEnv* jenv, jclass, jenv->DeleteLocalRef(jStr); } // if the statement doesn't return any results, instead return the id and number of changed rows if (rc == SQLITE_DONE) { jclass integerClass = jenv->FindClass("java/lang/Integer"); jmethodID intConstructor = jenv->GetMethodID(integerClass, "<init>", "(I)V"); jobjectArray jRow = jenv->NewObjectArray(2, objectClass, NULL); if (jRow == NULL) { asprintf(&errorMsg, "Can't allocate jRow Object[]\n"); goto error_close; } int id = f_sqlite3_last_insert_rowid(db); jobject jId = jenv->NewObject(integerClass, intConstructor, id); jenv->SetObjectArrayElement(jRow, 0, jId); // Return the id and number of changed rows in jQueryRes { long id = f_sqlite3_last_insert_rowid(db); jobject jId = jenv->NewObject(longClass, jLongConstructor, id); jenv->SetObjectArrayElement(jQueryRes, 0, jId); jenv->DeleteLocalRef(jId); int changed = f_sqlite3_changes(db); jobject jChanged = jenv->NewObject(integerClass, intConstructor, changed); jenv->SetObjectArrayElement(jRow, 1, jChanged); long changed = f_sqlite3_changes(db); jobject jChanged = jenv->NewObject(longClass, jLongConstructor, changed); jenv->SetObjectArrayElement(jQueryRes, 1, jChanged); jenv->DeleteLocalRef(jChanged); jenv->CallBooleanMethod(jArrayList, jArrayListAdd, jRow); jenv->DeleteLocalRef(jRow); } // For each row, add an Object[] to the passed ArrayList, Loading Loading
mobile/android/base/sqlite/SQLiteBridge.java +23 −61 Original line number Diff line number Diff line Loading @@ -26,15 +26,23 @@ import java.util.Set; */ public class SQLiteBridge { private static final String LOGTAG = "SQLiteBridge"; // Path to the database. We reopen it every query. private String mDb; // Remember column names from last query result. private ArrayList<String> mColumns; private Long[] mQueryResults; // Values remembered after a query. private int kResultInsertRowId = 0; private int kResultRowsChanged = 1; // JNI code in $(topdir)/mozglue/android/.. private static native void sqliteCall(String aDb, String aQuery, String[] aParams, ArrayList<String> aColumns, Long[] aUpdateResult, ArrayList<Object[]> aRes) throws SQLiteBridgeException; Loading @@ -46,21 +54,13 @@ public class SQLiteBridge { // Executes a simple line of sql. public void execSQL(String sql) throws SQLiteBridgeException { try { query(sql, null); } catch(SQLiteBridgeException ex) { throw ex; } } // Executes a simple line of sql. Allow you to bind arguments public void execSQL(String sql, String[] bindArgs) throws SQLiteBridgeException { try { query(sql, bindArgs); } catch(SQLiteBridgeException ex) { throw ex; } } // Executes a DELETE statement on the database Loading @@ -72,11 +72,8 @@ public class SQLiteBridge { sb.append(" WHERE " + whereClause); } try { return getIntResult(sb.toString(), whereArgs, 1); } catch(SQLiteBridgeException ex) { throw ex; } query(sb.toString(), whereArgs); return mQueryResults[kResultRowsChanged].intValue(); } public Cursor query(String table, Loading Loading @@ -118,15 +115,8 @@ public class SQLiteBridge { } ArrayList<Object[]> results; try { mColumns = null; results = query(sb.toString(), selectionArgs); } catch(SQLiteBridgeException ex) { throw ex; } MatrixCursor cursor = new MatrixCursor(mColumns.toArray(new String[0])); try { for (Object resultRow: results) { Loading Loading @@ -170,11 +160,8 @@ public class SQLiteBridge { String[] binds = new String[valueBinds.size()]; valueBinds.toArray(binds); try { return getIntResult(sb.toString(), binds, 0); } catch (SQLiteBridgeException ex) { throw ex; } query(sb.toString(), binds); return mQueryResults[kResultInsertRowId]; } public int update(String table, ContentValues values, String whereClause, String[] whereArgs) Loading Loading @@ -207,41 +194,15 @@ public class SQLiteBridge { String[] binds = new String[valueNames.size()]; valueNames.toArray(binds); try { return getIntResult(sb.toString(), binds, 1); } catch (SQLiteBridgeException ex) { throw ex; } } private int getIntResult(String query, String[] params, int resultIndex) throws SQLiteBridgeException { ArrayList<Object[]> results = null; try { mColumns = null; results = query(query, params); } catch(SQLiteBridgeException ex) { throw ex; } if (results != null) { for (Object resultRow: results) { Object[] resultColumns = (Object[])resultRow; return ((Number)resultColumns[resultIndex]).intValue(); } } return -1; query(sb.toString(), binds); return mQueryResults[kResultRowsChanged].intValue(); } public int getVersion() throws SQLiteBridgeException { ArrayList<Object[]> results = null; try { mColumns = null; results = query("PRAGMA user_version"); } catch(SQLiteBridgeException ex) { throw ex; } int ret = -1; if (results != null) { for (Object resultRow: results) { Loading @@ -253,11 +214,9 @@ public class SQLiteBridge { return ret; } // Do an SQL query without parameters public ArrayList<Object[]> query(String aQuery) throws SQLiteBridgeException { String[] params = new String[0]; return query(aQuery, params); return query(aQuery, null); } // Do an SQL query, substituting the parameters in the query with the passed Loading @@ -270,8 +229,11 @@ public class SQLiteBridge { public ArrayList<Object[]> query(String aQuery, String[] aParams) throws SQLiteBridgeException { ArrayList<Object[]> result = new ArrayList<Object[]>(); mQueryResults = new Long[2]; mColumns = new ArrayList<String>(); sqliteCall(mDb, aQuery, aParams, mColumns, result); sqliteCall(mDb, aQuery, aParams, mColumns, mQueryResults, result); return result; } Loading
mozglue/android/SQLiteBridge.cpp +20 −22 Original line number Diff line number Diff line Loading @@ -95,10 +95,12 @@ void setup_sqlite_functions(void *sqlite_handle) static bool initialized = false; static jclass stringClass; static jclass objectClass; static jclass longClass; static jclass byteBufferClass; static jclass arrayListClass; static jmethodID jByteBufferAllocateDirect; static jmethodID jArrayListAdd; static jmethodID jLongConstructor; static jobject jNull; static void Loading @@ -123,12 +125,14 @@ JNI_Setup(JNIEnv* jenv) objectClass = jenv->FindClass("java/lang/Object"); stringClass = jenv->FindClass("java/lang/String"); longClass = jenv->FindClass("java/lang/Long"); byteBufferClass = jenv->FindClass("java/nio/ByteBuffer"); arrayListClass = jenv->FindClass("java/util/ArrayList"); jNull = jenv->NewGlobalRef(NULL); if (stringClass == NULL || objectClass == NULL || byteBufferClass == NULL || arrayListClass == NULL) { || byteBufferClass == NULL || arrayListClass == NULL || longClass == NULL) { LOG("Error finding classes"); JNI_Throw(jenv, "org/mozilla/gecko/sqlite/SQLiteBridgeException", "FindClass error"); Loading @@ -141,8 +145,13 @@ JNI_Setup(JNIEnv* jenv) // boolean add(Object o) jArrayListAdd = jenv->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z"); // new Long(long i) jLongConstructor = jenv->GetMethodID(longClass, "<init>", "(J)V"); if (jByteBufferAllocateDirect == NULL || jArrayListAdd == NULL) { if (jByteBufferAllocateDirect == NULL || jArrayListAdd == NULL || jLongConstructor == NULL) { LOG("Error finding methods"); JNI_Throw(jenv, "org/mozilla/gecko/sqlite/SQLiteBridgeException", "GetMethodId error"); Loading @@ -158,6 +167,7 @@ Java_org_mozilla_gecko_sqlite_SQLiteBridge_sqliteCall(JNIEnv* jenv, jclass, jstring jQuery, jobjectArray jParams, jobject jColumns, jobjectArray jQueryRes, jobject jArrayList) { JNI_Setup(jenv); Loading Loading @@ -244,29 +254,17 @@ Java_org_mozilla_gecko_sqlite_SQLiteBridge_sqliteCall(JNIEnv* jenv, jclass, jenv->DeleteLocalRef(jStr); } // if the statement doesn't return any results, instead return the id and number of changed rows if (rc == SQLITE_DONE) { jclass integerClass = jenv->FindClass("java/lang/Integer"); jmethodID intConstructor = jenv->GetMethodID(integerClass, "<init>", "(I)V"); jobjectArray jRow = jenv->NewObjectArray(2, objectClass, NULL); if (jRow == NULL) { asprintf(&errorMsg, "Can't allocate jRow Object[]\n"); goto error_close; } int id = f_sqlite3_last_insert_rowid(db); jobject jId = jenv->NewObject(integerClass, intConstructor, id); jenv->SetObjectArrayElement(jRow, 0, jId); // Return the id and number of changed rows in jQueryRes { long id = f_sqlite3_last_insert_rowid(db); jobject jId = jenv->NewObject(longClass, jLongConstructor, id); jenv->SetObjectArrayElement(jQueryRes, 0, jId); jenv->DeleteLocalRef(jId); int changed = f_sqlite3_changes(db); jobject jChanged = jenv->NewObject(integerClass, intConstructor, changed); jenv->SetObjectArrayElement(jRow, 1, jChanged); long changed = f_sqlite3_changes(db); jobject jChanged = jenv->NewObject(longClass, jLongConstructor, changed); jenv->SetObjectArrayElement(jQueryRes, 1, jChanged); jenv->DeleteLocalRef(jChanged); jenv->CallBooleanMethod(jArrayList, jArrayListAdd, jRow); jenv->DeleteLocalRef(jRow); } // For each row, add an Object[] to the passed ArrayList, Loading