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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::error::*;
use crate::schema;
use rusqlite::{Connection, OpenFlags};
use std::{
ops::{Deref, DerefMut},
path::{Path, PathBuf},
};
use url::Url;
pub struct AutofillDb {
pub writer: Connection,
}
impl AutofillDb {
pub fn new(db_path: impl AsRef<Path>) -> Result<Self> {
let db_path = normalize_path(db_path)?;
Self::new_named(db_path)
}
#[cfg(test)]
pub fn new_memory(db_path: &str) -> Result<Self> {
let name = PathBuf::from(format!("file:{}?mode=memory&cache=shared", db_path));
Self::new_named(name)
}
#[allow(dead_code)]
fn new_named(db_path: PathBuf) -> Result<Self> {
let flags = OpenFlags::SQLITE_OPEN_NO_MUTEX
| OpenFlags::SQLITE_OPEN_URI
| OpenFlags::SQLITE_OPEN_CREATE
| OpenFlags::SQLITE_OPEN_READ_WRITE;
let conn = Connection::open_with_flags(db_path, flags)?;
#[allow(dead_code)]
init_sql_connection(&conn, true)?;
Ok(Self { writer: conn })
}
}
impl Deref for AutofillDb {
type Target = Connection;
fn deref(&self) -> &Self::Target {
&self.writer
}
}
impl DerefMut for AutofillDb {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.writer
}
}
fn init_sql_connection(conn: &Connection, is_writable: bool) -> Result<()> {
define_functions(&conn)?;
conn.set_prepared_statement_cache_capacity(128);
if is_writable {
let tx = conn.unchecked_transaction()?;
schema::init(&conn)?;
tx.commit()?;
};
Ok(())
}
fn unurl_path(p: impl AsRef<Path>) -> PathBuf {
p.as_ref()
.to_str()
.and_then(|s| Url::parse(s).ok())
.and_then(|u| {
if u.scheme() == "file" {
u.to_file_path().ok()
} else {
None
}
})
.unwrap_or_else(|| p.as_ref().to_owned())
}
fn normalize_path(p: impl AsRef<Path>) -> Result<PathBuf> {
let path = unurl_path(p);
if let Ok(canonical) = path.canonicalize() {
return Ok(canonical);
}
let file_name = path
.file_name()
.ok_or_else(|| ErrorKind::IllegalDatabasePath(path.clone()))?;
let parent = path
.parent()
.ok_or_else(|| ErrorKind::IllegalDatabasePath(path.clone()))?;
let mut canonical = parent.canonicalize()?;
canonical.push(file_name);
Ok(canonical)
}
#[allow(dead_code)]
fn define_functions(c: &Connection) -> Result<()> {
use rusqlite::functions::FunctionFlags;
c.create_scalar_function(
"generate_guid",
0,
FunctionFlags::SQLITE_UTF8,
sql_fns::generate_guid,
)?;
Ok(())
}
pub(crate) mod sql_fns {
use rusqlite::{functions::Context, Result};
use sync_guid::Guid as SyncGuid;
#[inline(never)]
#[allow(dead_code)]
pub fn generate_guid(_ctx: &Context<'_>) -> Result<SyncGuid> {
Ok(SyncGuid::random())
}
}
#[cfg(test)]
pub mod test {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
static ATOMIC_COUNTER: AtomicUsize = AtomicUsize::new(0);
pub fn new_mem_db() -> AutofillDb {
let _ = env_logger::try_init();
let counter = ATOMIC_COUNTER.fetch_add(1, Ordering::Relaxed);
AutofillDb::new_memory(&format!("test_autofill-api-{}", counter))
.expect("should get an API")
}
}