Skip to content

Commit 7e6d74f

Browse files
committed
Create SyncedDatabase in libsql_open_sync_with_config if primary url has query param
Signed-off-by: Piotr Jastrzebski <piotr@chiselstrike.com>
1 parent e4784d7 commit 7e6d74f

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/c/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tokio = { version = "1.29.1", features = [ "rt-multi-thread" ] }
1717
hyper-rustls = { version = "0.25", features = ["webpki-roots"]}
1818
tracing = "0.1.40"
1919
tracing-subscriber = "0.3.18"
20+
http = "1.1.0"
2021

2122
[target.'cfg(not(any(target_os = "ios", target_os = "android")))'.dependencies]
2223
libsql = { path = "../../libsql", features = ["encryption"] }

bindings/c/src/lib.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extern crate lazy_static;
66
mod types;
77

88
use crate::types::libsql_config;
9-
use libsql::{errors, LoadExtensionGuard};
9+
use libsql::{errors, Builder, LoadExtensionGuard};
1010
use tokio::runtime::Runtime;
1111
use types::{
1212
blob, libsql_connection, libsql_connection_t, libsql_database, libsql_database_t, libsql_row,
@@ -152,6 +152,44 @@ pub unsafe extern "C" fn libsql_open_sync_with_config(
152152
return 3;
153153
}
154154
};
155+
let uri: http::Uri = match primary_url.try_into() {
156+
Ok(uri) => uri,
157+
Err(e) => {
158+
set_err_msg(format!("Wrong primary URL: {e}"), out_err_msg);
159+
return 100;
160+
}
161+
};
162+
if let Some(query) = uri.query() {
163+
if query.contains("offline") {
164+
let mut builder = Builder::new_synced_database(
165+
db_path,
166+
primary_url.to_owned(),
167+
auth_token.to_owned(),
168+
);
169+
if config.with_webpki != 0 {
170+
let https = hyper_rustls::HttpsConnectorBuilder::new()
171+
.with_webpki_roots()
172+
.https_or_http()
173+
.enable_http1()
174+
.build();
175+
builder = builder.connector(https);
176+
}
177+
match RT.block_on(builder.build()) {
178+
Ok(db) => {
179+
let db = Box::leak(Box::new(libsql_database { db }));
180+
*out_db = libsql_database_t::from(db);
181+
return 0;
182+
}
183+
Err(e) => {
184+
set_err_msg(
185+
format!("Error opening offline db path {db_path}, primary url {primary_url}: {e}"),
186+
out_err_msg,
187+
);
188+
return 101;
189+
}
190+
}
191+
}
192+
}
155193
let mut builder = libsql::Builder::new_remote_replica(
156194
db_path,
157195
primary_url.to_string(),

0 commit comments

Comments
 (0)