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
use std::io;
use thiserror::Error as ThisError;
use crate::handles::{log_diagnostics, AsHandle, Record as DiagnosticRecord, SqlResult};
#[derive(Debug, ThisError)]
pub enum Error {
#[error("Failed to set connection pooling.")]
FailedSettingConnectionPooling,
#[error("Failed to allocate ODBC Environment.")]
FailedAllocatingEnvironment,
#[error(
"No Diagnostics available. The ODBC function call to {} returned an error. Sadly neither the
ODBC driver manager, nor the driver were polite enough to leave a diagnostic record
specifying what exactly went wrong.", function
)]
NoDiagnostics {
function: &'static str,
},
#[error("ODBC emitted an error calling '{function}':\n{record}")]
Diagnostics {
record: DiagnosticRecord,
function: &'static str,
},
#[error("The dialog shown to provide or complete the connection string has been aborted.")]
AbortedConnectionStringCompletion,
#[error(
"ODBC diver manager does not seem to support the required ODBC version 3.80. (Most \
likely you need to update unixODBC if you run on a Linux. Diagnostic record returned by \
SQLSetEnvAttr:\n{0}"
)]
UnsupportedOdbcApiVersion(DiagnosticRecord),
#[error("Sending data to the database at statement execution time failed. IO error:\n{0}")]
FailedReadingInput(io::Error),
#[error(
"An invalid row array size (aka. batch size) has been set. The ODBC drivers should just \
emit a warning and emmit smaller batches, but not all do (yours does not at least). Try \
fetching data from the database in smaller batches.\nRow array size (aka. batch size): \
{size}\n Diagnostic record returned by SQLSetEnvAttr:\n{record}"
)]
InvalidRowArraySize {
record: DiagnosticRecord,
size: usize,
},
}
impl<T> SqlResult<T> {
pub fn into_result(self, handle: &dyn AsHandle) -> Result<T, Error> {
match self {
SqlResult::Success(value) => Ok(value),
SqlResult::SuccessWithInfo(value) => {
log_diagnostics(handle);
Ok(value)
}
SqlResult::Error { function } => {
let mut record = DiagnosticRecord::default();
if record.fill_from(handle, 1) {
log_diagnostics(handle);
Err(Error::Diagnostics { record, function })
} else {
Err(Error::NoDiagnostics { function })
}
}
}
}
}