| 1 |
#![allow(unsafe_code)] // module uses ffi |
| 2 |
use std::ffi::CStr; |
| 3 |
use std::ptr::NonNull; |
| 4 |
use std::slice; |
| 5 |
|
| 6 |
use super::ffi; |
| 7 |
use crate::mysql::connection::bind::Flags; |
| 8 |
|
| 9 |
pub(in crate::mysql::connection) struct StatementMetadata { |
| 10 |
result: NonNull<ffi::MYSQL_RES>, |
| 11 |
} |
| 12 |
|
| 13 |
impl StatementMetadata { |
| 14 |
pub(in crate::mysql::connection) fn new(result: NonNull<ffi::MYSQL_RES>) -> Self { |
| 15 |
StatementMetadata { result } |
| 16 |
} |
| 17 |
|
| 18 |
pub(in crate::mysql::connection) fn fields(&'_ self) -> &'_ [MysqlFieldMetadata<'_>] { |
| 19 |
unsafe { |
| 20 |
let num_fields = ffi::mysql_num_fields(self.result.as_ptr()); |
| 21 |
let field_ptr = ffi::mysql_fetch_fields(self.result.as_ptr()); |
| 22 |
if field_ptr.is_null() { |
| 23 |
&[] |
| 24 |
} else { |
| 25 |
slice::from_raw_parts(field_ptr as _, num_fields as usize) |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl Drop for StatementMetadata { |
| 32 |
fn drop(&mut self) { |
| 33 |
unsafe { ffi::mysql_free_result(self.result.as_mut()) }; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
#[repr(transparent)] |
| 38 |
pub(in crate::mysql::connection) struct MysqlFieldMetadata<'a>( |
| 39 |
ffi::MYSQL_FIELD, |
| 40 |
std::marker::PhantomData<&'a ()>, |
| 41 |
); |
| 42 |
|
| 43 |
impl MysqlFieldMetadata<'_> { |
| 44 |
pub(in crate::mysql::connection) fn field_name(&self) -> Option<&str> { |
| 45 |
if self.0.name.is_null() { |
| 46 |
None |
| 47 |
} else { |
| 48 |
unsafe { |
| 49 |
Some(CStr::from_ptr(self.0.name).to_str().expect( |
| 50 |
"Expect mysql field names to be UTF-8, because we \ |
| 51 |
requested UTF-8 encoding on connection setup", |
| 52 |
)) |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
pub(in crate::mysql::connection) fn field_type(&self) -> ffi::enum_field_types { |
| 58 |
self.0.type_ |
| 59 |
} |
| 60 |
|
| 61 |
pub(in crate::mysql::connection) fn flags( |
| 62 |
&self, |
| 63 |
) -> Result<Flags, Box<dyn core::error::Error + Send + Sync>> { |
| 64 |
Flags::try_from(self.0.flags) |
| 65 |
} |
| 66 |
} |