1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Copyright by contributors to this project.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

use std::path::{Path, PathBuf};

use rusqlite::Connection;

use crate::SqLiteDataStorageError;

#[cfg(any(feature = "sqlcipher", feature = "sqlcipher-bundled"))]
pub use crate::cipher::*;

/// Trait that helps to set up a SQLite database connection.
pub trait ConnectionStrategy {
    /// Connect to the SQLite database.
    fn make_connection(&self) -> Result<Connection, SqLiteDataStorageError>;
}

/// Connection strategy that creates an in-memory database.
pub struct MemoryStrategy;

impl ConnectionStrategy for MemoryStrategy {
    fn make_connection(&self) -> Result<Connection, SqLiteDataStorageError> {
        Connection::open_in_memory().map_err(|e| SqLiteDataStorageError::SqlEngineError(e.into()))
    }
}

/// Connection strategy that connects to a database based on a file path.
pub struct FileConnectionStrategy {
    db_path: PathBuf,
}

impl FileConnectionStrategy {
    pub fn new(db_path: &Path) -> FileConnectionStrategy {
        FileConnectionStrategy {
            db_path: db_path.to_owned(),
        }
    }
}

impl ConnectionStrategy for FileConnectionStrategy {
    fn make_connection(&self) -> Result<Connection, SqLiteDataStorageError> {
        Connection::open(&self.db_path)
            .map_err(|e| SqLiteDataStorageError::SqlEngineError(e.into()))
    }
}