fix dir creation. accept read-only mode setting

pull/279/head
FabianLars 2 years ago
parent 988ac8e6e8
commit 481ff0caf0
No known key found for this signature in database
GPG Key ID: 3B12BC1DEBF61125

@ -39,6 +39,7 @@ export default class Database {
* - `sqlite::memory:`: Open an in-memory 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`: 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 * - `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 * @example
* ```ts * ```ts
@ -67,6 +68,7 @@ export default class Database {
* - `sqlite::memory:`: Open an in-memory 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`: 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 * - `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 * @example
* ```ts * ```ts

@ -76,39 +76,48 @@ fn path_mapper<R: Runtime>(
connection_string: &str, connection_string: &str,
dir: Option<BaseDirectory>, dir: Option<BaseDirectory>,
) -> Result<String> { ) -> Result<String> {
use std::path::Path;
use tauri::api::path::resolve_path; use tauri::api::path::resolve_path;
if connection_string.starts_with("sqlite::memory:") { if connection_string.starts_with("sqlite::memory:") {
return Ok(connection_string.to_string()); return Ok(connection_string.to_string());
} }
if connection_string.starts_with("sqlite:///") if connection_string.starts_with("sqlite:///")
|| connection_string.starts_with(r"sqlite://\\?\") || connection_string.starts_with(r"sqlite://\\?\")
{ {
create_dir_all( let path = connection_string
connection_string .replace("sqlite:///", "/")
.replace("sqlite:///", "/") .replace(r"sqlite://\\?\", "")
.replace(r"sqlite://\\?\", "") .replace("?mode=ro", "");
.replace("?mode=ro", ""), let path = Path::new(&path);
)?;
create_dir_all(path.parent().unwrap_or(path))?;
return Ok(connection_string.replace(r"\\?\", "/").replace('\\', "/")); 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("sqlite:", "")
.replace("?mode=ro", "");
let path = Path::new(&path);
let path = resolve_path( let path = resolve_path(
&app.config(), &app.config(),
app.package_info(), app.package_info(),
&app.env(), &app.env(),
connection_string, path.parent().unwrap_or(path),
#[allow(deprecated)] // FIXME: Use non deprecated variant in tauri v2 #[allow(deprecated)] // FIXME: Use non deprecated variant in tauri v2
dir.or(Some(BaseDirectory::App)), dir.or(Some(BaseDirectory::App)),
)?; )?;
Ok(format!( Ok(format!(
"sqlite://{}", "sqlite://{}{}",
path.display().to_string().replace(r"\\?\", "/") path.display().to_string().replace(r"\\?\", "/"),
if ro { "?mode=ro" } else { "" }
)) ))
} }

Loading…
Cancel
Save