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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#![deny(warnings)]
extern crate r2d2;
extern crate rusqlite;
use rusqlite::{Connection, Error, OpenFlags};
use std::path::{Path, PathBuf};
enum ConnectionConfig {
File(PathBuf, OpenFlags),
}
pub struct SqliteConnectionManager {
config: ConnectionConfig,
}
impl SqliteConnectionManager {
pub fn new<P: AsRef<Path>>(path: P) -> Self {
Self::new_with_flags(path, OpenFlags::default())
}
pub fn new_with_flags<P: AsRef<Path>>(path: P, flags: OpenFlags) -> Self {
SqliteConnectionManager {
config: ConnectionConfig::File(path.as_ref().to_path_buf(), flags),
}
}
}
impl r2d2::ManageConnection for SqliteConnectionManager {
type Connection = Connection;
type Error = rusqlite::Error;
fn connect(&self) -> Result<Connection, Error> {
match self.config {
ConnectionConfig::File(ref path, flags) => Connection::open_with_flags(path, flags),
}
.map_err(Into::into)
}
fn is_valid(&self, conn: &mut Connection) -> Result<(), Error> {
conn.execute_batch("").map_err(Into::into)
}
fn has_broken(&self, _: &mut Connection) -> bool {
false
}
}