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
use super::{ffi, safe, Environment, Handle, Result, Version3};
use super::result::{into_result, into_result_with};
pub struct Connection<'env> {
safe: safe::Connection<'env>,
}
impl<'env> Handle for Connection<'env> {
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>> {
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>> {
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> {
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> safe::Handle for Connection<'env> {
const HANDLE_TYPE: ffi::HandleType = ffi::SQL_HANDLE_DBC;
fn handle(&self) -> ffi::SQLHANDLE {
self.safe.as_raw() as ffi::SQLHANDLE
}
}