Skip to content

Commit

Permalink
move db_version to rust
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Nov 14, 2023
1 parent af7adac commit 6830385
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 27 deletions.
47 changes: 46 additions & 1 deletion core/rs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use backfill::*;
use c::{crsql_freeExtData, crsql_newExtData};
use core::ffi::{c_int, c_void, CStr};
use create_crr::create_crr;
use db_version::crsql_fill_db_version_if_needed;
use is_crr::*;
use sqlite::{Destructor, ResultCode};
use sqlite_nostd as sqlite;
Expand Down Expand Up @@ -227,6 +228,23 @@ pub extern "C" fn sqlite3_crsqlcore_init(
return null_mut();
}

let rc = db
.create_function_v2(
"crsql_db_version",
0,
sqlite::INNOCUOUS | sqlite::UTF8,
Some(ext_data as *mut c_void),
Some(x_crsql_db_version),
None,
None,
Some(x_free_connection_ext_data),
)
.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 @@ -245,11 +263,38 @@ unsafe extern "C" fn x_crsql_site_id(
sqlite::result_blob(ctx, site_id, consts::SITE_ID_LEN, Destructor::STATIC);
}

/**
* Return the current version of the database.
*
* `select crsql_db_version()`
*/
unsafe extern "C" fn x_crsql_db_version(
ctx: *mut sqlite::context,
argc: i32,
argv: *mut *mut sqlite::value,
) {
let ext_data = ctx.user_data() as *mut c::crsql_ExtData;
let db = ctx.db_handle();
let mut err_msg = null_mut();
let rc = crsql_fill_db_version_if_needed(db, ext_data, &mut err_msg as *mut _);
if rc != ResultCode::OK as c_int {
// TODO: pass err_msg!
ctx.result_error("failed to fill db version");
return;
}
sqlite::result_int64(ctx, (*ext_data).dbVersion);
}

unsafe extern "C" fn x_free_connection_ext_data(data: *mut c_void) {
let ext_data = data as *mut c::crsql_ExtData;
crsql_freeExtData(ext_data);
}

pub unsafe extern "C" fn crsql_sqlite_free(ptr: *mut c_void) {
sqlite::free(ptr);
}

pub unsafe extern "C" fn x_crsql_sync_bit(
unsafe extern "C" fn x_crsql_sync_bit(
ctx: *mut sqlite::context,
argc: i32,
argv: *mut *mut sqlite::value,
Expand Down
26 changes: 0 additions & 26 deletions core/src/crsqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,6 @@ SQLITE_EXTENSION_INIT1
unsigned char __rust_no_alloc_shim_is_unstable;
#endif

/**
* Return the current version of the database.
*
* `select crsql_db_version()`
*/
static void dbVersionFunc(sqlite3_context *context, int argc,
sqlite3_value **argv) {
char *errmsg = 0;
crsql_ExtData *pExtData = (crsql_ExtData *)sqlite3_user_data(context);
sqlite3 *db = sqlite3_context_db_handle(context);
int rc = crsql_fill_db_version_if_needed(db, pExtData, &errmsg);
if (rc != SQLITE_OK) {
sqlite3_result_error(context, errmsg, -1);
sqlite3_free(errmsg);
return;
}

sqlite3_result_int64(context, pExtData->dbVersion);
}

/**
* Return the next version of the database for use in inserts/updates/deletes
*
Expand Down Expand Up @@ -292,12 +272,6 @@ __declspec(dllexport)
return SQLITE_ERROR;
}

if (rc == SQLITE_OK) {
rc = sqlite3_create_function_v2(db, "crsql_db_version", 0,
// dbversion can change on each invocation.
SQLITE_UTF8 | SQLITE_INNOCUOUS, pExtData,
dbVersionFunc, 0, 0, freeConnectionExtData);
}
if (rc == SQLITE_OK) {
rc = sqlite3_create_function(db, "crsql_next_db_version", -1,
// dbversion can change on each invocation.
Expand Down

0 comments on commit 6830385

Please sign in to comment.