Skip to content

Commit 7742508

Browse files
committed
Prepare support for update hooks
1 parent 97440f7 commit 7742508

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

crates/core/src/crud_vtab.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,15 @@ extern "C" fn begin(vtab: *mut sqlite::vtab) -> c_int {
334334

335335
extern "C" fn commit(vtab: *mut sqlite::vtab) -> c_int {
336336
let tab = unsafe { &mut *(vtab.cast::<VirtualTable>()) };
337+
tab.state.track_commit();
337338
tab.end_transaction();
338339
ResultCode::OK as c_int
339340
}
340341

341342
extern "C" fn rollback(vtab: *mut sqlite::vtab) -> c_int {
342343
let tab = unsafe { &mut *(vtab.cast::<VirtualTable>()) };
343344
tab.end_transaction();
345+
tab.state.track_rollback();
344346
// ps_tx will be rolled back automatically
345347
ResultCode::OK as c_int
346348
}

crates/core/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#![no_std]
2-
#![feature(vec_into_raw_parts)]
32
#![allow(internal_features)]
3+
#![feature(btree_set_entry)]
44
#![feature(core_intrinsics)]
55
#![feature(assert_matches)]
66
#![feature(strict_overflow_ops)]
7+
#![feature(vec_into_raw_parts)]
78

89
extern crate alloc;
910

crates/core/src/state.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
use core::{
2+
cell::RefCell,
23
ffi::{c_int, c_void},
34
sync::atomic::{AtomicBool, Ordering},
45
};
56

6-
use alloc::sync::Arc;
7+
use alloc::{
8+
collections::btree_set::BTreeSet,
9+
string::{String, ToString},
10+
sync::Arc,
11+
};
712
use sqlite::{Connection, ResultCode};
813
use sqlite_nostd::{self as sqlite, Context};
914

@@ -14,12 +19,16 @@ use sqlite_nostd::{self as sqlite, Context};
1419
/// functions/vtabs that need access to it.
1520
pub struct DatabaseState {
1621
pub is_in_sync_local: AtomicBool,
22+
pending_updates: RefCell<BTreeSet<String>>,
23+
commited_updates: RefCell<BTreeSet<String>>,
1724
}
1825

1926
impl DatabaseState {
2027
pub fn new() -> Self {
2128
DatabaseState {
2229
is_in_sync_local: AtomicBool::new(false),
30+
pending_updates: Default::default(),
31+
commited_updates: Default::default(),
2332
}
2433
}
2534

@@ -39,6 +48,25 @@ impl DatabaseState {
3948
ClearOnDrop(self)
4049
}
4150

51+
pub fn track_update(self, tbl: &str) {
52+
let mut set = self.pending_updates.borrow_mut();
53+
set.get_or_insert_with(tbl, str::to_string);
54+
}
55+
56+
pub fn track_rollback(&self) {
57+
self.pending_updates.borrow_mut().clear();
58+
}
59+
60+
pub fn track_commit(&self) {
61+
let mut commited = self.commited_updates.borrow_mut();
62+
let mut pending = self.pending_updates.borrow_mut();
63+
let pending = core::mem::replace(&mut *pending, Default::default());
64+
65+
for pending in pending.into_iter() {
66+
commited.insert(pending);
67+
}
68+
}
69+
4270
pub unsafe extern "C" fn destroy_arc(ptr: *mut c_void) {
4371
drop(unsafe { Arc::from_raw(ptr.cast::<DatabaseState>()) });
4472
}

0 commit comments

Comments
 (0)