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
use super::{
as_handle::AsHandle, buffer::buf_ptr, drop_handle, error::Error, error::IntoResult,
statement::Statement,
};
use odbc_sys::{
CompletionType, ConnectionAttribute, DriverConnectOption, HDbc, HEnv, HStmt, Handle,
HandleType, Pointer, SQLAllocHandle, SQLConnectW, SQLDisconnect, SQLDriverConnectW, SQLEndTran,
SQLSetConnectAttrW,
};
use std::{convert::TryInto, marker::PhantomData, ptr::null_mut};
use widestring::U16Str;
pub struct Connection<'c> {
parent: PhantomData<&'c HEnv>,
handle: HDbc,
}
unsafe impl<'c> AsHandle for Connection<'c> {
fn as_handle(&self) -> Handle {
self.handle as Handle
}
fn handle_type(&self) -> HandleType {
HandleType::Dbc
}
}
impl<'c> Drop for Connection<'c> {
fn drop(&mut self) {
unsafe {
drop_handle(self.handle as Handle, HandleType::Dbc);
}
}
}
impl<'c> Connection<'c> {
pub unsafe fn new(handle: HDbc) -> Self {
Self {
handle,
parent: PhantomData,
}
}
pub fn connect(
&mut self,
data_source_name: &U16Str,
user: &U16Str,
pwd: &U16Str,
) -> Result<(), Error> {
unsafe {
SQLConnectW(
self.handle,
buf_ptr(data_source_name.as_slice()),
data_source_name.len().try_into().unwrap(),
buf_ptr(user.as_slice()),
user.len().try_into().unwrap(),
buf_ptr(pwd.as_slice()),
pwd.len().try_into().unwrap(),
)
.into_result(self)?;
Ok(())
}
}
pub fn connect_with_connection_string(
&mut self,
connection_string: &U16Str,
) -> Result<(), Error> {
unsafe {
let window_handle = null_mut();
let out_connection_string = null_mut();
let out_connection_string_len = null_mut();
SQLDriverConnectW(
self.handle,
window_handle,
buf_ptr(connection_string.as_slice()),
connection_string.len().try_into().unwrap(),
out_connection_string,
0,
out_connection_string_len,
DriverConnectOption::NoPrompt,
)
.into_result(self)?;
Ok(())
}
}
pub fn disconnect(&mut self) -> Result<(), Error> {
unsafe { SQLDisconnect(self.handle).into_result(self) }
}
pub fn allocate_statement(&self) -> Result<Statement, Error> {
let mut out = null_mut();
unsafe {
SQLAllocHandle(HandleType::Stmt, self.as_handle(), &mut out).into_result(self)?;
Ok(Statement::new(out as HStmt))
}
}
pub fn set_autocommit(&mut self, enabled: bool) -> Result<(), Error> {
let val = if enabled { 1u32 } else { 0u32 };
unsafe {
SQLSetConnectAttrW(
self.handle,
ConnectionAttribute::AutoCommit,
val as Pointer,
0,
)
.into_result(self)
}
}
pub fn commit(&mut self) -> Result<(), Error> {
unsafe {
SQLEndTran(HandleType::Dbc, self.as_handle(), CompletionType::Commit).into_result(self)
}
}
pub fn rollback(&mut self) -> Result<(), Error> {
unsafe {
SQLEndTran(HandleType::Dbc, self.as_handle(), CompletionType::Rollback)
.into_result(self)
}
}
}