From dd597492cff90e62ee2685dd5e6a096324d58496 Mon Sep 17 00:00:00 2001 From: Michael Gauthier Date: Tue, 7 Sep 2021 21:22:11 -0300 Subject: [PATCH] Backport fix from /~https://github.com/OutSystems/Android-sqlite-native-driver/pull/1 This contains changes to the native C code. --- native/sqlc.c | 35 ++++++++++++++++++++++++----------- native/sqlc.h | 15 ++++++++++----- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/native/sqlc.c b/native/sqlc.c index 920bd4d..fb9f1d3 100644 --- a/native/sqlc.c +++ b/native/sqlc.c @@ -16,8 +16,6 @@ #include "fts5.h" -#define BASE_HANDLE_OFFSET 0x100000000LL - #ifdef SQLC_KEEP_ANDROID_LOG // ref: http://www.ibm.com/developerworks/opensource/tutorials/os-androidndk/index.html #define MYLOG(...) __android_log_print(ANDROID_LOG_VERBOSE, "sqlc", __VA_ARGS__) @@ -25,26 +23,32 @@ #define MYLOG(...) ; #endif -#define HANDLE_FROM_VP(p) ( BASE_HANDLE_OFFSET + ( (unsigned char *)(p) - (unsigned char *)NULL ) ) -#define HANDLE_TO_VP(h) (void *)( (unsigned char *)NULL + (ptrdiff_t)((h) - BASE_HANDLE_OFFSET) ) +#define HANDLE_FROM_VP(p) ( (unsigned char *)(p) - (unsigned char *)NULL ) +#define HANDLE_TO_VP(h) (void *)( (unsigned char *)NULL + (ptrdiff_t)((h)) ) int sqlc_api_version_check(int sqlc_api_version) { return (sqlc_api_version != SQLC_API_VERSION) ? SQLC_RESULT_ERROR : SQLC_RESULT_OK; } -sqlc_handle_t sqlc_api_db_open(int sqlc_api_version, const char *filename, int flags) +sqlc_handle_ct *sqlc_api_db_open(int sqlc_api_version, const char *filename, int flags) { - if (sqlc_api_version != SQLC_API_VERSION) return SQLC_RESULT_ERROR; + if (sqlc_api_version != SQLC_API_VERSION) { + sqlc_handle_ct *resp = malloc(sizeof(sqlc_handle_ct)); + resp->result = SQLC_RESULT_ERROR; + resp->handle = 0; + return resp; + } return sqlc_db_open(filename, flags); } -sqlc_handle_t sqlc_db_open(const char *filename, int flags) +sqlc_handle_ct *sqlc_db_open(const char *filename, int flags) { + sqlc_handle_ct *resp; sqlite3 *d1; int r1; - const char * err; + const char *err; MYLOG("db_open %s %d", filename, flags); @@ -63,7 +67,11 @@ sqlc_handle_t sqlc_db_open(const char *filename, int flags) sqlite3_exec(d1, "PRAGMA journal_mode=WAL;", NULL, NULL, NULL); - return HANDLE_FROM_VP(d1); + resp = malloc(sizeof(sqlc_handle_ct)); + resp->result = (r1 == 0) ? 0 : -r1; + resp->handle = HANDLE_FROM_VP(d1); + + return resp; } sqlc_handle_t sqlc_syn_context_create(sqlc_handle_t db) @@ -145,8 +153,9 @@ int sqlc_tokenizer_register_all(sqlc_handle_t db, sqlc_handle_t syn_context_h, s return r1; } -sqlc_handle_t sqlc_db_prepare_st(sqlc_handle_t db, const char *sql) +sqlc_handle_ct *sqlc_db_prepare_st(sqlc_handle_t db, const char *sql) { + sqlc_handle_ct *resp; sqlite3 *mydb = HANDLE_TO_VP(db); sqlite3_stmt *s; int rv; @@ -155,7 +164,11 @@ sqlc_handle_t sqlc_db_prepare_st(sqlc_handle_t db, const char *sql) rv = sqlite3_prepare_v2(mydb, sql, -1, &s, NULL); - return (rv == 0) ? HANDLE_FROM_VP(s) : -rv; + resp = malloc(sizeof(sqlc_handle_ct)); + resp->result = (rv == 0) ? 0 : -rv; + resp->handle = HANDLE_FROM_VP(s); + + return resp; } /** FUTURE TBD (???) for sqlcipher: diff --git a/native/sqlc.h b/native/sqlc.h index cea12ce..d2381e2 100644 --- a/native/sqlc.h +++ b/native/sqlc.h @@ -39,14 +39,19 @@ typedef long long sqlc_long_t; /* negative number indicates an error: */ typedef sqlc_long_t sqlc_handle_t; +typedef struct { + int result; + sqlc_handle_t handle; +} sqlc_handle_ct; + /* RECOMMENDED (alt 1): Use this call at startup to check Java/native library match * (returns SQLC_RESULT_OK [0] if OK, other value in case of mismatch) */ int sqlc_api_version_check(int sqlc_api_version); /* RECOMMENDED (alt 2): Check Java/native library match and open database handle */ -sqlc_handle_t sqlc_api_db_open(int sqlc_api_version, const char *filename, int flags); +sqlc_handle_ct *sqlc_api_db_open(int sqlc_api_version, const char *filename, int flags); -sqlc_handle_t sqlc_db_open(const char *filename, int flags); +sqlc_handle_ct *sqlc_db_open(const char *filename, int flags); /* Custom tokenizers */ sqlc_handle_t sqlc_syn_context_create(sqlc_handle_t db); @@ -66,14 +71,14 @@ int sqlc_db_key_native_string(sqlc_handle_t db, char *key_string); // FUTURE TBD (???) for sqlcipher: // int sqlc_db_rekey_string_native(sqlc_handle_t db, char *key_string); -sqlc_handle_t sqlc_db_prepare_st(sqlc_handle_t db, const char *sql); +sqlc_handle_ct *sqlc_db_prepare_st(sqlc_handle_t db, const char *sql); sqlc_long_t sqlc_db_last_insert_rowid(sqlc_handle_t db); int sqlc_db_total_changes(sqlc_handle_t db); int sqlc_db_errcode(sqlc_handle_t db); -const char * sqlc_db_errmsg_native(sqlc_handle_t db); -const char * sqlc_errstr_native(int errcode); +const char *sqlc_db_errmsg_native(sqlc_handle_t db); +const char *sqlc_errstr_native(int errcode); // FUTURE TBD bind blob: // int sqlc_st_bind_blob(sqlc_handle_t st, int pos, const void *val, int len); // ??