1use std::ffi::{c_int, c_void};
3use std::panic::catch_unwind;
4use std::time::Duration;
5
6use crate::ffi;
7use crate::{Connection, InnerConnection, Result};
8
9impl Connection {
10 pub fn busy_timeout(&self, timeout: Duration) -> Result<()> {
25 let ms: i32 = timeout
26 .as_secs()
27 .checked_mul(1000)
28 .and_then(|t| t.checked_add(timeout.subsec_millis().into()))
29 .and_then(|t| t.try_into().ok())
30 .expect("too big");
31 self.db.borrow_mut().busy_timeout(ms)
32 }
33
34 pub fn busy_handler<F>(&mut self, callback: Option<F>) -> Result<()>
56 where
57 F: FnMut(i32) -> bool + Send + 'static,
58 {
59 unsafe extern "C" fn busy_handler_callback<F>(p_arg: *mut c_void, count: c_int) -> c_int
60 where
61 F: FnMut(i32) -> bool,
62 {
63 unsafe {
64 c_int::from(
65 catch_unwind(|| {
66 let handler_fn = p_arg.cast::<F>();
67 (*handler_fn)(count)
68 })
69 .unwrap_or_default(),
70 )
71 }
72 }
73 let x = callback.as_ref().map(|_| busy_handler_callback::<F> as _);
74 let mut c = self.db.borrow_mut();
75 c.set_clientdata(c"sqlite3_busy_handler", callback, |db, bh| unsafe {
76 ffi::sqlite3_busy_handler(db, x, bh)
77 })?;
78 Ok(())
79 }
80}
81
82impl InnerConnection {
83 #[inline]
84 fn busy_timeout(&mut self, timeout: c_int) -> Result<()> {
85 let r = unsafe { ffi::sqlite3_busy_timeout(self.db, timeout) };
86 let res = self.decode_result(r);
87 if res.is_ok() {
88 self.set_clientdata(c"sqlite3_busy_handler", None::<c_void>, |_, _| {
89 ffi::SQLITE_OK
90 })?;
91 }
92 res
93 }
94}
95
96#[cfg(all(test, not(miri)))]
97mod test {
98 #[cfg(all(target_family = "wasm", target_os = "unknown"))]
99 use wasm_bindgen_test::wasm_bindgen_test as test;
100
101 use crate::{Connection, ErrorCode, Result, TransactionBehavior};
102 use std::sync::atomic::{AtomicBool, Ordering};
103
104 #[cfg_attr(
105 all(target_family = "wasm", target_os = "unknown"),
106 ignore = "no filesystem on this platform"
107 )]
108 #[test]
109 fn test_default_busy() -> Result<()> {
110 if cfg!(feature = "bundled") {
111 return Ok(());
112 }
113
114 let temp_dir = tempfile::tempdir().unwrap();
115 let path = temp_dir.path().join("test.db3");
116
117 let mut db1 = Connection::open(&path)?;
118 let tx1 = db1.transaction_with_behavior(TransactionBehavior::Exclusive)?;
119 let db2 = Connection::open(&path)?;
120 let r: Result<()> = db2.query_row("PRAGMA schema_version", [], |_| unreachable!());
121 assert_eq!(
122 r.unwrap_err().sqlite_error_code(),
123 Some(ErrorCode::DatabaseBusy)
124 );
125 tx1.rollback()
126 }
127
128 #[cfg_attr(
129 all(target_family = "wasm", target_os = "unknown"),
130 ignore = "no filesystem on this platform"
131 )]
132 #[test]
133 fn test_busy_handler() -> Result<()> {
134 if cfg!(feature = "bundled") {
135 return Ok(());
136 }
137
138 static CALLED: AtomicBool = AtomicBool::new(false);
139 fn busy_handler(n: i32) -> bool {
140 if n > 2 {
141 false
142 } else {
143 CALLED.swap(true, Ordering::Relaxed)
144 }
145 }
146
147 let temp_dir = tempfile::tempdir().unwrap();
148 let path = temp_dir.path().join("busy-handler.db3");
149
150 let mut db1 = Connection::open(&path)?;
151 db1.execute_batch("CREATE TABLE IF NOT EXISTS t(a)")?;
152 let mut db2 = Connection::open(&path)?;
153 db2.busy_handler(Some(busy_handler))?;
154 db1.execute_batch("BEGIN EXCLUSIVE")?;
155 let err = db2.prepare("SELECT * FROM t").unwrap_err();
156 assert_eq!(err.sqlite_error_code(), Some(ErrorCode::DatabaseBusy));
157 assert!(CALLED.load(Ordering::Relaxed));
158 db2.busy_handler(None::<fn(i32) -> bool>)?;
159
160 db1.busy_handler(Some(busy_handler))?;
161 db1.busy_timeout(std::time::Duration::from_millis(5))?;
162 assert!(
163 unsafe { db1.get_clientdata::<std::ffi::c_void, _>(c"sqlite3_busy_handler")? }
164 .is_none()
165 );
166 Ok(())
167 }
168}