Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
This contains changes to the native C code.
  • Loading branch information
gauthierm committed Sep 8, 2021
1 parent 1851c68 commit dd59749
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
35 changes: 24 additions & 11 deletions native/sqlc.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,39 @@

#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__)
#else
#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);

Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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:
Expand Down
15 changes: 10 additions & 5 deletions native/sqlc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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); // ??
Expand Down

0 comments on commit dd59749

Please sign in to comment.