Merge pull request #792 from cache-missing/master

 feat(sqlite3): support set sqlcipher key
This commit is contained in:
Chris Tsang 2022-06-14 19:16:10 +08:00 committed by GitHub
commit 5b5aa07227
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -13,6 +13,7 @@ pub use db_connection::*;
#[cfg(feature = "mock")] #[cfg(feature = "mock")]
pub use mock::*; pub use mock::*;
pub use statement::*; pub use statement::*;
use std::borrow::Cow;
pub use stream::*; pub use stream::*;
use tracing::instrument; use tracing::instrument;
pub use transaction::*; pub use transaction::*;
@ -24,7 +25,7 @@ use crate::DbErr;
pub struct Database; pub struct Database;
/// Defines the configuration options of a database /// Defines the configuration options of a database
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct ConnectOptions { pub struct ConnectOptions {
/// The URI of the database /// The URI of the database
pub(crate) url: String, pub(crate) url: String,
@ -41,6 +42,8 @@ pub struct ConnectOptions {
pub(crate) max_lifetime: Option<Duration>, pub(crate) max_lifetime: Option<Duration>,
/// Enable SQLx statement logging /// Enable SQLx statement logging
pub(crate) sqlx_logging: bool, pub(crate) sqlx_logging: bool,
/// set sqlcipher key
pub(crate) sqlcipher_key: Option<Cow<'static, str>>,
} }
impl Database { impl Database {
@ -104,6 +107,7 @@ impl ConnectOptions {
idle_timeout: None, idle_timeout: None,
max_lifetime: None, max_lifetime: None,
sqlx_logging: true, sqlx_logging: true,
sqlcipher_key: None,
} }
} }
@ -206,4 +210,13 @@ impl ConnectOptions {
pub fn get_sqlx_logging(&self) -> bool { pub fn get_sqlx_logging(&self) -> bool {
self.sqlx_logging self.sqlx_logging
} }
/// set key for sqlcipher
pub fn sqlcipher_key<T>(&mut self, value: T) -> &mut Self
where
T: Into<Cow<'static, str>>,
{
self.sqlcipher_key = Some(value.into());
self
}
} }

View File

@ -47,6 +47,9 @@ impl SqlxSqliteConnector {
.url .url
.parse::<SqliteConnectOptions>() .parse::<SqliteConnectOptions>()
.map_err(|e| DbErr::Conn(e.to_string()))?; .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 { if !options.sqlx_logging {
use sqlx::ConnectOptions; use sqlx::ConnectOptions;
opt.disable_statement_logging(); opt.disable_statement_logging();