33#include < string>
44#include < cstring>
55
6- extern " C" {
6+ typedef struct context {
7+ JavaVM *javaVM;
8+ jobject bindingsObj;
9+ jclass bindingsClz;
10+ } Context;
11+ Context g_ctx;
712
8- typedef struct hooks {
9- JavaVM *jvm;
10- jclass update_hook_receiver_class;
11- jobject update_hook_receiver_instance;
12- } Hooks;
13+ extern " C" {
1314
1415JNIEXPORT
1516jint JNICALL JNI_OnLoad (JavaVM *vm, void *reserved) {
1617 JNIEnv *env;
18+ memset (&g_ctx, 0 , sizeof (g_ctx));
19+ g_ctx.javaVM = vm;
1720
1821 if (vm->GetEnv ((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
1922 return JNI_ERR; // JNI version not supported.
@@ -23,42 +26,66 @@ jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
2326}
2427
2528static void update_hook_callback (void *pData, int opCode, char const *pDbName, char const *pTableName, sqlite3_int64 iRow) {
29+ // Get JNIEnv for the current thread
30+ JNIEnv *env;
31+ JavaVM *javaVM = g_ctx.javaVM ;
32+ javaVM->GetEnv ((void **) &env, JNI_VERSION_1_6);
2633
34+ if (g_ctx.bindingsClz ) {
35+ jmethodID updateId = env->GetMethodID (
36+ g_ctx.bindingsClz , " onTableUpdate" , " (Ljava/lang/String;)V" );
37+
38+ jstring tableString = env->NewStringUTF (std::string (pTableName).c_str ());
39+ env->CallVoidMethod (g_ctx.bindingsObj , updateId, tableString);
40+ }
2741}
2842
2943static int commit_hook (void *pool) {
44+ // Get JNIEnv for the current thread
45+ JNIEnv *env;
46+ JavaVM *javaVM = g_ctx.javaVM ;
47+ javaVM->GetEnv ((void **) &env, JNI_VERSION_1_6);
48+
49+ if (g_ctx.bindingsClz ) {
50+ jmethodID methodId = env->GetMethodID (
51+ g_ctx.bindingsClz , " onTransactionCommit" , " (Z)V" );
52+
53+ env->CallVoidMethod (g_ctx.bindingsObj , methodId, JNI_TRUE);
54+ }
55+
3056 return 0 ;
3157}
3258
3359static void rollback_hook (void *pool) {
60+ // Get JNIEnv for the current thread
61+ JNIEnv *env;
62+ JavaVM *javaVM = g_ctx.javaVM ;
63+ javaVM->GetEnv ((void **) &env, JNI_VERSION_1_6);
64+
65+ if (g_ctx.bindingsClz ) {
66+ jmethodID methodId = env->GetMethodID (
67+ g_ctx.bindingsClz , " onTransactionCommit" , " (Z)V" );
3468
69+ env->CallVoidMethod (g_ctx.bindingsObj , methodId, JNI_FALSE);
70+ }
3571}
3672
3773JNIEXPORT
3874int powersync_init (sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi) {
3975 sqlite3_initialize ();
4076
41- return SQLITE_OK;
42- }
77+ sqlite3_update_hook (db, update_hook_callback, NULL );
78+ sqlite3_commit_hook (db, commit_hook, NULL );
79+ sqlite3_rollback_hook (db, rollback_hook, NULL );
4380
44- JNIEXPORT
45- void JNICALL Java_com_powersync_DatabaseDriverFactory_registerUpdates (JNIEnv *env, jlong dbPtr, jobject receiver) {
46- jclass clz = env->GetObjectClass (receiver);
47-
48- sqlite3 *db = (sqlite3*) dbPtr;
49- Hooks *hooks = static_cast <Hooks*>(calloc (1 , sizeof (Hooks)));
50- env->GetJavaVM (&hooks->jvm );
51- hooks->update_hook_receiver_class = (jclass) env->NewGlobalRef (clz);
52- hooks->update_hook_receiver_instance = env->NewGlobalRef (receiver);
53-
54- sqlite3_update_hook (db, update_hook_callback, hooks);
55- sqlite3_commit_hook (db, commit_hook, hooks);
56- sqlite3_rollback_hook (db, rollback_hook, hooks);
81+ return SQLITE_OK;
5782}
5883
5984JNIEXPORT
60- void JNICALL Java_com_powersync_DatabaseDriverFactory_unregisterUpdates (JNIEnv *env, jlong dbPtr, jobject receiver) {
61-
85+ void JNICALL Java_com_powersync_DatabaseDriverFactory_setupSqliteBinding (JNIEnv *env, jobject thiz) {
86+ jclass clz = env->GetObjectClass (thiz);
87+ g_ctx.bindingsClz = (jclass) env->NewGlobalRef (clz);
88+ g_ctx.bindingsObj = env->NewGlobalRef (thiz);
6289}
6390
6491}
0 commit comments