Skip to content

Commit 81f4dd9

Browse files
committed
Add powersync_client_id() function.
1 parent c93513c commit 81f4dd9

File tree

2 files changed

+81
-16
lines changed

2 files changed

+81
-16
lines changed

crates/core/src/client_id.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
extern crate alloc;
2+
3+
use alloc::format;
4+
use alloc::string::{String, ToString};
5+
use alloc::vec::Vec;
6+
use core::ffi::c_int;
7+
use core::slice;
8+
9+
use serde::{Deserialize, Serialize};
10+
use serde_json as json;
11+
use sqlite::ResultCode;
12+
use sqlite_nostd as sqlite;
13+
use sqlite_nostd::{Connection, Context, Value};
14+
15+
use crate::create_sqlite_text_fn;
16+
use crate::error::SQLiteError;
17+
use crate::sync_types::Checkpoint;
18+
19+
fn powersync_client_id_impl(
20+
ctx: *mut sqlite::context,
21+
args: &[*mut sqlite::value],
22+
) -> Result<String, SQLiteError> {
23+
let db = ctx.db_handle();
24+
25+
// language=SQLite
26+
let statement = db.prepare_v2("select value from ps_kv where key = 'client_id'")?;
27+
28+
if statement.step()? == ResultCode::ROW {
29+
let client_id = statement.column_text(0)?;
30+
return Ok(client_id.to_string());
31+
} else {
32+
return Err(SQLiteError(
33+
ResultCode::ABORT,
34+
Some(format!("No client_id found in ps_kv")),
35+
));
36+
}
37+
}
38+
39+
create_sqlite_text_fn!(
40+
powersync_client_id,
41+
powersync_client_id_impl,
42+
"powersync_client_id"
43+
);
44+
45+
pub fn register(db: *mut sqlite::sqlite3) -> Result<(), ResultCode> {
46+
db.create_function_v2(
47+
"powersync_client_id",
48+
0,
49+
sqlite::UTF8 | sqlite::DETERMINISTIC,
50+
None,
51+
Some(powersync_client_id),
52+
None,
53+
None,
54+
None,
55+
)?;
56+
57+
Ok(())
58+
}

crates/core/src/lib.rs

+23-16
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,24 @@ use core::ffi::{c_char, c_int};
1111
use sqlite::ResultCode;
1212
use sqlite_nostd as sqlite;
1313

14-
mod util;
15-
mod uuid;
16-
mod views;
17-
mod view_admin;
18-
mod macros;
14+
mod checkpoint;
15+
mod client_id;
16+
mod crud_vtab;
1917
mod diff;
20-
mod schema_management;
21-
mod operations_vtab;
22-
mod operations;
23-
mod ext;
2418
mod error;
25-
mod crud_vtab;
26-
mod vtab_util;
19+
mod ext;
20+
mod macros;
21+
mod operations;
22+
mod operations_vtab;
23+
mod schema_management;
2724
mod sync_local;
28-
mod checkpoint;
29-
mod version;
3025
mod sync_types;
26+
mod util;
27+
mod uuid;
28+
mod version;
29+
mod view_admin;
30+
mod views;
31+
mod vtab_util;
3132

3233
#[no_mangle]
3334
pub extern "C" fn sqlite3_powersync_init(
@@ -43,7 +44,7 @@ pub extern "C" fn sqlite3_powersync_init(
4344
code as c_int
4445
} else {
4546
ResultCode::OK as c_int
46-
}
47+
};
4748
}
4849

4950
fn init_extension(db: *mut sqlite::sqlite3) -> Result<(), ResultCode> {
@@ -53,6 +54,7 @@ fn init_extension(db: *mut sqlite::sqlite3) -> Result<(), ResultCode> {
5354
crate::diff::register(db)?;
5455
crate::view_admin::register(db)?;
5556
crate::checkpoint::register(db)?;
57+
crate::client_id::register(db)?;
5658

5759
crate::schema_management::register(db)?;
5860
crate::operations_vtab::register(db)?;
@@ -61,12 +63,17 @@ fn init_extension(db: *mut sqlite::sqlite3) -> Result<(), ResultCode> {
6163
Ok(())
6264
}
6365

64-
6566
extern "C" {
6667
#[cfg(feature = "static")]
6768
#[allow(non_snake_case)]
6869
pub fn sqlite3_auto_extension(
69-
xEntryPoint: Option<extern "C" fn(*mut sqlite::sqlite3, *mut *mut c_char, *mut sqlite::api_routines) -> c_int>,
70+
xEntryPoint: Option<
71+
extern "C" fn(
72+
*mut sqlite::sqlite3,
73+
*mut *mut c_char,
74+
*mut sqlite::api_routines,
75+
) -> c_int,
76+
>,
7077
) -> ::core::ffi::c_int;
7178
}
7279

0 commit comments

Comments
 (0)