| void throw_sqlite3_exception(JNIEnv *env, sqlite3 *handle, int errcode) { |
| 6 | if (SQLITE_OK == errcode) { |
| 7 | errcode = sqlite3_errcode(handle); |
| 8 | } |
| 9 | const char *errmsg = sqlite3_errmsg(handle); |
| 10 | jclass exClass = env->FindClass("org/telegram/SQLite/SQLiteException"); |
| 11 | env->ThrowNew(exClass, errmsg); |
| 12 | } |
| 13 |
| 14 | extern "C" { |
| 15 |
| 16 | int Java_org_telegram_SQLite_SQLitePreparedStatement_step(JNIEnv *env, jobject object, int statementHandle) { |
| 17 | sqlite3_stmt *handle = (sqlite3_stmt *) statementHandle; |
| 18 |
| 19 | int errcode = sqlite3_step(handle); |
| 20 | if (errcode == SQLITE_ROW) { |
| 21 | return 0; |
| 22 | } else if(errcode == SQLITE_DONE) { |
| 23 | return 1; |
| 24 | } else if(errcode == SQLITE_BUSY) { |
| 25 | return -1; |
| 26 | } |
| 27 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 28 | } |
| 29 |
| 30 | int Java_org_telegram_SQLite_SQLitePreparedStatement_prepare(JNIEnv *env, jobject object, int sqliteHandle, jstring sql) { |
| 31 | sqlite3 *handle = (sqlite3 *) 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 (int) stmt_handle; |
| 47 | } |
| 48 |
| 49 | void Java_org_telegram_SQLite_SQLitePreparedStatement_reset(JNIEnv *env, jobject object, int statementHandle) { |
| 50 | sqlite3_stmt *handle = (sqlite3_stmt *) 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, int statementHandle) { |
| 59 | sqlite3_finalize((sqlite3_stmt *) statementHandle); |
| 60 | } |
| 61 |
| 62 | void Java_org_telegram_SQLite_SQLitePreparedStatement_bindByteBuffer(JNIEnv *env, jobject object, int statementHandle, int index, jobject value, int length) { |
| 63 | sqlite3_stmt *handle = (sqlite3_stmt *) 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, int statementHandle, int index, jstring value) { |
| 73 | sqlite3_stmt *handle = (sqlite3_stmt *) 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, int statementHandle, int index, int value) { |
| 88 | sqlite3_stmt *handle = (sqlite3_stmt *) 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, int statementHandle, int index, long long value) { |
| 97 | sqlite3_stmt *handle = (sqlite3_stmt *) 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, int statementHandle, int index, double value) { |
| 106 | sqlite3_stmt *handle = (sqlite3_stmt *) 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, int statementHandle, int index) { |
| 115 | sqlite3_stmt *handle = (sqlite3_stmt *) 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, int sqliteHandle) { |
| 124 | sqlite3 *handle = (sqlite3 *)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, int sqliteHandle) { |
| 132 | sqlite3 *handle = (sqlite3 *)sqliteHandle; |
| 133 | sqlite3_exec(handle, "BEGIN", 0, 0, 0); |
| 134 | } |
| 135 |
| 136 | void Java_org_telegram_SQLite_SQLiteDatabase_commitTransaction(JNIEnv *env, jobject object, int sqliteHandle) { |
| 137 | sqlite3 *handle = (sqlite3 *)sqliteHandle; |
| 138 | sqlite3_exec(handle, "COMMIT", 0, 0, 0); |
| 139 | } |
| 140 |
| 141 | int 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) { |
| 146 | sqlite3_free(sqlite3_temp_directory); |
| 147 | } |
| 148 | sqlite3_temp_directory = sqlite3_mprintf("%s", tempDirStr); |
| 149 |
| 150 | sqlite3 *handle = 0; |
| 151 | int err = sqlite3_open(fileNameStr, &handle); |
| 152 | if (SQLITE_OK != err) { |
| 153 | throw_sqlite3_exception(env, handle, err); |
| 154 | } |
| 155 | if (fileNameStr != 0) { |
| 156 | env->ReleaseStringUTFChars(fileName, fileNameStr); |
| 157 | } |
| 158 | if (tempDirStr != 0) { |
| 159 | env->ReleaseStringUTFChars(tempDir, tempDirStr); |
| 160 | } |
| 161 | return (int)handle; |
| 162 | } |
| 163 |
| 164 | int Java_org_telegram_SQLite_SQLiteCursor_columnType(JNIEnv *env, jobject object, int statementHandle, int columnIndex) { |
| 165 | sqlite3_stmt *handle = (sqlite3_stmt *)statementHandle; |
| 166 | return sqlite3_column_type(handle, columnIndex); |
| 167 | } |
| 168 |
| 169 | int Java_org_telegram_SQLite_SQLiteCursor_columnIsNull(JNIEnv *env, jobject object, int statementHandle, int columnIndex) { |
| 170 | sqlite3_stmt *handle = (sqlite3_stmt *)statementHandle; |
| 171 | int valType = sqlite3_column_type(handle, columnIndex); |
| 172 | return SQLITE_NULL == valType; |
| 173 | } |
| 174 |
| 175 | int Java_org_telegram_SQLite_SQLiteCursor_columnIntValue(JNIEnv *env, jobject object, int statementHandle, int columnIndex) { |
| 176 | sqlite3_stmt *handle = (sqlite3_stmt *)statementHandle; |
| 177 | int valType = sqlite3_column_type(handle, columnIndex); |
| 178 | if (SQLITE_NULL == valType) { |
| 179 | return 0; |
| 180 | } |
| 181 | return sqlite3_column_int(handle, columnIndex); |
| 182 | } |
| 183 |
| 184 | long long Java_org_telegram_SQLite_SQLiteCursor_columnLongValue(JNIEnv *env, jobject object, int statementHandle, int columnIndex) { |
| 185 | sqlite3_stmt *handle = (sqlite3_stmt *)statementHandle; |
| 186 | int valType = sqlite3_column_type(handle, columnIndex); |
| 187 | if (SQLITE_NULL == valType) { |
| 188 | return 0; |
| 189 | } |
| 190 | return sqlite3_column_int64(handle, columnIndex); |
| 191 | } |
| 192 |
| 193 | double Java_org_telegram_SQLite_SQLiteCursor_columnDoubleValue(JNIEnv *env, jobject object, int statementHandle, int columnIndex) { |
| 194 | sqlite3_stmt *handle = (sqlite3_stmt *)statementHandle; |
| 195 | int valType = sqlite3_column_type(handle, columnIndex); |
| 196 | if (SQLITE_NULL == valType) { |
| 197 | return 0; |
| 198 | } |
| 199 | return sqlite3_column_double(handle, columnIndex); |
| 200 | } |
| 201 |
| 202 | jstring Java_org_telegram_SQLite_SQLiteCursor_columnStringValue(JNIEnv *env, jobject object, int statementHandle, int columnIndex) { |
| 203 | sqlite3_stmt *handle = (sqlite3_stmt *)statementHandle; |
| 204 | const char *str = (const char *) sqlite3_column_text(handle, columnIndex); |
| 205 | if (str != 0) { |
| 206 | return env->NewStringUTF(str); |
| 207 | } |
| 208 | return 0; |
| 209 | } |
| 210 |
| 211 | jbyteArray Java_org_telegram_SQLite_SQLiteCursor_columnByteArrayValue(JNIEnv *env, jobject object, int statementHandle, int columnIndex) { |
| 212 | sqlite3_stmt *handle = (sqlite3_stmt *)statementHandle; |
| 213 | const jbyte *buf = (const jbyte *) sqlite3_column_blob(handle, columnIndex); |
| 214 | int length = sqlite3_column_bytes(handle, columnIndex); |
| 215 | if (buf != nullptr && length > 0) { |
| 216 | jbyteArray result = env->NewByteArray(length); |
| 217 | env->SetByteArrayRegion(result, 0, length, buf); |
| 218 | return result; |
| 219 | } |
| 220 | return nullptr; |
| 221 | } |
| 222 |
| 223 | int Java_org_telegram_SQLite_SQLiteCursor_columnByteBufferValue(JNIEnv *env, jobject object, int statementHandle, int columnIndex) { |
| 224 | sqlite3_stmt *handle = (sqlite3_stmt *) statementHandle; |
| 225 | int length = sqlite3_column_bytes(handle, columnIndex); |
| 226 | if (length <= 0) { |
| 227 | return 0; |
| 228 | } |
| 229 | NativeByteBuffer *buffer = BuffersStorage::getInstance().getFreeBuffer(length); |
| 230 | if (buffer == nullptr) { |
| 231 | return 0; |
| 232 | } |
| 233 | const char *buf = (const char *) sqlite3_column_blob(handle, columnIndex); |
| 234 | if (buf == nullptr) { |
| 235 | return 0; |
| 236 | } |
| 237 | memcpy(buffer->bytes(), buf, length); |
| 238 | return (int) buffer; |
| 239 | } |
| 240 |
| 241 | } |
| 242 |