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