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
use {ffi, Raii, Return, Handle, Statement, Result, Prepared, Allocated, NoResult, ResultSetState};
impl<'a, 'b> Statement<'a, 'b, Allocated, NoResult> {
pub fn prepare(mut self, sql_text: &str) -> Result<Statement<'a, 'b, Prepared, NoResult>> {
self.raii.prepare(sql_text).into_result(&mut self)?;
Ok(Statement::with_raii(self.raii))
}
}
impl<'a, 'b> Statement<'a, 'b, Prepared, NoResult> {
pub fn execute(mut self) -> Result<ResultSetState<'a, 'b, Prepared>> {
if self.raii.execute().into_result(&mut self)? {
Ok(ResultSetState::Data(Statement::with_raii(self.raii)))
} else {
Ok(ResultSetState::NoData(Statement::with_raii(self.raii)))
}
}
}
impl Raii<ffi::Stmt> {
fn prepare(&mut self, sql_text: &str) -> Return<()> {
match unsafe {
ffi::SQLPrepare(self.handle(),
sql_text.as_bytes().as_ptr(),
sql_text.as_bytes().len() as ffi::SQLINTEGER)
} {
ffi::SQL_SUCCESS => Return::Success(()),
ffi::SQL_SUCCESS_WITH_INFO => Return::SuccessWithInfo(()),
ffi::SQL_ERROR => Return::Error,
r => panic!("SQLPrepare returned unexpected result: {:?}", r),
}
}
fn execute(&mut self) -> Return<bool> {
match unsafe { ffi::SQLExecute(self.handle()) } {
ffi::SQL_SUCCESS => Return::Success(true),
ffi::SQL_SUCCESS_WITH_INFO => Return::SuccessWithInfo(true),
ffi::SQL_ERROR => Return::Error,
ffi::SQL_NO_DATA => Return::Success(false),
r => panic!("SQLExecute returned unexpected result: {:?}", r),
}
}
}