Skip to content

Commit

Permalink
Create SyncedDatabase in libsql_open_sync_with_config if primary url …
Browse files Browse the repository at this point in the history
…has query param

Signed-off-by: Piotr Jastrzebski <[email protected]>
  • Loading branch information
haaawk committed Nov 25, 2024
1 parent 5fe0a63 commit 7eb42d4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bindings/c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tokio = { version = "1.29.1", features = [ "rt-multi-thread" ] }
hyper-rustls = { version = "0.25", features = ["webpki-roots"]}
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
http = "1.1.0"

[target.'cfg(not(any(target_os = "ios", target_os = "android")))'.dependencies]
libsql = { path = "../../libsql", features = ["encryption"] }
Expand Down
36 changes: 35 additions & 1 deletion bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extern crate lazy_static;
mod types;

use crate::types::libsql_config;
use libsql::{errors, LoadExtensionGuard};
use libsql::{Builder, errors, LoadExtensionGuard};
use tokio::runtime::Runtime;
use types::{
blob, libsql_connection, libsql_connection_t, libsql_database, libsql_database_t, libsql_row,
Expand Down Expand Up @@ -152,6 +152,40 @@ pub unsafe extern "C" fn libsql_open_sync_with_config(
return 3;
}
};
let uri: http::Uri = match primary_url.try_into(){
Ok(uri) => uri,
Err(e) => {
set_err_msg(format!("Wrong primary URL: {e}"), out_err_msg);
return 100;
}
};
if let Some(query) = uri.query() {
if query.contains("offline") {
let mut builder = Builder::new_synced_database(db_path, primary_url.to_owned(), auth_token.to_owned());
if config.with_webpki != 0 {
let https = hyper_rustls::HttpsConnectorBuilder::new()
.with_webpki_roots()
.https_or_http()
.enable_http1()
.build();
builder = builder.connector(https);
}
match RT.block_on(builder.build()) {
Ok(db) => {
let db = Box::leak(Box::new(libsql_database { db }));
*out_db = libsql_database_t::from(db);
return 0;
}
Err(e) => {
set_err_msg(
format!("Error opening offline db path {db_path}, primary url {primary_url}: {e}"),
out_err_msg,
);
return 101;
}
}
}
}
let mut builder = libsql::Builder::new_remote_replica(
db_path,
primary_url.to_string(),
Expand Down

0 comments on commit 7eb42d4

Please sign in to comment.