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
use rusqlite::{Connection, OpenFlags};
use crate::error::StorageError;
use crate::schema;
pub struct Database {
connection: Connection,
}
impl Database {
/// Open or create database at the given path.
pub fn open(path: &str) -> Result<Self, StorageError> {
let connection = Connection::open(path)?;
schema::apply_schema(&connection)?;
Ok(Self { connection })
}
/// Open an existing database in read-only mode.
///
/// Skips schema application: the caller must ensure the schema already exists
/// (a read-only connection cannot run `CREATE TABLE` or set `PRAGMA journal_mode`).
/// Intended for defense-in-depth scenarios where the source database must not
/// be mutated (e.g. `engram migrate`).
pub fn open_read_only(path: &str) -> Result<Self, StorageError> {
let connection = Connection::open_with_flags(
path,
OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_URI,
)?;
Ok(Self { connection })
}
/// Create an in-memory database (for testing).
pub fn in_memory() -> Result<Self, StorageError> {
Self::open(":memory:")
}
pub fn connection(&self) -> &Connection {
&self.connection
}
}