sql_support/
lib.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5#![allow(unknown_lints)]
6#![warn(rust_2018_idioms)]
7
8//! A crate with various sql/sqlcipher helpers.
9
10mod conn_ext;
11pub mod debug_tools;
12
13mod each_chunk;
14mod lazy;
15mod maintenance;
16mod maybe_cached;
17pub mod open_database;
18mod repeat;
19
20pub use conn_ext::*;
21pub use each_chunk::*;
22pub use lazy::*;
23pub use maintenance::run_maintenance;
24pub use maybe_cached::*;
25pub use repeat::*;
26
27// reexport logging helpers.
28use error_support::{debug, info, warn};
29
30/// In PRAGMA foo='bar', `'bar'` must be a constant string (it cannot be a
31/// bound parameter), so we need to escape manually. According to
32/// <https://www.sqlite.org/faq.html>, the only character that must be escaped is
33/// the single quote, which is escaped by placing two single quotes in a row.
34pub fn escape_string_for_pragma(s: &str) -> String {
35    s.replace('\'', "''")
36}
37
38/// Default SQLite pragmas
39///
40/// Most components should just stick to these defaults.
41pub fn setup_sqlite_defaults(conn: &rusqlite::Connection) -> rusqlite::Result<()> {
42    conn.execute_batch(
43        "
44        PRAGMA temp_store = 2;
45        PRAGMA journal_mode = WAL;
46        ",
47    )?;
48    let page_size: usize = conn.query_row("PRAGMA page_size", (), |row| row.get(0))?;
49    // Aim to checkpoint at 512Kb
50    let target_checkpoint_size = 2usize.pow(19);
51    // Truncate the journal if it more than 3x larger than the target size
52    let journal_size_limit = target_checkpoint_size * 3;
53    conn.execute_batch(&format!(
54        "
55        PRAGMA wal_autocheckpoint = {};
56        PRAGMA journal_size_limit = {};
57        ",
58        target_checkpoint_size / page_size,
59        journal_size_limit,
60    ))?;
61
62    Ok(())
63}
64
65#[cfg(test)]
66mod test {
67    use super::*;
68    #[test]
69    fn test_escape_string_for_pragma() {
70        assert_eq!(escape_string_for_pragma("foobar"), "foobar");
71        assert_eq!(escape_string_for_pragma("'foo'bar'"), "''foo''bar''");
72        assert_eq!(escape_string_for_pragma("''"), "''''");
73    }
74
75    #[test]
76    fn test_sqlite_defaults() {
77        let conn = rusqlite::Connection::open_in_memory().unwrap();
78        // Simulate a default page size,
79        // On Mobile, these are set by the OS.  On Desktop, these are set by the build system when
80        // we compile SQLite.
81        conn.execute("PRAGMA page_size = 8192", ()).unwrap();
82        setup_sqlite_defaults(&conn).unwrap();
83        let autocheckpoint: usize = conn
84            .query_row("PRAGMA wal_autocheckpoint", (), |row| row.get(0))
85            .unwrap();
86        // We should aim to auto-checkpoint at 512kb, which is 64 pages when the page size is 8k
87        assert_eq!(autocheckpoint, 64);
88        // We could also check the journal size limit, but that's harder to query with a pragma.
89        // If we go the math right once, we should get it for the other case.
90    }
91}