| 1 |
//! Commit, Data Change and Rollback Notification Callbacks |
| 2 |
#![expect(non_camel_case_types)] |
| 3 |
|
| 4 |
use std::os::raw::{c_char, c_int, c_void}; |
| 5 |
use std::panic::catch_unwind; |
| 6 |
use std::ptr; |
| 7 |
|
| 8 |
use crate::ffi; |
| 9 |
|
| 10 |
use crate::{error::decode_result_raw, Connection, DatabaseName, InnerConnection, Result}; |
| 11 |
|
| 12 |
#[cfg(feature = "preupdate_hook")] |
| 13 |
pub use preupdate_hook::*; |
| 14 |
|
| 15 |
#[cfg(feature = "preupdate_hook")] |
| 16 |
mod preupdate_hook; |
| 17 |
|
| 18 |
/// Action Codes |
| 19 |
#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 20 |
#[repr(i32)] |
| 21 |
#[non_exhaustive] |
| 22 |
pub enum Action { |
| 23 |
/// Unsupported / unexpected action |
| 24 |
UNKNOWN = -1, |
| 25 |
/// DELETE command |
| 26 |
SQLITE_DELETE = ffi::SQLITE_DELETE, |
| 27 |
/// INSERT command |
| 28 |
SQLITE_INSERT = ffi::SQLITE_INSERT, |
| 29 |
/// UPDATE command |
| 30 |
SQLITE_UPDATE = ffi::SQLITE_UPDATE, |
| 31 |
} |
| 32 |
|
| 33 |
impl From<i32> for Action { |
| 34 |
#[inline] |
| 35 |
fn from(code: i32) -> Self { |
| 36 |
match code { |
| 37 |
ffi::SQLITE_DELETE => Self::SQLITE_DELETE, |
| 38 |
ffi::SQLITE_INSERT => Self::SQLITE_INSERT, |
| 39 |
ffi::SQLITE_UPDATE => Self::SQLITE_UPDATE, |
| 40 |
_ => Self::UNKNOWN, |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/// The context received by an authorizer hook. |
| 46 |
/// |
| 47 |
/// See <https://sqlite.org/c3ref/set_authorizer.html> for more info. |
| 48 |
#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 49 |
pub struct AuthContext<'c> { |
| 50 |
/// The action to be authorized. |
| 51 |
pub action: AuthAction<'c>, |
| 52 |
|
| 53 |
/// The database name, if applicable. |
| 54 |
pub database_name: Option<&'c str>, |
| 55 |
|
| 56 |
/// The inner-most trigger or view responsible for the access attempt. |
| 57 |
/// `None` if the access attempt was made by top-level SQL code. |
| 58 |
pub accessor: Option<&'c str>, |
| 59 |
} |
| 60 |
|
| 61 |
/// Actions and arguments found within a statement during |
| 62 |
/// preparation. |
| 63 |
/// |
| 64 |
/// See <https://sqlite.org/c3ref/c_alter_table.html> for more info. |
| 65 |
#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 66 |
#[non_exhaustive] |
| 67 |
#[allow(missing_docs)] |
| 68 |
pub enum AuthAction<'c> { |
| 69 |
/// This variant is not normally produced by SQLite. You may encounter it |
| 70 |
// if you're using a different version than what's supported by this library. |
| 71 |
Unknown { |
| 72 |
/// The unknown authorization action code. |
| 73 |
code: i32, |
| 74 |
/// The third arg to the authorizer callback. |
| 75 |
arg1: Option<&'c str>, |
| 76 |
/// The fourth arg to the authorizer callback. |
| 77 |
arg2: Option<&'c str>, |
| 78 |
}, |
| 79 |
CreateIndex { |
| 80 |
index_name: &'c str, |
| 81 |
table_name: &'c str, |
| 82 |
}, |
| 83 |
CreateTable { |
| 84 |
table_name: &'c str, |
| 85 |
}, |
| 86 |
CreateTempIndex { |
| 87 |
index_name: &'c str, |
| 88 |
table_name: &'c str, |
| 89 |
}, |
| 90 |
CreateTempTable { |
| 91 |
table_name: &'c str, |
| 92 |
}, |
| 93 |
CreateTempTrigger { |
| 94 |
trigger_name: &'c str, |
| 95 |
table_name: &'c str, |
| 96 |
}, |
| 97 |
CreateTempView { |
| 98 |
view_name: &'c str, |
| 99 |
}, |
| 100 |
CreateTrigger { |
| 101 |
trigger_name: &'c str, |
| 102 |
table_name: &'c str, |
| 103 |
}, |
| 104 |
CreateView { |
| 105 |
view_name: &'c str, |
| 106 |
}, |
| 107 |
Delete { |
| 108 |
table_name: &'c str, |
| 109 |
}, |
| 110 |
DropIndex { |
| 111 |
index_name: &'c str, |
| 112 |
table_name: &'c str, |
| 113 |
}, |
| 114 |
DropTable { |
| 115 |
table_name: &'c str, |
| 116 |
}, |
| 117 |
DropTempIndex { |
| 118 |
index_name: &'c str, |
| 119 |
table_name: &'c str, |
| 120 |
}, |
| 121 |
DropTempTable { |
| 122 |
table_name: &'c str, |
| 123 |
}, |
| 124 |
DropTempTrigger { |
| 125 |
trigger_name: &'c str, |
| 126 |
table_name: &'c str, |
| 127 |
}, |
| 128 |
DropTempView { |
| 129 |
view_name: &'c str, |
| 130 |
}, |
| 131 |
DropTrigger { |
| 132 |
trigger_name: &'c str, |
| 133 |
table_name: &'c str, |
| 134 |
}, |
| 135 |
DropView { |
| 136 |
view_name: &'c str, |
| 137 |
}, |
| 138 |
Insert { |
| 139 |
table_name: &'c str, |
| 140 |
}, |
| 141 |
Pragma { |
| 142 |
pragma_name: &'c str, |
| 143 |
/// The pragma value, if present (e.g., `PRAGMA name = value;`). |
| 144 |
pragma_value: Option<&'c str>, |
| 145 |
}, |
| 146 |
Read { |
| 147 |
table_name: &'c str, |
| 148 |
column_name: &'c str, |
| 149 |
}, |
| 150 |
Select, |
| 151 |
Transaction { |
| 152 |
operation: TransactionOperation, |
| 153 |
}, |
| 154 |
Update { |
| 155 |
table_name: &'c str, |
| 156 |
column_name: &'c str, |
| 157 |
}, |
| 158 |
Attach { |
| 159 |
filename: &'c str, |
| 160 |
}, |
| 161 |
Detach { |
| 162 |
database_name: &'c str, |
| 163 |
}, |
| 164 |
AlterTable { |
| 165 |
database_name: &'c str, |
| 166 |
table_name: &'c str, |
| 167 |
}, |
| 168 |
Reindex { |
| 169 |
index_name: &'c str, |
| 170 |
}, |
| 171 |
Analyze { |
| 172 |
table_name: &'c str, |
| 173 |
}, |
| 174 |
CreateVtable { |
| 175 |
table_name: &'c str, |
| 176 |
module_name: &'c str, |
| 177 |
}, |
| 178 |
DropVtable { |
| 179 |
table_name: &'c str, |
| 180 |
module_name: &'c str, |
| 181 |
}, |
| 182 |
Function { |
| 183 |
function_name: &'c str, |
| 184 |
}, |
| 185 |
Savepoint { |
| 186 |
operation: TransactionOperation, |
| 187 |
savepoint_name: &'c str, |
| 188 |
}, |
| 189 |
Recursive, |
| 190 |
} |
| 191 |
|
| 192 |
impl<'c> AuthAction<'c> { |
| 193 |
fn from_raw(code: i32, arg1: Option<&'c str>, arg2: Option<&'c str>) -> Self { |
| 194 |
match (code, arg1, arg2) { |
| 195 |
(ffi::SQLITE_CREATE_INDEX, Some(index_name), Some(table_name)) => Self::CreateIndex { |
| 196 |
index_name, |
| 197 |
table_name, |
| 198 |
}, |
| 199 |
(ffi::SQLITE_CREATE_TABLE, Some(table_name), _) => Self::CreateTable { table_name }, |
| 200 |
(ffi::SQLITE_CREATE_TEMP_INDEX, Some(index_name), Some(table_name)) => { |
| 201 |
Self::CreateTempIndex { |
| 202 |
index_name, |
| 203 |
table_name, |
| 204 |
} |
| 205 |
} |
| 206 |
(ffi::SQLITE_CREATE_TEMP_TABLE, Some(table_name), _) => { |
| 207 |
Self::CreateTempTable { table_name } |
| 208 |
} |
| 209 |
(ffi::SQLITE_CREATE_TEMP_TRIGGER, Some(trigger_name), Some(table_name)) => { |
| 210 |
Self::CreateTempTrigger { |
| 211 |
trigger_name, |
| 212 |
table_name, |
| 213 |
} |
| 214 |
} |
| 215 |
(ffi::SQLITE_CREATE_TEMP_VIEW, Some(view_name), _) => { |
| 216 |
Self::CreateTempView { view_name } |
| 217 |
} |
| 218 |
(ffi::SQLITE_CREATE_TRIGGER, Some(trigger_name), Some(table_name)) => { |
| 219 |
Self::CreateTrigger { |
| 220 |
trigger_name, |
| 221 |
table_name, |
| 222 |
} |
| 223 |
} |
| 224 |
(ffi::SQLITE_CREATE_VIEW, Some(view_name), _) => Self::CreateView { view_name }, |
| 225 |
(ffi::SQLITE_DELETE, Some(table_name), None) => Self::Delete { table_name }, |
| 226 |
(ffi::SQLITE_DROP_INDEX, Some(index_name), Some(table_name)) => Self::DropIndex { |
| 227 |
index_name, |
| 228 |
table_name, |
| 229 |
}, |
| 230 |
(ffi::SQLITE_DROP_TABLE, Some(table_name), _) => Self::DropTable { table_name }, |
| 231 |
(ffi::SQLITE_DROP_TEMP_INDEX, Some(index_name), Some(table_name)) => { |
| 232 |
Self::DropTempIndex { |
| 233 |
index_name, |
| 234 |
table_name, |
| 235 |
} |
| 236 |
} |
| 237 |
(ffi::SQLITE_DROP_TEMP_TABLE, Some(table_name), _) => { |
| 238 |
Self::DropTempTable { table_name } |
| 239 |
} |
| 240 |
(ffi::SQLITE_DROP_TEMP_TRIGGER, Some(trigger_name), Some(table_name)) => { |
| 241 |
Self::DropTempTrigger { |
| 242 |
trigger_name, |
| 243 |
table_name, |
| 244 |
} |
| 245 |
} |
| 246 |
(ffi::SQLITE_DROP_TEMP_VIEW, Some(view_name), _) => Self::DropTempView { view_name }, |
| 247 |
(ffi::SQLITE_DROP_TRIGGER, Some(trigger_name), Some(table_name)) => Self::DropTrigger { |
| 248 |
trigger_name, |
| 249 |
table_name, |
| 250 |
}, |
| 251 |
(ffi::SQLITE_DROP_VIEW, Some(view_name), _) => Self::DropView { view_name }, |
| 252 |
(ffi::SQLITE_INSERT, Some(table_name), _) => Self::Insert { table_name }, |
| 253 |
(ffi::SQLITE_PRAGMA, Some(pragma_name), pragma_value) => Self::Pragma { |
| 254 |
pragma_name, |
| 255 |
pragma_value, |
| 256 |
}, |
| 257 |
(ffi::SQLITE_READ, Some(table_name), Some(column_name)) => Self::Read { |
| 258 |
table_name, |
| 259 |
column_name, |
| 260 |
}, |
| 261 |
(ffi::SQLITE_SELECT, ..) => Self::Select, |
| 262 |
(ffi::SQLITE_TRANSACTION, Some(operation_str), _) => Self::Transaction { |
| 263 |
operation: TransactionOperation::from_str(operation_str), |
| 264 |
}, |
| 265 |
(ffi::SQLITE_UPDATE, Some(table_name), Some(column_name)) => Self::Update { |
| 266 |
table_name, |
| 267 |
column_name, |
| 268 |
}, |
| 269 |
(ffi::SQLITE_ATTACH, Some(filename), _) => Self::Attach { filename }, |
| 270 |
(ffi::SQLITE_DETACH, Some(database_name), _) => Self::Detach { database_name }, |
| 271 |
(ffi::SQLITE_ALTER_TABLE, Some(database_name), Some(table_name)) => Self::AlterTable { |
| 272 |
database_name, |
| 273 |
table_name, |
| 274 |
}, |
| 275 |
(ffi::SQLITE_REINDEX, Some(index_name), _) => Self::Reindex { index_name }, |
| 276 |
(ffi::SQLITE_ANALYZE, Some(table_name), _) => Self::Analyze { table_name }, |
| 277 |
(ffi::SQLITE_CREATE_VTABLE, Some(table_name), Some(module_name)) => { |
| 278 |
Self::CreateVtable { |
| 279 |
table_name, |
| 280 |
module_name, |
| 281 |
} |
| 282 |
} |
| 283 |
(ffi::SQLITE_DROP_VTABLE, Some(table_name), Some(module_name)) => Self::DropVtable { |
| 284 |
table_name, |
| 285 |
module_name, |
| 286 |
}, |
| 287 |
(ffi::SQLITE_FUNCTION, _, Some(function_name)) => Self::Function { function_name }, |
| 288 |
(ffi::SQLITE_SAVEPOINT, Some(operation_str), Some(savepoint_name)) => Self::Savepoint { |
| 289 |
operation: TransactionOperation::from_str(operation_str), |
| 290 |
savepoint_name, |
| 291 |
}, |
| 292 |
(ffi::SQLITE_RECURSIVE, ..) => Self::Recursive, |
| 293 |
(code, arg1, arg2) => Self::Unknown { code, arg1, arg2 }, |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
pub(crate) type BoxedAuthorizer = |
| 299 |
Box<dyn for<'c> FnMut(AuthContext<'c>) -> Authorization + Send + 'static>; |
| 300 |
|
| 301 |
/// A transaction operation. |
| 302 |
#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 303 |
#[non_exhaustive] |
| 304 |
#[allow(missing_docs)] |
| 305 |
pub enum TransactionOperation { |
| 306 |
Unknown, |
| 307 |
Begin, |
| 308 |
Release, |
| 309 |
Rollback, |
| 310 |
} |
| 311 |
|
| 312 |
impl TransactionOperation { |
| 313 |
fn from_str(op_str: &str) -> Self { |
| 314 |
match op_str { |
| 315 |
"BEGIN" => Self::Begin, |
| 316 |
"RELEASE" => Self::Release, |
| 317 |
"ROLLBACK" => Self::Rollback, |
| 318 |
_ => Self::Unknown, |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/// [`authorizer`](Connection::authorizer) return code |
| 324 |
#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 325 |
#[non_exhaustive] |
| 326 |
pub enum Authorization { |
| 327 |
/// Authorize the action. |
| 328 |
Allow, |
| 329 |
/// Don't allow access, but don't trigger an error either. |
| 330 |
Ignore, |
| 331 |
/// Trigger an error. |
| 332 |
Deny, |
| 333 |
} |
| 334 |
|
| 335 |
impl Authorization { |
| 336 |
fn into_raw(self) -> c_int { |
| 337 |
match self { |
| 338 |
Self::Allow => ffi::SQLITE_OK, |
| 339 |
Self::Ignore => ffi::SQLITE_IGNORE, |
| 340 |
Self::Deny => ffi::SQLITE_DENY, |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
impl Connection { |
| 346 |
/// Register a callback function to be invoked whenever |
| 347 |
/// a transaction is committed. |
| 348 |
/// |
| 349 |
/// The callback returns `true` to rollback. |
| 350 |
#[inline] |
| 351 |
pub fn commit_hook<F>(&self, hook: Option<F>) |
| 352 |
where |
| 353 |
F: FnMut() -> bool + Send + 'static, |
| 354 |
{ |
| 355 |
self.db.borrow_mut().commit_hook(hook); |
| 356 |
} |
| 357 |
|
| 358 |
/// Register a callback function to be invoked whenever |
| 359 |
/// a transaction is committed. |
| 360 |
#[inline] |
| 361 |
pub fn rollback_hook<F>(&self, hook: Option<F>) |
| 362 |
where |
| 363 |
F: FnMut() + Send + 'static, |
| 364 |
{ |
| 365 |
self.db.borrow_mut().rollback_hook(hook); |
| 366 |
} |
| 367 |
|
| 368 |
/// Register a callback function to be invoked whenever |
| 369 |
/// a row is updated, inserted or deleted in a rowid table. |
| 370 |
/// |
| 371 |
/// The callback parameters are: |
| 372 |
/// |
| 373 |
/// - the type of database update (`SQLITE_INSERT`, `SQLITE_UPDATE` or |
| 374 |
/// `SQLITE_DELETE`), |
| 375 |
/// - the name of the database ("main", "temp", ...), |
| 376 |
/// - the name of the table that is updated, |
| 377 |
/// - the ROWID of the row that is updated. |
| 378 |
#[inline] |
| 379 |
pub fn update_hook<F>(&self, hook: Option<F>) |
| 380 |
where |
| 381 |
F: FnMut(Action, &str, &str, i64) + Send + 'static, |
| 382 |
{ |
| 383 |
self.db.borrow_mut().update_hook(hook); |
| 384 |
} |
| 385 |
|
| 386 |
/// Register a callback that is invoked each time data is committed to a database in wal mode. |
| 387 |
/// |
| 388 |
/// A single database handle may have at most a single write-ahead log callback registered at one time. |
| 389 |
/// Calling `wal_hook` replaces any previously registered write-ahead log callback. |
| 390 |
/// Note that the `sqlite3_wal_autocheckpoint()` interface and the `wal_autocheckpoint` pragma |
| 391 |
/// both invoke `sqlite3_wal_hook()` and will overwrite any prior `sqlite3_wal_hook()` settings. |
| 392 |
pub fn wal_hook(&self, hook: Option<fn(&Wal, c_int) -> Result<()>>) { |
| 393 |
unsafe extern "C" fn wal_hook_callback( |
| 394 |
client_data: *mut c_void, |
| 395 |
db: *mut ffi::sqlite3, |
| 396 |
db_name: *const c_char, |
| 397 |
pages: c_int, |
| 398 |
) -> c_int { |
| 399 |
let hook_fn: fn(&Wal, c_int) -> Result<()> = std::mem::transmute(client_data); |
| 400 |
let wal = Wal { db, db_name }; |
| 401 |
catch_unwind(|| match hook_fn(&wal, pages) { |
| 402 |
Ok(_) => ffi::SQLITE_OK, |
| 403 |
Err(e) => e |
| 404 |
.sqlite_error() |
| 405 |
.map_or(ffi::SQLITE_ERROR, |x| x.extended_code), |
| 406 |
}) |
| 407 |
.unwrap_or_default() |
| 408 |
} |
| 409 |
let c = self.db.borrow_mut(); |
| 410 |
match hook { |
| 411 |
Some(f) => unsafe { |
| 412 |
ffi::sqlite3_wal_hook(c.db(), Some(wal_hook_callback), f as *mut c_void) |
| 413 |
}, |
| 414 |
None => unsafe { ffi::sqlite3_wal_hook(c.db(), None, ptr::null_mut()) }, |
| 415 |
}; |
| 416 |
} |
| 417 |
|
| 418 |
/// Register a query progress callback. |
| 419 |
/// |
| 420 |
/// The parameter `num_ops` is the approximate number of virtual machine |
| 421 |
/// instructions that are evaluated between successive invocations of the |
| 422 |
/// `handler`. If `num_ops` is less than one then the progress handler |
| 423 |
/// is disabled. |
| 424 |
/// |
| 425 |
/// If the progress callback returns `true`, the operation is interrupted. |
| 426 |
pub fn progress_handler<F>(&self, num_ops: c_int, handler: Option<F>) |
| 427 |
where |
| 428 |
F: FnMut() -> bool + Send + 'static, |
| 429 |
{ |
| 430 |
self.db.borrow_mut().progress_handler(num_ops, handler); |
| 431 |
} |
| 432 |
|
| 433 |
/// Register an authorizer callback that's invoked |
| 434 |
/// as a statement is being prepared. |
| 435 |
#[inline] |
| 436 |
pub fn authorizer<'c, F>(&self, hook: Option<F>) |
| 437 |
where |
| 438 |
F: for<'r> FnMut(AuthContext<'r>) -> Authorization + Send + 'static, |
| 439 |
{ |
| 440 |
self.db.borrow_mut().authorizer(hook); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
/// Checkpoint mode |
| 445 |
#[derive(Clone, Copy)] |
| 446 |
#[repr(i32)] |
| 447 |
#[non_exhaustive] |
| 448 |
pub enum CheckpointMode { |
| 449 |
/// Do as much as possible w/o blocking |
| 450 |
PASSIVE = ffi::SQLITE_CHECKPOINT_PASSIVE, |
| 451 |
/// Wait for writers, then checkpoint |
| 452 |
FULL = ffi::SQLITE_CHECKPOINT_FULL, |
| 453 |
/// Like FULL but wait for readers |
| 454 |
RESTART = ffi::SQLITE_CHECKPOINT_RESTART, |
| 455 |
/// Like RESTART but also truncate WAL |
| 456 |
TRUNCATE = ffi::SQLITE_CHECKPOINT_TRUNCATE, |
| 457 |
} |
| 458 |
|
| 459 |
/// Write-Ahead Log |
| 460 |
pub struct Wal { |
| 461 |
db: *mut ffi::sqlite3, |
| 462 |
db_name: *const c_char, |
| 463 |
} |
| 464 |
|
| 465 |
impl Wal { |
| 466 |
/// Checkpoint a database |
| 467 |
pub fn checkpoint(&self) -> Result<()> { |
| 468 |
unsafe { decode_result_raw(self.db, ffi::sqlite3_wal_checkpoint(self.db, self.db_name)) } |
| 469 |
} |
| 470 |
/// Checkpoint a database |
| 471 |
pub fn checkpoint_v2(&self, mode: CheckpointMode) -> Result<(c_int, c_int)> { |
| 472 |
let mut n_log = 0; |
| 473 |
let mut n_ckpt = 0; |
| 474 |
unsafe { |
| 475 |
decode_result_raw( |
| 476 |
self.db, |
| 477 |
ffi::sqlite3_wal_checkpoint_v2( |
| 478 |
self.db, |
| 479 |
self.db_name, |
| 480 |
mode as c_int, |
| 481 |
&mut n_log, |
| 482 |
&mut n_ckpt, |
| 483 |
), |
| 484 |
)? |
| 485 |
}; |
| 486 |
Ok((n_log, n_ckpt)) |
| 487 |
} |
| 488 |
|
| 489 |
/// Name of the database that was written to |
| 490 |
pub fn name(&self) -> DatabaseName<'_> { |
| 491 |
DatabaseName::from_cstr(unsafe { std::ffi::CStr::from_ptr(self.db_name) }) |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
impl InnerConnection { |
| 496 |
#[inline] |
| 497 |
pub fn remove_hooks(&mut self) { |
| 498 |
self.update_hook(None::<fn(Action, &str, &str, i64)>); |
| 499 |
self.commit_hook(None::<fn() -> bool>); |
| 500 |
self.rollback_hook(None::<fn()>); |
| 501 |
self.progress_handler(0, None::<fn() -> bool>); |
| 502 |
self.authorizer(None::<fn(AuthContext<'_>) -> Authorization>); |
| 503 |
} |
| 504 |
|
| 505 |
/// ```compile_fail |
| 506 |
/// use rusqlite::{Connection, Result}; |
| 507 |
/// fn main() -> Result<()> { |
| 508 |
/// let db = Connection::open_in_memory()?; |
| 509 |
/// { |
| 510 |
/// let mut called = std::sync::atomic::AtomicBool::new(false); |
| 511 |
/// db.commit_hook(Some(|| { |
| 512 |
/// called.store(true, std::sync::atomic::Ordering::Relaxed); |
| 513 |
/// true |
| 514 |
/// })); |
| 515 |
/// } |
| 516 |
/// assert!(db |
| 517 |
/// .execute_batch( |
| 518 |
/// "BEGIN; |
| 519 |
/// CREATE TABLE foo (t TEXT); |
| 520 |
/// COMMIT;", |
| 521 |
/// ) |
| 522 |
/// .is_err()); |
| 523 |
/// Ok(()) |
| 524 |
/// } |
| 525 |
/// ``` |
| 526 |
fn commit_hook<F>(&mut self, hook: Option<F>) |
| 527 |
where |
| 528 |
F: FnMut() -> bool + Send + 'static, |
| 529 |
{ |
| 530 |
unsafe extern "C" fn call_boxed_closure<F>(p_arg: *mut c_void) -> c_int |
| 531 |
where |
| 532 |
F: FnMut() -> bool, |
| 533 |
{ |
| 534 |
let r = catch_unwind(|| { |
| 535 |
let boxed_hook: *mut F = p_arg.cast::<F>(); |
| 536 |
(*boxed_hook)() |
| 537 |
}); |
| 538 |
c_int::from(r.unwrap_or_default()) |
| 539 |
} |
| 540 |
|
| 541 |
// unlike `sqlite3_create_function_v2`, we cannot specify a `xDestroy` with |
| 542 |
// `sqlite3_commit_hook`. so we keep the `xDestroy` function in |
| 543 |
// `InnerConnection.free_boxed_hook`. |
| 544 |
let free_commit_hook = if hook.is_some() { |
| 545 |
Some(free_boxed_hook::<F> as unsafe fn(*mut c_void)) |
| 546 |
} else { |
| 547 |
None |
| 548 |
}; |
| 549 |
|
| 550 |
let previous_hook = match hook { |
| 551 |
Some(hook) => { |
| 552 |
let boxed_hook: *mut F = Box::into_raw(Box::new(hook)); |
| 553 |
unsafe { |
| 554 |
ffi::sqlite3_commit_hook( |
| 555 |
self.db(), |
| 556 |
Some(call_boxed_closure::<F>), |
| 557 |
boxed_hook.cast(), |
| 558 |
) |
| 559 |
} |
| 560 |
} |
| 561 |
_ => unsafe { ffi::sqlite3_commit_hook(self.db(), None, ptr::null_mut()) }, |
| 562 |
}; |
| 563 |
if !previous_hook.is_null() { |
| 564 |
if let Some(free_boxed_hook) = self.free_commit_hook { |
| 565 |
unsafe { free_boxed_hook(previous_hook) }; |
| 566 |
} |
| 567 |
} |
| 568 |
self.free_commit_hook = free_commit_hook; |
| 569 |
} |
| 570 |
|
| 571 |
/// ```compile_fail |
| 572 |
/// use rusqlite::{Connection, Result}; |
| 573 |
/// fn main() -> Result<()> { |
| 574 |
/// let db = Connection::open_in_memory()?; |
| 575 |
/// { |
| 576 |
/// let mut called = std::sync::atomic::AtomicBool::new(false); |
| 577 |
/// db.rollback_hook(Some(|| { |
| 578 |
/// called.store(true, std::sync::atomic::Ordering::Relaxed); |
| 579 |
/// })); |
| 580 |
/// } |
| 581 |
/// assert!(db |
| 582 |
/// .execute_batch( |
| 583 |
/// "BEGIN; |
| 584 |
/// CREATE TABLE foo (t TEXT); |
| 585 |
/// ROLLBACK;", |
| 586 |
/// ) |
| 587 |
/// .is_err()); |
| 588 |
/// Ok(()) |
| 589 |
/// } |
| 590 |
/// ``` |
| 591 |
fn rollback_hook<F>(&mut self, hook: Option<F>) |
| 592 |
where |
| 593 |
F: FnMut() + Send + 'static, |
| 594 |
{ |
| 595 |
unsafe extern "C" fn call_boxed_closure<F>(p_arg: *mut c_void) |
| 596 |
where |
| 597 |
F: FnMut(), |
| 598 |
{ |
| 599 |
drop(catch_unwind(|| { |
| 600 |
let boxed_hook: *mut F = p_arg.cast::<F>(); |
| 601 |
(*boxed_hook)(); |
| 602 |
})); |
| 603 |
} |
| 604 |
|
| 605 |
let free_rollback_hook = if hook.is_some() { |
| 606 |
Some(free_boxed_hook::<F> as unsafe fn(*mut c_void)) |
| 607 |
} else { |
| 608 |
None |
| 609 |
}; |
| 610 |
|
| 611 |
let previous_hook = match hook { |
| 612 |
Some(hook) => { |
| 613 |
let boxed_hook: *mut F = Box::into_raw(Box::new(hook)); |
| 614 |
unsafe { |
| 615 |
ffi::sqlite3_rollback_hook( |
| 616 |
self.db(), |
| 617 |
Some(call_boxed_closure::<F>), |
| 618 |
boxed_hook.cast(), |
| 619 |
) |
| 620 |
} |
| 621 |
} |
| 622 |
_ => unsafe { ffi::sqlite3_rollback_hook(self.db(), None, ptr::null_mut()) }, |
| 623 |
}; |
| 624 |
if !previous_hook.is_null() { |
| 625 |
if let Some(free_boxed_hook) = self.free_rollback_hook { |
| 626 |
unsafe { free_boxed_hook(previous_hook) }; |
| 627 |
} |
| 628 |
} |
| 629 |
self.free_rollback_hook = free_rollback_hook; |
| 630 |
} |
| 631 |
|
| 632 |
/// ```compile_fail |
| 633 |
/// use rusqlite::{Connection, Result}; |
| 634 |
/// fn main() -> Result<()> { |
| 635 |
/// let db = Connection::open_in_memory()?; |
| 636 |
/// { |
| 637 |
/// let mut called = std::sync::atomic::AtomicBool::new(false); |
| 638 |
/// db.update_hook(Some(|_, _: &str, _: &str, _| { |
| 639 |
/// called.store(true, std::sync::atomic::Ordering::Relaxed); |
| 640 |
/// })); |
| 641 |
/// } |
| 642 |
/// db.execute_batch("CREATE TABLE foo AS SELECT 1 AS bar;") |
| 643 |
/// } |
| 644 |
/// ``` |
| 645 |
fn update_hook<F>(&mut self, hook: Option<F>) |
| 646 |
where |
| 647 |
F: FnMut(Action, &str, &str, i64) + Send + 'static, |
| 648 |
{ |
| 649 |
unsafe extern "C" fn call_boxed_closure<F>( |
| 650 |
p_arg: *mut c_void, |
| 651 |
action_code: c_int, |
| 652 |
p_db_name: *const c_char, |
| 653 |
p_table_name: *const c_char, |
| 654 |
row_id: i64, |
| 655 |
) where |
| 656 |
F: FnMut(Action, &str, &str, i64), |
| 657 |
{ |
| 658 |
let action = Action::from(action_code); |
| 659 |
drop(catch_unwind(|| { |
| 660 |
let boxed_hook: *mut F = p_arg.cast::<F>(); |
| 661 |
(*boxed_hook)( |
| 662 |
action, |
| 663 |
expect_utf8(p_db_name, "database name"), |
| 664 |
expect_utf8(p_table_name, "table name"), |
| 665 |
row_id, |
| 666 |
); |
| 667 |
})); |
| 668 |
} |
| 669 |
|
| 670 |
let free_update_hook = if hook.is_some() { |
| 671 |
Some(free_boxed_hook::<F> as unsafe fn(*mut c_void)) |
| 672 |
} else { |
| 673 |
None |
| 674 |
}; |
| 675 |
|
| 676 |
let previous_hook = match hook { |
| 677 |
Some(hook) => { |
| 678 |
let boxed_hook: *mut F = Box::into_raw(Box::new(hook)); |
| 679 |
unsafe { |
| 680 |
ffi::sqlite3_update_hook( |
| 681 |
self.db(), |
| 682 |
Some(call_boxed_closure::<F>), |
| 683 |
boxed_hook.cast(), |
| 684 |
) |
| 685 |
} |
| 686 |
} |
| 687 |
_ => unsafe { ffi::sqlite3_update_hook(self.db(), None, ptr::null_mut()) }, |
| 688 |
}; |
| 689 |
if !previous_hook.is_null() { |
| 690 |
if let Some(free_boxed_hook) = self.free_update_hook { |
| 691 |
unsafe { free_boxed_hook(previous_hook) }; |
| 692 |
} |
| 693 |
} |
| 694 |
self.free_update_hook = free_update_hook; |
| 695 |
} |
| 696 |
|
| 697 |
/// ```compile_fail |
| 698 |
/// use rusqlite::{Connection, Result}; |
| 699 |
/// fn main() -> Result<()> { |
| 700 |
/// let db = Connection::open_in_memory()?; |
| 701 |
/// { |
| 702 |
/// let mut called = std::sync::atomic::AtomicBool::new(false); |
| 703 |
/// db.progress_handler( |
| 704 |
/// 1, |
| 705 |
/// Some(|| { |
| 706 |
/// called.store(true, std::sync::atomic::Ordering::Relaxed); |
| 707 |
/// true |
| 708 |
/// }), |
| 709 |
/// ); |
| 710 |
/// } |
| 711 |
/// assert!(db |
| 712 |
/// .execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;") |
| 713 |
/// .is_err()); |
| 714 |
/// Ok(()) |
| 715 |
/// } |
| 716 |
/// ``` |
| 717 |
fn progress_handler<F>(&mut self, num_ops: c_int, handler: Option<F>) |
| 718 |
where |
| 719 |
F: FnMut() -> bool + Send + 'static, |
| 720 |
{ |
| 721 |
unsafe extern "C" fn call_boxed_closure<F>(p_arg: *mut c_void) -> c_int |
| 722 |
where |
| 723 |
F: FnMut() -> bool, |
| 724 |
{ |
| 725 |
let r = catch_unwind(|| { |
| 726 |
let boxed_handler: *mut F = p_arg.cast::<F>(); |
| 727 |
(*boxed_handler)() |
| 728 |
}); |
| 729 |
c_int::from(r.unwrap_or_default()) |
| 730 |
} |
| 731 |
|
| 732 |
if let Some(handler) = handler { |
| 733 |
let boxed_handler = Box::new(handler); |
| 734 |
unsafe { |
| 735 |
ffi::sqlite3_progress_handler( |
| 736 |
self.db(), |
| 737 |
num_ops, |
| 738 |
Some(call_boxed_closure::<F>), |
| 739 |
&*boxed_handler as *const F as *mut _, |
| 740 |
); |
| 741 |
} |
| 742 |
self.progress_handler = Some(boxed_handler); |
| 743 |
} else { |
| 744 |
unsafe { ffi::sqlite3_progress_handler(self.db(), num_ops, None, ptr::null_mut()) } |
| 745 |
self.progress_handler = None; |
| 746 |
}; |
| 747 |
} |
| 748 |
|
| 749 |
/// ```compile_fail |
| 750 |
/// use rusqlite::{Connection, Result}; |
| 751 |
/// fn main() -> Result<()> { |
| 752 |
/// let db = Connection::open_in_memory()?; |
| 753 |
/// { |
| 754 |
/// let mut called = std::sync::atomic::AtomicBool::new(false); |
| 755 |
/// db.authorizer(Some(|_: rusqlite::hooks::AuthContext<'_>| { |
| 756 |
/// called.store(true, std::sync::atomic::Ordering::Relaxed); |
| 757 |
/// rusqlite::hooks::Authorization::Deny |
| 758 |
/// })); |
| 759 |
/// } |
| 760 |
/// assert!(db |
| 761 |
/// .execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;") |
| 762 |
/// .is_err()); |
| 763 |
/// Ok(()) |
| 764 |
/// } |
| 765 |
/// ``` |
| 766 |
fn authorizer<'c, F>(&'c mut self, authorizer: Option<F>) |
| 767 |
where |
| 768 |
F: for<'r> FnMut(AuthContext<'r>) -> Authorization + Send + 'static, |
| 769 |
{ |
| 770 |
unsafe extern "C" fn call_boxed_closure<'c, F>( |
| 771 |
p_arg: *mut c_void, |
| 772 |
action_code: c_int, |
| 773 |
param1: *const c_char, |
| 774 |
param2: *const c_char, |
| 775 |
db_name: *const c_char, |
| 776 |
trigger_or_view_name: *const c_char, |
| 777 |
) -> c_int |
| 778 |
where |
| 779 |
F: FnMut(AuthContext<'c>) -> Authorization + Send + 'static, |
| 780 |
{ |
| 781 |
catch_unwind(|| { |
| 782 |
let action = AuthAction::from_raw( |
| 783 |
action_code, |
| 784 |
expect_optional_utf8(param1, "authorizer param 1"), |
| 785 |
expect_optional_utf8(param2, "authorizer param 2"), |
| 786 |
); |
| 787 |
let auth_ctx = AuthContext { |
| 788 |
action, |
| 789 |
database_name: expect_optional_utf8(db_name, "database name"), |
| 790 |
accessor: expect_optional_utf8( |
| 791 |
trigger_or_view_name, |
| 792 |
"accessor (inner-most trigger or view)", |
| 793 |
), |
| 794 |
}; |
| 795 |
let boxed_hook: *mut F = p_arg.cast::<F>(); |
| 796 |
(*boxed_hook)(auth_ctx) |
| 797 |
}) |
| 798 |
.map_or_else(|_| ffi::SQLITE_ERROR, Authorization::into_raw) |
| 799 |
} |
| 800 |
|
| 801 |
let callback_fn = authorizer |
| 802 |
.as_ref() |
| 803 |
.map(|_| call_boxed_closure::<'c, F> as unsafe extern "C" fn(_, _, _, _, _, _) -> _); |
| 804 |
let boxed_authorizer = authorizer.map(Box::new); |
| 805 |
|
| 806 |
match unsafe { |
| 807 |
ffi::sqlite3_set_authorizer( |
| 808 |
self.db(), |
| 809 |
callback_fn, |
| 810 |
boxed_authorizer |
| 811 |
.as_ref() |
| 812 |
.map_or_else(ptr::null_mut, |f| &**f as *const F as *mut _), |
| 813 |
) |
| 814 |
} { |
| 815 |
ffi::SQLITE_OK => { |
| 816 |
self.authorizer = boxed_authorizer.map(|ba| ba as _); |
| 817 |
} |
| 818 |
err_code => { |
| 819 |
// The only error that `sqlite3_set_authorizer` returns is `SQLITE_MISUSE` |
| 820 |
// when compiled with `ENABLE_API_ARMOR` and the db pointer is invalid. |
| 821 |
// This library does not allow constructing a null db ptr, so if this branch |
| 822 |
// is hit, something very bad has happened. Panicking instead of returning |
| 823 |
// `Result` keeps this hook's API consistent with the others. |
| 824 |
panic!("unexpectedly failed to set_authorizer: {}", unsafe { |
| 825 |
crate::error::error_from_handle(self.db(), err_code) |
| 826 |
}); |
| 827 |
} |
| 828 |
} |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
unsafe fn free_boxed_hook<F>(p: *mut c_void) { |
| 833 |
drop(Box::from_raw(p.cast::<F>())); |
| 834 |
} |
| 835 |
|
| 836 |
unsafe fn expect_utf8<'a>(p_str: *const c_char, description: &'static str) -> &'a str { |
| 837 |
expect_optional_utf8(p_str, description) |
| 838 |
.unwrap_or_else(|| panic!("received empty {description}")) |
| 839 |
} |
| 840 |
|
| 841 |
unsafe fn expect_optional_utf8<'a>( |
| 842 |
p_str: *const c_char, |
| 843 |
description: &'static str, |
| 844 |
) -> Option<&'a str> { |
| 845 |
if p_str.is_null() { |
| 846 |
return None; |
| 847 |
} |
| 848 |
std::ffi::CStr::from_ptr(p_str) |
| 849 |
.to_str() |
| 850 |
.unwrap_or_else(|_| panic!("received non-utf8 string as {description}")) |
| 851 |
.into() |
| 852 |
} |
| 853 |
|
| 854 |
#[cfg(test)] |
| 855 |
mod test { |
| 856 |
use super::Action; |
| 857 |
use crate::{Connection, DatabaseName, Result}; |
| 858 |
use std::sync::atomic::{AtomicBool, Ordering}; |
| 859 |
|
| 860 |
#[test] |
| 861 |
fn test_commit_hook() -> Result<()> { |
| 862 |
let db = Connection::open_in_memory()?; |
| 863 |
|
| 864 |
static CALLED: AtomicBool = AtomicBool::new(false); |
| 865 |
db.commit_hook(Some(|| { |
| 866 |
CALLED.store(true, Ordering::Relaxed); |
| 867 |
false |
| 868 |
})); |
| 869 |
db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;")?; |
| 870 |
assert!(CALLED.load(Ordering::Relaxed)); |
| 871 |
Ok(()) |
| 872 |
} |
| 873 |
|
| 874 |
#[test] |
| 875 |
fn test_fn_commit_hook() -> Result<()> { |
| 876 |
let db = Connection::open_in_memory()?; |
| 877 |
|
| 878 |
fn hook() -> bool { |
| 879 |
true |
| 880 |
} |
| 881 |
|
| 882 |
db.commit_hook(Some(hook)); |
| 883 |
db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;") |
| 884 |
.unwrap_err(); |
| 885 |
Ok(()) |
| 886 |
} |
| 887 |
|
| 888 |
#[test] |
| 889 |
fn test_rollback_hook() -> Result<()> { |
| 890 |
let db = Connection::open_in_memory()?; |
| 891 |
|
| 892 |
static CALLED: AtomicBool = AtomicBool::new(false); |
| 893 |
db.rollback_hook(Some(|| { |
| 894 |
CALLED.store(true, Ordering::Relaxed); |
| 895 |
})); |
| 896 |
db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); ROLLBACK;")?; |
| 897 |
assert!(CALLED.load(Ordering::Relaxed)); |
| 898 |
Ok(()) |
| 899 |
} |
| 900 |
|
| 901 |
#[test] |
| 902 |
fn test_update_hook() -> Result<()> { |
| 903 |
let db = Connection::open_in_memory()?; |
| 904 |
|
| 905 |
static CALLED: AtomicBool = AtomicBool::new(false); |
| 906 |
db.update_hook(Some(|action, db: &str, tbl: &str, row_id| { |
| 907 |
assert_eq!(Action::SQLITE_INSERT, action); |
| 908 |
assert_eq!("main", db); |
| 909 |
assert_eq!("foo", tbl); |
| 910 |
assert_eq!(1, row_id); |
| 911 |
CALLED.store(true, Ordering::Relaxed); |
| 912 |
})); |
| 913 |
db.execute_batch("CREATE TABLE foo (t TEXT)")?; |
| 914 |
db.execute_batch("INSERT INTO foo VALUES ('lisa')")?; |
| 915 |
assert!(CALLED.load(Ordering::Relaxed)); |
| 916 |
Ok(()) |
| 917 |
} |
| 918 |
|
| 919 |
#[test] |
| 920 |
fn test_progress_handler() -> Result<()> { |
| 921 |
let db = Connection::open_in_memory()?; |
| 922 |
|
| 923 |
static CALLED: AtomicBool = AtomicBool::new(false); |
| 924 |
db.progress_handler( |
| 925 |
1, |
| 926 |
Some(|| { |
| 927 |
CALLED.store(true, Ordering::Relaxed); |
| 928 |
false |
| 929 |
}), |
| 930 |
); |
| 931 |
db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;")?; |
| 932 |
assert!(CALLED.load(Ordering::Relaxed)); |
| 933 |
Ok(()) |
| 934 |
} |
| 935 |
|
| 936 |
#[test] |
| 937 |
fn test_progress_handler_interrupt() -> Result<()> { |
| 938 |
let db = Connection::open_in_memory()?; |
| 939 |
|
| 940 |
fn handler() -> bool { |
| 941 |
true |
| 942 |
} |
| 943 |
|
| 944 |
db.progress_handler(1, Some(handler)); |
| 945 |
db.execute_batch("BEGIN; CREATE TABLE foo (t TEXT); COMMIT;") |
| 946 |
.unwrap_err(); |
| 947 |
Ok(()) |
| 948 |
} |
| 949 |
|
| 950 |
#[test] |
| 951 |
fn test_authorizer() -> Result<()> { |
| 952 |
use super::{AuthAction, AuthContext, Authorization}; |
| 953 |
|
| 954 |
let db = Connection::open_in_memory()?; |
| 955 |
db.execute_batch("CREATE TABLE foo (public TEXT, private TEXT)")?; |
| 956 |
|
| 957 |
let authorizer = move |ctx: AuthContext<'_>| match ctx.action { |
| 958 |
AuthAction::Read { |
| 959 |
column_name: "private", |
| 960 |
.. |
| 961 |
} => Authorization::Ignore, |
| 962 |
AuthAction::DropTable { .. } => Authorization::Deny, |
| 963 |
AuthAction::Pragma { .. } => panic!("shouldn't be called"), |
| 964 |
_ => Authorization::Allow, |
| 965 |
}; |
| 966 |
|
| 967 |
db.authorizer(Some(authorizer)); |
| 968 |
db.execute_batch( |
| 969 |
"BEGIN TRANSACTION; INSERT INTO foo VALUES ('pub txt', 'priv txt'); COMMIT;", |
| 970 |
)?; |
| 971 |
db.query_row_and_then("SELECT * FROM foo", [], |row| -> Result<()> { |
| 972 |
assert_eq!(row.get::<_, String>("public")?, "pub txt"); |
| 973 |
assert!(row.get::<_, Option<String>>("private")?.is_none()); |
| 974 |
Ok(()) |
| 975 |
})?; |
| 976 |
db.execute_batch("DROP TABLE foo").unwrap_err(); |
| 977 |
|
| 978 |
db.authorizer(None::<fn(AuthContext<'_>) -> Authorization>); |
| 979 |
db.execute_batch("PRAGMA user_version=1")?; // Disallowed by first authorizer, but it's now removed. |
| 980 |
|
| 981 |
Ok(()) |
| 982 |
} |
| 983 |
|
| 984 |
#[test] |
| 985 |
fn wal_hook() -> Result<()> { |
| 986 |
let temp_dir = tempfile::tempdir().unwrap(); |
| 987 |
let path = temp_dir.path().join("wal-hook.db3"); |
| 988 |
|
| 989 |
let db = Connection::open(&path)?; |
| 990 |
let journal_mode: String = |
| 991 |
db.pragma_update_and_check(None, "journal_mode", "wal", |row| row.get(0))?; |
| 992 |
assert_eq!(journal_mode, "wal"); |
| 993 |
|
| 994 |
static CALLED: AtomicBool = AtomicBool::new(false); |
| 995 |
db.wal_hook(Some(|wal, pages| { |
| 996 |
assert_eq!(wal.name(), DatabaseName::Main); |
| 997 |
assert!(pages > 0); |
| 998 |
CALLED.swap(true, Ordering::Relaxed); |
| 999 |
wal.checkpoint() |
| 1000 |
})); |
| 1001 |
db.execute_batch("CREATE TABLE x(c);")?; |
| 1002 |
assert!(CALLED.load(Ordering::Relaxed)); |
| 1003 |
|
| 1004 |
db.wal_hook(Some(|wal, pages| { |
| 1005 |
assert!(pages > 0); |
| 1006 |
let (log, ckpt) = wal.checkpoint_v2(super::CheckpointMode::TRUNCATE)?; |
| 1007 |
assert_eq!(log, 0); |
| 1008 |
assert_eq!(ckpt, 0); |
| 1009 |
Ok(()) |
| 1010 |
})); |
| 1011 |
db.execute_batch("CREATE TABLE y(c);")?; |
| 1012 |
|
| 1013 |
db.wal_hook(None); |
| 1014 |
Ok(()) |
| 1015 |
} |
| 1016 |
} |