diff --git a/src/database/mod.rs b/src/database/mod.rs index ea00f742..879f7a37 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -13,6 +13,7 @@ pub use db_connection::*; #[cfg(feature = "mock")] pub use mock::*; pub use statement::*; +use std::borrow::Cow; pub use stream::*; use tracing::instrument; pub use transaction::*; @@ -24,7 +25,7 @@ use crate::DbErr; pub struct Database; /// Defines the configuration options of a database -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct ConnectOptions { /// The URI of the database pub(crate) url: String, @@ -41,6 +42,8 @@ pub struct ConnectOptions { pub(crate) max_lifetime: Option, /// Enable SQLx statement logging pub(crate) sqlx_logging: bool, + /// set sqlcipher key + pub(crate) sqlcipher_key: Option>, } impl Database { @@ -104,6 +107,7 @@ impl ConnectOptions { idle_timeout: None, max_lifetime: None, sqlx_logging: true, + sqlcipher_key: None, } } @@ -206,4 +210,13 @@ impl ConnectOptions { pub fn get_sqlx_logging(&self) -> bool { self.sqlx_logging } + + /// set key for sqlcipher + pub fn sqlcipher_key(&mut self, value: T) -> &mut Self + where + T: Into>, + { + self.sqlcipher_key = Some(value.into()); + self + } } diff --git a/src/driver/sqlx_sqlite.rs b/src/driver/sqlx_sqlite.rs index 9940b7fa..ae3edad2 100644 --- a/src/driver/sqlx_sqlite.rs +++ b/src/driver/sqlx_sqlite.rs @@ -47,6 +47,9 @@ impl SqlxSqliteConnector { .url .parse::() .map_err(|e| DbErr::Conn(e.to_string()))?; + if options.sqlcipher_key.is_some() { + opt = opt.pragma("key", options.sqlcipher_key.clone().unwrap()); + } if !options.sqlx_logging { use sqlx::ConnectOptions; opt.disable_statement_logging();