sqlx_sqlite/connection/
handle.rs

1use std::ffi::CString;
2use std::ptr;
3use std::ptr::NonNull;
4
5use crate::error::Error;
6use libsqlite3_sys::{
7    sqlite3, sqlite3_close, sqlite3_exec, sqlite3_last_insert_rowid, SQLITE_LOCKED_SHAREDCACHE,
8    SQLITE_OK,
9};
10
11use crate::{statement::unlock_notify, SqliteError};
12
13/// Managed handle to the raw SQLite3 database handle.
14/// The database handle will be closed when this is dropped and no `ConnectionHandleRef`s exist.
15#[derive(Debug)]
16pub(crate) struct ConnectionHandle(NonNull<sqlite3>);
17
18// A SQLite3 handle is safe to send between threads, provided not more than
19// one is accessing it at the same time. This is upheld as long as [SQLITE_CONFIG_MULTITHREAD] is
20// enabled and [SQLITE_THREADSAFE] was enabled when sqlite was compiled. We refuse to work
21// if these conditions are not upheld.
22
23// <https://www.sqlite.org/c3ref/threadsafe.html>
24
25// <https://www.sqlite.org/c3ref/c_config_covering_index_scan.html#sqliteconfigmultithread>
26
27unsafe impl Send for ConnectionHandle {}
28
29impl ConnectionHandle {
30    #[inline]
31    pub(super) unsafe fn new(ptr: *mut sqlite3) -> Self {
32        Self(NonNull::new_unchecked(ptr))
33    }
34
35    #[inline]
36    pub(crate) fn as_ptr(&self) -> *mut sqlite3 {
37        self.0.as_ptr()
38    }
39
40    pub(crate) fn as_non_null_ptr(&self) -> NonNull<sqlite3> {
41        self.0
42    }
43
44    pub(crate) fn last_insert_rowid(&mut self) -> i64 {
45        // SAFETY: we have exclusive access to the database handle
46        unsafe { sqlite3_last_insert_rowid(self.as_ptr()) }
47    }
48
49    pub(crate) fn last_error(&mut self) -> Option<SqliteError> {
50        // SAFETY: we have exclusive access to the database handle
51        unsafe { SqliteError::try_new(self.as_ptr()) }
52    }
53
54    #[track_caller]
55    pub(crate) fn expect_error(&mut self) -> SqliteError {
56        self.last_error()
57            .expect("expected error code to be set in current context")
58    }
59
60    pub(crate) fn exec(&mut self, query: impl Into<String>) -> Result<(), Error> {
61        let query = query.into();
62        let query = CString::new(query).map_err(|_| err_protocol!("query contains nul bytes"))?;
63
64        // SAFETY: we have exclusive access to the database handle
65        unsafe {
66            loop {
67                let status = sqlite3_exec(
68                    self.as_ptr(),
69                    query.as_ptr(),
70                    // callback if we wanted result rows
71                    None,
72                    // callback data
73                    ptr::null_mut(),
74                    // out-pointer for the error message, we just use `SqliteError::new()`
75                    ptr::null_mut(),
76                );
77
78                match status {
79                    SQLITE_OK => return Ok(()),
80                    SQLITE_LOCKED_SHAREDCACHE => unlock_notify::wait(self.as_ptr())?,
81                    _ => return Err(SqliteError::new(self.as_ptr()).into()),
82                }
83            }
84        }
85    }
86}
87
88impl Drop for ConnectionHandle {
89    fn drop(&mut self) {
90        unsafe {
91            // https://sqlite.org/c3ref/close.html
92            let status = sqlite3_close(self.0.as_ptr());
93            if status != SQLITE_OK {
94                // this should *only* happen due to an internal bug in SQLite where we left
95                // SQLite handles open
96                panic!("{}", SqliteError::new(self.0.as_ptr()));
97            }
98        }
99    }
100}