odbc_api/handles/
sql_result.rs1use odbc_sys::SqlReturn;
2
3#[derive(Debug, PartialEq, Eq, Clone, Copy)]
7pub enum SqlResult<T> {
8 Success(T),
10 SuccessWithInfo(T),
12 NoData,
14 NeedData,
17 StillExecuting,
19 Error {
21 function: &'static str,
25 },
26}
27
28impl SqlResult<()> {
29 pub fn on_success<F, T>(self, f: F) -> SqlResult<T>
31 where
32 F: FnOnce() -> T,
33 {
34 self.map(|()| f())
35 }
36}
37
38impl<T> SqlResult<T> {
39 pub fn is_err(&self) -> bool {
41 matches!(self, SqlResult::Error { .. })
42 }
43
44 pub fn map<U, F>(self, f: F) -> SqlResult<U>
46 where
47 F: FnOnce(T) -> U,
48 {
49 match self {
50 SqlResult::Success(v) => SqlResult::Success(f(v)),
51 SqlResult::SuccessWithInfo(v) => SqlResult::SuccessWithInfo(f(v)),
52 SqlResult::Error { function } => SqlResult::Error { function },
53 SqlResult::StillExecuting => SqlResult::StillExecuting,
54 SqlResult::NoData => SqlResult::NoData,
55 SqlResult::NeedData => SqlResult::NeedData,
56 }
57 }
58
59 pub fn on_no_data<F>(self, f: F) -> SqlResult<T>
62 where
63 F: FnOnce() -> T,
64 {
65 match self {
66 Self::NoData => SqlResult::Success(f()),
67 other => other,
68 }
69 }
70
71 pub fn on_need_data<F>(self, f: F) -> SqlResult<T>
74 where
75 F: FnOnce() -> T,
76 {
77 match self {
78 Self::NeedData => SqlResult::Success(f()),
79 other => other,
80 }
81 }
82
83 pub fn unwrap(self) -> T {
84 match self {
85 SqlResult::Success(v) | SqlResult::SuccessWithInfo(v) => v,
86 _ => panic!("Invalid unwrapping of SqlResult"),
87 }
88 }
89}
90
91pub trait ExtSqlReturn {
92 fn into_sql_result(self, function_name: &'static str) -> SqlResult<()>;
93}
94
95impl ExtSqlReturn for SqlReturn {
96 fn into_sql_result(self, function: &'static str) -> SqlResult<()> {
97 match self {
98 SqlReturn::SUCCESS => SqlResult::Success(()),
99 SqlReturn::SUCCESS_WITH_INFO => SqlResult::SuccessWithInfo(()),
100 SqlReturn::ERROR => SqlResult::Error { function },
101 SqlReturn::NO_DATA => SqlResult::NoData,
102 SqlReturn::NEED_DATA => SqlResult::NeedData,
103 SqlReturn::STILL_EXECUTING => SqlResult::StillExecuting,
104 r => panic!("Unexpected return value '{r:?}' for ODBC function '{function}'"),
105 }
106 }
107}