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
extern crate r2d2;
extern crate odbc;
extern crate odbc_safe as safe;
#[macro_use]
extern crate lazy_static;
use std::error::Error;
use std::fmt;
use odbc::*;
#[derive(Debug)]
pub struct ODBCConnectionManager {
connection_string: String
}
#[derive(Debug)]
pub struct ODBCConnectionManagerTx {
connection_string: String
}
pub struct ODBCConnection<'a, AC: safe::AutocommitMode>(Connection<'a, AC>);
unsafe impl Send for ODBCConnection<'static, safe::AutocommitOn> {}
unsafe impl Send for ODBCConnection<'static, safe::AutocommitOff> {}
impl <'a, AC: safe::AutocommitMode> ODBCConnection<'a, AC> {
pub fn raw(&self) -> &Connection<'a, AC> {
&self.0
}
}
pub struct ODBCEnv(Environment<Version3>);
unsafe impl Sync for ODBCEnv {}
unsafe impl Send for ODBCEnv {}
#[derive(Debug)]
pub struct ODBCError(Box<dyn Error>);
lazy_static! {
static ref ENV: ODBCEnv = ODBCEnv(create_environment_v3().unwrap());
}
impl Error for ODBCError {
fn description(&self) -> &str {
"Error connecting DB"
}
}
impl fmt::Display for ODBCError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl From<DiagnosticRecord> for ODBCError {
fn from(err: DiagnosticRecord) -> Self {
println!("ODBC ERROR {}", err);
ODBCError(Box::new(err))
}
}
impl <E: 'static> From<std::sync::PoisonError<E>> for ODBCError {
fn from(err: std::sync::PoisonError<E>) -> Self {
ODBCError(Box::new(err))
}
}
impl ODBCConnectionManager {
pub fn new<S: Into<String>>(connection_string: S) -> ODBCConnectionManager
{
ODBCConnectionManager {
connection_string: connection_string.into()
}
}
}
impl ODBCConnectionManagerTx {
pub fn new<S: Into<String>>(connection_string: S) -> ODBCConnectionManagerTx
{
ODBCConnectionManagerTx {
connection_string: connection_string.into()
}
}
}
impl r2d2::ManageConnection for ODBCConnectionManager {
type Connection = ODBCConnection<'static, safe::AutocommitOn>;
type Error = ODBCError;
fn connect(&self) -> std::result::Result<Self::Connection, Self::Error> {
let env = &ENV.0;
Ok(ODBCConnection(env.connect_with_connection_string(&self.connection_string)?))
}
fn is_valid(&self, _conn: &mut Self::Connection) -> std::result::Result<(), Self::Error> {
Ok(())
}
fn has_broken(&self, _conn: &mut Self::Connection) -> bool {
false
}
}
impl r2d2::ManageConnection for ODBCConnectionManagerTx {
type Connection = ODBCConnection<'static, safe::AutocommitOff>;
type Error = ODBCError;
fn connect(&self) -> std::result::Result<Self::Connection, Self::Error> {
let env = &ENV.0;
let conn = env.connect_with_connection_string(&self.connection_string)?;
let conn_result = conn.disable_autocommit();
match conn_result {
Ok(conn) => Ok(ODBCConnection(conn)),
_ => Err(ODBCError("Unable to use transactions".into()))
}
}
fn is_valid(&self, _conn: &mut Self::Connection) -> std::result::Result<(), Self::Error> {
Ok(())
}
fn has_broken(&self, _conn: &mut Self::Connection) -> bool {
false
}
}