-
Notifications
You must be signed in to change notification settings - Fork 404
feat(sql): support for SQLite pragma and encryption pragmas #2553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2
Are you sure you want to change the base?
Changes from all commits
2477559
65a77b7
30df392
49d9a38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"rust-analyzer.cargo.features": ["mysql", "postgres", "sqlite"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,15 @@ | |
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
#[cfg(feature = "sqlite")] | ||
use std::collections::HashMap; | ||
#[cfg(feature = "sqlite")] | ||
use std::fs::create_dir_all; | ||
|
||
use indexmap::IndexMap; | ||
use serde_json::Value as JsonValue; | ||
#[cfg(feature = "sqlite")] | ||
use sqlx::sqlite::SqliteConnectOptions; | ||
#[cfg(any(feature = "sqlite", feature = "mysql", feature = "postgres"))] | ||
use sqlx::{migrate::MigrateDatabase, Column, Executor, Pool, Row}; | ||
#[cfg(any(feature = "sqlite", feature = "mysql", feature = "postgres"))] | ||
|
@@ -33,6 +37,40 @@ pub enum DbPool { | |
None, | ||
} | ||
|
||
#[cfg(feature = "sqlite")] | ||
#[derive(serde::Serialize, serde::Deserialize, Debug)] | ||
pub struct SqliteOptions { | ||
pub pragmas: HashMap<String, String>, | ||
} | ||
|
||
#[cfg(feature = "sqlite")] | ||
impl Default for SqliteOptions { | ||
fn default() -> Self { | ||
Self { | ||
pragmas: HashMap::new(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(serde::Serialize, serde::Deserialize, Debug)] | ||
pub struct ConnectionOptions { | ||
#[cfg(feature = "sqlite")] | ||
pub sqlite: Option<SqliteOptions>, | ||
// #[cfg(feature = "mysql")] | ||
// mysql: Option<MySqlOptions>, | ||
// #[cfg(feature = "postgres")] | ||
// postgres: Option<PostgresOptions>, | ||
} | ||
|
||
impl Default for ConnectionOptions { | ||
fn default() -> Self { | ||
Self { | ||
#[cfg(feature = "sqlite")] | ||
sqlite: None, | ||
} | ||
} | ||
} | ||
|
||
// public methods | ||
/* impl DbPool { | ||
/// Get the inner Sqlite Pool. Returns None for MySql and Postgres pools. | ||
|
@@ -68,6 +106,7 @@ impl DbPool { | |
pub(crate) async fn connect<R: Runtime>( | ||
conn_url: &str, | ||
_app: &AppHandle<R>, | ||
options: Option<ConnectionOptions>, | ||
) -> Result<Self, crate::Error> { | ||
match conn_url | ||
.split_once(':') | ||
|
@@ -82,13 +121,23 @@ impl DbPool { | |
.expect("No App config path was found!"); | ||
|
||
create_dir_all(&app_path).expect("Couldn't create app config dir"); | ||
|
||
let conn_url = &path_mapper(app_path, conn_url); | ||
let filename = conn_url.split_once(':').unwrap().1; | ||
|
||
if !Sqlite::database_exists(conn_url).await.unwrap_or(false) { | ||
Sqlite::create_database(conn_url).await?; | ||
let mut sqlite_options = SqliteConnectOptions::new() | ||
.filename(filename) | ||
.create_if_missing(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Load extensions with entry points. |
||
// Apply pragmas if provided | ||
if let Some(conn_opts) = options { | ||
if let Some(sqlite_opts) = conn_opts.sqlite { | ||
for (pragma_name, pragma_value) in sqlite_opts.pragmas { | ||
sqlite_options = sqlite_options.pragma(pragma_name, pragma_value); | ||
} | ||
} | ||
} | ||
Ok(Self::Sqlite(Pool::connect(conn_url).await?)) | ||
|
||
// Connect with options (which includes create_if_missing) | ||
Ok(Self::Sqlite(Pool::connect_with(sqlite_options).await?)) | ||
} | ||
#[cfg(feature = "mysql")] | ||
"mysql" => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you are already adding
options
. Could you add support for extensions, to fix #1705