Telegram-Android/TMessagesProj/jni/SqliteWrapper.cpp

250 lines
9.7 KiB
C++
Raw Normal View History

2020-09-30 15:48:47 +02:00
#include <cstring>
#include <jni.h>
2016-05-25 23:49:47 +02:00
#include "sqlite/sqlite3.h"
#include "tgnet/NativeByteBuffer.h"
#include "tgnet/BuffersStorage.h"
void throw_sqlite3_exception(JNIEnv *env, sqlite3 *handle, int errcode) {
const char *errmsg = sqlite3_errmsg(handle);
jclass exClass = env->FindClass("org/telegram/SQLite/SQLiteException");
env->ThrowNew(exClass, errmsg);
}
extern "C" {
2020-09-30 15:48:47 +02:00
JNIEXPORT jint Java_org_telegram_SQLite_SQLitePreparedStatement_step(JNIEnv *env, jobject object, jlong statementHandle) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
2016-05-25 23:49:47 +02:00
int errcode = sqlite3_step(handle);
2018-07-30 04:07:02 +02:00
if (errcode == SQLITE_ROW) {
2016-05-25 23:49:47 +02:00
return 0;
2018-07-30 04:07:02 +02:00
} else if (errcode == SQLITE_DONE) {
2016-05-25 23:49:47 +02:00
return 1;
2018-07-30 04:07:02 +02:00
} else if (errcode == SQLITE_BUSY) {
2016-05-25 23:49:47 +02:00
return -1;
}
throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode);
2018-07-30 04:07:02 +02:00
return 0;
2016-05-25 23:49:47 +02:00
}
2020-09-30 15:48:47 +02:00
JNIEXPORT jlong Java_org_telegram_SQLite_SQLitePreparedStatement_prepare(JNIEnv *env, jobject object, jlong sqliteHandle, jstring sql) {
2018-07-30 04:07:02 +02:00
sqlite3 *handle = (sqlite3 *) (intptr_t) sqliteHandle;
2016-05-25 23:49:47 +02:00
char const *sqlStr = env->GetStringUTFChars(sql, 0);
2018-07-30 04:07:02 +02:00
2016-05-25 23:49:47 +02:00
sqlite3_stmt *stmt_handle;
2018-07-30 04:07:02 +02:00
2016-05-25 23:49:47 +02:00
int errcode = sqlite3_prepare_v2(handle, sqlStr, -1, &stmt_handle, 0);
if (SQLITE_OK != errcode) {
throw_sqlite3_exception(env, handle, errcode);
}
2018-07-30 04:07:02 +02:00
2016-05-25 23:49:47 +02:00
if (sqlStr != 0) {
env->ReleaseStringUTFChars(sql, sqlStr);
}
2018-07-30 04:07:02 +02:00
return (jlong) stmt_handle;
2016-05-25 23:49:47 +02:00
}
2020-09-30 15:48:47 +02:00
JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_reset(JNIEnv *env, jobject object, jlong statementHandle) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
2016-05-25 23:49:47 +02:00
int errcode = sqlite3_reset(handle);
if (SQLITE_OK != errcode) {
throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode);
}
}
2020-09-30 15:48:47 +02:00
JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_finalize(JNIEnv *env, jobject object, jlong statementHandle) {
2018-07-30 04:07:02 +02:00
sqlite3_finalize((sqlite3_stmt *) (intptr_t) statementHandle);
2016-05-25 23:49:47 +02:00
}
2020-09-30 15:48:47 +02:00
JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_bindByteBuffer(JNIEnv *env, jobject object, jlong statementHandle, jint index, jobject value, jint length) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
2016-05-25 23:49:47 +02:00
void *buf = env->GetDirectBufferAddress(value);
2018-07-30 04:07:02 +02:00
2016-05-25 23:49:47 +02:00
int errcode = sqlite3_bind_blob(handle, index, buf, length, SQLITE_STATIC);
if (SQLITE_OK != errcode) {
throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode);
}
}
2020-09-30 15:48:47 +02:00
JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_bindString(JNIEnv *env, jobject object, jlong statementHandle, jint index, jstring value) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
2016-05-25 23:49:47 +02:00
char const *valueStr = env->GetStringUTFChars(value, 0);
2018-07-30 04:07:02 +02:00
2016-05-25 23:49:47 +02:00
int errcode = sqlite3_bind_text(handle, index, valueStr, -1, SQLITE_TRANSIENT);
if (SQLITE_OK != errcode) {
throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode);
}
2018-07-30 04:07:02 +02:00
2016-05-25 23:49:47 +02:00
if (valueStr != 0) {
env->ReleaseStringUTFChars(value, valueStr);
}
}
2020-09-30 15:48:47 +02:00
JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_bindInt(JNIEnv *env, jobject object, jlong statementHandle, jint index, jint value) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
2016-05-25 23:49:47 +02:00
int errcode = sqlite3_bind_int(handle, index, value);
if (SQLITE_OK != errcode) {
throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode);
}
}
2020-09-30 15:48:47 +02:00
JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_bindLong(JNIEnv *env, jobject object, jlong statementHandle, jint index, jlong value) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
2016-05-25 23:49:47 +02:00
int errcode = sqlite3_bind_int64(handle, index, value);
if (SQLITE_OK != errcode) {
throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode);
}
}
2020-09-30 15:48:47 +02:00
JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_bindDouble(JNIEnv *env, jobject object, jlong statementHandle, jint index, double value) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
2016-05-25 23:49:47 +02:00
int errcode = sqlite3_bind_double(handle, index, value);
if (SQLITE_OK != errcode) {
throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode);
}
}
2020-09-30 15:48:47 +02:00
JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_bindNull(JNIEnv *env, jobject object, jlong statementHandle, jint index) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
2016-05-25 23:49:47 +02:00
int errcode = sqlite3_bind_null(handle, index);
if (SQLITE_OK != errcode) {
throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode);
}
}
2020-09-30 15:48:47 +02:00
JNIEXPORT void Java_org_telegram_SQLite_SQLiteDatabase_closedb(JNIEnv *env, jobject object, jlong sqliteHandle) {
2018-07-30 04:07:02 +02:00
sqlite3 *handle = (sqlite3 *) (intptr_t) sqliteHandle;
2016-05-25 23:49:47 +02:00
int err = sqlite3_close(handle);
if (SQLITE_OK != err) {
throw_sqlite3_exception(env, handle, err);
}
}
2020-09-30 15:48:47 +02:00
JNIEXPORT void Java_org_telegram_SQLite_SQLiteDatabase_beginTransaction(JNIEnv *env, jobject object, jlong sqliteHandle) {
2018-07-30 04:07:02 +02:00
sqlite3 *handle = (sqlite3 *) (intptr_t) sqliteHandle;
2016-05-25 23:49:47 +02:00
sqlite3_exec(handle, "BEGIN", 0, 0, 0);
}
2020-09-30 15:48:47 +02:00
JNIEXPORT void Java_org_telegram_SQLite_SQLiteDatabase_commitTransaction(JNIEnv *env, jobject object, jlong sqliteHandle) {
2018-07-30 04:07:02 +02:00
sqlite3 *handle = (sqlite3 *) (intptr_t) sqliteHandle;
2016-05-25 23:49:47 +02:00
sqlite3_exec(handle, "COMMIT", 0, 0, 0);
}
2020-09-30 15:48:47 +02:00
JNIEXPORT jlong Java_org_telegram_SQLite_SQLiteDatabase_opendb(JNIEnv *env, jobject object, jstring fileName, jstring tempDir) {
2016-05-25 23:49:47 +02:00
char const *fileNameStr = env->GetStringUTFChars(fileName, 0);
char const *tempDirStr = env->GetStringUTFChars(tempDir, 0);
2018-07-30 04:07:02 +02:00
2018-08-27 10:33:11 +02:00
if (sqlite3_temp_directory != 0 && strcmp(sqlite3_temp_directory, tempDirStr)) {
2016-05-25 23:49:47 +02:00
sqlite3_free(sqlite3_temp_directory);
}
2018-08-27 10:33:11 +02:00
if (sqlite3_temp_directory == 0) {
sqlite3_temp_directory = sqlite3_mprintf("%s", tempDirStr);
}
2018-07-30 04:07:02 +02:00
2016-05-25 23:49:47 +02:00
sqlite3 *handle = 0;
int err = sqlite3_open(fileNameStr, &handle);
if (SQLITE_OK != err) {
throw_sqlite3_exception(env, handle, err);
}
if (fileNameStr != 0) {
env->ReleaseStringUTFChars(fileName, fileNameStr);
}
if (tempDirStr != 0) {
env->ReleaseStringUTFChars(tempDir, tempDirStr);
}
2018-07-30 04:07:02 +02:00
return (jlong) handle;
2016-05-25 23:49:47 +02:00
}
2020-09-30 15:48:47 +02:00
JNIEXPORT jint Java_org_telegram_SQLite_SQLiteCursor_columnCount(JNIEnv *env, jobject object, jlong statementHandle) {
2019-05-14 14:08:05 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
return sqlite3_column_count(handle);
}
2020-09-30 15:48:47 +02:00
JNIEXPORT jint Java_org_telegram_SQLite_SQLiteCursor_columnType(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
return sqlite3_column_type(handle, columnIndex);
2016-05-25 23:49:47 +02:00
}
2020-09-30 15:48:47 +02:00
JNIEXPORT jint Java_org_telegram_SQLite_SQLiteCursor_columnIsNull(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
int valType = sqlite3_column_type(handle, columnIndex);
2019-05-14 14:08:05 +02:00
return SQLITE_NULL == valType ? 1 : 0;
2016-05-25 23:49:47 +02:00
}
2020-09-30 15:48:47 +02:00
JNIEXPORT jint Java_org_telegram_SQLite_SQLiteCursor_columnIntValue(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
int valType = sqlite3_column_type(handle, columnIndex);
if (SQLITE_NULL == valType) {
return 0;
}
return sqlite3_column_int(handle, columnIndex);
2016-05-25 23:49:47 +02:00
}
2020-09-30 15:48:47 +02:00
JNIEXPORT jlong Java_org_telegram_SQLite_SQLiteCursor_columnLongValue(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
int valType = sqlite3_column_type(handle, columnIndex);
if (SQLITE_NULL == valType) {
return 0;
}
return sqlite3_column_int64(handle, columnIndex);
2016-05-25 23:49:47 +02:00
}
2020-09-30 15:48:47 +02:00
JNIEXPORT jdouble Java_org_telegram_SQLite_SQLiteCursor_columnDoubleValue(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
int valType = sqlite3_column_type(handle, columnIndex);
if (SQLITE_NULL == valType) {
return 0;
}
return sqlite3_column_double(handle, columnIndex);
2016-05-25 23:49:47 +02:00
}
2020-09-30 15:48:47 +02:00
JNIEXPORT jstring Java_org_telegram_SQLite_SQLiteCursor_columnStringValue(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
const char *str = (const char *) sqlite3_column_text(handle, columnIndex);
if (str != 0) {
return env->NewStringUTF(str);
}
return 0;
2016-05-25 23:49:47 +02:00
}
2020-09-30 15:48:47 +02:00
JNIEXPORT jbyteArray Java_org_telegram_SQLite_SQLiteCursor_columnByteArrayValue(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
const jbyte *buf = (const jbyte *) sqlite3_column_blob(handle, columnIndex);
int length = sqlite3_column_bytes(handle, columnIndex);
if (buf != nullptr && length > 0) {
jbyteArray result = env->NewByteArray(length);
2016-05-25 23:49:47 +02:00
env->SetByteArrayRegion(result, 0, length, buf);
return result;
2018-07-30 04:07:02 +02:00
}
return nullptr;
2016-05-25 23:49:47 +02:00
}
2020-09-30 15:48:47 +02:00
JNIEXPORT jlong Java_org_telegram_SQLite_SQLiteCursor_columnByteBufferValue(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) {
2018-07-30 04:07:02 +02:00
sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle;
uint32_t length = (uint32_t) sqlite3_column_bytes(handle, columnIndex);
2016-05-25 23:49:47 +02:00
if (length <= 0) {
return 0;
}
NativeByteBuffer *buffer = BuffersStorage::getInstance().getFreeBuffer(length);
if (buffer == nullptr) {
return 0;
}
const char *buf = (const char *) sqlite3_column_blob(handle, columnIndex);
if (buf == nullptr) {
return 0;
}
memcpy(buffer->bytes(), buf, length);
2018-07-30 04:07:02 +02:00
return (jlong) buffer;
2016-05-25 23:49:47 +02:00
}
}