@@ -1328,7 +1328,6 @@ JNIEXPORT void JNICALL Java_org_sqlite_core_NativeDB_free_1functions(
13281328 }
13291329}
13301330
1331-
13321331// COMPOUND FUNCTIONS ///////////////////////////////////////////////
13331332
13341333JNIEXPORT jobjectArray JNICALL Java_org_sqlite_core_NativeDB_column_1metadata (
@@ -1607,3 +1606,76 @@ JNIEXPORT void JNICALL Java_org_sqlite_core_NativeDB_clear_1progress_1handler(
16071606 sqlite3_progress_handler (gethandle (env , this ), 0 , NULL , NULL );
16081607 (* env )-> DeleteGlobalRef (env , progress_handler_context .phandler );
16091608}
1609+
1610+ // Update hook
1611+
1612+ struct UpdateHandlerContext {
1613+ JavaVM * vm ;
1614+ jmethodID method ;
1615+ jobject handler ;
1616+ };
1617+
1618+ static struct UpdateHandlerContext update_handler_context ;
1619+
1620+
1621+ void update_hook (void * context , int type , char const * database , char const * table , sqlite3_int64 row ) {
1622+ JNIEnv * env = 0 ;
1623+ (* update_handler_context .vm )-> AttachCurrentThread (update_handler_context .vm , (void * * )& env , 0 );
1624+
1625+ jstring databaseString = (* env )-> NewStringUTF (env , database );
1626+ jstring tableString = (* env )-> NewStringUTF (env , table );
1627+
1628+ (* env )-> CallVoidMethod (env , update_handler_context .handler , update_handler_context .method , type , databaseString , tableString , row );
1629+
1630+ (* env )-> DeleteLocalRef (env , databaseString );
1631+ (* env )-> DeleteLocalRef (env , tableString );
1632+ }
1633+
1634+ JNIEXPORT void JNICALL Java_org_sqlite_core_NativeDB_set_1update_1listener (JNIEnv * env , jobject this , jboolean enabled ) {
1635+ if (enabled ) {
1636+ update_handler_context .method = (* env )-> GetMethodID (env , dbclass , "onUpdate" , "(ILjava/lang/String;Ljava/lang/String;J)V" );
1637+ update_handler_context .handler = (* env )-> NewGlobalRef (env , this );
1638+ (* env )-> GetJavaVM (env , & update_handler_context .vm );
1639+ sqlite3_update_hook (gethandle (env , this ), & update_hook , NULL );
1640+ } else {
1641+ sqlite3_update_hook (gethandle (env , this ), NULL , NULL );
1642+ (* env )-> DeleteGlobalRef (env , update_handler_context .handler );
1643+ }
1644+ }
1645+
1646+ // Commit hook
1647+
1648+ struct CommitHandlerContext {
1649+ JavaVM * vm ;
1650+ jmethodID method ;
1651+ jobject handler ;
1652+ };
1653+
1654+ static struct CommitHandlerContext commit_handler_context ;
1655+
1656+ int commit_hook (void * context ) {
1657+ JNIEnv * env = 0 ;
1658+ (* commit_handler_context .vm )-> AttachCurrentThread (commit_handler_context .vm , (void * * )& env , 0 );
1659+ (* env )-> CallVoidMethod (env , commit_handler_context .handler , commit_handler_context .method , 1 );
1660+ return 0 ;
1661+ }
1662+
1663+ void rollback_hook (void * context ) {
1664+ JNIEnv * env = 0 ;
1665+ (* commit_handler_context .vm )-> AttachCurrentThread (commit_handler_context .vm , (void * * )& env , 0 );
1666+ (* env )-> CallVoidMethod (env , commit_handler_context .handler , commit_handler_context .method , 0 );
1667+ }
1668+
1669+ JNIEXPORT void JNICALL Java_org_sqlite_core_NativeDB_set_1commit_1listener (JNIEnv * env , jobject this , jboolean enabled ) {
1670+ if (enabled ) {
1671+ commit_handler_context .method = (* env )-> GetMethodID (env , dbclass , "onCommit" , "(Z)V" );
1672+ commit_handler_context .handler = (* env )-> NewGlobalRef (env , this );
1673+ (* env )-> GetJavaVM (env , & commit_handler_context .vm );
1674+ sqlite3_commit_hook (gethandle (env , this ), & commit_hook , NULL );
1675+ sqlite3_rollback_hook (gethandle (env , this ), & rollback_hook , NULL );
1676+ } else {
1677+ sqlite3_commit_hook (gethandle (env , this ), NULL , NULL );
1678+ sqlite3_update_hook (gethandle (env , this ), NULL , NULL );
1679+ (* env )-> DeleteGlobalRef (env , commit_handler_context .handler );
1680+ }
1681+ }
0 commit comments