1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use super::{ffi, Handle, OdbcObject};
use std::fmt;
use std::ffi::CStr;
use std::error::Error;
pub struct DiagnosticRecord {
state: [ffi::SQLCHAR; ffi::SQL_SQLSTATE_SIZE + 1],
message: [ffi::SQLCHAR; ffi::SQL_MAX_MESSAGE_LENGTH as usize],
message_length: ffi::SQLSMALLINT,
native_error: ffi::SQLINTEGER,
}
impl DiagnosticRecord {
fn new() -> DiagnosticRecord {
DiagnosticRecord {
state: [0u8; ffi::SQL_SQLSTATE_SIZE + 1],
message: [0u8; ffi::SQL_MAX_MESSAGE_LENGTH as usize],
native_error: 0,
message_length: 0,
}
}
pub fn get_raw_state(&self) -> &[u8] {
&self.state
}
pub fn get_raw_message(&self) -> &[u8] {
&self.message[0..self.message_length as usize]
}
pub fn get_native_error(&self) -> i32 {
self.native_error
}
}
impl fmt::Display for DiagnosticRecord {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let state = CStr::from_bytes_with_nul(&self.state).unwrap();
let message = CStr::from_bytes_with_nul(&self.message[0..
(self.message_length as usize + 1)])
.unwrap();
write!(f,
"State: {}, Native error: {}, Message: {}",
state.to_str().unwrap(),
self.native_error,
message.to_str().unwrap())
}
}
impl fmt::Debug for DiagnosticRecord {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl Error for DiagnosticRecord {
fn description(&self) -> &str {
CStr::from_bytes_with_nul(&self.message[0..(self.message_length as usize + 1)])
.unwrap()
.to_str()
.unwrap()
}
fn cause(&self) -> Option<&Error> {
None
}
}
pub trait GetDiagRec {
fn get_diag_rec(&self, record_number: i16) -> Option<DiagnosticRecord>;
}
impl<H> GetDiagRec for H
where H: Handle,
H::To: OdbcObject
{
fn get_diag_rec(&self, record_number: i16) -> Option<(DiagnosticRecord)> {
let mut result = DiagnosticRecord::new();
match unsafe {
ffi::SQLGetDiagRec(H::To::handle_type(),
self.handle() as ffi::SQLHANDLE,
record_number,
result.state.as_mut_ptr(),
&mut result.native_error as *mut ffi::SQLINTEGER,
result.message.as_mut_ptr(),
ffi::SQL_MAX_MESSAGE_LENGTH,
&mut result.message_length as *mut ffi::SQLSMALLINT)
} {
ffi::SQL_SUCCESS => Some(result),
ffi::SQL_NO_DATA => None,
ffi::SQL_SUCCESS_WITH_INFO => Some(result),
ffi::SQL_ERROR => {
if record_number > 0 {
panic!("SQLGetDiagRec returned SQL_ERROR")
} else {
panic!("record number start at 1 has been {}", record_number)
}
}
_ => panic!("SQLGetDiag returned unexpected result"),
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn formatting() {
let message = b"[Microsoft][ODBC Driver Manager] Function sequence error\0";
let mut rec = DiagnosticRecord::new();
rec.state = b"HY010\0".clone();
rec.message_length = 56;
for i in 0..(rec.message_length as usize) {
rec.message[i] = message[i];
}
assert_eq!(format!("{}", rec),
"State: HY010, Native error: 0, Message: [Microsoft][ODBC Driver Manager] \
Function sequence error");
}
}