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
use odbc_sys::SqlReturn;
#[derive(Debug)]
pub enum SqlResult<T> {
Success(T),
SuccessWithInfo(T),
Error {
function: &'static str,
},
}
impl SqlResult<()> {
pub fn on_success<F, T>(self, f: F) -> SqlResult<T>
where
F: FnOnce() -> T,
{
self.map(|()| f())
}
}
impl<T> SqlResult<T> {
pub fn is_err(&self) -> bool {
matches!(self, SqlResult::Error { .. })
}
pub fn map<U, F>(self, f: F) -> SqlResult<U>
where
F: FnOnce(T) -> U,
{
match self {
SqlResult::Success(v) => SqlResult::Success(f(v)),
SqlResult::SuccessWithInfo(v) => SqlResult::SuccessWithInfo(f(v)),
SqlResult::Error { function } => SqlResult::Error { function },
}
}
pub fn unwrap(self) -> T {
match self {
SqlResult::Success(v) | SqlResult::SuccessWithInfo(v) => v,
SqlResult::Error { .. } => panic!("Unwraping SqlResult::Error"),
}
}
}
pub trait ExtSqlReturn {
fn into_sql_result(self, function_name: &'static str) -> SqlResult<()>;
fn into_opt_sql_result(self, function_name: &'static str) -> Option<SqlResult<()>>;
}
impl ExtSqlReturn for SqlReturn {
fn into_sql_result(self, function: &'static str) -> SqlResult<()> {
match self {
SqlReturn::SUCCESS => SqlResult::Success(()),
SqlReturn::SUCCESS_WITH_INFO => SqlResult::SuccessWithInfo(()),
SqlReturn::ERROR => SqlResult::Error { function },
r => panic!(
"Unexpected return value '{:?}' for ODBC function '{}'",
r, function
),
}
}
fn into_opt_sql_result(self, function: &'static str) -> Option<SqlResult<()>> {
match self {
SqlReturn::NO_DATA => None,
other => Some(other.into_sql_result(function)),
}
}
}