Skip to content

Commit

Permalink
move finalize to rust
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Nov 14, 2023
1 parent abc1e3d commit 6cb8bea
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions core/rs/core/src/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
27 changes: 27 additions & 0 deletions core/rs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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,
Expand Down
13 changes: 0 additions & 13 deletions core/src/crsqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 6cb8bea

Please sign in to comment.