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
use super::{as_handle::AsHandle, error::ToResult, logging::log_diagnostics, Connection, Error};
use log::debug;
use odbc_sys::{
AttrOdbcVersion, EnvironmentAttribute, HDbc, HEnv, Handle, HandleType, SQLAllocHandle,
SQLFreeHandle, SQLSetEnvAttr, SqlReturn,
};
use std::{ptr::null_mut, thread::panicking};
#[derive(Debug)]
pub struct Environment {
handle: HEnv,
}
unsafe impl AsHandle for Environment {
fn as_handle(&self) -> Handle {
self.handle as Handle
}
fn handle_type(&self) -> HandleType {
HandleType::Env
}
}
impl Drop for Environment {
fn drop(&mut self) {
unsafe {
match SQLFreeHandle(HandleType::Env, self.handle as Handle) {
SqlReturn::SUCCESS => (),
other => {
if !panicking() {
panic!("Unexepected return value of SQLFreeHandle: {:?}", other)
}
}
}
}
}
}
impl Environment {
pub unsafe fn new() -> Result<Self, Error> {
let mut handle = null_mut();
let (handle, info) = match SQLAllocHandle(HandleType::Env, null_mut(), &mut handle) {
SqlReturn::ERROR => return Err(Error::NoDiagnostics),
SqlReturn::SUCCESS => (handle, false),
SqlReturn::SUCCESS_WITH_INFO => (handle, true),
other => panic!(
"Unexpected Return value for allocating ODBC Environment: {:?}",
other
),
};
debug!("ODBC Environment created.");
let env = Environment {
handle: handle as HEnv,
};
if info {
log_diagnostics(&env);
}
Ok(env)
}
pub fn declare_version(&self, version: AttrOdbcVersion) -> Result<(), Error> {
unsafe {
SQLSetEnvAttr(
self.handle,
EnvironmentAttribute::OdbcVersion,
version.into(),
0,
)
.to_result(self)
}
}
pub fn allocate_connection(&self) -> Result<Connection, Error> {
let mut handle = null_mut();
unsafe {
SQLAllocHandle(HandleType::Dbc, self.as_handle(), &mut handle).to_result(self)?;
Ok(Connection::new(handle as HDbc))
}
}
pub fn as_raw(&self) -> HEnv {
self.handle
}
}