From 6cb8bea65782c8d6d482f4f8efd8d5b4e17da1d7 Mon Sep 17 00:00:00 2001 From: Matt <1009003+tantaman@users.noreply.github.com> Date: Tue, 14 Nov 2023 11:34:18 -0500 Subject: [PATCH] move finalize to rust --- core/rs/core/src/c.rs | 1 + core/rs/core/src/lib.rs | 27 +++++++++++++++++++++++++++ core/src/crsqlite.c | 13 ------------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/core/rs/core/src/c.rs b/core/rs/core/src/c.rs index 5f489d6ae..0c04e4ec7 100644 --- a/core/rs/core/src/c.rs +++ b/core/rs/core/src/c.rs @@ -108,6 +108,7 @@ extern "C" { siteIdBuffer: *mut c_char, ) -> *mut crsql_ExtData; pub fn crsql_freeExtData(pExtData: *mut crsql_ExtData); + pub fn crsql_finalize(pExtData: *mut crsql_ExtData); } #[test] diff --git a/core/rs/core/src/lib.rs b/core/rs/core/src/lib.rs index 7b16cf088..9e2bb3887 100644 --- a/core/rs/core/src/lib.rs +++ b/core/rs/core/src/lib.rs @@ -314,6 +314,23 @@ pub extern "C" fn sqlite3_crsqlcore_init( return null_mut(); } + let rc = db + .create_function_v2( + "crsql_finalize", + -1, + sqlite::UTF8 | sqlite::DIRECTONLY, + Some(ext_data as *mut c_void), + Some(x_crsql_finalize), + None, + None, + None, + ) + .unwrap_or(ResultCode::ERROR); + if rc != ResultCode::OK { + unsafe { crsql_freeExtData(ext_data) }; + return null_mut(); + } + return ext_data as *mut c_void; } @@ -332,6 +349,16 @@ unsafe extern "C" fn x_crsql_site_id( sqlite::result_blob(ctx, site_id, consts::SITE_ID_LEN, Destructor::STATIC); } +unsafe extern "C" fn x_crsql_finalize( + ctx: *mut sqlite::context, + argc: i32, + argv: *mut *mut sqlite::value, +) { + let ext_data = ctx.user_data() as *mut c::crsql_ExtData; + c::crsql_finalize(ext_data); + ctx.result_text_static("finalized"); +} + unsafe extern "C" fn x_crsql_commit_alter( ctx: *mut sqlite::context, argc: i32, diff --git a/core/src/crsqlite.c b/core/src/crsqlite.c index 1248c4e3f..f60908bdd 100644 --- a/core/src/crsqlite.c +++ b/core/src/crsqlite.c @@ -119,12 +119,6 @@ static void freeConnectionExtData(void *pUserData) { crsql_freeExtData(pExtData); } -static void crsqlFinalize(sqlite3_context *context, int argc, - sqlite3_value **argv) { - crsql_ExtData *pExtData = (crsql_ExtData *)sqlite3_user_data(context); - crsql_finalize(pExtData); -} - static void crsqlRowsImpacted(sqlite3_context *context, int argc, sqlite3_value **argv) { crsql_ExtData *pExtData = (crsql_ExtData *)sqlite3_user_data(context); @@ -205,13 +199,6 @@ __declspec(dllexport) crsqlBeginAlterFunc, 0, 0); } - if (rc == SQLITE_OK) { - // see https://sqlite.org/forum/forumpost/c94f943821 - rc = sqlite3_create_function(db, "crsql_finalize", -1, - SQLITE_UTF8 | SQLITE_DIRECTONLY, pExtData, - crsqlFinalize, 0, 0); - } - if (rc == SQLITE_OK) { rc = sqlite3_create_function(db, "crsql_after_update", -1, SQLITE_UTF8 | SQLITE_INNOCUOUS, pExtData,