diesel/mysql_like/connection/error_information.rs
1#![allow(unsafe_code)] // module uses ffi
2use core::ffi::{CStr, c_char};
3
4use crate::result::{DatabaseErrorInformation, SqlState};
5
6pub(super) struct MysqlLikeErrorInformation {
7 pub(super) message: String,
8 pub(super) sqlstate: Option<SqlState>,
9}
10
11/// Reads the result of `mysql_sqlstate` or `mysql_stmt_sqlstate`.
12///
13/// # Safety
14///
15/// `sqlstate` must be null or point to a NUL terminated buffer that stays
16/// unchanged for the duration of the call.
17pub(super) unsafe fn sqlstate_from_ptr(sqlstate: *const c_char) -> Option<SqlState> {
18 if sqlstate.is_null() {
19 return None;
20 }
21 // SAFETY: non-null, and the caller guarantees NUL termination and stability.
22 let bytes = unsafe { CStr::from_ptr(sqlstate) }.to_bytes();
23 <[u8; 5]>::try_from(bytes).ok().and_then(SqlState::new)
24}
25
26impl DatabaseErrorInformation for MysqlLikeErrorInformation {
27 fn message(&self) -> &str {
28 &self.message
29 }
30
31 fn details(&self) -> Option<&str> {
32 None
33 }
34
35 fn hint(&self) -> Option<&str> {
36 None
37 }
38
39 fn table_name(&self) -> Option<&str> {
40 None
41 }
42
43 fn column_name(&self) -> Option<&str> {
44 None
45 }
46
47 fn constraint_name(&self) -> Option<&str> {
48 None
49 }
50
51 fn statement_position(&self) -> Option<i32> {
52 None
53 }
54
55 fn sqlstate(&self) -> Option<SqlState> {
56 self.sqlstate
57 }
58}