static jbyteArray nativeGetBlob(JNIEnv* env, jclass clazz, jlong windowPtr, jint row, jint column) { CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); LOG_WINDOW("Getting blob for %d,%d from %p", row, column, window); CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column); if (!fieldSlot) { throwExceptionWithRowCol(env, row, column); return NULL; } int32_t type = window->getFieldSlotType(fieldSlot); if (type == CursorWindow::FIELD_TYPE_BLOB || type == CursorWindow::FIELD_TYPE_STRING) { size_t size; const void* value = window->getFieldSlotValueBlob(fieldSlot, &size); jbyteArray byteArray = env->NewByteArray(size); if (!byteArray) { env->ExceptionClear(); throw_sqlite3_exception(env, "Native could not create new byte[]"); return NULL; } env->SetByteArrayRegion(byteArray, 0, size, static_cast<const jbyte*>(value)); return byteArray; } else if (type == CursorWindow::FIELD_TYPE_INTEGER) { throw_sqlite3_exception(env, "INTEGER data in nativeGetBlob "); } else if (type == CursorWindow::FIELD_TYPE_FLOAT) { throw_sqlite3_exception(env, "FLOAT data in nativeGetBlob "); } else if (type == CursorWindow::FIELD_TYPE_NULL) { // do nothing } else { throwUnknownTypeException(env, type); } return NULL; }
void Java_com_jim_multipos_utils_database_AbstractSQLite_tableinfo(JNIEnv *env, jobject object, int dbHandle, jstring tableName) { sqlite3 *db = (sqlite3 *) dbHandle; sqlite3_stmt *handle; char const *tn = env->GetStringUTFChars(tableName, 0); char test[100] = "CREATE TABLE IF NOT EXISTS foo (id INTEGER, name TEXT, test TEXT);"; int rc = sqlite3_exec(db, test, NULL, 0, 0); if (rc != SQLITE_OK) { throw_sqlite3_exception(env, db, rc); return; } else __android_log_write(ANDROID_LOG_DEBUG, "com.jim.multipos", "table created"); std::string temp = "PRAGMA table_info(foo)"; char query[100]; strcpy(query, temp.c_str()); rc = sqlite3_prepare_v2(db, query, sizeof(query), &handle, NULL); if (rc != SQLITE_OK) { throw_sqlite3_exception(env, db, rc); return; } while (sqlite3_step(handle) == SQLITE_ROW) { __android_log_write(ANDROID_LOG_DEBUG, "com.jim.multipos", std::to_string(sqlite3_column_count(handle)).c_str()); const char *str = (const char *) sqlite3_column_text(handle, 0); __android_log_write(ANDROID_LOG_DEBUG, "com.jim.multipos", str); } sqlite3_finalize(handle); if (tn != 0) { env->ReleaseStringUTFChars(tableName, tn); } }
static jbyteArray getBlob_native(JNIEnv* env, jobject object, jint row, jint column) { int32_t err; CursorWindow * window = GET_WINDOW(env, object); LOG_WINDOW("Getting blob for %d,%d from %p", row, column, window); field_slot_t field; err = window->read_field_slot(row, column, &field); if (err != 0) { throwExceptionWithRowCol(env, row, column); return NULL; } uint8_t type = field.type; if (type == FIELD_TYPE_BLOB || type == FIELD_TYPE_STRING) { jbyteArray byteArray = env->NewByteArray(field.data.buffer.size); LOG_ASSERT(byteArray, "Native could not create new byte[]"); env->SetByteArrayRegion(byteArray, 0, field.data.buffer.size, (const jbyte*)window->offsetToPtr(field.data.buffer.offset)); return byteArray; } else if (type == FIELD_TYPE_INTEGER) { throw_sqlite3_exception(env, "INTEGER data in getBlob_native "); } else if (type == FIELD_TYPE_FLOAT) { throw_sqlite3_exception(env, "FLOAT data in getBlob_native "); } else if (type == FIELD_TYPE_NULL) { // do nothing } else { throwUnknowTypeException(env, type); } return NULL; }
/* throw a SQLiteException for a given error code */ void throw_sqlite3_exception_errcode(JNIEnv* env, int errcode, const char* message) { if (errcode == SQLITE_DONE) { throw_sqlite3_exception(env, errcode, NULL, message); } else { char temp[21]; sprintf(temp, "error code %d", errcode); throw_sqlite3_exception(env, errcode, temp, message); } }
static int executeNonQuery(JNIEnv* env, SQLiteConnection* connection, sqlite3_stmt* statement) { int err = sqlite3_step(statement); if (err == SQLITE_ROW) { throw_sqlite3_exception(env, "Queries can be performed using SQLiteDatabase query or rawQuery methods only."); } else if (err != SQLITE_DONE) { throw_sqlite3_exception(env, connection->db); } return err; }
/* throw a SQLiteException with a message appropriate for the error in handle concatenated with the given message */ void throw_sqlite3_exception(JNIEnv* env, sqlite3* handle, const char* message) { if (handle) { throw_sqlite3_exception(env, sqlite3_errcode(handle), sqlite3_errmsg(handle), message); } else { // we use SQLITE_OK so that a generic SQLiteException is thrown; // any code not specified in the switch statement below would do. throw_sqlite3_exception(env, SQLITE_OK, "unknown error", message); } }
/* * Class: com_sabo_sqlite_SQLiteDatabase_SQLiteQuery * Method: nativeQuery * Signature: (J)[Ljava/lang/String; */ JNIEXPORT jobjectArray JNICALL Java_com_sabo_sqlite_SQLiteDatabase_00024SQLiteQuery_nativeQuery (JNIEnv *env, jobject clazz, jlong statementPtr) { sqlite3_stmt *pStmt; jobjectArray objArray; int nCol; int i; int rc; int nLen; jchar *zVal; jclass clazzString; jstring val; assert(statementPtr != 0); pStmt = (sqlite3_stmt *)statementPtr; nCol = sqlite3_column_count(pStmt); assert(nCol > 0); clazzString = env->FindClass("java/lang/String"); assert(clazzString != NULL); /** Ignore to check clazzString. */ if (nCol <= 0) throw_sqlite3_exception(env, "[SQLite]: nCol is less than 1."); objArray = env->NewObjectArray(nCol, clazzString, NULL); if (objArray == NULL) throw_sqlite3_exception(env, "[SQLite]: Allocate objArray failed (No enough memory)"); rc = sqlite3_step(pStmt); if (rc == SQLITE_ROW) { for (i = 0; i < nCol; ++i) { zVal = (jchar *)sqlite3_column_text16(pStmt, i); nLen = 0; if (zVal) { nLen = sqlite3_column_bytes16(pStmt, i); assert(nLen > 0); } if (nLen > 0) { val = env->NewString(zVal, nLen/sizeof(jchar)); assert(val != NULL); if (val == NULL) throw_sqlite3_exception(env, "[SQLite]: NewString failed..."); } else { val = NULL; } env->SetObjectArrayElement(objArray, i, val); } } else { if (rc == SQLITE_OK || rc == SQLITE_DONE) { return NULL; } throw_sqlite3_exception(env, "[SQLITE]: Fail to run sqltie3_step."); } return objArray; }
/* throw a SQLiteException with a message appropriate for the error in handle concatenated with the given message */ void throw_sqlite3_exception(JNIEnv* env, sqlite3* handle, const char* message) { if (handle) { // get the error code and message from the SQLite connection // the error message may contain more information than the error code // because it is based on the extended error code rather than the simplified // error code that SQLite normally returns. throw_sqlite3_exception(env, sqlite3_extended_errcode(handle), sqlite3_errmsg(handle), message); } else { // we use SQLITE_OK so that a generic SQLiteException is thrown; // any code not specified in the switch statement below would do. throw_sqlite3_exception(env, SQLITE_OK, "unknown error", message); } }
/* public native void native_execSQL(String sql); */ static void native_execSQL(JNIEnv* env, jobject object, jstring sqlString) { int err; int stepErr; sqlite3_stmt * statement = NULL; sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle); jchar const * sql = env->GetStringChars(sqlString, NULL); jsize sqlLen = env->GetStringLength(sqlString); if (sql == NULL || sqlLen == 0) { jniThrowException(env, "java/lang/IllegalArgumentException", "You must supply an SQL string"); return; } err = sqlite3_prepare16_v2(handle, sql, sqlLen * 2, &statement, NULL); env->ReleaseStringChars(sqlString, sql); if (err != SQLITE_OK) { char const * sql8 = env->GetStringUTFChars(sqlString, NULL); LOGE("Failure %d (%s) on %p when preparing '%s'.\n", err, sqlite3_errmsg(handle), handle, sql8); throw_sqlite3_exception(env, handle, sql8); env->ReleaseStringUTFChars(sqlString, sql8); return; } stepErr = sqlite3_step(statement); err = sqlite3_finalize(statement); if (stepErr != SQLITE_DONE) { if (stepErr == SQLITE_ROW) { throw_sqlite3_exception(env, "Queries cannot be performed using execSQL(), use query() instead."); } else { char const * sql8 = env->GetStringUTFChars(sqlString, NULL); LOGE("Failure %d (%s) on %p when executing '%s'\n", err, sqlite3_errmsg(handle), handle, sql8); throw_sqlite3_exception(env, handle, sql8); env->ReleaseStringUTFChars(sqlString, sql8); } } else #ifndef DB_LOG_STATEMENTS IF_LOGV() #endif { char const * sql8 = env->GetStringUTFChars(sqlString, NULL); LOGV("Success on %p when executing '%s'\n", handle, sql8); env->ReleaseStringUTFChars(sqlString, sql8); } }
//close db void Java_com_jim_multipos_utils_database_AbstractSQLite_closedb(JNIEnv *env, jobject object, int sqliteHandle) { sqlite3 *handle = (sqlite3 *)sqliteHandle; int err = sqlite3_close(handle); if (SQLITE_OK != err) { throw_sqlite3_exception(env, handle, err); } }
static jdouble nativeGetDouble(JNIEnv* env, jclass clazz, jlong windowPtr, jint row, jint column) { CursorWindow* window = reinterpret_cast<CursorWindow*>(windowPtr); LOG_WINDOW("Getting double for %d,%d from %p", row, column, window); CursorWindow::FieldSlot* fieldSlot = window->getFieldSlot(row, column); if (!fieldSlot) { throwExceptionWithRowCol(env, row, column); return 0.0; } int32_t type = window->getFieldSlotType(fieldSlot); if (type == CursorWindow::FIELD_TYPE_FLOAT) { return window->getFieldSlotValueDouble(fieldSlot); } else if (type == CursorWindow::FIELD_TYPE_STRING) { size_t sizeIncludingNull; const char* value = window->getFieldSlotValueString(fieldSlot, &sizeIncludingNull); return sizeIncludingNull > 1 ? strtod(value, NULL) : 0.0; } else if (type == CursorWindow::FIELD_TYPE_INTEGER) { return jdouble(window->getFieldSlotValueLong(fieldSlot)); } else if (type == CursorWindow::FIELD_TYPE_NULL) { return 0.0; } else if (type == CursorWindow::FIELD_TYPE_BLOB) { throw_sqlite3_exception(env, "Unable to convert BLOB to double"); return 0.0; } else { throwUnknownTypeException(env, type); return 0.0; } }
void Java_com_jmv_frre_moduloestudiante_SQLite_SQLiteDatabase_closedb(JNIEnv *env, jobject object, int sqliteHandle) { sqlite3 *handle = (sqlite3 *)sqliteHandle; int err = sqlite3_close(handle); if (SQLITE_OK != err) { throw_sqlite3_exception(env, handle, err); } }
void Java_com_jim_multipos_utils_database_SQLitePreparedStatement_reset(JNIEnv *env, jobject object, int statementHandle) { sqlite3_stmt *handle = (sqlite3_stmt *) statementHandle; int errcode = sqlite3_reset(handle); if (SQLITE_OK != errcode) { throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); } }
static void native_finalize(JNIEnv* env, jobject object) { char buf[66]; strcpy(buf, "com_sqlcrypt_database_SQLiteProgram->native_finalize() not implemented"); throw_sqlite3_exception(env, GET_HANDLE(env, object), buf); return; }
/* public native void close(); */ static void dbclose(JNIEnv* env, jobject object) { sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle); if (handle != NULL) { // release the memory associated with the traceFuncArg in enableSqlTracing function void *traceFuncArg = sqlite3_trace(handle, &sqlTrace, NULL); if (traceFuncArg != NULL) { free(traceFuncArg); } // release the memory associated with the traceFuncArg in enableSqlProfiling function traceFuncArg = sqlite3_profile(handle, &sqlProfile, NULL); if (traceFuncArg != NULL) { free(traceFuncArg); } LOGV("Closing database: handle=%p\n", handle); int result = sqlite3_close(handle); if (result == SQLITE_OK) { LOGV("Closed %p\n", handle); env->SetIntField(object, offset_db_handle, 0); } else { // This can happen if sub-objects aren't closed first. Make sure the caller knows. throw_sqlite3_exception(env, handle); LOGE("sqlite3_close(%p) failed: %d\n", handle, result); } } }
static void native_compile(JNIEnv* env, jobject object, jstring sqlString) { char buf[65]; strcpy(buf, "android_database_SQLiteProgram->native_compile() not implemented"); throw_sqlite3_exception(env, GET_HANDLE(env, object), buf); return; }
void Java_ir_persianfox_SQLite_SQLiteDatabase_closedb(JNIEnv *env, jobject object, int sqliteHandle) { sqlite3 *handle = (sqlite3 *)sqliteHandle; int err = sqlite3_close(handle); if (SQLITE_OK != err) { throw_sqlite3_exception(env, handle, err); } }
static jlong nativePrepareStatement(JNIEnv* env, jclass clazz, jlong connectionPtr, jstring sqlString) { SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); jsize sqlLength = env->GetStringLength(sqlString); const jchar* sql = env->GetStringCritical(sqlString, NULL); sqlite3_stmt* statement; int err = sqlite3_prepare16_v2(connection->db, sql, sqlLength * sizeof(jchar), &statement, NULL); env->ReleaseStringCritical(sqlString, sql); if (err != SQLITE_OK) { // Error messages like 'near ")": syntax error' are not // always helpful enough, so construct an error string that // includes the query itself. const char *query = env->GetStringUTFChars(sqlString, NULL); char *message = (char*) malloc(strlen(query) + 50); if (message) { strcpy(message, ", while compiling: "); // less than 50 chars strcat(message, query); } env->ReleaseStringUTFChars(sqlString, query); throw_sqlite3_exception(env, connection->db, message); free(message); return 0; } ALOGV("Prepared statement %p on connection %p", statement, connection->db); return reinterpret_cast<jlong>(statement); }
static int executeOneRowQuery(JNIEnv* env, SQLiteConnection* connection, sqlite3_stmt* statement) { int err = sqlite3_step(statement); if (err != SQLITE_ROW) { throw_sqlite3_exception(env, connection->db); } return err; }
//bind long void Java_com_jim_multipos_utils_database_SQLitePreparedStatement_bindLong(JNIEnv *env, jobject object, int statementHandle, int index, long long value) { sqlite3_stmt *handle = (sqlite3_stmt *) statementHandle; int errcode = sqlite3_bind_int64(handle, index, value); if (SQLITE_OK != errcode) { throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); } }
void native_key_char(JNIEnv* env, jobject object, jcharArray jKey) { char *keyUtf8 = 0; int lenUtf8 = 0; jchar* keyUtf16 = 0; jsize lenUtf16 = 0; UErrorCode status = U_ZERO_ERROR; UConverter *encoding = 0; sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle); keyUtf16 = env->GetCharArrayElements(jKey, 0); lenUtf16 = env->GetArrayLength(jKey); // no key, bailing out. if ( lenUtf16 == 0 ) goto done; encoding = ucnv_open("UTF-8", &status); if( U_FAILURE(status) ) { throw_sqlite3_exception(env, "native_key_char: opening encoding converter failed"); goto done; } lenUtf8 = ucnv_fromUChars(encoding, NULL, 0, keyUtf16, lenUtf16, &status); status = (status == U_BUFFER_OVERFLOW_ERROR) ? U_ZERO_ERROR : status; if( U_FAILURE(status) ) { throw_sqlite3_exception(env, "native_key_char: utf8 length unknown"); goto done; } keyUtf8 = (char*) malloc(lenUtf8 * sizeof(char)); ucnv_fromUChars(encoding, keyUtf8, lenUtf8, keyUtf16, lenUtf16, &status); if( U_FAILURE(status) ) { throw_sqlite3_exception(env, "native_key_char: utf8 conversion failed"); goto done; } if ( sqlite3_key(handle, keyUtf8, lenUtf8) != SQLITE_OK ) { throw_sqlite3_exception(env, handle); } done: env->ReleaseCharArrayElements(jKey, keyUtf16, 0); if(encoding != 0) ucnv_close(encoding); if(keyUtf8 != 0) free(keyUtf8); }
static jstring getString_native(JNIEnv* env, jobject object, jint row, jint column) { int32_t err; CursorWindow * window = GET_WINDOW(env, object); LOG_WINDOW("Getting string for %d,%d from %p", row, column, window); field_slot_t field; err = window->read_field_slot(row, column, &field); if (err != 0) { throwExceptionWithRowCol(env, row, column); return NULL; } uint8_t type = field.type; if (type == FIELD_TYPE_STRING) { uint32_t size = field.data.buffer.size; if (size > 0) { #if WINDOW_STORAGE_UTF8 // Pass size - 1 since the UTF8 is null terminated and we don't want a null terminator on the UTF16 string String16 utf16((char const *)window->offsetToPtr(field.data.buffer.offset), size - 1); return env->NewString((jchar const *)utf16.string(), utf16.size()); #else return env->NewString((jchar const *)window->offsetToPtr(field.data.buffer.offset), size / 2); #endif } else { return env->NewStringUTF(""); } } else if (type == FIELD_TYPE_INTEGER) { int64_t value; if (window->getLong(row, column, &value)) { char buf[32]; snprintf(buf, sizeof(buf), "%lld", value); return env->NewStringUTF(buf); } return NULL; } else if (type == FIELD_TYPE_FLOAT) { double value; if (window->getDouble(row, column, &value)) { char buf[32]; //selete the print way by code to impove the precision if (((value > 0.0001) && (value < 1000000)) || ((value < -0.0001) && (value > -1000000))) snprintf(buf, sizeof(buf), "%lf", value); else snprintf(buf, sizeof(buf), "%e", value); return env->NewStringUTF(buf); } return NULL; } else if (type == FIELD_TYPE_NULL) { return NULL; } else if (type == FIELD_TYPE_BLOB) { throw_sqlite3_exception(env, "Unable to convert BLOB to string"); return NULL; } else { throwUnknowTypeException(env, type); return NULL; } }
//binding none primitive types void Java_com_jim_multipos_utils_database_SQLitePreparedStatement_bindByteBuffer(JNIEnv *env, jobject object, int statementHandle, int index, jobject value, int length) { sqlite3_stmt *handle = (sqlite3_stmt *) statementHandle; void *buf = env->GetDirectBufferAddress(value); int errcode = sqlite3_bind_blob(handle, index, buf, length, SQLITE_STATIC); if (SQLITE_OK != errcode) { throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); } }
ECode CursorWindow::NativeGetBlob( /* [in] */ Int32 row, /* [in] */ Int32 column, /* [out] */ ArrayOf<Byte>** blob) { ECode ec = NOERROR; int32_t err; NativeCursorWindow * window = mNativeWindow; // LOG_WINDOW("Getting blob for %d,%d from %p", row, column, window); field_slot_t field; err = window->read_field_slot(row, column, &field); if (err != 0) { *blob = NULL; // throwExceptionWithRowCol(env, row, column); return E_ILLEGAL_STATE_EXCEPTION; } uint8_t type = field.type; if (type == FIELD_TYPE_BLOB || type == FIELD_TYPE_STRING) { ArrayOf<Byte>* byteArray = ArrayOf<Byte>::Alloc(field.data.buffer.size); // LOG_ASSERT(byteArray, "Native could not create new byte[]"); memcpy(byteArray->GetPayload(), window->offsetToPtr(field.data.buffer.offset), field.data.buffer.size); *blob = byteArray; return NOERROR; } else if (type == FIELD_TYPE_INTEGER) { ec = throw_sqlite3_exception(NULL); } else if (type == FIELD_TYPE_FLOAT) { ec = throw_sqlite3_exception(NULL); } else if (type == FIELD_TYPE_NULL) { // do nothing } else { // throwUnknowTypeException(env, type); ec = E_ILLEGAL_STATE_EXCEPTION; } *blob = NULL; return ec; }
void native_rawExecSQL(JNIEnv* env, jobject object, jstring sql) { sqlite3 * handle = (sqlite3 *)env->GetLongField(object, offset_db_handle); char const * sqlCommand = env->GetStringUTFChars(sql, NULL); int status = sqlite3_exec(handle, sqlCommand, NULL, NULL, NULL); env->ReleaseStringUTFChars(sql, sqlCommand); if(status != SQLITE_OK){ throw_sqlite3_exception(env, handle); } }
static void nativeBindDouble(JNIEnv* env, jclass clazz, jlong connectionPtr, jlong statementPtr, jint index, jdouble value) { SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); sqlite3_stmt* statement = reinterpret_cast<sqlite3_stmt*>(statementPtr); int err = sqlite3_bind_double(statement, index, value); if (err != SQLITE_OK) { throw_sqlite3_exception(env, connection->db, NULL); } }
static void native_clear_bindings(JNIEnv* env, jobject object) { int err; sqlite3_stmt * statement = GET_STATEMENT(env, object); err = sqlite3_clear_bindings(statement); if (err != SQLITE_OK) { throw_sqlite3_exception(env, GET_HANDLE(env, object)); return; } }
int native_status(JNIEnv* env, jobject object, jint operation, jboolean reset) { int value; int highWater; sqlite3 * handle = (sqlite3 *)env->GetLongField(object, offset_db_handle); int status = sqlite3_status(operation, &value, &highWater, reset); if(status != SQLITE_OK){ throw_sqlite3_exception(env, handle); } return value; }
//binding string void Java_com_jim_multipos_utils_database_SQLitePreparedStatement_bindString(JNIEnv *env, jobject object, int statementHandle, int index, jstring value) { sqlite3_stmt *handle = (sqlite3_stmt *) statementHandle; char const *valueStr = env->GetStringUTFChars(value, 0); int errcode = sqlite3_bind_text(handle, index, valueStr, -1, SQLITE_TRANSIENT); if (SQLITE_OK != errcode) { throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); } if (valueStr != 0) { env->ReleaseStringUTFChars(value, valueStr); } }
static void nativeRegisterLocalizedCollators(JNIEnv* env, jclass clazz, jlong connectionPtr, jstring localeStr) { SQLiteConnection* connection = reinterpret_cast<SQLiteConnection*>(connectionPtr); const char* locale = env->GetStringUTFChars(localeStr, NULL); int err = register_localized_collators(connection->db, locale, UTF16_STORAGE); env->ReleaseStringUTFChars(localeStr, locale); if (err != SQLITE_OK) { throw_sqlite3_exception(env, connection->db); } }