-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathoperations.rs
More file actions
69 lines (55 loc) · 1.94 KB
/
operations.rs
File metadata and controls
69 lines (55 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::error::PowerSyncError;
use crate::sync::line::DataLine;
use crate::sync::operations::insert_bucket_operations;
use crate::sync::storage_adapter::StorageAdapter;
use alloc::vec::Vec;
use powersync_sqlite_nostd as sqlite;
use powersync_sqlite_nostd::{Connection, ResultCode};
use serde::Deserialize;
use crate::ext::SafeManagedStmt;
// Run inside a transaction
pub fn insert_operation(db: *mut sqlite::sqlite3, data: &str) -> Result<(), PowerSyncError> {
#[derive(Deserialize)]
struct BucketBatch<'a> {
#[serde(borrow)]
buckets: Vec<DataLine<'a>>,
}
let batch: BucketBatch =
serde_json::from_str(data).map_err(PowerSyncError::as_argument_error)?;
let adapter = StorageAdapter::new(db)?;
for line in &batch.buckets {
insert_bucket_operations(&adapter, &line)?;
}
Ok(())
}
pub fn clear_remove_ops(_db: *mut sqlite::sqlite3, _data: &str) -> Result<(), ResultCode> {
// No-op
Ok(())
}
pub fn delete_pending_buckets(_db: *mut sqlite::sqlite3, _data: &str) -> Result<(), ResultCode> {
// No-op
Ok(())
}
pub fn delete_bucket(db: *mut sqlite::sqlite3, name: &str) -> Result<(), ResultCode> {
// language=SQLite
let statement = db.prepare_v2("DELETE FROM ps_buckets WHERE name = ?1 RETURNING id")?;
statement.bind_text(1, name, sqlite::Destructor::STATIC)?;
if statement.step()? == ResultCode::ROW {
let bucket_id = statement.column_int64(0);
// language=SQLite
let updated_statement = db.prepare_v2(
"\
INSERT OR IGNORE INTO ps_updated_rows(row_type, row_id)
SELECT row_type, row_id
FROM ps_oplog
WHERE bucket = ?1",
)?;
updated_statement.bind_int64(1, bucket_id)?;
updated_statement.exec()?;
// language=SQLite
let delete_statement = db.prepare_v2("DELETE FROM ps_oplog WHERE bucket=?1")?;
delete_statement.bind_int64(1, bucket_id)?;
delete_statement.exec()?;
}
Ok(())
}