From 481ff0caf0f8970eb9dddb08aac839aeb9507fbe Mon Sep 17 00:00:00 2001 From: FabianLars Date: Thu, 16 Feb 2023 18:45:44 +0100 Subject: [PATCH] fix dir creation. accept read-only mode setting --- plugins/sql/guest-js/index.ts | 2 ++ plugins/sql/src/plugin.rs | 31 ++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/plugins/sql/guest-js/index.ts b/plugins/sql/guest-js/index.ts index d3faedce..b48795b4 100644 --- a/plugins/sql/guest-js/index.ts +++ b/plugins/sql/guest-js/index.ts @@ -39,6 +39,7 @@ export default class Database { * - `sqlite::memory:`: Open an in-memory database. * - `sqlite:///data.db`: Open the file `data.db` from the root directory. `sqlite://\\?\` will be automatically converted to `sqlite:///` * - `sqlite://data.db` or `sqlite:data.db`: Open the file `data.db` relative to the `dir` argument, or BaseDirectory.App if not provided + * Appending `?mode=ro` to the path will open the database in read-only mode. * * @example * ```ts @@ -67,6 +68,7 @@ export default class Database { * - `sqlite::memory:`: Open an in-memory database. * - `sqlite:///data.db`: Open the file `data.db` from the root directory. `sqlite://\\?\` will be automatically converted to `sqlite:///` * - `sqlite://data.db` or `sqlite:data.db`: Open the file `data.db` relative to the `dir` argument, or BaseDirectory.App if not provided + * Appending `?mode=ro` to the path will open the database in read-only mode. * * @example * ```ts diff --git a/plugins/sql/src/plugin.rs b/plugins/sql/src/plugin.rs index 02a6a3b5..17507f93 100644 --- a/plugins/sql/src/plugin.rs +++ b/plugins/sql/src/plugin.rs @@ -76,39 +76,48 @@ fn path_mapper( connection_string: &str, dir: Option, ) -> Result { + use std::path::Path; use tauri::api::path::resolve_path; if connection_string.starts_with("sqlite::memory:") { return Ok(connection_string.to_string()); } + if connection_string.starts_with("sqlite:///") || connection_string.starts_with(r"sqlite://\\?\") { - create_dir_all( - connection_string - .replace("sqlite:///", "/") - .replace(r"sqlite://\\?\", "") - .replace("?mode=ro", ""), - )?; + let path = connection_string + .replace("sqlite:///", "/") + .replace(r"sqlite://\\?\", "") + .replace("?mode=ro", ""); + let path = Path::new(&path); + + create_dir_all(path.parent().unwrap_or(path))?; + return Ok(connection_string.replace(r"\\?\", "/").replace('\\', "/")); } - let connection_string = connection_string + let ro = connection_string.ends_with("?mode=ro"); + + let path = connection_string .replace("sqlite://", "") - .replace("sqlite:", ""); + .replace("sqlite:", "") + .replace("?mode=ro", ""); + let path = Path::new(&path); let path = resolve_path( &app.config(), app.package_info(), &app.env(), - connection_string, + path.parent().unwrap_or(path), #[allow(deprecated)] // FIXME: Use non deprecated variant in tauri v2 dir.or(Some(BaseDirectory::App)), )?; Ok(format!( - "sqlite://{}", - path.display().to_string().replace(r"\\?\", "/") + "sqlite://{}{}", + path.display().to_string().replace(r"\\?\", "/"), + if ro { "?mode=ro" } else { "" } )) }