feat(sqlite3): support set sqlcipher key

This commit is contained in:
cache-missing 2022-06-13 08:41:41 +08:00 committed by kaihaojiang
parent 4301383b40
commit 73af72200a
2 changed files with 17 additions and 1 deletions

View File

@ -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<Duration>,
/// Enable SQLx statement logging
pub(crate) sqlx_logging: bool,
/// set sqlcipher key
pub(crate) sqlcipher_key: Option<Cow<'static, str>>,
}
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<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
.parse::<SqliteConnectOptions>()
.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();