rusqlite/
config.rs

1//! Configure database connections
2
3use std::os::raw::c_int;
4
5use crate::error::check;
6use crate::ffi;
7use crate::{Connection, Result};
8
9/// Database Connection Configuration Options
10/// See [Database Connection Configuration Options](https://sqlite.org/c3ref/c_dbconfig_enable_fkey.html) for details.
11#[repr(i32)]
12#[derive(Copy, Clone, Debug)]
13#[allow(non_snake_case, non_camel_case_types)]
14#[non_exhaustive]
15#[allow(clippy::upper_case_acronyms)]
16pub enum DbConfig {
17    //SQLITE_DBCONFIG_MAINDBNAME = 1000, /* const char* */
18    //SQLITE_DBCONFIG_LOOKASIDE = 1001,  /* void* int int */
19    /// Enable or disable the enforcement of foreign key constraints.
20    SQLITE_DBCONFIG_ENABLE_FKEY = ffi::SQLITE_DBCONFIG_ENABLE_FKEY,
21    /// Enable or disable triggers.
22    SQLITE_DBCONFIG_ENABLE_TRIGGER = ffi::SQLITE_DBCONFIG_ENABLE_TRIGGER,
23    /// Enable or disable the fts3_tokenizer() function which is part of the
24    /// FTS3 full-text search engine extension.
25    SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER = ffi::SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, // 3.12.0
26    //SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION = 1005,
27    /// In WAL mode, enable or disable the checkpoint operation before closing
28    /// the connection.
29    SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE = 1006, // 3.16.2
30    /// Activates or deactivates the query planner stability guarantee (QPSG).
31    SQLITE_DBCONFIG_ENABLE_QPSG = 1007, // 3.20.0
32    /// Includes or excludes output for any operations performed by trigger
33    /// programs from the output of EXPLAIN QUERY PLAN commands.
34    SQLITE_DBCONFIG_TRIGGER_EQP = 1008, // 3.22.0
35    /// Activates or deactivates the "reset" flag for a database connection.
36    /// Run VACUUM with this flag set to reset the database.
37    SQLITE_DBCONFIG_RESET_DATABASE = 1009, // 3.24.0
38    /// Activates or deactivates the "defensive" flag for a database connection.
39    SQLITE_DBCONFIG_DEFENSIVE = 1010, // 3.26.0
40    /// Activates or deactivates the "writable_schema" flag.
41    #[cfg(feature = "modern_sqlite")]
42    SQLITE_DBCONFIG_WRITABLE_SCHEMA = 1011, // 3.28.0
43    /// Activates or deactivates the legacy behavior of the ALTER TABLE RENAME
44    /// command.
45    #[cfg(feature = "modern_sqlite")]
46    SQLITE_DBCONFIG_LEGACY_ALTER_TABLE = 1012, // 3.29
47    /// Activates or deactivates the legacy double-quoted string literal
48    /// misfeature for DML statements only.
49    #[cfg(feature = "modern_sqlite")]
50    SQLITE_DBCONFIG_DQS_DML = 1013, // 3.29.0
51    /// Activates or deactivates the legacy double-quoted string literal
52    /// misfeature for DDL statements.
53    #[cfg(feature = "modern_sqlite")]
54    SQLITE_DBCONFIG_DQS_DDL = 1014, // 3.29.0
55    /// Enable or disable views.
56    #[cfg(feature = "modern_sqlite")]
57    SQLITE_DBCONFIG_ENABLE_VIEW = 1015, // 3.30.0
58    /// Activates or deactivates the legacy file format flag.
59    #[cfg(feature = "modern_sqlite")]
60    SQLITE_DBCONFIG_LEGACY_FILE_FORMAT = 1016, // 3.31.0
61    /// Tells SQLite to assume that database schemas (the contents of the
62    /// sqlite_master tables) are untainted by malicious content.
63    #[cfg(feature = "modern_sqlite")]
64    SQLITE_DBCONFIG_TRUSTED_SCHEMA = 1017, // 3.31.0
65    /// Sets or clears a flag that enables collection of the
66    /// sqlite3_stmt_scanstatus_v2() statistics
67    #[cfg(feature = "modern_sqlite")]
68    SQLITE_DBCONFIG_STMT_SCANSTATUS = 1018, // 3.42.0
69    /// Changes the default order in which tables and indexes are scanned
70    #[cfg(feature = "modern_sqlite")]
71    SQLITE_DBCONFIG_REVERSE_SCANORDER = 1019, // 3.42.0
72}
73
74impl Connection {
75    /// Returns the current value of a `config`.
76    ///
77    /// - `SQLITE_DBCONFIG_ENABLE_FKEY`: return `false` or `true` to indicate
78    ///   whether FK enforcement is off or on
79    /// - `SQLITE_DBCONFIG_ENABLE_TRIGGER`: return `false` or `true` to indicate
80    ///   whether triggers are disabled or enabled
81    /// - `SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER`: return `false` or `true` to
82    ///   indicate whether `fts3_tokenizer` are disabled or enabled
83    /// - `SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE`: return `false` to indicate
84    ///   checkpoints-on-close are not disabled or `true` if they are
85    /// - `SQLITE_DBCONFIG_ENABLE_QPSG`: return `false` or `true` to indicate
86    ///   whether the QPSG is disabled or enabled
87    /// - `SQLITE_DBCONFIG_TRIGGER_EQP`: return `false` to indicate
88    ///   output-for-trigger are not disabled or `true` if it is
89    #[inline]
90    pub fn db_config(&self, config: DbConfig) -> Result<bool> {
91        let c = self.db.borrow();
92        unsafe {
93            let mut val = 0;
94            check(ffi::sqlite3_db_config(
95                c.db(),
96                config as c_int,
97                -1,
98                &mut val,
99            ))?;
100            Ok(val != 0)
101        }
102    }
103
104    /// Make configuration changes to a database connection
105    ///
106    /// - `SQLITE_DBCONFIG_ENABLE_FKEY`: `false` to disable FK enforcement,
107    ///   `true` to enable FK enforcement
108    /// - `SQLITE_DBCONFIG_ENABLE_TRIGGER`: `false` to disable triggers, `true`
109    ///   to enable triggers
110    /// - `SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER`: `false` to disable
111    ///   `fts3_tokenizer()`, `true` to enable `fts3_tokenizer()`
112    /// - `SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE`: `false` (the default) to enable
113    ///   checkpoints-on-close, `true` to disable them
114    /// - `SQLITE_DBCONFIG_ENABLE_QPSG`: `false` to disable the QPSG, `true` to
115    ///   enable QPSG
116    /// - `SQLITE_DBCONFIG_TRIGGER_EQP`: `false` to disable output for trigger
117    ///   programs, `true` to enable it
118    #[inline]
119    pub fn set_db_config(&self, config: DbConfig, new_val: bool) -> Result<bool> {
120        let c = self.db.borrow_mut();
121        unsafe {
122            let mut val = 0;
123            check(ffi::sqlite3_db_config(
124                c.db(),
125                config as c_int,
126                new_val as c_int,
127                &mut val,
128            ))?;
129            Ok(val != 0)
130        }
131    }
132}
133
134#[cfg(test)]
135mod test {
136    use super::DbConfig;
137    use crate::{Connection, Result};
138
139    #[test]
140    fn test_db_config() -> Result<()> {
141        let db = Connection::open_in_memory()?;
142
143        let opposite = !db.db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_FKEY)?;
144        assert_eq!(
145            db.set_db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_FKEY, opposite),
146            Ok(opposite)
147        );
148        assert_eq!(
149            db.db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_FKEY),
150            Ok(opposite)
151        );
152
153        let opposite = !db.db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_TRIGGER)?;
154        assert_eq!(
155            db.set_db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_TRIGGER, opposite),
156            Ok(opposite)
157        );
158        assert_eq!(
159            db.db_config(DbConfig::SQLITE_DBCONFIG_ENABLE_TRIGGER),
160            Ok(opposite)
161        );
162        Ok(())
163    }
164}