Debian Codegraph @

third_party/rust/sql-support/src/lib.rs

raw
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
10 mod conn_ext;
11
12 // XXX - temporarily disable our debug_tools, to avoid pulling in:
13 // prettytable-rs = { version = "0.10", optional = true }
14 // while vendoring into m-c :(
15 pub mod debug_tools {
16 pub fn define_debug_functions(_c: &rusqlite::Connection) -> rusqlite::Result<()> {
17 Ok(())
18 }
19 }
20
21 mod each_chunk;
22 mod lazy;
23 mod maybe_cached;
24 pub mod open_database;
25 mod repeat;
26
27 pub use conn_ext::*;
28 pub use each_chunk::*;
29 pub use lazy::*;
30 pub use maybe_cached::*;
31 pub use repeat::*;
32
33 // reexport logging helpers.
34 use error_support::{debug, info, warn};
35
36 /// In PRAGMA foo='bar', `'bar'` must be a constant string (it cannot be a
37 /// bound parameter), so we need to escape manually. According to
38 /// <https://www.sqlite.org/faq.html>, the only character that must be escaped is
39 /// the single quote, which is escaped by placing two single quotes in a row.
40 pub fn escape_string_for_pragma(s: &str) -> String {
41 s.replace('\'', "''")
42 }
43
44 /// Default SQLite pragmas
45 ///
46 /// Most components should just stick to these defaults.
47 pub fn setup_sqlite_defaults(conn: &rusqlite::Connection) -> rusqlite::Result<()> {
48 conn.execute_batch(
49 "
50 PRAGMA temp_store = 2;
51 PRAGMA journal_mode = WAL;
52 ",
53 )?;
54 let page_size: usize = conn.query_row("PRAGMA page_size", (), |row| row.get(0))?;
55 // Aim to checkpoint at 512Kb
56 let target_checkpoint_size = 2usize.pow(19);
57 // Truncate the journal if it more than 3x larger than the target size
58 let journal_size_limit = target_checkpoint_size * 3;
59 conn.execute_batch(&format!(
60 "
61 PRAGMA wal_autocheckpoint = {};
62 PRAGMA journal_size_limit = {};
63 ",
64 target_checkpoint_size / page_size,
65 journal_size_limit,
66 ))?;
67
68 Ok(())
69 }
70
71 #[cfg(test)]
72 mod test {
73 use super::*;
74 #[test]
75 fn test_escape_string_for_pragma() {
76 assert_eq!(escape_string_for_pragma("foobar"), "foobar");
77 assert_eq!(escape_string_for_pragma("'foo'bar'"), "''foo''bar''");
78 assert_eq!(escape_string_for_pragma("''"), "''''");
79 }
80
81 #[test]
82 fn test_sqlite_defaults() {
83 let conn = rusqlite::Connection::open_in_memory().unwrap();
84 // Simulate a default page size,
85 // On Mobile, these are set by the OS. On Desktop, these are set by the build system when
86 // we compile SQLite.
87 conn.execute("PRAGMA page_size = 8192", ()).unwrap();
88 setup_sqlite_defaults(&conn).unwrap();
89 let autocheckpoint: usize = conn
90 .query_row("PRAGMA wal_autocheckpoint", (), |row| row.get(0))
91 .unwrap();
92 // We should aim to auto-checkpoint at 512kb, which is 64 pages when the page size is 8k
93 assert_eq!(autocheckpoint, 64);
94 // We could also check the journal size limit, but that's harder to query with a pragma.
95 // If we go the math right once, we should get it for the other case.
96 }
97 }