Skip to main content

diesel/mysql_like/connection/stmt/
mod.rs

1#![allow(unsafe_code)] // module uses ffi
2use alloc::ffi::CString;
3use core::ffi as libc;
4use core::ffi::CStr;
5use core::marker::PhantomData;
6use core::ptr::NonNull;
7use mysqlclient_sys as ffi;
8
9use super::bind::{OutputBinds, PreparedStatementBinds};
10use super::error_information::{MysqlLikeErrorInformation, sqlstate_from_ptr};
11use crate::connection::statement_cache::{MaybeCached, PreparedStatement};
12use crate::mysql_like::{MysqlLikeBackend, MysqlType};
13use crate::result::{Error, QueryResult, SqlState};
14
15pub(super) mod iterator;
16mod metadata;
17
18pub(super) use self::metadata::{MysqlFieldMetadata, StatementMetadata};
19
20#[allow(dead_code, missing_debug_implementations)]
21// https://github.com/rust-lang/rust/issues/81658
22pub struct Statement<DB: MysqlLikeBackend> {
23    stmt: NonNull<ffi::MYSQL_STMT>,
24    input_binds: Option<PreparedStatementBinds>,
25    _phantom: PhantomData<DB>,
26}
27
28impl<DB> PreparedStatement for Statement<DB>
29where
30    DB: MysqlLikeBackend,
31{
32    fn parameter_count(&self) -> u64 {
33        // SAFETY:
34        // stmt is a valid pointer to MYSQL_STMT that's not deallocated yet
35        //
36        // On windows that returns a u32
37        unsafe { ffi::mysql_stmt_param_count(self.stmt.as_ptr()) as u64 }
38    }
39}
40
41// mysql connection can be shared between threads according to libmysqlclients documentation
42#[allow(unsafe_code)]
43unsafe impl<DB: MysqlLikeBackend> Send for Statement<DB> {}
44
45impl<DB: MysqlLikeBackend> Statement<DB> {
46    pub(crate) fn new(stmt: NonNull<ffi::MYSQL_STMT>) -> Self {
47        Statement {
48            stmt,
49            input_binds: None,
50            _phantom: PhantomData,
51        }
52    }
53
54    pub fn prepare(&self, query: &str) -> QueryResult<()> {
55        use crate::result::Error::InvalidCString;
56
57        let query = CString::new(query).map_err(InvalidCString)?;
58        unsafe {
59            ffi::mysql_stmt_prepare(
60                self.stmt.as_ptr(),
61                query.as_ptr() as *const libc::c_char,
62                query.count_bytes() as libc::c_ulong,
63            );
64        }
65        self.did_an_error_occur()
66    }
67
68    pub fn bind<Iter>(&mut self, binds: Iter) -> QueryResult<()>
69    where
70        Iter: IntoIterator<Item = (MysqlType, Option<Vec<u8>>)>,
71    {
72        let input_binds = PreparedStatementBinds::from_input_data(binds);
73        self.input_bind(input_binds)
74    }
75
76    pub(super) fn input_bind(
77        &mut self,
78        mut input_binds: PreparedStatementBinds,
79    ) -> QueryResult<()> {
80        if input_binds.len() != self.parameter_count() {
81            return Err(
82                crate::connection::statement_cache::invalid_prepared_statement_parameter_count(
83                    self.parameter_count().try_into().unwrap_or(usize::MAX),
84                    input_binds.len().try_into().unwrap_or(usize::MAX),
85                ),
86            );
87        }
88
89        input_binds.with_mysql_binds(|bind_ptr| {
90            // This relies on the invariant that the current value of `self.input_binds`
91            // will not change without this function being called
92            unsafe {
93                ffi::mysql_stmt_bind_param(self.stmt.as_ptr(), bind_ptr);
94            }
95        });
96        self.input_binds = Some(input_binds);
97        self.did_an_error_occur()
98    }
99
100    fn last_error_message(&self) -> String {
101        unsafe { CStr::from_ptr(ffi::mysql_stmt_error(self.stmt.as_ptr())) }
102            .to_string_lossy()
103            .into_owned()
104    }
105
106    fn last_error_sqlstate(&self) -> Option<SqlState> {
107        // SAFETY: `self.stmt` is the live statement handle owned by `self`.
108        let sqlstate = unsafe { ffi::mysql_stmt_sqlstate(self.stmt.as_ptr()) };
109        // SAFETY: the result is null or the NUL terminated SQLSTATE stored on the
110        // statement handle, which no call changes while `self` is borrowed here.
111        unsafe { sqlstate_from_ptr(sqlstate) }
112    }
113
114    ///we return a Option here because mariadb sometimes doesn't provide the statement metadata before the execute
115    pub(super) fn metadata(&self) -> QueryResult<Option<StatementMetadata>> {
116        let result_ptr = unsafe { ffi::mysql_stmt_result_metadata(self.stmt.as_ptr()) };
117        self.did_an_error_occur()?;
118        Ok(NonNull::new(result_ptr).map(StatementMetadata::new))
119    }
120
121    pub(super) fn did_an_error_occur(&self) -> QueryResult<()> {
122        use crate::result::Error::DatabaseError;
123
124        let error_message = self.last_error_message();
125        if error_message.is_empty() {
126            Ok(())
127        } else {
128            let error_number = self.last_error_number();
129            Err(DatabaseError(
130                DB::map_error_number(error_number),
131                Box::new(MysqlLikeErrorInformation {
132                    message: error_message,
133                    sqlstate: self.last_error_sqlstate(),
134                    native_code: i32::try_from(error_number).ok().filter(|&code| code != 0),
135                }),
136            ))
137        }
138    }
139
140    fn last_error_number(&self) -> u32 {
141        // SAFETY: `self.stmt` is the live statement handle owned by `self`.
142        unsafe { ffi::mysql_stmt_errno(self.stmt.as_ptr()) }
143    }
144
145    /// If the pointers referenced by the `MYSQL_BIND` structures are invalidated,
146    /// you must call this function again before calling `mysql_stmt_fetch`.
147    pub unsafe fn bind_result(&self, binds: *mut ffi::MYSQL_BIND) -> QueryResult<()> {
148        unsafe {
149            ffi::mysql_stmt_bind_result(self.stmt.as_ptr(), binds);
150        }
151        self.did_an_error_occur()
152    }
153}
154
155impl<'a, DB: MysqlLikeBackend> MaybeCached<'a, Statement<DB>> {
156    pub(super) fn execute_statement(
157        self,
158        binds: &mut OutputBinds,
159    ) -> QueryResult<StatementUse<'a, DB>> {
160        unsafe {
161            binds.with_mysql_binds(|bind_ptr| self.bind_result(bind_ptr))?;
162            self.execute()
163        }
164    }
165
166    /// This function should be called instead of `results` on queries which
167    /// have no return value. It should never be called on a statement on
168    /// which `results` has previously been called?
169    pub(super) unsafe fn execute(self) -> QueryResult<StatementUse<'a, DB>> {
170        unsafe {
171            ffi::mysql_stmt_execute(self.stmt.as_ptr());
172        }
173        self.did_an_error_occur()?;
174        unsafe {
175            ffi::mysql_stmt_store_result(self.stmt.as_ptr());
176        }
177        let ret = StatementUse { inner: self };
178        ret.inner.did_an_error_occur()?;
179        Ok(ret)
180    }
181}
182
183impl<DB: MysqlLikeBackend> Drop for Statement<DB> {
184    fn drop(&mut self) {
185        unsafe { ffi::mysql_stmt_close(self.stmt.as_ptr()) };
186    }
187}
188
189#[allow(missing_debug_implementations)]
190pub(super) struct StatementUse<'a, DB: MysqlLikeBackend> {
191    inner: MaybeCached<'a, Statement<DB>>,
192}
193
194impl<DB: MysqlLikeBackend> StatementUse<'_, DB> {
195    pub(in crate::mysql_like::connection) fn affected_rows(&self) -> QueryResult<usize> {
196        let affected_rows = unsafe { ffi::mysql_stmt_affected_rows(self.inner.stmt.as_ptr()) };
197        affected_rows
198            .try_into()
199            .map_err(|e| Error::DeserializationError(Box::new(e)))
200    }
201
202    /// The executed statement's `mysql_stmt_insert_id`, zero when it set no
203    /// `AUTO_INCREMENT` value.
204    ///
205    /// The C API calls that zero "undefined", meaning an unspecified value and
206    /// not undefined behaviour: only a statement returning a result set can
207    /// inherit a stale id from the connection.
208    pub(in crate::mysql_like::connection) fn insert_id(&self) -> u64 {
209        // SAFETY: `self.inner` holds a live `MYSQL_STMT` and this reads an
210        // initialised scalar field.
211        unsafe { ffi::mysql_stmt_insert_id(self.inner.stmt.as_ptr()) }
212    }
213
214    /// This function should be called after `execute` only
215    /// otherwise it's not guaranteed to return a valid result
216    pub(in crate::mysql_like::connection) unsafe fn result_size(&mut self) -> QueryResult<usize> {
217        let size = unsafe { ffi::mysql_stmt_num_rows(self.inner.stmt.as_ptr()) };
218        usize::try_from(size).map_err(|e| Error::DeserializationError(Box::new(e)))
219    }
220
221    pub(super) fn populate_row_buffers(&self, binds: &mut OutputBinds) -> QueryResult<Option<()>> {
222        // We're about to call `mysql_stmt_fetch` we need to check if our binds are still valid
223        if binds.are_invalid() {
224            binds.bind_results(self)?;
225        }
226        let next_row_result = unsafe { ffi::mysql_stmt_fetch(self.inner.stmt.as_ptr()) };
227        if next_row_result < 0 {
228            self.inner.did_an_error_occur().map(Some)
229        } else {
230            #[allow(clippy::cast_sign_loss)] // that's how it's supposed to be based on the API
231            match next_row_result as libc::c_uint {
232                ffi::MYSQL_NO_DATA => Ok(None),
233                ffi::MYSQL_DATA_TRUNCATED => binds.populate_dynamic_buffers(self).map(Some),
234                0 => {
235                    binds.update_buffer_lengths();
236                    Ok(Some(()))
237                }
238                _error => self.inner.did_an_error_occur().map(Some),
239            }
240        }
241    }
242
243    pub(in crate::mysql_like::connection) unsafe fn fetch_column(
244        &self,
245        bind: &mut ffi::MYSQL_BIND,
246        idx: usize,
247        offset: usize,
248    ) -> QueryResult<()> {
249        unsafe {
250            ffi::mysql_stmt_fetch_column(
251                self.inner.stmt.as_ptr(),
252                bind,
253                idx.try_into()
254                    .map_err(|e| Error::DeserializationError(Box::new(e)))?,
255                offset as libc::c_ulong,
256            );
257        }
258        self.inner.did_an_error_occur()
259    }
260
261    /// If the pointers referenced by the `MYSQL_BIND` structures are invalidated,
262    /// you must call this function again before calling `mysql_stmt_fetch`.
263    pub(in crate::mysql_like::connection) unsafe fn bind_result(
264        &self,
265        binds: *mut ffi::MYSQL_BIND,
266    ) -> QueryResult<()> {
267        unsafe { self.inner.bind_result(binds) }
268    }
269
270    pub(super) fn metadata(&self) -> QueryResult<StatementMetadata> {
271        use crate::result::Error::DeserializationError;
272
273        //At this stage the statement was executed and should therefore provide its metadata
274        self.inner
275            .metadata()?
276            .ok_or_else(|| DeserializationError("No metadata exists".into()))
277    }
278}
279
280impl<DB: MysqlLikeBackend> Drop for StatementUse<'_, DB> {
281    fn drop(&mut self) {
282        unsafe {
283            ffi::mysql_stmt_free_result(self.inner.stmt.as_ptr());
284        }
285    }
286}