From 73af72200ae39b9489862422f362f587dc3c34ea Mon Sep 17 00:00:00 2001 From: cache-missing Date: Mon, 13 Jun 2022 08:41:41 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(sqlite3):=20support=20set=20sq?= =?UTF-8?q?lcipher=20key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/database/mod.rs | 15 ++++++++++++++- src/driver/sqlx_sqlite.rs | 3 +++ 2 files changed, 17 insertions(+), 1 deletion(-) 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();