Skip to content

Commit

Permalink
move crsql_after_update to Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Nov 14, 2023
1 parent 540376b commit a625aa8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
1 change: 0 additions & 1 deletion core/rs/core/src/changes_vtab_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use alloc::format;
use alloc::vec::Vec;
use core::ffi::{c_char, c_int};
use core::mem;
use core::slice;
use sqlite::Stmt;
use sqlite_nostd as sqlite;
use sqlite_nostd::{sqlite3, ResultCode, Value};
Expand Down
23 changes: 21 additions & 2 deletions core/rs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ mod triggers;
mod unpack_columns_vtab;
mod util;

use core::ffi::c_char;
use core::mem;
use core::ptr::null_mut;
use core::{ffi::c_char, slice};
extern crate alloc;
use alter::crsql_compact_post_alter;
use automigrate::*;
Expand All @@ -56,6 +56,7 @@ use core::ffi::{c_int, c_void, CStr};
use create_crr::create_crr;
use db_version::{crsql_fill_db_version_if_needed, crsql_next_db_version};
use is_crr::*;
use local_writes::after_update::x_crsql_after_update;
use sqlite::{Destructor, ResultCode};
use sqlite_nostd as sqlite;
use sqlite_nostd::{Connection, Context, Value};
Expand Down Expand Up @@ -348,6 +349,23 @@ pub extern "C" fn sqlite3_crsqlcore_init(
return null_mut();
}

let rc = db
.create_function_v2(
"crsql_after_update",
-1,
sqlite::UTF8 | sqlite::INNOCUOUS,
Some(ext_data as *mut c_void),
Some(x_crsql_after_update),
None,
None,
None,
)
.unwrap_or(ResultCode::ERROR);
if rc != ResultCode::OK {
unsafe { crsql_freeExtData(ext_data) };
return null_mut();
}

let rc = db
.create_function_v2(
"crsql_finalize",
Expand Down Expand Up @@ -462,7 +480,8 @@ unsafe extern "C" fn x_crsql_begin_alter(
}

let args = sqlite::args!(argc, argv);
let (schema_name, table_name) = if argc == 2 {
// TODO: use schema name!
let (_schema_name, table_name) = if argc == 2 {
(args[0].text(), args[1].text())
} else {
("main", args[0].text())
Expand Down
3 changes: 1 addition & 2 deletions core/rs/core/src/local_writes/after_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use crate::{c::crsql_ExtData, tableinfo::TableInfo};

use super::trigger_fn_preamble;

#[no_mangle]
pub unsafe extern "C" fn crsql_after_update(
pub unsafe extern "C" fn x_crsql_after_update(
ctx: *mut sqlite::context,
argc: c_int,
argv: *mut *mut sqlite::value,
Expand Down
2 changes: 1 addition & 1 deletion core/rs/core/src/local_writes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::tableinfo::{crsql_ensure_table_infos_are_up_to_date, ColumnInfo, Tabl

mod after_delete;
mod after_insert;
mod after_update;
pub mod after_update;

fn trigger_fn_preamble<F>(
ctx: *mut sqlite::context,
Expand Down
5 changes: 0 additions & 5 deletions core/src/crsqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ __declspec(dllexport)
return SQLITE_ERROR;
}

if (rc == SQLITE_OK) {
rc = sqlite3_create_function(db, "crsql_after_update", -1,
SQLITE_UTF8 | SQLITE_INNOCUOUS, pExtData,
crsql_after_update, 0, 0);
}
if (rc == SQLITE_OK) {
rc = sqlite3_create_function(db, "crsql_after_insert", -1,
SQLITE_UTF8 | SQLITE_INNOCUOUS, pExtData,
Expand Down

0 comments on commit a625aa8

Please sign in to comment.