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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use std::io;
use thiserror::Error as ThisError;
use crate::handles::{log_diagnostics, Diagnostics, Record as DiagnosticRecord, SqlResult, State};
#[derive(Debug)]
pub struct TooLargeBufferSize {
pub num_elements: usize,
pub element_size: usize,
}
impl TooLargeBufferSize {
pub fn add_context(self, buffer_index: u16) -> Error {
Error::TooLargeColumnBufferSize {
buffer_index,
num_elements: self.num_elements,
element_size: self.element_size,
}
}
}
#[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(
"The ODBC diver manager installed in your system does not seem to support ODBC API version
3.80. Which is required by this application. Most likely you need to update your driver
manager. Your driver manager is most likely 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,
},
#[error(
"SQLFetch came back with an error indicating you specified an invalid SQL Type. You very
likely did not do that however. Actually SQLFetch is not supposed to return that error type.
You should have received it back than you were still binding columns or parameters. All this
is circumstancial evidence that you are using an Oracle Database and want to use 64Bit
integers, which are not supported by Oracles ODBC driver manager. In case this diagnose is
wrong the original error is:\n{0}."
)]
OracleOdbcDriverDoesNotSupport64Bit(DiagnosticRecord),
#[error(
"There is not enough memory to allocate enough memory for a column buffer. Number of \
elements requested for the column buffer: {num_elements}; Size of an element: \
{element_size}."
)]
TooLargeColumnBufferSize {
buffer_index: u16,
num_elements: usize,
element_size: usize,
},
#[error(
"The number of diagnostic records returned by ODBC seems to exceed `32,767` This means not
all (diagnostic) records could be inspected. This in turn may be problematic if invariants
rely on checking that certain errors did not occurr. Usually this many warnings are only
generated, if one or more warnings per row is generated, and then there are many rows. Maybe
try fewer rows, or fix the cause of some of these warnings/errors?"
)]
TooManyDiagnostics,
#[error(
"A value (at least one) is too large to be written into the allocated buffer without
truncation."
)]
TooLargeValueForBuffer,
}
impl Error {
fn provide_context_for_diagnostic<F>(self, f: F) -> Self
where
F: FnOnce(DiagnosticRecord, &'static str) -> Error,
{
if let Error::Diagnostics { record, function } = self {
f(record, function)
} else {
self
}
}
}
pub(crate) trait ExtendResult {
fn provide_context_for_diagnostic<F>(self, f: F) -> Self
where
F: FnOnce(DiagnosticRecord, &'static str) -> Error;
}
impl<T> ExtendResult for Result<T, Error> {
fn provide_context_for_diagnostic<F>(self, f: F) -> Self
where
F: FnOnce(DiagnosticRecord, &'static str) -> Error,
{
self.map_err(|error| error.provide_context_for_diagnostic(f))
}
}
impl<T> SqlResult<T> {
pub fn into_result(self, handle: &impl Diagnostics) -> Result<T, Error> {
let error_for_truncation = false;
self.into_result_with_trunaction_check(handle, error_for_truncation)
}
pub fn into_result_with_trunaction_check(
self,
handle: &impl Diagnostics,
error_for_truncation: bool,
) -> Result<T, Error> {
match self {
SqlResult::Success(value) => Ok(value),
SqlResult::SuccessWithInfo(value) => {
log_diagnostics(handle);
if error_for_truncation {
check_for_truncation(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 })
}
}
}
}
}
fn check_for_truncation(handle: &impl Diagnostics) -> Result<(), Error> {
let mut empty = [];
let mut rec_number = 1;
while let Some(result) = handle.diagnostic_record(1, &mut empty) {
if result.state == State::STRING_DATA_RIGHT_TRUNCATION {
return Err(Error::TooLargeValueForBuffer);
}
if rec_number == i16::MAX {
return Err(Error::TooManyDiagnostics);
}
rec_number += 1;
}
Ok(())
}