2012-05-21 15:12:37 +04: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-20 00:19:56 +04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2011-12-17 03:11:09 +04:00
|
|
|
#include <stdio.h>
|
2012-01-20 00:19:56 +04:00
|
|
|
#include <jni.h>
|
|
|
|
#include <android/log.h>
|
|
|
|
#include "dlfcn.h"
|
|
|
|
#include "APKOpen.h"
|
2012-01-20 12:49:07 +04:00
|
|
|
#include "ElfLoader.h"
|
2012-01-20 00:19:56 +04:00
|
|
|
#include "SQLiteBridge.h"
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define LOG(x...) __android_log_print(ANDROID_LOG_INFO, "GeckoJNI", x)
|
|
|
|
#else
|
|
|
|
#define LOG(x...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define SQLITE_WRAPPER_INT(name) name ## _t f_ ## name;
|
|
|
|
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_open)
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_errmsg)
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_prepare_v2)
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_bind_parameter_count)
|
2015-08-08 07:43:47 +03:00
|
|
|
SQLITE_WRAPPER_INT(sqlite3_bind_null)
|
2012-01-20 00:19:56 +04:00
|
|
|
SQLITE_WRAPPER_INT(sqlite3_bind_text)
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_step)
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_column_count)
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_finalize)
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_close)
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_column_name)
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_column_type)
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_column_blob)
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_column_bytes)
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_column_text)
|
2011-12-17 03:11:09 +04:00
|
|
|
SQLITE_WRAPPER_INT(sqlite3_changes)
|
|
|
|
SQLITE_WRAPPER_INT(sqlite3_last_insert_rowid)
|
2012-01-20 00:19:56 +04:00
|
|
|
|
|
|
|
void setup_sqlite_functions(void *sqlite_handle)
|
|
|
|
{
|
2012-06-18 20:06:59 +04:00
|
|
|
#define GETFUNC(name) f_ ## name = (name ## _t) (uintptr_t) __wrap_dlsym(sqlite_handle, #name)
|
2012-01-20 00:19:56 +04:00
|
|
|
GETFUNC(sqlite3_open);
|
|
|
|
GETFUNC(sqlite3_errmsg);
|
|
|
|
GETFUNC(sqlite3_prepare_v2);
|
|
|
|
GETFUNC(sqlite3_bind_parameter_count);
|
2015-08-08 07:43:47 +03:00
|
|
|
GETFUNC(sqlite3_bind_null);
|
2012-01-20 00:19:56 +04:00
|
|
|
GETFUNC(sqlite3_bind_text);
|
|
|
|
GETFUNC(sqlite3_step);
|
|
|
|
GETFUNC(sqlite3_column_count);
|
|
|
|
GETFUNC(sqlite3_finalize);
|
|
|
|
GETFUNC(sqlite3_close);
|
|
|
|
GETFUNC(sqlite3_column_name);
|
|
|
|
GETFUNC(sqlite3_column_type);
|
|
|
|
GETFUNC(sqlite3_column_blob);
|
|
|
|
GETFUNC(sqlite3_column_bytes);
|
|
|
|
GETFUNC(sqlite3_column_text);
|
2011-12-17 03:11:09 +04:00
|
|
|
GETFUNC(sqlite3_changes);
|
|
|
|
GETFUNC(sqlite3_last_insert_rowid);
|
2012-01-20 00:19:56 +04:00
|
|
|
#undef GETFUNC
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool initialized = false;
|
|
|
|
static jclass stringClass;
|
|
|
|
static jclass objectClass;
|
|
|
|
static jclass byteBufferClass;
|
2012-02-27 15:28:22 +04:00
|
|
|
static jclass cursorClass;
|
2012-01-20 00:19:56 +04:00
|
|
|
static jmethodID jByteBufferAllocateDirect;
|
2012-02-27 15:28:22 +04:00
|
|
|
static jmethodID jCursorConstructor;
|
|
|
|
static jmethodID jCursorAddRow;
|
2012-01-20 00:19:56 +04:00
|
|
|
|
2012-06-18 20:07:07 +04:00
|
|
|
static jobject sqliteInternalCall(JNIEnv* jenv, sqlite3 *db, jstring jQuery,
|
|
|
|
jobjectArray jParams, jlongArray jQueryRes);
|
|
|
|
|
2014-02-01 06:18:55 +04:00
|
|
|
static void throwSqliteException(JNIEnv* jenv, const char* aFormat, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, aFormat);
|
|
|
|
char* msg = nullptr;
|
|
|
|
vasprintf(&msg, aFormat, ap);
|
|
|
|
LOG("Error in SQLiteBridge: %s\n", msg);
|
|
|
|
JNI_Throw(jenv, "org/mozilla/gecko/sqlite/SQLiteBridgeException", msg);
|
2014-02-05 22:39:48 +04:00
|
|
|
free(msg);
|
2014-02-01 06:18:55 +04:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2012-01-20 00:19:56 +04:00
|
|
|
static void
|
|
|
|
JNI_Setup(JNIEnv* jenv)
|
|
|
|
{
|
|
|
|
if (initialized) return;
|
|
|
|
|
2012-02-27 15:28:22 +04:00
|
|
|
jclass lObjectClass = jenv->FindClass("java/lang/Object");
|
|
|
|
jclass lStringClass = jenv->FindClass("java/lang/String");
|
|
|
|
jclass lByteBufferClass = jenv->FindClass("java/nio/ByteBuffer");
|
|
|
|
jclass lCursorClass = jenv->FindClass("org/mozilla/gecko/sqlite/MatrixBlobCursor");
|
|
|
|
|
2013-11-11 23:14:45 +04:00
|
|
|
if (lStringClass == nullptr
|
|
|
|
|| lObjectClass == nullptr
|
|
|
|
|| lByteBufferClass == nullptr
|
|
|
|
|| lCursorClass == nullptr) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv, "FindClass error");
|
2012-02-27 15:28:22 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Those are only local references. Make them global so they work
|
|
|
|
// across calls and threads.
|
|
|
|
objectClass = (jclass)jenv->NewGlobalRef(lObjectClass);
|
|
|
|
stringClass = (jclass)jenv->NewGlobalRef(lStringClass);
|
|
|
|
byteBufferClass = (jclass)jenv->NewGlobalRef(lByteBufferClass);
|
|
|
|
cursorClass = (jclass)jenv->NewGlobalRef(lCursorClass);
|
2012-01-20 00:19:56 +04:00
|
|
|
|
2013-11-11 23:14:45 +04:00
|
|
|
if (stringClass == nullptr || objectClass == nullptr
|
|
|
|
|| byteBufferClass == nullptr
|
|
|
|
|| cursorClass == nullptr) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv, "NewGlobalRef error");
|
2012-01-20 00:19:56 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// public static ByteBuffer allocateDirect(int capacity)
|
|
|
|
jByteBufferAllocateDirect =
|
|
|
|
jenv->GetStaticMethodID(byteBufferClass, "allocateDirect", "(I)Ljava/nio/ByteBuffer;");
|
2012-02-27 15:28:22 +04:00
|
|
|
// new MatrixBlobCursor(String [])
|
|
|
|
jCursorConstructor =
|
|
|
|
jenv->GetMethodID(cursorClass, "<init>", "([Ljava/lang/String;)V");
|
|
|
|
// public void addRow (Object[] columnValues)
|
|
|
|
jCursorAddRow =
|
|
|
|
jenv->GetMethodID(cursorClass, "addRow", "([Ljava/lang/Object;)V");
|
2012-01-20 00:19:56 +04:00
|
|
|
|
2013-11-11 23:14:45 +04:00
|
|
|
if (jByteBufferAllocateDirect == nullptr
|
|
|
|
|| jCursorConstructor == nullptr
|
|
|
|
|| jCursorAddRow == nullptr) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv, "GetMethodId error");
|
2012-01-20 00:19:56 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
2016-11-29 22:27:30 +03:00
|
|
|
extern "C" APKOPEN_EXPORT jobject MOZ_JNICALL
|
2012-01-20 00:19:56 +04:00
|
|
|
Java_org_mozilla_gecko_sqlite_SQLiteBridge_sqliteCall(JNIEnv* jenv, jclass,
|
|
|
|
jstring jDb,
|
|
|
|
jstring jQuery,
|
|
|
|
jobjectArray jParams,
|
2012-02-27 15:28:22 +04:00
|
|
|
jlongArray jQueryRes)
|
2012-01-20 00:19:56 +04:00
|
|
|
{
|
|
|
|
JNI_Setup(jenv);
|
|
|
|
|
2012-04-09 21:08:37 +04:00
|
|
|
int rc;
|
2013-11-11 23:14:45 +04:00
|
|
|
jobject jCursor = nullptr;
|
2012-04-09 21:08:37 +04:00
|
|
|
const char* dbPath;
|
|
|
|
sqlite3 *db;
|
2011-12-17 03:11:09 +04:00
|
|
|
|
2013-11-11 23:14:45 +04:00
|
|
|
dbPath = jenv->GetStringUTFChars(jDb, nullptr);
|
2012-04-09 21:08:37 +04:00
|
|
|
rc = f_sqlite3_open(dbPath, &db);
|
|
|
|
jenv->ReleaseStringUTFChars(jDb, dbPath);
|
|
|
|
if (rc != SQLITE_OK) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv,
|
|
|
|
"Can't open database: %s", f_sqlite3_errmsg(db));
|
|
|
|
f_sqlite3_close(db); // close db even if open failed
|
|
|
|
return nullptr;
|
2012-04-09 21:08:37 +04:00
|
|
|
}
|
2014-02-01 06:18:55 +04:00
|
|
|
jCursor = sqliteInternalCall(jenv, db, jQuery, jParams, jQueryRes);
|
2012-04-09 21:08:37 +04:00
|
|
|
f_sqlite3_close(db);
|
|
|
|
return jCursor;
|
|
|
|
}
|
|
|
|
|
2016-11-29 22:27:30 +03:00
|
|
|
extern "C" APKOPEN_EXPORT jobject MOZ_JNICALL
|
2012-04-09 21:08:37 +04:00
|
|
|
Java_org_mozilla_gecko_sqlite_SQLiteBridge_sqliteCallWithDb(JNIEnv* jenv, jclass,
|
|
|
|
jlong jDb,
|
|
|
|
jstring jQuery,
|
|
|
|
jobjectArray jParams,
|
|
|
|
jlongArray jQueryRes)
|
|
|
|
{
|
|
|
|
JNI_Setup(jenv);
|
|
|
|
|
2013-11-11 23:14:45 +04:00
|
|
|
jobject jCursor = nullptr;
|
2012-04-09 21:08:37 +04:00
|
|
|
sqlite3 *db = (sqlite3*)jDb;
|
|
|
|
jCursor = sqliteInternalCall(jenv, db, jQuery, jParams, jQueryRes);
|
|
|
|
return jCursor;
|
|
|
|
}
|
|
|
|
|
2016-11-29 22:27:30 +03:00
|
|
|
extern "C" APKOPEN_EXPORT jlong MOZ_JNICALL
|
2012-04-09 21:08:37 +04:00
|
|
|
Java_org_mozilla_gecko_sqlite_SQLiteBridge_openDatabase(JNIEnv* jenv, jclass,
|
|
|
|
jstring jDb)
|
|
|
|
{
|
|
|
|
JNI_Setup(jenv);
|
2012-04-09 21:08:37 +04:00
|
|
|
|
|
|
|
int rc;
|
2012-04-09 21:08:37 +04:00
|
|
|
const char* dbPath;
|
|
|
|
sqlite3 *db;
|
|
|
|
|
2013-11-11 23:14:45 +04:00
|
|
|
dbPath = jenv->GetStringUTFChars(jDb, nullptr);
|
2012-04-09 21:44:32 +04:00
|
|
|
rc = f_sqlite3_open(dbPath, &db);
|
|
|
|
jenv->ReleaseStringUTFChars(jDb, dbPath);
|
|
|
|
if (rc != SQLITE_OK) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv,
|
|
|
|
"Can't open database: %s", f_sqlite3_errmsg(db));
|
|
|
|
f_sqlite3_close(db); // close db even if open failed
|
|
|
|
return 0;
|
2012-04-09 21:44:32 +04:00
|
|
|
}
|
2012-04-09 21:08:37 +04:00
|
|
|
return (jlong)db;
|
|
|
|
}
|
|
|
|
|
2016-11-29 22:27:30 +03:00
|
|
|
extern "C" APKOPEN_EXPORT void MOZ_JNICALL
|
2012-04-09 21:08:37 +04:00
|
|
|
Java_org_mozilla_gecko_sqlite_SQLiteBridge_closeDatabase(JNIEnv* jenv, jclass,
|
|
|
|
jlong jDb)
|
|
|
|
{
|
|
|
|
JNI_Setup(jenv);
|
|
|
|
|
|
|
|
sqlite3 *db = (sqlite3*)jDb;
|
|
|
|
f_sqlite3_close(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
static jobject
|
|
|
|
sqliteInternalCall(JNIEnv* jenv,
|
|
|
|
sqlite3 *db,
|
|
|
|
jstring jQuery,
|
|
|
|
jobjectArray jParams,
|
|
|
|
jlongArray jQueryRes)
|
|
|
|
{
|
|
|
|
JNI_Setup(jenv);
|
|
|
|
|
2013-11-11 23:14:45 +04:00
|
|
|
jobject jCursor = nullptr;
|
2012-04-09 21:08:37 +04:00
|
|
|
jsize numPars = 0;
|
|
|
|
|
|
|
|
const char *pzTail;
|
|
|
|
sqlite3_stmt *ppStmt;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
const char* queryStr;
|
2013-11-11 23:14:45 +04:00
|
|
|
queryStr = jenv->GetStringUTFChars(jQuery, nullptr);
|
2012-01-20 00:19:56 +04:00
|
|
|
|
|
|
|
rc = f_sqlite3_prepare_v2(db, queryStr, -1, &ppStmt, &pzTail);
|
2013-11-11 23:14:45 +04:00
|
|
|
if (rc != SQLITE_OK || ppStmt == nullptr) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv,
|
|
|
|
"Can't prepare statement: %s", f_sqlite3_errmsg(db));
|
|
|
|
return nullptr;
|
2012-01-20 00:19:56 +04:00
|
|
|
}
|
|
|
|
jenv->ReleaseStringUTFChars(jQuery, queryStr);
|
|
|
|
|
|
|
|
// Check if number of parameters matches
|
2013-11-11 23:14:45 +04:00
|
|
|
if (jParams != nullptr) {
|
2011-12-17 03:11:09 +04:00
|
|
|
numPars = jenv->GetArrayLength(jParams);
|
|
|
|
}
|
2012-01-20 00:19:56 +04:00
|
|
|
int sqlNumPars;
|
|
|
|
sqlNumPars = f_sqlite3_bind_parameter_count(ppStmt);
|
|
|
|
if (numPars != sqlNumPars) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv,
|
|
|
|
"Passed parameter count (%d) "
|
|
|
|
"doesn't match SQL parameter count (%d)",
|
2012-01-20 00:19:56 +04:00
|
|
|
numPars, sqlNumPars);
|
2014-02-01 06:18:55 +04:00
|
|
|
return nullptr;
|
2012-01-20 00:19:56 +04:00
|
|
|
}
|
2011-12-17 03:11:09 +04:00
|
|
|
|
2013-11-11 23:14:45 +04:00
|
|
|
if (jParams != nullptr) {
|
2011-12-17 03:11:09 +04:00
|
|
|
// Bind parameters, if any
|
|
|
|
if (numPars > 0) {
|
|
|
|
for (int i = 0; i < numPars; i++) {
|
|
|
|
jobject jObjectParam = jenv->GetObjectArrayElement(jParams, i);
|
|
|
|
// IsInstanceOf or isAssignableFrom? String is final, so IsInstanceOf
|
|
|
|
// should be OK.
|
|
|
|
jboolean isString = jenv->IsInstanceOf(jObjectParam, stringClass);
|
|
|
|
if (isString != JNI_TRUE) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv,
|
|
|
|
"Parameter is not of String type");
|
|
|
|
return nullptr;
|
2011-12-17 03:11:09 +04:00
|
|
|
}
|
2015-08-08 07:43:47 +03:00
|
|
|
|
2011-12-17 03:11:09 +04:00
|
|
|
// SQLite parameters index from 1.
|
2015-08-08 07:43:47 +03:00
|
|
|
if (jObjectParam == nullptr) {
|
|
|
|
rc = f_sqlite3_bind_null(ppStmt, i + 1);
|
|
|
|
} else {
|
|
|
|
jstring jStringParam = (jstring) jObjectParam;
|
|
|
|
const char* paramStr = jenv->GetStringUTFChars(jStringParam, nullptr);
|
|
|
|
rc = f_sqlite3_bind_text(ppStmt, i + 1, paramStr, -1, SQLITE_TRANSIENT);
|
|
|
|
jenv->ReleaseStringUTFChars(jStringParam, paramStr);
|
|
|
|
}
|
|
|
|
|
2011-12-17 03:11:09 +04:00
|
|
|
if (rc != SQLITE_OK) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv, "Error binding query parameter");
|
|
|
|
return nullptr;
|
2011-12-17 03:11:09 +04:00
|
|
|
}
|
2012-01-20 00:19:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the query and step through the results
|
|
|
|
rc = f_sqlite3_step(ppStmt);
|
|
|
|
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv,
|
|
|
|
"Can't step statement: (%d) %s", rc, f_sqlite3_errmsg(db));
|
|
|
|
return nullptr;
|
2012-01-20 00:19:56 +04:00
|
|
|
}
|
|
|
|
|
2012-02-27 15:28:22 +04:00
|
|
|
// Get the column count and names
|
2012-01-20 00:19:56 +04:00
|
|
|
int cols;
|
|
|
|
cols = f_sqlite3_column_count(ppStmt);
|
2012-02-27 15:28:22 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
// Allocate a String[cols]
|
|
|
|
jobjectArray jStringArray = jenv->NewObjectArray(cols,
|
|
|
|
stringClass,
|
2013-11-11 23:14:45 +04:00
|
|
|
nullptr);
|
|
|
|
if (jStringArray == nullptr) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv, "Can't allocate String[]");
|
|
|
|
return nullptr;
|
2012-02-27 15:28:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assign column names to the String[]
|
|
|
|
for (int i = 0; i < cols; i++) {
|
|
|
|
const char* colName = f_sqlite3_column_name(ppStmt, i);
|
|
|
|
jstring jStr = jenv->NewStringUTF(colName);
|
|
|
|
jenv->SetObjectArrayElement(jStringArray, i, jStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the MatrixCursor(String[]) with given column names
|
|
|
|
jCursor = jenv->NewObject(cursorClass,
|
|
|
|
jCursorConstructor,
|
|
|
|
jStringArray);
|
2013-11-11 23:14:45 +04:00
|
|
|
if (jCursor == nullptr) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv, "Can't allocate MatrixBlobCursor");
|
|
|
|
return nullptr;
|
2012-02-27 15:28:22 +04:00
|
|
|
}
|
2012-01-20 00:19:56 +04:00
|
|
|
}
|
|
|
|
|
2012-02-27 15:28:21 +04:00
|
|
|
// Return the id and number of changed rows in jQueryRes
|
|
|
|
{
|
2012-02-27 15:28:22 +04:00
|
|
|
jlong id = f_sqlite3_last_insert_rowid(db);
|
|
|
|
jenv->SetLongArrayRegion(jQueryRes, 0, 1, &id);
|
|
|
|
|
|
|
|
jlong changed = f_sqlite3_changes(db);
|
|
|
|
jenv->SetLongArrayRegion(jQueryRes, 1, 1, &changed);
|
2011-12-17 03:11:09 +04:00
|
|
|
}
|
|
|
|
|
2012-01-20 00:19:56 +04:00
|
|
|
// For each row, add an Object[] to the passed ArrayList,
|
|
|
|
// with that containing either String or ByteArray objects
|
|
|
|
// containing the columns
|
2012-01-25 10:53:04 +04:00
|
|
|
while (rc != SQLITE_DONE) {
|
2012-01-20 00:19:56 +04:00
|
|
|
// Process row
|
|
|
|
// Construct Object[]
|
|
|
|
jobjectArray jRow = jenv->NewObjectArray(cols,
|
|
|
|
objectClass,
|
2013-11-11 23:14:45 +04:00
|
|
|
nullptr);
|
|
|
|
if (jRow == nullptr) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv, "Can't allocate jRow Object[]");
|
|
|
|
return nullptr;
|
2012-01-20 00:19:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < cols; i++) {
|
|
|
|
int colType = f_sqlite3_column_type(ppStmt, i);
|
|
|
|
if (colType == SQLITE_BLOB) {
|
|
|
|
// Treat as blob
|
|
|
|
const void* blob = f_sqlite3_column_blob(ppStmt, i);
|
|
|
|
int colLen = f_sqlite3_column_bytes(ppStmt, i);
|
|
|
|
|
|
|
|
// Construct ByteBuffer of correct size
|
|
|
|
jobject jByteBuffer =
|
|
|
|
jenv->CallStaticObjectMethod(byteBufferClass,
|
|
|
|
jByteBufferAllocateDirect,
|
|
|
|
colLen);
|
2013-11-11 23:14:45 +04:00
|
|
|
if (jByteBuffer == nullptr) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv,
|
|
|
|
"Failure calling ByteBuffer.allocateDirect");
|
|
|
|
return nullptr;
|
2012-01-20 00:19:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get its backing array
|
|
|
|
void* bufferArray = jenv->GetDirectBufferAddress(jByteBuffer);
|
2013-11-11 23:14:45 +04:00
|
|
|
if (bufferArray == nullptr) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv,
|
|
|
|
"Failure calling GetDirectBufferAddress");
|
|
|
|
return nullptr;
|
2012-01-20 00:19:56 +04:00
|
|
|
}
|
|
|
|
memcpy(bufferArray, blob, colLen);
|
|
|
|
|
|
|
|
jenv->SetObjectArrayElement(jRow, i, jByteBuffer);
|
|
|
|
jenv->DeleteLocalRef(jByteBuffer);
|
|
|
|
} else if (colType == SQLITE_NULL) {
|
2013-11-11 23:14:45 +04:00
|
|
|
jenv->SetObjectArrayElement(jRow, i, nullptr);
|
2012-01-20 00:19:56 +04:00
|
|
|
} else {
|
|
|
|
// Treat everything else as text
|
|
|
|
const char* txt = (const char*)f_sqlite3_column_text(ppStmt, i);
|
|
|
|
jstring jStr = jenv->NewStringUTF(txt);
|
|
|
|
jenv->SetObjectArrayElement(jRow, i, jStr);
|
|
|
|
jenv->DeleteLocalRef(jStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-27 15:28:22 +04:00
|
|
|
// Append Object[] to Cursor
|
|
|
|
jenv->CallVoidMethod(jCursor, jCursorAddRow, jRow);
|
2012-01-20 00:19:56 +04:00
|
|
|
|
|
|
|
// Clean up
|
|
|
|
jenv->DeleteLocalRef(jRow);
|
|
|
|
|
|
|
|
// Get next row
|
|
|
|
rc = f_sqlite3_step(ppStmt);
|
|
|
|
// Real error?
|
|
|
|
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv,
|
|
|
|
"Can't re-step statement:(%d) %s", rc, f_sqlite3_errmsg(db));
|
|
|
|
return nullptr;
|
2012-01-20 00:19:56 +04:00
|
|
|
}
|
2012-01-25 10:53:04 +04:00
|
|
|
}
|
2012-01-20 00:19:56 +04:00
|
|
|
|
|
|
|
rc = f_sqlite3_finalize(ppStmt);
|
|
|
|
if (rc != SQLITE_OK) {
|
2014-02-01 06:18:55 +04:00
|
|
|
throwSqliteException(jenv,
|
|
|
|
"Can't finalize statement: %s", f_sqlite3_errmsg(db));
|
|
|
|
return nullptr;
|
2012-01-20 00:19:56 +04:00
|
|
|
}
|
|
|
|
|
2012-02-27 15:28:22 +04:00
|
|
|
return jCursor;
|
2012-01-20 00:19:56 +04:00
|
|
|
}
|