Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod wallet;

use dash_sdk::dpp::dashcore::Network;
use rusqlite::{Connection, Params};
use std::sync::Mutex;
use std::sync::{Arc, Mutex};

/// Error indicating a corrupted data blob in the database.
///
Expand All @@ -41,17 +41,25 @@ impl From<CorruptedBlobError> for rusqlite::Error {

#[derive(Debug)]
pub struct Database {
conn: Mutex<Connection>,
conn: Arc<Mutex<Connection>>,
}

impl Database {
pub fn new<P: AsRef<std::path::Path>>(path: P) -> rusqlite::Result<Self> {
let conn = Connection::open(path)?;
Ok(Self {
conn: Mutex::new(conn),
conn: Arc::new(Mutex::new(conn)),
})
}

/// Get a shared reference to the underlying connection.
///
/// Used by `ClientPersistentCommitmentTree` to share the same SQLite
/// connection for the shielded commitment tree tables.
pub fn shared_connection(&self) -> Arc<Mutex<Connection>> {
self.conn.clone()
}

pub fn execute<P: Params>(&self, sql: &str, params: P) -> rusqlite::Result<usize> {
let conn = self.conn.lock().unwrap();
conn.execute(sql, params)
Expand Down
Loading