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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use super::*;
use odbc_sys::*;
use std::marker::PhantomData;
#[derive(Debug)]
pub struct Statement<'con, 'param, 'col, C = NoCursor, A = Unprepared> {
cursor: PhantomData<C>,
access_plan: PhantomData<A>,
parameters: PhantomData<&'param [u8]>,
columns: PhantomData<&'col [u8]>,
handle: HStmt<'con>,
}
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub enum Open {}
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub enum NoCursor {}
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub enum Positioned {}
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub enum Prepared {}
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub enum Unprepared {}
pub trait CursorState {}
impl CursorState for Open {}
impl CursorState for Positioned {}
impl<'con, 'param, 'col, S, A> Statement<'con, 'param, 'col, S, A> {
pub fn as_raw(&self) -> SQLHSTMT {
self.handle.as_raw()
}
pub fn bind_input_parameter<'p, T>(
mut self,
parameter_number: SQLUSMALLINT,
parameter_type: DataType,
value: Option<&'p T>,
) -> Return<Statement<'con, 'p, 'col, S, A>, Self>
where
T: CDataType + ?Sized,
'param: 'p,
{
unsafe {
match self.handle.bind_input_parameter(
parameter_number,
parameter_type,
value,
) {
Success(()) => Success(self.transit()),
Info(()) => Info(self.transit()),
Error(()) => Error(self.transit()),
}
}
}
pub fn bind_col<'col_new, T>(
mut self,
column_number: SQLUSMALLINT,
value: &'col_new mut T,
indicator: &'col_new mut SQLLEN,
) -> Return<Statement<'con, 'param, 'col_new, S, A>, Self>
where
T: CDataType + ?Sized,
'col: 'col_new,
{
unsafe {
match self.handle.bind_col(column_number, value, indicator) {
Success(()) => Success(self.transit()),
Info(()) => Info(self.transit()),
Error(()) => Error(self.transit()),
}
}
}
pub fn reset_parameters(mut self) -> Statement<'con, 'static, 'col, S, A> {
self.handle.reset_parameters().unwrap();
self.transit()
}
pub fn reset_columns(mut self) -> Statement<'con, 'param, 'static, S, A> {
self.handle.reset_columns().unwrap();
self.transit()
}
fn transit<'p, 'c, S2, A2>(self) -> Statement<'con, 'p, 'c, S2, A2> {
Statement {
handle: self.handle,
parameters: PhantomData,
columns: PhantomData,
cursor: PhantomData,
access_plan: PhantomData,
}
}
}
impl<'con, 'param, 'col, C, A> Statement<'con, 'param, 'col, C, A>
where
C: CursorState,
{
pub fn num_result_cols(&self) -> Return<SQLSMALLINT> {
self.handle.num_result_cols()
}
pub fn fetch(
mut self,
) -> ReturnOption<
Statement<'con, 'param, 'col, Positioned, A>,
Statement<'con, 'param, 'col, NoCursor, A>,
> {
match self.handle.fetch() {
ReturnOption::Success(()) => ReturnOption::Success(self.transit()),
ReturnOption::Info(()) => ReturnOption::Info(self.transit()),
ReturnOption::NoData(()) => ReturnOption::NoData(self.transit()),
ReturnOption::Error(()) => ReturnOption::Error(self.transit()),
}
}
pub fn close_cursor(
mut self,
) -> Return<Statement<'con, 'param, 'col, NoCursor>, Statement<'con, 'param, 'col, C, A>> {
match self.handle.close_cursor() {
Success(()) => Success(self.transit()),
Info(()) => Info(self.transit()),
Error(()) => Error(self.transit()),
}
}
}
impl<'con, 'param, 'col> Statement<'con, 'param, 'col, NoCursor, Unprepared> {
pub fn with_parent(parent: &'con Connection) -> Return<Self> {
HStmt::allocate(parent.as_hdbc()).map(|handle| {
Statement {
handle,
parameters: PhantomData,
cursor: PhantomData,
columns: PhantomData,
access_plan: PhantomData,
}
})
}
pub fn prepare<T>(
mut self,
statement_text: &T,
) -> Return<
Statement<'con, 'param, 'col, NoCursor, Prepared>,
Statement<'con, 'param, 'col, NoCursor>,
>
where
T: SqlStr + ?Sized,
{
match self.handle.prepare(statement_text) {
Success(()) => Success(self.transit()),
Info(()) => Info(self.transit()),
Error(()) => Error(self.transit()),
}
}
pub fn exec_direct<T>(
mut self,
statement_text: &T,
) -> ReturnOption<
ResultSet<'con, 'param, 'col, Unprepared>,
Statement<'con, 'param, 'col, NoCursor>,
>
where
T: SqlStr + ?Sized,
{
match self.handle.exec_direct(statement_text) {
ReturnOption::Success(()) => ReturnOption::Success(self.transit()),
ReturnOption::Info(()) => ReturnOption::Info(self.transit()),
ReturnOption::NoData(()) => ReturnOption::NoData(self.transit()),
ReturnOption::Error(()) => ReturnOption::Error(self.transit()),
}
}
}
impl<'con, 'param, 'col> Statement<'con, 'param, 'col, NoCursor, Prepared> {
pub fn execute(mut self) -> ReturnOption<ResultSet<'con, 'param, 'col, Prepared>, Self> {
match self.handle.execute() {
ReturnOption::Success(()) => ReturnOption::Success(self.transit()),
ReturnOption::Info(()) => ReturnOption::Info(self.transit()),
ReturnOption::Error(()) => ReturnOption::Error(self.transit()),
ReturnOption::NoData(()) => ReturnOption::NoData(self.transit()),
}
}
}
impl<'con, 'param, 'col, A> Statement<'con, 'param, 'col, Positioned, A> {
pub fn get_data<T>(
&mut self,
col_or_param_num: SQLUSMALLINT,
target: &mut T,
) -> ReturnOption<Indicator>
where
T: CDataType + ?Sized,
{
self.handle.get_data(col_or_param_num, target)
}
}
impl<'con, 'param, 'col, C> Diagnostics for Statement<'con, 'param, 'col, C> {
fn diagnostics(
&self,
rec_number: SQLSMALLINT,
message_text: &mut [SQLCHAR],
) -> ReturnOption<DiagResult> {
self.handle.diagnostics(rec_number, message_text)
}
}