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