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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use super::{
as_handle::AsHandle,
buffer::mut_buf_ptr,
drop_handle,
sql_char::{
binary_length, is_truncated_bin, resize_to_fit_with_tz, resize_to_fit_without_tz, SqlChar,
SqlText,
},
sql_result::ExtSqlReturn,
statement::StatementImpl,
OutputStringBuffer, SqlResult,
};
use odbc_sys::{
CompletionType, ConnectionAttribute, DriverConnectOption, HDbc, HEnv, HStmt, HWnd, Handle,
HandleType, InfoType, Pointer, SQLAllocHandle, SQLDisconnect, SQLEndTran, IS_UINTEGER,
};
use std::{ffi::c_void, marker::PhantomData, mem::size_of, ptr::null_mut};
#[cfg(feature = "narrow")]
use odbc_sys::{
SQLConnect as sql_connect, SQLDriverConnect as sql_driver_connect,
SQLGetConnectAttr as sql_get_connect_attr, SQLGetInfo as sql_get_info,
SQLSetConnectAttr as sql_set_connect_attr,
};
#[cfg(not(feature = "narrow"))]
use odbc_sys::{
SQLConnectW as sql_connect, SQLDriverConnectW as sql_driver_connect,
SQLGetConnectAttrW as sql_get_connect_attr, SQLGetInfoW as sql_get_info,
SQLSetConnectAttrW as sql_set_connect_attr,
};
pub struct Connection<'c> {
parent: PhantomData<&'c HEnv>,
handle: HDbc,
}
unsafe impl<'c> AsHandle for Connection<'c> {
fn as_handle(&self) -> Handle {
self.handle as Handle
}
fn handle_type(&self) -> HandleType {
HandleType::Dbc
}
}
impl<'c> Drop for Connection<'c> {
fn drop(&mut self) {
unsafe {
drop_handle(self.handle as Handle, HandleType::Dbc);
}
}
}
impl<'c> Connection<'c> {
pub unsafe fn new(handle: HDbc) -> Self {
Self {
handle,
parent: PhantomData,
}
}
pub fn as_sys(&self) -> HDbc {
self.handle
}
pub fn connect(
&mut self,
data_source_name: &SqlText,
user: &SqlText,
pwd: &SqlText,
) -> SqlResult<()> {
unsafe {
sql_connect(
self.handle,
data_source_name.ptr(),
data_source_name.len_char().try_into().unwrap(),
user.ptr(),
user.len_char().try_into().unwrap(),
pwd.ptr(),
pwd.len_char().try_into().unwrap(),
)
.into_sql_result("SQLConnect")
}
}
pub fn connect_with_connection_string(&mut self, connection_string: &SqlText) -> SqlResult<()> {
unsafe {
let parent_window = null_mut();
let mut completed_connection_string = OutputStringBuffer::empty();
self.driver_connect(
connection_string,
parent_window,
&mut completed_connection_string,
DriverConnectOption::NoPrompt,
)
.unwrap()
}
}
pub unsafe fn driver_connect(
&mut self,
connection_string: &SqlText,
parent_window: HWnd,
completed_connection_string: &mut OutputStringBuffer,
driver_completion: DriverConnectOption,
) -> Option<SqlResult<()>> {
sql_driver_connect(
self.handle,
parent_window,
connection_string.ptr(),
connection_string.len_char().try_into().unwrap(),
completed_connection_string.mut_buf_ptr(),
completed_connection_string.buf_len(),
completed_connection_string.mut_actual_len_ptr(),
driver_completion,
)
.into_opt_sql_result("SQLDriverConnect")
}
pub fn disconnect(&mut self) -> SqlResult<()> {
unsafe { SQLDisconnect(self.handle).into_sql_result("SQLDisconnect") }
}
pub fn allocate_statement(&self) -> SqlResult<StatementImpl<'_>> {
let mut out = null_mut();
unsafe {
SQLAllocHandle(HandleType::Stmt, self.as_handle(), &mut out)
.into_sql_result("SQLAllocHandle")
.on_success(|| StatementImpl::new(out as HStmt))
}
}
pub fn set_autocommit(&self, enabled: bool) -> SqlResult<()> {
let val = if enabled { 1u32 } else { 0u32 };
unsafe {
sql_set_connect_attr(
self.handle,
ConnectionAttribute::AutoCommit,
val as Pointer,
0,
)
.into_sql_result("SQLSetConnectAttr")
}
}
pub fn commit(&self) -> SqlResult<()> {
unsafe {
SQLEndTran(HandleType::Dbc, self.as_handle(), CompletionType::Commit)
.into_sql_result("SQLEndTran")
}
}
pub fn rollback(&self) -> SqlResult<()> {
unsafe {
SQLEndTran(HandleType::Dbc, self.as_handle(), CompletionType::Rollback)
.into_sql_result("SQLEndTran")
}
}
pub fn fetch_database_management_system_name(&self, buf: &mut Vec<SqlChar>) -> SqlResult<()> {
let mut string_length_in_bytes: i16 = 0;
buf.resize(buf.capacity(), 0);
unsafe {
let mut res = sql_get_info(
self.handle,
InfoType::DbmsName,
mut_buf_ptr(buf) as Pointer,
binary_length(buf).try_into().unwrap(),
&mut string_length_in_bytes as *mut i16,
)
.into_sql_result("SQLGetInfo");
if res.is_err() {
return res;
}
if is_truncated_bin(buf, string_length_in_bytes.try_into().unwrap()) {
resize_to_fit_with_tz(buf, string_length_in_bytes.try_into().unwrap());
res = sql_get_info(
self.handle,
InfoType::DbmsName,
mut_buf_ptr(buf) as Pointer,
binary_length(buf).try_into().unwrap(),
&mut string_length_in_bytes as *mut i16,
)
.into_sql_result("SQLGetInfo");
if res.is_err() {
return res;
}
}
resize_to_fit_without_tz(buf, string_length_in_bytes.try_into().unwrap());
res
}
}
fn info_u16(&self, info_type: InfoType) -> SqlResult<u16> {
unsafe {
let mut value = 0u16;
sql_get_info(
self.handle,
info_type,
&mut value as *mut u16 as Pointer,
size_of::<*mut u16>() as i16,
null_mut(),
)
.into_sql_result("SQLGetInfo")
.on_success(|| value)
}
}
pub fn max_catalog_name_len(&self) -> SqlResult<u16> {
self.info_u16(InfoType::MaxCatalogNameLen)
}
pub fn max_schema_name_len(&self) -> SqlResult<u16> {
self.info_u16(InfoType::MaxSchemaNameLen)
}
pub fn max_table_name_len(&self) -> SqlResult<u16> {
self.info_u16(InfoType::MaxTableNameLen)
}
pub fn max_column_name_len(&self) -> SqlResult<u16> {
self.info_u16(InfoType::MaxColumnNameLen)
}
pub fn fetch_current_catalog(&self, buffer: &mut Vec<SqlChar>) -> SqlResult<()> {
let mut string_length_in_bytes: i32 = 0;
buffer.resize(buffer.capacity(), 0);
unsafe {
let mut res = sql_get_connect_attr(
self.handle,
ConnectionAttribute::CurrentCatalog,
mut_buf_ptr(buffer) as Pointer,
binary_length(buffer).try_into().unwrap(),
&mut string_length_in_bytes as *mut i32,
)
.into_sql_result("SQLGetConnectAttr");
if res.is_err() {
return res;
}
if is_truncated_bin(buffer, string_length_in_bytes.try_into().unwrap()) {
resize_to_fit_with_tz(buffer, string_length_in_bytes.try_into().unwrap());
res = sql_get_connect_attr(
self.handle,
ConnectionAttribute::CurrentCatalog,
mut_buf_ptr(buffer) as Pointer,
binary_length(buffer).try_into().unwrap(),
&mut string_length_in_bytes as *mut i32,
)
.into_sql_result("SQLGetConnectAttr");
}
if res.is_err() {
return res;
}
resize_to_fit_without_tz(buffer, string_length_in_bytes.try_into().unwrap());
res
}
}
pub fn is_dead(&self) -> SqlResult<bool> {
unsafe {
self.attribute_u32(ConnectionAttribute::ConnectionDead)
.map(|v| match v {
0 => false,
1 => true,
other => panic!("Unexpected result value from SQLGetConnectAttr: {}", other),
})
}
}
unsafe fn attribute_u32(&self, attribute: ConnectionAttribute) -> SqlResult<u32> {
let mut out: u32 = 0;
sql_get_connect_attr(
self.handle,
attribute,
&mut out as *mut u32 as *mut c_void,
IS_UINTEGER,
null_mut(),
)
.into_sql_result("SQLGetConnectAttr")
.on_success(|| out)
}
}