-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathr2d2_sqlite.rs
More file actions
39 lines (31 loc) · 1007 Bytes
/
r2d2_sqlite.rs
File metadata and controls
39 lines (31 loc) · 1007 Bytes
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
use rusqlite::{Connection, OpenFlags, Result};
use std::path::{Path, PathBuf};
use uuid::Uuid;
pub struct SqliteConnectionManager {
source: PathBuf,
flags: OpenFlags,
}
impl SqliteConnectionManager {
pub fn file(path: impl AsRef<Path>) -> Self {
Self { source: path.as_ref().to_path_buf(), flags: OpenFlags::default() }
}
pub fn memory() -> Self {
Self { source: format!("file:{}?mode=memory&cache=shared", Uuid::new_v4()).into(), flags: OpenFlags::default() }
}
pub fn with_flags(self, flags: OpenFlags) -> Self {
Self { flags, ..self }
}
}
impl r2d2::ManageConnection for SqliteConnectionManager {
type Connection = Connection;
type Error = rusqlite::Error;
fn connect(&self) -> Result<Connection> {
Connection::open_with_flags(&self.source, self.flags)
}
fn is_valid(&self, _conn: &mut Connection) -> Result<()> {
Ok(())
}
fn has_broken(&self, _: &mut Connection) -> bool {
false
}
}