| 5 | #include "tgnet/BuffersStorage.h" |
| 6 |
| 7 | void throw_sqlite3_exception(JNIEnv *env, sqlite3 *handle, int errcode) { |
| 8 | const char *errmsg = sqlite3_errmsg(handle); |
| 9 | jclass exClass = env->FindClass("org/telegram/SQLite/SQLiteException"); |
| 10 | env->ThrowNew(exClass, errmsg); |
| 11 | } |
| 12 |
| 13 | extern "C" { |
| 14 |
| 15 | JNIEXPORT jint Java_org_telegram_SQLite_SQLitePreparedStatement_step(JNIEnv *env, jobject object, jlong statementHandle) { |
| 16 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 17 |
| 18 | int errcode = sqlite3_step(handle); |
| 19 | if (errcode == SQLITE_ROW) { |
| 20 | return 0; |
| 21 | } else if (errcode == SQLITE_DONE) { |
| 22 | return 1; |
| 23 | } else if (errcode == SQLITE_BUSY) { |
| 24 | return -1; |
| 25 | } |
| 26 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 27 |
| 28 | return 0; |
| 29 | } |
| 30 |
| 31 | JNIEXPORT jlong Java_org_telegram_SQLite_SQLitePreparedStatement_prepare(JNIEnv *env, jobject object, jlong sqliteHandle, jstring sql) { |
| 32 | sqlite3 *handle = (sqlite3 *) (intptr_t) sqliteHandle; |
| 33 |
| 34 | char const *sqlStr = env->GetStringUTFChars(sql, 0); |
| 35 |
| 36 | sqlite3_stmt *stmt_handle; |
| 37 |
| 38 | int errcode = sqlite3_prepare_v2(handle, sqlStr, -1, &stmt_handle, 0); |
| 39 | if (SQLITE_OK != errcode) { |
| 40 | throw_sqlite3_exception(env, handle, errcode); |
| 41 | } |
| 42 |
| 43 | if (sqlStr != 0) { |
| 44 | env->ReleaseStringUTFChars(sql, sqlStr); |
| 45 | } |
| 46 |
| 47 | return (jlong) stmt_handle; |
| 48 | } |
| 49 |
| 50 | JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_reset(JNIEnv *env, jobject object, jlong statementHandle) { |
| 51 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 52 |
| 53 | int errcode = sqlite3_reset(handle); |
| 54 | if (SQLITE_OK != errcode) { |
| 55 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 56 | } |
| 57 | } |
| 58 |
| 59 | JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_finalize(JNIEnv *env, jobject object, jlong statementHandle) { |
| 60 | sqlite3_finalize((sqlite3_stmt *) (intptr_t) statementHandle); |
| 61 | } |
| 62 |
| 63 | JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_bindByteBuffer(JNIEnv *env, jobject object, jlong statementHandle, jint index, jobject value, jint length) { |
| 64 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 65 | void *buf = env->GetDirectBufferAddress(value); |
| 66 |
| 67 | int errcode = sqlite3_bind_blob(handle, index, buf, length, SQLITE_STATIC); |
| 68 | if (SQLITE_OK != errcode) { |
| 69 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 70 | } |
| 71 | } |
| 72 |
| 73 | JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_bindString(JNIEnv *env, jobject object, jlong statementHandle, jint index, jstring value) { |
| 74 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 75 |
| 76 | char const *valueStr = env->GetStringUTFChars(value, 0); |
| 77 |
| 78 | int errcode = sqlite3_bind_text(handle, index, valueStr, -1, SQLITE_TRANSIENT); |
| 79 | if (SQLITE_OK != errcode) { |
| 80 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 81 | } |
| 82 |
| 83 | if (valueStr != 0) { |
| 84 | env->ReleaseStringUTFChars(value, valueStr); |
| 85 | } |
| 86 | } |
| 87 |
| 88 | JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_bindInt(JNIEnv *env, jobject object, jlong statementHandle, jint index, jint value) { |
| 89 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 90 |
| 91 | int errcode = sqlite3_bind_int(handle, index, value); |
| 92 | if (SQLITE_OK != errcode) { |
| 93 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 94 | } |
| 95 | } |
| 96 |
| 97 | JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_bindLong(JNIEnv *env, jobject object, jlong statementHandle, jint index, jlong value) { |
| 98 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 99 |
| 100 | int errcode = sqlite3_bind_int64(handle, index, value); |
| 101 | if (SQLITE_OK != errcode) { |
| 102 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 103 | } |
| 104 | } |
| 105 |
| 106 | JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_bindDouble(JNIEnv *env, jobject object, jlong statementHandle, jint index, double value) { |
| 107 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 108 |
| 109 | int errcode = sqlite3_bind_double(handle, index, value); |
| 110 | if (SQLITE_OK != errcode) { |
| 111 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 112 | } |
| 113 | } |
| 114 |
| 115 | JNIEXPORT void Java_org_telegram_SQLite_SQLitePreparedStatement_bindNull(JNIEnv *env, jobject object, jlong statementHandle, jint index) { |
| 116 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 117 |
| 118 | int errcode = sqlite3_bind_null(handle, index); |
| 119 | if (SQLITE_OK != errcode) { |
| 120 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 121 | } |
| 122 | } |
| 123 |
| 124 | JNIEXPORT void Java_org_telegram_SQLite_SQLiteDatabase_closedb(JNIEnv *env, jobject object, jlong sqliteHandle) { |
| 125 | sqlite3 *handle = (sqlite3 *) (intptr_t) sqliteHandle; |
| 126 | int err = sqlite3_close(handle); |
| 127 | if (SQLITE_OK != err) { |
| 128 | throw_sqlite3_exception(env, handle, err); |
| 129 | } |
| 130 | } |
| 131 |
| 132 | JNIEXPORT void Java_org_telegram_SQLite_SQLiteDatabase_beginTransaction(JNIEnv *env, jobject object, jlong sqliteHandle) { |
| 133 | sqlite3 *handle = (sqlite3 *) (intptr_t) sqliteHandle; |
| 134 | sqlite3_exec(handle, "BEGIN", 0, 0, 0); |
| 135 | } |
| 136 |
| 137 | JNIEXPORT void Java_org_telegram_SQLite_SQLiteDatabase_commitTransaction(JNIEnv *env, jobject object, jlong sqliteHandle) { |
| 138 | sqlite3 *handle = (sqlite3 *) (intptr_t) sqliteHandle; |
| 139 | sqlite3_exec(handle, "COMMIT", 0, 0, 0); |
| 140 | } |
| 141 |
| 142 | JNIEXPORT jlong Java_org_telegram_SQLite_SQLiteDatabase_opendb(JNIEnv *env, jobject object, jstring fileName, jstring tempDir) { |
| 143 | char const *fileNameStr = env->GetStringUTFChars(fileName, 0); |
| 144 | char const *tempDirStr = env->GetStringUTFChars(tempDir, 0); |
| 145 |
| 146 | if (sqlite3_temp_directory != 0 && strcmp(sqlite3_temp_directory, tempDirStr)) { |
| 147 | sqlite3_free(sqlite3_temp_directory); |
| 148 | } |
| 149 | if (sqlite3_temp_directory == 0) { |
| 150 | sqlite3_temp_directory = sqlite3_mprintf("%s", tempDirStr); |
| 151 | } |
| 152 |
| 153 | sqlite3 *handle = 0; |
| 154 | int err = sqlite3_open(fileNameStr, &handle); |
| 155 | if (SQLITE_OK != err) { |
| 156 | throw_sqlite3_exception(env, handle, err); |
| 157 | } |
| 158 | if (fileNameStr != 0) { |
| 159 | env->ReleaseStringUTFChars(fileName, fileNameStr); |
| 160 | } |
| 161 | if (tempDirStr != 0) { |
| 162 | env->ReleaseStringUTFChars(tempDir, tempDirStr); |
| 163 | } |
| 164 | return (jlong) handle; |
| 165 | } |
| 166 |
| 167 | JNIEXPORT jint Java_org_telegram_SQLite_SQLiteCursor_columnCount(JNIEnv *env, jobject object, jlong statementHandle) { |
| 168 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 169 | return sqlite3_column_count(handle); |
| 170 | } |
| 171 |
| 172 | JNIEXPORT jint Java_org_telegram_SQLite_SQLiteCursor_columnType(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) { |
| 173 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 174 | return sqlite3_column_type(handle, columnIndex); |
| 175 | } |
| 176 |
| 177 | JNIEXPORT jint Java_org_telegram_SQLite_SQLiteCursor_columnIsNull(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) { |
| 178 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 179 | int valType = sqlite3_column_type(handle, columnIndex); |
| 180 | return SQLITE_NULL == valType ? 1 : 0; |
| 181 | } |
| 182 |
| 183 | JNIEXPORT jint Java_org_telegram_SQLite_SQLiteCursor_columnIntValue(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) { |
| 184 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 185 | int valType = sqlite3_column_type(handle, columnIndex); |
| 186 | if (SQLITE_NULL == valType) { |
| 187 | return 0; |
| 188 | } |
| 189 | return sqlite3_column_int(handle, columnIndex); |
| 190 | } |
| 191 |
| 192 | JNIEXPORT jlong Java_org_telegram_SQLite_SQLiteCursor_columnLongValue(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) { |
| 193 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 194 | int valType = sqlite3_column_type(handle, columnIndex); |
| 195 | if (SQLITE_NULL == valType) { |
| 196 | return 0; |
| 197 | } |
| 198 | return sqlite3_column_int64(handle, columnIndex); |
| 199 | } |
| 200 |
| 201 | JNIEXPORT jdouble Java_org_telegram_SQLite_SQLiteCursor_columnDoubleValue(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) { |
| 202 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 203 | int valType = sqlite3_column_type(handle, columnIndex); |
| 204 | if (SQLITE_NULL == valType) { |
| 205 | return 0; |
| 206 | } |
| 207 | return sqlite3_column_double(handle, columnIndex); |
| 208 | } |
| 209 |
| 210 | JNIEXPORT jstring Java_org_telegram_SQLite_SQLiteCursor_columnStringValue(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) { |
| 211 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 212 | const char *str = (const char *) sqlite3_column_text(handle, columnIndex); |
| 213 | if (str != 0) { |
| 214 | return env->NewStringUTF(str); |
| 215 | } |
| 216 | return 0; |
| 217 | } |
| 218 |
| 219 | JNIEXPORT jbyteArray Java_org_telegram_SQLite_SQLiteCursor_columnByteArrayValue(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) { |
| 220 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 221 | const jbyte *buf = (const jbyte *) sqlite3_column_blob(handle, columnIndex); |
| 222 | int length = sqlite3_column_bytes(handle, columnIndex); |
| 223 | if (buf != nullptr && length > 0) { |
| 224 | jbyteArray result = env->NewByteArray(length); |
| 225 | env->SetByteArrayRegion(result, 0, length, buf); |
| 226 | return result; |
| 227 | } |
| 228 | return nullptr; |
| 229 | } |
| 230 |
| 231 | JNIEXPORT jlong Java_org_telegram_SQLite_SQLiteCursor_columnByteBufferValue(JNIEnv *env, jobject object, jlong statementHandle, jint columnIndex) { |
| 232 | sqlite3_stmt *handle = (sqlite3_stmt *) (intptr_t) statementHandle; |
| 233 | uint32_t length = (uint32_t) sqlite3_column_bytes(handle, columnIndex); |
| 234 | if (length <= 0) { |
| 235 | return 0; |
| 236 | } |
| 237 | NativeByteBuffer *buffer = BuffersStorage::getInstance().getFreeBuffer(length); |
| 238 | if (buffer == nullptr) { |
| 239 | return 0; |
| 240 | } |
| 241 | const char *buf = (const char *) sqlite3_column_blob(handle, columnIndex); |
| 242 | if (buf == nullptr) { |
| 243 | return 0; |
| 244 | } |
| 245 | memcpy(buffer->bytes(), buf, length); |
| 246 | return (jlong) buffer; |
| 247 | } |
| 248 |
| 249 | } |
| 250 |