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
mod list_data_sources;
pub use self::list_data_sources::{DataSourceInfo, DriverInfo};
use super::{ffi, into_result, safe, try_into_option, DiagnosticRecord, GetDiagRec, Handle, Result};
use std;
pub type Version3 = safe::Odbc3;
pub static mut OS_ENCODING: &encoding_rs::Encoding = encoding_rs::UTF_8;
pub static mut DB_ENCODING: &encoding_rs::Encoding = encoding_rs::UTF_8;
#[derive(Debug)]
pub struct Environment<V> {
safe: safe::Environment<V>,
}
impl<V> Handle for Environment<V> {
type To = ffi::Env;
unsafe fn handle(&self) -> ffi::SQLHENV {
self.safe.as_raw()
}
}
impl<V: safe::Version> Environment<V> {
pub fn new() -> std::result::Result<Environment<V>, Option<DiagnosticRecord>> {
let safe = match safe::Environment::new() {
safe::Success(v) => v,
safe::Info(v) => {
warn!("{}", v.get_diag_rec(1).unwrap_or_else(DiagnosticRecord::empty));
v
}
safe::Error(()) => return Err(None),
};
let safe = into_result(safe.declare_version())?;
Ok(Environment { safe })
}
pub(crate) fn as_safe(&self) -> &safe::Environment<V> {
&self.safe
}
}
unsafe impl<V> safe::Handle for Environment<V> {
const HANDLE_TYPE : ffi::HandleType = ffi::SQL_HANDLE_ENV;
fn handle(&self) -> ffi::SQLHANDLE {
self.safe.as_raw() as ffi::SQLHANDLE
}
}
pub fn create_environment_v3()
-> std::result::Result<Environment<Version3>, Option<DiagnosticRecord>>
{
Environment::new()
}
pub fn create_environment_v3_with_os_db_encoding(os_encoding: &str, db_encoding: &str)
-> std::result::Result<Environment<Version3>, Option<DiagnosticRecord>>
{
unsafe {
OS_ENCODING = encoding_rs::Encoding::for_label(os_encoding.as_bytes()).unwrap();
DB_ENCODING = encoding_rs::Encoding::for_label(db_encoding.as_bytes()).unwrap();
}
Environment::new()
}