From 540376b76c81aa9aefd330ee5de867c5ae45691a Mon Sep 17 00:00:00 2001 From: Matt <1009003+tantaman@users.noreply.github.com> Date: Tue, 14 Nov 2023 13:41:28 -0500 Subject: [PATCH] move `begin_alter` to Rust --- core/rs/core/src/automigrate.rs | 1 - core/rs/core/src/changes_vtab.rs | 1 - core/rs/core/src/lib.rs | 70 +++++++++++++++++++++------- core/rs/core/src/local_writes/mod.rs | 1 - core/rs/core/src/pack_columns.rs | 1 - core/src/crsqlite.c | 47 ------------------- core/src/rust.h | 1 - 7 files changed, 54 insertions(+), 68 deletions(-) diff --git a/core/rs/core/src/automigrate.rs b/core/rs/core/src/automigrate.rs index 397eb68c7..550b0fd9f 100644 --- a/core/rs/core/src/automigrate.rs +++ b/core/rs/core/src/automigrate.rs @@ -9,7 +9,6 @@ use alloc::string::ToString; use alloc::vec; use alloc::vec::Vec; use core::ffi::{c_char, c_int}; -use core::slice; use sqlite::ColumnType; use sqlite_nostd as sqlite; diff --git a/core/rs/core/src/changes_vtab.rs b/core/rs/core/src/changes_vtab.rs index af9692984..73da25c0a 100644 --- a/core/rs/core/src/changes_vtab.rs +++ b/core/rs/core/src/changes_vtab.rs @@ -10,7 +10,6 @@ use alloc::vec::Vec; use core::ffi::{c_char, c_int, CStr}; use core::mem::{self, forget}; use core::ptr::null_mut; -use core::slice; use alloc::ffi::CString; #[cfg(not(feature = "std"))] diff --git a/core/rs/core/src/lib.rs b/core/rs/core/src/lib.rs index faa85f0ca..6f3c5ef4e 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_begin_alter", + -1, + sqlite::UTF8 | sqlite::DIRECTONLY, + None, + Some(x_crsql_begin_alter), + 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_commit_alter", @@ -431,6 +448,43 @@ unsafe extern "C" fn x_crsql_as_crr( ctx.result_text_static("OK"); } +unsafe extern "C" fn x_crsql_begin_alter( + ctx: *mut sqlite::context, + argc: i32, + argv: *mut *mut sqlite::value, +) { + if argc == 0 { + ctx.result_error( + "Wrong number of args provided to crsql_begin_alter. Provide the + schema name and table name or just the table name.", + ); + return; + } + + let args = sqlite::args!(argc, argv); + let (schema_name, table_name) = if argc == 2 { + (args[0].text(), args[1].text()) + } else { + ("main", args[0].text()) + }; + + let db = ctx.db_handle(); + let mut err_msg = null_mut(); + let rc = db.exec_safe("SAVEPOINT alter_crr"); + if rc.is_err() { + ctx.result_error("failed to start alter_crr savepoint"); + return; + } + let rc = remove_crr_triggers_if_exist(db, table_name); + if rc.is_err() { + sqlite::result_error(ctx, err_msg, -1); + sqlite::result_error_code(ctx, rc.unwrap_err() as c_int); + let _ = db.exec_safe("ROLLBACK"); + return; + } + ctx.result_text_static("OK"); +} + unsafe extern "C" fn x_crsql_commit_alter( ctx: *mut sqlite::context, argc: i32, @@ -590,22 +644,6 @@ unsafe extern "C" fn x_crsql_sync_bit( ctx.result_int(*sync_bit_ptr); } -#[no_mangle] -pub extern "C" fn crsql_remove_crr_triggers_if_exist( - db: *mut sqlite::sqlite3, - table: *const c_char, -) -> c_int { - if let Ok(table) = unsafe { CStr::from_ptr(table).to_str() } { - let result = remove_crr_triggers_if_exist(db, table); - match result { - Ok(result) => result as c_int, - Err(result) => result as c_int, - } - } else { - ResultCode::NOMEM as c_int - } -} - #[no_mangle] pub extern "C" fn crsql_is_crr(db: *mut sqlite::sqlite3, table: *const c_char) -> c_int { if let Ok(table) = unsafe { CStr::from_ptr(table).to_str() } { diff --git a/core/rs/core/src/local_writes/mod.rs b/core/rs/core/src/local_writes/mod.rs index d28e4ceaa..cfa75f3c3 100644 --- a/core/rs/core/src/local_writes/mod.rs +++ b/core/rs/core/src/local_writes/mod.rs @@ -6,7 +6,6 @@ use crate::c::crsql_ExtData; use crate::stmt_cache::reset_cached_stmt; use alloc::boxed::Box; use alloc::format; -use alloc::slice; use alloc::string::String; use alloc::vec::Vec; use sqlite::sqlite3; diff --git a/core/rs/core/src/pack_columns.rs b/core/rs/core/src/pack_columns.rs index 752796999..048d2b173 100644 --- a/core/rs/core/src/pack_columns.rs +++ b/core/rs/core/src/pack_columns.rs @@ -4,7 +4,6 @@ use alloc::string::String; use alloc::vec; use alloc::vec::Vec; use bytes::{Buf, BufMut}; -use core::slice; #[cfg(not(feature = "std"))] use num_traits::FromPrimitive; use sqlite_nostd as sqlite; diff --git a/core/src/crsqlite.c b/core/src/crsqlite.c index 8bf8e490b..6c219959b 100644 --- a/core/src/crsqlite.c +++ b/core/src/crsqlite.c @@ -20,47 +20,6 @@ SQLITE_EXTENSION_INIT1 unsigned char __rust_no_alloc_shim_is_unstable; #endif -static void crsqlBeginAlterFunc(sqlite3_context *context, int argc, - sqlite3_value **argv) { - const char *tblName = 0; - const char *schemaName = 0; - int rc = SQLITE_OK; - sqlite3 *db = sqlite3_context_db_handle(context); - char *errmsg = 0; - - if (argc == 0) { - sqlite3_result_error( - context, - "Wrong number of args provided to crsql_as_crr. Provide the schema " - "name and table name or just the table name.", - -1); - return; - } - - if (argc == 2) { - schemaName = (const char *)sqlite3_value_text(argv[0]); - tblName = (const char *)sqlite3_value_text(argv[1]); - } else { - schemaName = "main"; - tblName = (const char *)sqlite3_value_text(argv[0]); - } - - rc = sqlite3_exec(db, "SAVEPOINT alter_crr", 0, 0, &errmsg); - if (rc != SQLITE_OK) { - sqlite3_result_error(context, errmsg, -1); - sqlite3_free(errmsg); - return; - } - - rc = crsql_remove_crr_triggers_if_exist(db, tblName); - if (rc != SQLITE_OK) { - sqlite3_result_error(context, errmsg, -1); - sqlite3_free(errmsg); - sqlite3_exec(db, "ROLLBACK", 0, 0, 0); - return; - } -} - int crsql_compact_post_alter(sqlite3 *db, const char *tblName, crsql_ExtData *pExtData, char **errmsg); @@ -132,12 +91,6 @@ __declspec(dllexport) return SQLITE_ERROR; } - if (rc == SQLITE_OK) { - rc = sqlite3_create_function(db, "crsql_begin_alter", -1, - SQLITE_UTF8 | SQLITE_DIRECTONLY, 0, - crsqlBeginAlterFunc, 0, 0); - } - if (rc == SQLITE_OK) { rc = sqlite3_create_function(db, "crsql_after_update", -1, SQLITE_UTF8 | SQLITE_INNOCUOUS, pExtData, diff --git a/core/src/rust.h b/core/src/rust.h index 2431bafdc..e10c68620 100644 --- a/core/src/rust.h +++ b/core/src/rust.h @@ -10,7 +10,6 @@ // These are those definitions. int crsql_is_crr(sqlite3 *db, const char *tblName); -int crsql_remove_crr_triggers_if_exist(sqlite3 *db, const char *tblName); int crsql_init_site_id(sqlite3 *db, unsigned char *ret); int crsql_init_peer_tracking_table(sqlite3 *db);