Skip to content

Commit

Permalink
move begin_alter to Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Nov 14, 2023
1 parent 17c92a8 commit 540376b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 68 deletions.
1 change: 0 additions & 1 deletion core/rs/core/src/automigrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 0 additions & 1 deletion core/rs/core/src/changes_vtab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down
70 changes: 54 additions & 16 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_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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() } {
Expand Down
1 change: 0 additions & 1 deletion core/rs/core/src/local_writes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion core/rs/core/src/pack_columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 0 additions & 47 deletions core/src/crsqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion core/src/rust.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 540376b

Please sign in to comment.