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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
use std::{cmp::max, collections::HashMap};
use crate::{handles, Connection, Error};
use odbc_sys::{AttrOdbcVersion, FetchOrientation};
use widestring::{U16CStr, U16Str, U16String};
pub struct Environment {
environment: handles::Environment,
}
impl Environment {
pub unsafe fn new() -> Result<Self, Error> {
let environment = crate::handles::Environment::new()?;
environment.declare_version(AttrOdbcVersion::Odbc3_80)?;
Ok(Self { environment })
}
pub fn connect(
&self,
data_source_name: &str,
user: &str,
pwd: &str,
) -> Result<Connection, Error> {
let data_source_name = U16String::from_str(data_source_name);
let user = U16String::from_str(user);
let pwd = U16String::from_str(pwd);
self.connect_utf16(&data_source_name, &user, &pwd)
}
pub fn connect_utf16(
&self,
data_source_name: &U16Str,
user: &U16Str,
pwd: &U16Str,
) -> Result<Connection, Error> {
let mut connection = self.environment.allocate_connection()?;
connection.connect(data_source_name, user, pwd)?;
Ok(Connection::new(connection))
}
pub fn connect_with_connection_string(
&self,
connection_string: &str,
) -> Result<Connection, Error> {
let connection_string = U16String::from_str(connection_string);
self.connect_with_connection_string_utf16(&connection_string)
}
pub fn connect_with_connection_string_utf16(
&self,
connection_string: &U16Str,
) -> Result<Connection, Error> {
let mut connection = self.environment.allocate_connection()?;
connection.connect_with_connection_string(connection_string)?;
Ok(Connection::new(connection))
}
pub fn drivers(&mut self) -> Result<Vec<DriverInfo>, Error> {
let (mut desc_len, mut attr_len) = if let Some(v) = self
.environment
.drivers_buffer_len(FetchOrientation::First)?
{
v
} else {
return Ok(Vec::new());
};
while let Some((candidate_desc_len, candidate_attr_len)) = self
.environment
.drivers_buffer_len(FetchOrientation::Next)?
{
desc_len = max(candidate_desc_len, desc_len);
attr_len = max(candidate_attr_len, attr_len);
}
let mut desc_buf = vec![0; desc_len as usize + 1];
let mut attr_buf = vec![0; attr_len as usize + 1];
let mut driver_info = Vec::new();
while self.environment.drivers_buffer_fill(
FetchOrientation::Next,
&mut desc_buf,
&mut attr_buf,
)? {
let description = U16CStr::from_slice_with_nul(&desc_buf).unwrap();
let attributes = U16CStr::from_slice_with_nul(&attr_buf).unwrap();
let description = description.to_string().unwrap();
let attributes = attributes.to_string().unwrap();
let attributes = attributes_iter(&attributes).collect();
driver_info.push(DriverInfo {
description,
attributes,
});
}
Ok(driver_info)
}
pub fn data_sources(&mut self) -> Result<Vec<DataSourceInfo>, Error> {
self.data_sources_impl(FetchOrientation::First)
}
pub fn system_data_sources(&mut self) -> Result<Vec<DataSourceInfo>, Error> {
self.data_sources_impl(FetchOrientation::FirstSystem)
}
pub fn user_data_sources(&mut self) -> Result<Vec<DataSourceInfo>, Error> {
self.data_sources_impl(FetchOrientation::FirstUser)
}
fn data_sources_impl(
&mut self,
direction: FetchOrientation,
) -> Result<Vec<DataSourceInfo>, Error> {
let (mut server_name_len, mut driver_len) =
if let Some(v) = self.environment.data_source_buffer_len(direction)? {
v
} else {
return Ok(Vec::new());
};
while let Some((candidate_name_len, candidate_decs_len)) = self
.environment
.drivers_buffer_len(FetchOrientation::Next)?
{
server_name_len = max(candidate_name_len, server_name_len);
driver_len = max(candidate_decs_len, driver_len);
}
let mut server_name_buf = vec![0; server_name_len as usize + 1];
let mut driver_buf = vec![0; driver_len as usize + 1];
let mut data_source_info = Vec::new();
let mut not_empty = self.environment.data_source_buffer_fill(
direction,
&mut server_name_buf,
&mut driver_buf,
)?;
while not_empty {
let server_name = U16CStr::from_slice_with_nul(&server_name_buf).unwrap();
let driver = U16CStr::from_slice_with_nul(&driver_buf).unwrap();
let server_name = server_name.to_string().unwrap();
let driver = driver.to_string().unwrap();
data_source_info.push(DataSourceInfo {
server_name,
driver,
});
not_empty = self.environment.data_source_buffer_fill(
FetchOrientation::Next,
&mut server_name_buf,
&mut driver_buf,
)?;
}
Ok(data_source_info)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DriverInfo {
pub description: String,
pub attributes: HashMap<String, String>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DataSourceInfo {
pub server_name: String,
pub driver: String,
}
fn attributes_iter(attributes: &str) -> impl Iterator<Item = (String, String)> + '_ {
attributes
.split('\0')
.take_while(|kv_str| *kv_str != String::new())
.map(|kv_str| {
let mut iter = kv_str.split('=');
let key = iter.next().unwrap();
let value = iter.next().unwrap();
(key.to_string(), value.to_string())
})
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse_attributes() {
let buffer = "APILevel=2\0ConnectFunctions=YYY\0CPTimeout=60\0DriverODBCVer=03.\
50\0FileUsage=0\0SQLLevel=1\0UsageCount=1\0\0";
let attributes: HashMap<_, _> = attributes_iter(buffer).collect();
assert_eq!(attributes["APILevel"], "2");
assert_eq!(attributes["ConnectFunctions"], "YYY");
assert_eq!(attributes["CPTimeout"], "60");
assert_eq!(attributes["DriverODBCVer"], "03.50");
assert_eq!(attributes["FileUsage"], "0");
assert_eq!(attributes["SQLLevel"], "1");
assert_eq!(attributes["UsageCount"], "1");
}
}