Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert after_delete to a function #372

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions core/rs/core/src/local_writes/after_delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use alloc::string::String;
use core::ffi::c_int;
use sqlite::sqlite3;
use sqlite::value;
use sqlite::Context;
use sqlite::ResultCode;
use sqlite_nostd as sqlite;

use crate::db_version;
use crate::{c::crsql_ExtData, tableinfo::TableInfo};

use super::bump_seq;
use super::trigger_fn_preamble;

/**
* crsql_after_delete("table", old_pk_values...)
*/
#[no_mangle]
pub unsafe extern "C" fn crsql_after_delete(
ctx: *mut sqlite::context,
argc: c_int,
argv: *mut *mut sqlite::value,
) {
let result = trigger_fn_preamble(ctx, argc, argv, |table_info, values, ext_data| {
after_delete(ctx.db_handle(), ext_data, table_info, &values[1..])
});

match result {
Ok(_) => {
ctx.result_int64(0);
}
Err(msg) => {
ctx.result_error(&msg);
}
}
}

fn after_delete(
db: *mut sqlite3,
ext_data: *mut crsql_ExtData,
tbl_info: &TableInfo,
pks_old: &[*mut value],
) -> Result<ResultCode, String> {
let db_version = crate::db_version::next_db_version(db, ext_data, None)?;
let seq = bump_seq(ext_data);

let mark_locally_deleted_stmt_ref = tbl_info
.get_mark_locally_deleted_stmt(db)
.or_else(|_e| Err("failed to get mark_locally_deleted_stmt"))?;
let mark_locally_deleted_stmt = mark_locally_deleted_stmt_ref
.as_ref()
.ok_or("Failed to deref sentinel stmt")?;
for (i, pk) in pks_old.iter().enumerate() {
mark_locally_deleted_stmt
.bind_value(i as i32 + 1, *pk)
.or_else(|_e| Err("failed to bind pks to mark_locally_deleted_stmt"))?;
}
mark_locally_deleted_stmt
.bind_int64(pks_old.len() as i32 + 1, db_version)
.and_then(|_| mark_locally_deleted_stmt.bind_int(pks_old.len() as i32 + 2, seq))
.and_then(|_| mark_locally_deleted_stmt.bind_int64(pks_old.len() as i32 + 3, db_version))
.and_then(|_| mark_locally_deleted_stmt.bind_int(pks_old.len() as i32 + 4, seq))
.or_else(|_| Err("failed binding to mark locally deleted stmt"))?;
super::step_trigger_stmt(mark_locally_deleted_stmt)?;

// now actually delete the row metadata
let drop_clocks_stmt_ref = tbl_info
.get_merge_delete_drop_clocks_stmt(db)
.or_else(|_e| Err("failed to get mark_locally_deleted_stmt"))?;
let drop_clocks_stmt = drop_clocks_stmt_ref
.as_ref()
.ok_or("Failed to deref sentinel stmt")?;

for (i, pk) in pks_old.iter().enumerate() {
drop_clocks_stmt
.bind_value(i as i32 + 1, *pk)
.or_else(|_e| Err("failed to bind pks to mark_locally_deleted_stmt"))?;
}
super::step_trigger_stmt(drop_clocks_stmt)
}
1 change: 1 addition & 0 deletions core/rs/core/src/local_writes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use sqlite_nostd::ResultCode;

use crate::tableinfo::{crsql_ensure_table_infos_are_up_to_date, ColumnInfo, TableInfo};

mod after_delete;
mod after_insert;
mod after_update;

Expand Down
26 changes: 1 addition & 25 deletions core/rs/core/src/triggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,39 +84,15 @@ fn create_delete_trigger(
) -> Result<ResultCode, ResultCode> {
let table_name = &table_info.tbl_name;
let pk_columns = &table_info.pks;
let pk_list = crate::util::as_identifier_list(pk_columns, None)?;
let pk_old_list = crate::util::as_identifier_list(pk_columns, Some("OLD."))?;
let pk_where_list = crate::util::pk_where_list(pk_columns, Some("OLD."))?;

let create_trigger_sql = format!(
"CREATE TRIGGER IF NOT EXISTS \"{table_name}__crsql_dtrig\"
AFTER DELETE ON \"{table_name}\" WHEN crsql_internal_sync_bit() = 0
BEGIN
INSERT INTO \"{table_name}__crsql_clock\" (
{pk_list},
__crsql_col_name,
__crsql_col_version,
__crsql_db_version,
__crsql_seq,
__crsql_site_id
) SELECT
{pk_old_list},
'{sentinel}',
2,
crsql_next_db_version(),
crsql_increment_and_get_seq(),
NULL WHERE true
ON CONFLICT DO UPDATE SET
__crsql_col_version = 1 + __crsql_col_version,
__crsql_db_version = crsql_next_db_version(),
__crsql_seq = crsql_get_seq() - 1,
__crsql_site_id = NULL;
DELETE FROM \"{table_name}__crsql_clock\"
WHERE {pk_where_list} AND __crsql_col_name != '{sentinel}';
VALUES (crsql_after_delete('{}', {pk_old_list}));
END;",
table_name = crate::util::escape_ident(table_name),
sentinel = crate::c::DELETE_SENTINEL,
pk_where_list = pk_where_list,
pk_old_list = pk_old_list
);

Expand Down
5 changes: 5 additions & 0 deletions core/src/crsqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ __declspec(dllexport)
SQLITE_UTF8 | SQLITE_INNOCUOUS, pExtData,
crsql_after_insert, 0, 0);
}
if (rc == SQLITE_OK) {
rc = sqlite3_create_function(db, "crsql_after_delete", -1,
SQLITE_UTF8 | SQLITE_INNOCUOUS, pExtData,
crsql_after_delete, 0, 0);
}

if (rc == SQLITE_OK) {
rc = sqlite3_create_function(db, "crsql_rows_impacted", 0,
Expand Down
2 changes: 2 additions & 0 deletions core/src/rust.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ void crsql_after_update(sqlite3_context *context, int argc,
sqlite3_value **argv);
void crsql_after_insert(sqlite3_context *context, int argc,
sqlite3_value **argv);
void crsql_after_delete(sqlite3_context *context, int argc,
sqlite3_value **argv);
#endif