Debian Codegraph @

third_party/rust/mls-rs-provider-sqlite/src/connection_strategy.rs

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // Copyright by contributors to this project.
3 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
4
5 use std::path::{Path, PathBuf};
6
7 use rusqlite::Connection;
8
9 use crate::SqLiteDataStorageError;
10
11 #[cfg(any(feature = "sqlcipher", feature = "sqlcipher-bundled"))]
12 pub use crate::cipher::*;
13
14 /// Trait that helps to set up a SQLite database connection.
15 pub trait ConnectionStrategy {
16 /// Connect to the SQLite database.
17 fn make_connection(&self) -> Result<Connection, SqLiteDataStorageError>;
18 }
19
20 /// Connection strategy that creates an in-memory database.
21 pub struct MemoryStrategy;
22
23 impl ConnectionStrategy for MemoryStrategy {
24 fn make_connection(&self) -> Result<Connection, SqLiteDataStorageError> {
25 Connection::open_in_memory().map_err(|e| SqLiteDataStorageError::SqlEngineError(e.into()))
26 }
27 }
28
29 /// Connection strategy that connects to a database based on a file path.
30 pub struct FileConnectionStrategy {
31 db_path: PathBuf,
32 }
33
34 impl FileConnectionStrategy {
35 pub fn new(db_path: &Path) -> FileConnectionStrategy {
36 FileConnectionStrategy {
37 db_path: db_path.to_owned(),
38 }
39 }
40 }
41
42 impl ConnectionStrategy for FileConnectionStrategy {
43 fn make_connection(&self) -> Result<Connection, SqLiteDataStorageError> {
44 Connection::open(&self.db_path)
45 .map_err(|e| SqLiteDataStorageError::SqlEngineError(e.into()))
46 }
47 }