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
use {ffi, ColumnDescriptor, Raii, Return, Handle, Statement, Result, Prepared, Allocated,
NoResult, ResultSetState};
use odbc_safe::AutocommitMode;
impl<'a, 'b, AC: AutocommitMode> Statement<'a, 'b, Allocated, NoResult, AC> {
pub fn prepare(mut self, sql_text: &str) -> Result<Statement<'a, 'b, Prepared, NoResult, AC>> {
self.raii.prepare(sql_text).into_result(&mut self)?;
Ok(Statement::with_raii(self.raii))
}
pub fn prepare_bytes(mut self, bytes: &[u8]) -> Result<Statement<'a, 'b, Prepared, NoResult, AC>> {
self.raii.prepare_byte(bytes).into_result(&mut self)?;
Ok(Statement::with_raii(self.raii))
}
}
impl<'a, 'b, AC: AutocommitMode> Statement<'a, 'b, Prepared, NoResult, AC> {
pub fn num_result_cols(&self) -> Result<i16> {
self.raii.num_result_cols().into_result(self)
}
pub fn describe_col(&self, idx: u16) -> Result<ColumnDescriptor> {
self.raii.describe_col(idx).into_result(self)
}
pub fn execute(mut self) -> Result<ResultSetState<'a, 'b, Prepared, AC>> {
if self.raii.execute().into_result(&mut self)? {
let num_cols = self.raii.num_result_cols().into_result(&self)?;
if num_cols > 0 {
Ok(ResultSetState::Data(Statement::with_raii(self.raii)))
} else {
Ok(ResultSetState::NoData(Statement::with_raii(self.raii)))
}
} else {
Ok(ResultSetState::NoData(Statement::with_raii(self.raii)))
}
}
}
impl<'p> Raii<'p, ffi::Stmt> {
fn prepare(&mut self, sql_text: &str) -> Return<()> {
let bytes = unsafe { crate::environment::DB_ENCODING }.encode(sql_text).0;
match unsafe {
ffi::SQLPrepare(
self.handle(),
bytes.as_ptr(),
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 prepare_byte(&mut self, bytes: &[u8]) -> Return<()> {
match unsafe {
ffi::SQLPrepare(
self.handle(),
bytes.as_ptr(),
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),
}
}
}