use odpi::structs::ODPIErrorInfo;
use std::ffi::CStr;
use std::{fmt, slice};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Info {
code: i32,
offset: u16,
message: String,
fn_name: String,
action: String,
sql_state: String,
recoverable: bool,
}
impl Info {
pub fn new(
code: i32,
offset: u16,
message: String,
fn_name: String,
action: String,
sql_state: String,
recoverable: bool,
) -> Self {
Self {
code,
offset,
message,
fn_name,
action,
sql_state,
recoverable,
}
}
pub fn code(&self) -> i32 {
self.code
}
pub fn offset(&self) -> u16 {
self.offset
}
pub fn message(&self) -> &str {
&self.message
}
pub fn fn_name(&self) -> &str {
&self.fn_name
}
pub fn action(&self) -> &str {
&self.action
}
pub fn sql_state(&self) -> &str {
&self.sql_state
}
pub fn recoverable(&self) -> bool {
self.recoverable
}
}
impl fmt::Display for Info {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(
f,
"{}: {}\nfn: {}\naction: {}\nsql_state: {}\nrecoverable: {}",
self.code, self.message, self.fn_name, self.action, self.sql_state, self.recoverable
)
}
}
impl From<ODPIErrorInfo> for Info {
fn from(err: ODPIErrorInfo) -> Self {
let slice =
unsafe { slice::from_raw_parts(err.message as *mut u8, err.message_length as usize) };
let fn_name = unsafe { CStr::from_ptr(err.fn_name) }
.to_string_lossy()
.into_owned();
let action = unsafe { CStr::from_ptr(err.action) }
.to_string_lossy()
.into_owned();
let sql_state = unsafe { CStr::from_ptr(err.sql_state) }
.to_string_lossy()
.into_owned();
Self::new(
err.code,
err.offset,
String::from_utf8_lossy(slice).into_owned(),
fn_name,
action,
sql_state,
err.is_recoverable.is_positive(),
)
}
}