-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathkv.rs
More file actions
89 lines (75 loc) · 2.16 KB
/
kv.rs
File metadata and controls
89 lines (75 loc) · 2.16 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
extern crate alloc;
use alloc::string::{String, ToString};
use core::ffi::c_int;
use powersync_sqlite_nostd as sqlite;
use powersync_sqlite_nostd::{Connection, Context};
use sqlite::ResultCode;
use crate::create_sqlite_optional_text_fn;
use crate::create_sqlite_text_fn;
use crate::error::PowerSyncError;
use crate::sync::BucketPriority;
fn powersync_client_id_impl(
ctx: *mut sqlite::context,
_args: &[*mut sqlite::value],
) -> Result<String, PowerSyncError> {
let db = ctx.db_handle();
client_id(db)
}
pub fn client_id(db: *mut sqlite::sqlite3) -> Result<String, PowerSyncError> {
// language=SQLite
let statement = db.prepare_v2("select value from ps_kv where key = 'client_id'")?;
if statement.step()? == ResultCode::ROW {
let client_id = statement.column_text(0)?;
Ok(client_id.to_string())
} else {
Err(PowerSyncError::missing_client_id())
}
}
create_sqlite_text_fn!(
powersync_client_id,
powersync_client_id_impl,
"powersync_client_id"
);
fn powersync_last_synced_at_impl(
ctx: *mut sqlite::context,
_args: &[*mut sqlite::value],
) -> Result<Option<String>, ResultCode> {
let db = ctx.db_handle();
// language=SQLite
let statement = db.prepare_v2("select last_synced_at from ps_sync_state where priority = ?")?;
statement.bind_int(1, BucketPriority::SENTINEL.into())?;
if statement.step()? == ResultCode::ROW {
let client_id = statement.column_text(0)?;
Ok(Some(client_id.to_string()))
} else {
Ok(None)
}
}
create_sqlite_optional_text_fn!(
powersync_last_synced_at,
powersync_last_synced_at_impl,
"powersync_last_synced_at"
);
pub fn register(db: *mut sqlite::sqlite3) -> Result<(), ResultCode> {
db.create_function_v2(
"powersync_client_id",
0,
sqlite::UTF8 | sqlite::DETERMINISTIC,
None,
Some(powersync_client_id),
None,
None,
None,
)?;
db.create_function_v2(
"powersync_last_synced_at",
0,
sqlite::UTF8 | sqlite::DETERMINISTIC,
None,
Some(powersync_last_synced_at),
None,
None,
None,
)?;
Ok(())
}