Skip to content

Commit 548dec6

Browse files
committed
Revert unintentional changes
1 parent 9aab987 commit 548dec6

File tree

4 files changed

+52
-35
lines changed

4 files changed

+52
-35
lines changed

core/src/androidMain/kotlin/com/powersync/UpdateHookReceiver.kt

Lines changed: 0 additions & 10 deletions
This file was deleted.

core/src/jvmNative/cpp/sqlite_bindings.cpp

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
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

1415
JNIEXPORT
1516
jint 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

2528
static 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

2943
static 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

3359
static 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

3773
JNIEXPORT
3874
int 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

5984
JNIEXPORT
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
}

core/src/jvmTest/kotlin/com/powersync/testutils/TestUtils.jvm.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.powersync.DatabaseDriverFactory
44
import java.io.File
55

66
actual val factory: DatabaseDriverFactory
7-
get() = DatabaseDriverFactory()
7+
get() = DatabaseDriverFactory()
88

99
actual fun cleanup(path: String) {
1010
File(path).delete()

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ compose.kotlin.native.manageCacheKind=false
1414
# Development
1515
development=true
1616
# Release
17-
RELEASE_SIGNING_ENABLED=false
17+
RELEASE_SIGNING_ENABLED=true
1818
# Library config
1919
GROUP=com.powersync
2020
LIBRARY_VERSION=1.0.0-BETA25

0 commit comments

Comments
 (0)