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
use super::{ffi, safe, Environment, Handle, Result, Version3};
use super::result::{into_result, into_result_with};
use odbc_safe::{AutocommitMode, AutocommitOn, AutocommitOff};
#[derive(Debug)]
pub struct Connection<'env, AC: AutocommitMode> {
safe: safe::Connection<'env, AC>,
}
impl<'env, AC: AutocommitMode> Handle for Connection<'env, AC> {
type To = ffi::Dbc;
unsafe fn handle(&self) -> ffi::SQLHDBC {
self.safe.as_raw()
}
}
impl Environment<Version3> {
pub fn connect<'env>(&'env self, dsn: &str, usr: &str, pwd: &str) -> Result<Connection<'env, AutocommitOn>> {
let safe = into_result_with(self, safe::DataSource::with_parent(self.as_safe()))?;
let safe = into_result(safe.connect(dsn, usr, pwd))?;
Ok(Connection { safe })
}
pub fn connect_with_connection_string<'env>(
&'env self,
connection_str: &str,
) -> Result<Connection<'env, AutocommitOn>> {
let safe = into_result_with(self, safe::DataSource::with_parent(self.as_safe()))?;
let safe = into_result(safe.connect_with_connection_string(connection_str))?;
Ok(Connection { safe })
}
}
impl <'env> Connection<'env, AutocommitOn> {
pub fn disable_autocommit(mut self) -> std::result::Result<Connection<'env, AutocommitOff>, Self> {
let ret = self.safe.disable_autocommit();
match ret {
safe::Return::Success(value) => Ok(Connection { safe: value }),
safe::Return::Info(value) => Ok(Connection { safe: value }),
safe::Return::Error(value) => Err(Connection { safe: value })
}
}
}
impl <'env> Connection<'env, AutocommitOff> {
pub fn enable_autocommit(mut self) -> std::result::Result<Connection<'env, AutocommitOn>, Self> {
let ret = self.safe.enable_autocommit();
match ret {
safe::Return::Success(value) => Ok(Connection { safe: value }),
safe::Return::Info(value) => Ok(Connection { safe: value }),
safe::Return::Error(value) => Err(Connection { safe: value })
}
}
pub fn commit(&mut self) -> Result<()> {
let ret = self.safe.commit();
into_result_with(&self.safe, ret)
}
pub fn rollback(&mut self) -> Result<()> {
let ret = self.safe.rollback();
into_result_with(&self.safe, ret)
}
}
impl<'env, AC: AutocommitMode> Connection<'env, AC> {
pub fn is_read_only(&mut self) -> Result<bool> {
let ret = self.safe.is_read_only();
into_result_with(&self.safe, ret)
}
pub fn disconnect(self) -> Result<()> {
into_result(self.safe.disconnect())?;
Ok(())
}
}
unsafe impl<'env, AC: AutocommitMode> safe::Handle for Connection<'env, AC> {
const HANDLE_TYPE: ffi::HandleType = ffi::SQL_HANDLE_DBC;
fn handle(&self) -> ffi::SQLHANDLE {
self.safe.as_raw() as ffi::SQLHANDLE
}
}