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