Skip to content

Commit

Permalink
Merge complex type updates from OutSystems fork
Browse files Browse the repository at this point in the history
Merge commit 'a69fa8fea29d77b781b1250554932174328ff337' of /~https://github.com/OutSystems/Android-sqlite-native-driver into main

include some updates from this PR on the OutSystems fork:

- OutSystems/Android-sqlite-native-driver#1
  • Loading branch information
Christopher J. Brody committed Mar 22, 2021
2 parents edec3e4 + a69fa8f commit b18bd72
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 30 deletions.
4 changes: 2 additions & 2 deletions java/io/liteglue/SQLiteNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public class SQLiteNative {
public static native long sqlc_db_last_insert_rowid(long db);

/** Interface to C language function: <br> <code> sqlc_handle_t sqlc_db_open(const char * filename, int flags); </code> */
public static native long sqlc_db_open(String filename, int flags);
public static native SQLiteResponse sqlc_db_open(String filename, int flags);

/** Interface to C language function: <br> <code> sqlc_handle_t sqlc_db_prepare_st(sqlc_handle_t db, const char * sql); </code> */
public static native long sqlc_db_prepare_st(long db, String sql);
public static native SQLiteResponse sqlc_db_prepare_st(long db, String sql);

/** Interface to C language function: <br> <code> int sqlc_db_total_changes(sqlc_handle_t db); </code> */
public static native int sqlc_db_total_changes(long db);
Expand Down
20 changes: 20 additions & 0 deletions java/io/liteglue/SQLiteResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.liteglue;

public class SQLiteResponse {
private int result;
private long handle;

public SQLiteResponse(int result, long handle) {
this.result = result;
this.handle = handle;
}

public int getResult() {
return this.result;
}

public long getHandle() {
return this.handle;
}

}
51 changes: 36 additions & 15 deletions native/SQLiteNative_JNI.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

/* Java->C glue code:
* Java package: io.liteglue.SQLiteNative
* Java method: long sqlc_api_db_open(int sqlc_api_version, java.lang.String filename, int flags)
* C function: sqlc_handle_t sqlc_api_db_open(int sqlc_api_version, const char * filename, int flags);
* Java method: SQLiteResponse sqlc_api_db_open(int sqlc_api_version, java.lang.String filename, int flags)
* C function: sqlc_handle_ct* sqlc_api_db_open(int sqlc_api_version, const char * filename, int flags);
*/
JNIEXPORT jlong JNICALL
JNIEXPORT jobject JNICALL
Java_io_liteglue_SQLiteNative_sqlc_1api_1db_1open__ILjava_lang_String_2I(JNIEnv *env, jclass _unused, jint sqlc_api_version, jstring filename, jint flags) {
const char* _strchars_filename = NULL;
sqlc_handle_t _res;
sqlc_handle_ct* _res;
if ( NULL != filename ) {
_strchars_filename = (*env)->GetStringUTFChars(env, filename, (jboolean*)NULL);
if ( NULL == _strchars_filename ) {
Expand All @@ -27,7 +27,14 @@ Java_io_liteglue_SQLiteNative_sqlc_1api_1db_1open__ILjava_lang_String_2I(JNIEnv
if ( NULL != filename ) {
(*env)->ReleaseStringUTFChars(env, filename, _strchars_filename);
}
return _res;

jclass class = (*env)->FindClass(env,"io/liteglue/SQLiteResponse");
jmethodID constructor = (*env)->GetMethodID(env, class, "<init>", "(IJ)V");
jobject instance = (*env)->NewObject(env, class, constructor, _res->result, _res->handle);

free(_res);

return instance;
}


Expand Down Expand Up @@ -124,13 +131,13 @@ Java_io_liteglue_SQLiteNative_sqlc_1db_1last_1insert_1rowid__J(JNIEnv *env, jcla

/* Java->C glue code:
* Java package: io.liteglue.SQLiteNative
* Java method: long sqlc_db_open(java.lang.String filename, int flags)
* C function: sqlc_handle_t sqlc_db_open(const char * filename, int flags);
* Java method: SQLiteResponse sqlc_db_open(java.lang.String filename, int flags)
* C function: sqlc_handle_ct* sqlc_db_open(const char * filename, int flags);
*/
JNIEXPORT jlong JNICALL
JNIEXPORT jobject JNICALL
Java_io_liteglue_SQLiteNative_sqlc_1db_1open__Ljava_lang_String_2I(JNIEnv *env, jclass _unused, jstring filename, jint flags) {
const char* _strchars_filename = NULL;
sqlc_handle_t _res;
sqlc_handle_ct* _res;
if ( NULL != filename ) {
_strchars_filename = (*env)->GetStringUTFChars(env, filename, (jboolean*)NULL);
if ( NULL == _strchars_filename ) {
Expand All @@ -143,19 +150,26 @@ Java_io_liteglue_SQLiteNative_sqlc_1db_1open__Ljava_lang_String_2I(JNIEnv *env,
if ( NULL != filename ) {
(*env)->ReleaseStringUTFChars(env, filename, _strchars_filename);
}
return _res;

jclass class = (*env)->FindClass(env,"io/liteglue/SQLiteResponse");
jmethodID constructor = (*env)->GetMethodID(env, class, "<init>", "(IJ)V");
jobject instance = (*env)->NewObject(env, class, constructor, _res->result, _res->handle);

free(_res);

return instance;
}


/* Java->C glue code:
* Java package: io.liteglue.SQLiteNative
* Java method: long sqlc_db_prepare_st(long db, java.lang.String sql)
* C function: sqlc_handle_t sqlc_db_prepare_st(sqlc_handle_t db, const char * sql);
* Java method: SQLiteResponse sqlc_db_prepare_st(long db, java.lang.String sql)
* C function: sqlc_handle_ct* sqlc_db_prepare_st(sqlc_handle_t db, const char * sql);
*/
JNIEXPORT jlong JNICALL
JNIEXPORT jobject JNICALL
Java_io_liteglue_SQLiteNative_sqlc_1db_1prepare_1st__JLjava_lang_String_2(JNIEnv *env, jclass _unused, jlong db, jstring sql) {
const char* _strchars_sql = NULL;
sqlc_handle_t _res;
sqlc_handle_ct* _res;
if ( NULL != sql ) {
_strchars_sql = (*env)->GetStringUTFChars(env, sql, (jboolean*)NULL);
if ( NULL == _strchars_sql ) {
Expand All @@ -168,7 +182,14 @@ Java_io_liteglue_SQLiteNative_sqlc_1db_1prepare_1st__JLjava_lang_String_2(JNIEnv
if ( NULL != sql ) {
(*env)->ReleaseStringUTFChars(env, sql, _strchars_sql);
}
return _res;

jclass class = (*env)->FindClass(env,"io/liteglue/SQLiteResponse");
jmethodID constructor = (*env)->GetMethodID(env, class, "<init>", "(IJ)V");
jobject instance = (*env)->NewObject(env, class, constructor, _res->result, _res->handle);

free(_res);

return instance;
}


Expand Down
33 changes: 23 additions & 10 deletions native/sqlc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,36 @@

#include "sqlite3.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;

Expand All @@ -43,11 +47,16 @@ sqlc_handle_t sqlc_db_open(const char *filename, int flags)

sqlite3_db_config(d1, SQLITE_DBCONFIG_DEFENSIVE, 1, NULL);

return (r1 == 0) ? HANDLE_FROM_VP(d1) : -r1;
resp = malloc (sizeof (sqlc_handle_ct));
resp->result = (r1 == 0) ? 0 : -r1;
resp->handle = HANDLE_FROM_VP(d1);

return resp;
}

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 @@ -56,7 +65,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
12 changes: 9 additions & 3 deletions native/sqlc.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ typedef long long sqlc_long_t;
/* negative number indicates an error: */
typedef sqlc_long_t sqlc_handle_t;

#pragma once
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);

// FUTURE TBD (???):
//sqlc_handle_t sqlc_db_open_vfs(const char *filename, int flags, const char *vfs);
Expand All @@ -59,7 +65,7 @@ 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);
Expand Down

0 comments on commit b18bd72

Please sign in to comment.