| 6 | JNIEnv* env = 0; |
| 7 |
| 8 | if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) { |
| 9 | return -1; |
| 10 | } |
| 11 |
| 12 | jclass class = (*env)->FindClass(env, "org/telegram/SQLite/SQLitePreparedStatement"); |
| 13 | queryArgsCountField = (*env)->GetFieldID(env, class, "queryArgsCount", "I"); |
| 14 |
| 15 | return JNI_VERSION_1_4; |
| 16 | } |
| 17 |
| 18 | int Java_org_telegram_SQLite_SQLitePreparedStatement_step(JNIEnv* env, jobject object, int statementHandle) { |
| 19 | sqlite3_stmt *handle = (sqlite3_stmt *)statementHandle; |
| 20 |
| 21 | int errcode = sqlite3_step(handle); |
| 22 | if (errcode == SQLITE_ROW) { |
| 23 | return 0; |
| 24 | } else if(errcode == SQLITE_DONE) { |
| 25 | return 1; |
| 26 | } else if(errcode == SQLITE_BUSY) { |
| 27 | return -1; |
| 28 | } |
| 29 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 30 | } |
| 31 |
| 32 | int Java_org_telegram_SQLite_SQLitePreparedStatement_prepare(JNIEnv *env, jobject object, int sqliteHandle, jstring sql) { |
| 33 | sqlite3* handle = (sqlite3 *)sqliteHandle; |
| 34 |
| 35 | char const *sqlStr = (*env)->GetStringUTFChars(env, sql, 0); |
| 36 |
| 37 | sqlite3_stmt *stmt_handle; |
| 38 |
| 39 | int errcode = sqlite3_prepare_v2(handle, sqlStr, -1, &stmt_handle, 0); |
| 40 | if (SQLITE_OK != errcode) { |
| 41 | throw_sqlite3_exception(env, handle, errcode); |
| 42 | } else { |
| 43 | int argsCount = sqlite3_bind_parameter_count(stmt_handle); |
| 44 | (*env)->SetIntField(env, object, queryArgsCountField, argsCount); |
| 45 | } |
| 46 |
| 47 | if (sqlStr != 0) { |
| 48 | (*env)->ReleaseStringUTFChars(env, sql, sqlStr); |
| 49 | } |
| 50 |
| 51 | return (int)stmt_handle; |
| 52 | } |
| 53 |
| 54 | void Java_org_telegram_SQLite_SQLitePreparedStatement_reset(JNIEnv *env, jobject object, int statementHandle) { |
| 55 | sqlite3_stmt *handle = (sqlite3_stmt *)statementHandle; |
| 56 |
| 57 | int errcode = sqlite3_reset(handle); |
| 58 | if (SQLITE_OK != errcode) { |
| 59 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 60 | } |
| 61 | } |
| 62 |
| 63 | void Java_org_telegram_SQLite_SQLitePreparedStatement_finalize(JNIEnv *env, jobject object, int statementHandle) { |
| 64 | sqlite3_stmt *handle = (sqlite3_stmt *)statementHandle; |
| 65 |
| 66 | int errcode = sqlite3_finalize (handle); |
| 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_bindByteArray(JNIEnv *env, jobject object, int statementHandle, int index, jbyteArray value) { |
| 73 | sqlite3_stmt *handle = (sqlite3_stmt *)statementHandle; |
| 74 |
| 75 | const void *buf = (*env)->GetByteArrayElements(env, value, 0); |
| 76 | int length = (*env)->GetArrayLength(env, value); |
| 77 |
| 78 | int errcode = sqlite3_bind_blob(handle, index, buf, length, SQLITE_STATIC); |
| 79 | if (SQLITE_OK != errcode) { |
| 80 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 81 | } |
| 82 |
| 83 | if (buf != 0) { |
| 84 | (*env)->ReleaseByteArrayElements(env, value, buf, 0); |
| 85 | } |
| 86 | } |
| 87 |
| 88 | void Java_org_telegram_SQLite_SQLitePreparedStatement_bindString(JNIEnv *env, jobject object, int statementHandle, int index, jstring value) { |
| 89 | sqlite3_stmt *handle = (sqlite3_stmt*)statementHandle; |
| 90 |
| 91 | char const *valueStr = (*env)->GetStringUTFChars(env, value, 0); |
| 92 |
| 93 | int errcode = sqlite3_bind_text(handle, index, valueStr, -1, SQLITE_TRANSIENT); |
| 94 | if (SQLITE_OK != errcode) { |
| 95 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 96 | } |
| 97 |
| 98 | if (valueStr != 0) { |
| 99 | (*env)->ReleaseStringUTFChars(env, value, valueStr); |
| 100 | } |
| 101 | } |
| 102 |
| 103 | void Java_org_telegram_SQLite_SQLitePreparedStatement_bindInt(JNIEnv *env, jobject object, int statementHandle, int index, int value) { |
| 104 | sqlite3_stmt *handle = (sqlite3_stmt*)statementHandle; |
| 105 |
| 106 | int errcode = sqlite3_bind_int(handle, index, value); |
| 107 | if (SQLITE_OK != errcode) { |
| 108 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 109 | } |
| 110 | } |
| 111 |
| 112 | void Java_org_telegram_SQLite_SQLitePreparedStatement_bindLong(JNIEnv *env, jobject object, int statementHandle, int index, long long value) { |
| 113 | sqlite3_stmt *handle = (sqlite3_stmt*)statementHandle; |
| 114 |
| 115 | int errcode = sqlite3_bind_int64(handle, index, value); |
| 116 | if (SQLITE_OK != errcode) { |
| 117 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 118 | } |
| 119 | } |
| 120 |
| 121 | void Java_org_telegram_SQLite_SQLitePreparedStatement_bindDouble(JNIEnv* env, jobject object, int statementHandle, int index, double value) { |
| 122 | sqlite3_stmt *handle = (sqlite3_stmt*)statementHandle; |
| 123 |
| 124 | int errcode = sqlite3_bind_double(handle, index, value); |
| 125 | if (SQLITE_OK != errcode) { |
| 126 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 127 | } |
| 128 | } |
| 129 |
| 130 | void Java_org_telegram_SQLite_SQLitePreparedStatement_bindNull(JNIEnv* env, jobject object, int statementHandle, int index) { |
| 131 | sqlite3_stmt *handle = (sqlite3_stmt*)statementHandle; |
| 132 |
| 133 | int errcode = sqlite3_bind_null(handle, index); |
| 134 | if (SQLITE_OK != errcode) { |
| 135 | throw_sqlite3_exception(env, sqlite3_db_handle(handle), errcode); |
| 136 | } |
| 137 | } |
| 138 |
| 139 |