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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use conn_core::connect_params::ConnectParams;
use protocol::parts::command_info::CommandInfo;
use protocol::parts::server_error::ServerError;
use protocol::server_resource_consumption_info::ServerResourceConsumptionInfo;
use {HdbError, HdbResponse, HdbResult};
use prepared_statement::factory as PreparedStatementFactory;
use prepared_statement::PreparedStatement;
use authentication;
use conn_core::{AmConnCore, ConnectionCore};
use protocol::argument::Argument;
use protocol::part::Part;
use protocol::partkind::PartKind;
use protocol::parts::resultset::ResultSet;
use protocol::reply::SkipLastSpace;
use protocol::request::Request;
use protocol::request_type::RequestType;
use xa_impl::new_resource_manager;
use chrono::Local;
use dist_tx::rm::ResourceManager;
use std::error::Error;
use std::sync::Arc;
#[derive(Debug)]
pub struct Connection {
params: ConnectParams,
am_conn_core: AmConnCore,
}
#[allow(unknown_lints)]
#[allow(unit_arg)]
impl Connection {
pub fn new(params: ConnectParams) -> HdbResult<Connection> {
trace!("Entering connect()");
let start = Local::now();
let mut am_conn_core = ConnectionCore::initialize(params.clone())?;
authentication::authenticate(
&mut (am_conn_core),
params.dbuser(),
params.password(),
params.clientlocale(),
)?;
{
let guard = am_conn_core.lock()?;
debug!(
"user \"{}\" successfully logged on ({} µs) to {} of {} (HANA version: {})",
params.dbuser(),
Local::now()
.signed_duration_since(start)
.num_microseconds()
.unwrap_or(-1),
guard.get_database_name(),
guard.get_system_id(),
guard.get_full_version_string()
);
}
Ok(Connection {
params,
am_conn_core,
})
}
pub fn set_auto_commit(&mut self, ac: bool) -> HdbResult<()> {
Ok(self.am_conn_core.lock()?.set_auto_commit(ac))
}
pub fn is_auto_commit(&self) -> HdbResult<bool> {
Ok(self.am_conn_core.lock()?.is_auto_commit())
}
pub fn set_fetch_size(&mut self, fetch_size: u32) -> HdbResult<()> {
Ok(self.am_conn_core.lock()?.set_fetch_size(fetch_size))
}
pub fn get_lob_read_length(&self) -> HdbResult<i32> {
Ok(self.am_conn_core.lock()?.get_lob_read_length())
}
pub fn set_lob_read_length(&mut self, lob_read_length: i32) -> HdbResult<()> {
Ok(self
.am_conn_core
.lock()?
.set_lob_read_length(lob_read_length))
}
pub fn get_server_resource_consumption_info(&self) -> HdbResult<ServerResourceConsumptionInfo> {
Ok(self
.am_conn_core
.lock()?
.server_resource_consumption_info()
.clone())
}
pub fn get_call_count(&self) -> HdbResult<i32> {
Ok(self.am_conn_core.lock()?.last_seq_number())
}
pub fn set_client_info(
&self,
application: &str,
application_version: &str,
application_source: &str,
application_user: &str,
) -> HdbResult<()> {
self.am_conn_core.lock()?.set_client_info(
application,
application_version,
application_source,
application_user,
)
}
pub fn statement(&mut self, stmt: &str) -> HdbResult<HdbResponse> {
execute(&mut self.am_conn_core, String::from(stmt), None)
}
pub fn query(&mut self, stmt: &str) -> HdbResult<ResultSet> {
self.statement(stmt)?.into_resultset()
}
pub fn dml(&mut self, stmt: &str) -> HdbResult<usize> {
let vec = &(self.statement(stmt)?.into_affected_rows()?);
match vec.len() {
1 => Ok(vec[0]),
_ => Err(HdbError::Usage(
"number of affected-rows-counts <> 1".to_owned(),
)),
}
}
pub fn exec(&mut self, stmt: &str) -> HdbResult<()> {
self.statement(stmt)?.into_success()
}
pub fn prepare(&self, stmt: &str) -> HdbResult<PreparedStatement> {
Ok(PreparedStatementFactory::prepare(
Arc::clone(&self.am_conn_core),
String::from(stmt),
)?)
}
pub fn commit(&mut self) -> HdbResult<()> {
self.statement("commit")?.into_success()
}
pub fn rollback(&mut self) -> HdbResult<()> {
self.statement("rollback")?.into_success()
}
pub fn spawn(&self) -> HdbResult<Connection> {
let mut other_conn = Connection::new(self.params.clone())?;
{
let am_conn_core = self.am_conn_core.lock()?;
other_conn.set_auto_commit(am_conn_core.is_auto_commit())?;
other_conn.set_fetch_size(am_conn_core.get_fetch_size())?;
other_conn.set_lob_read_length(am_conn_core.get_lob_read_length())?;
}
Ok(other_conn)
}
pub fn multiple_statements_ignore_err<S: AsRef<str>>(&mut self, stmts: Vec<S>) {
for s in stmts {
trace!("multiple_statements_ignore_err: firing \"{}\"", s.as_ref());
let result = self.statement(s.as_ref());
match result {
Ok(_) => {}
Err(e) => debug!("Error intentionally ignored: {}", e.description()),
}
}
}
pub fn multiple_statements<S: AsRef<str>>(&mut self, stmts: Vec<S>) -> HdbResult<()> {
for s in stmts {
self.statement(s.as_ref())?;
}
Ok(())
}
pub fn pop_warnings(&self) -> HdbResult<Option<Vec<ServerError>>> {
self.am_conn_core.lock()?.pop_warnings()
}
pub fn get_resource_manager(&self) -> Box<ResourceManager> {
Box::new(new_resource_manager(Arc::clone(&self.am_conn_core)))
}
pub fn execute_with_debuginfo(
&mut self,
stmt: &str,
module: &str,
line: i32,
) -> HdbResult<HdbResponse> {
execute(
&mut self.am_conn_core,
String::from(stmt),
Some(CommandInfo::new(line, module)),
)
}
}
fn execute(
am_conn_core: &mut AmConnCore,
stmt: String,
o_ci: Option<CommandInfo>,
) -> HdbResult<HdbResponse> {
debug!("connection::execute({})", stmt);
let command_options = 0b_1000;
let fetch_size: u32 = { am_conn_core.lock()?.get_fetch_size() };
let mut request = Request::new(RequestType::ExecuteDirect, command_options);
request.push(Part::new(
PartKind::FetchSize,
Argument::FetchSize(fetch_size),
));
if let Option::Some(ci) = o_ci {
request.push(Part::new(PartKind::CommandInfo, Argument::CommandInfo(ci)));
}
{
let mut guard = am_conn_core.lock()?;
if guard.is_client_info_touched() {
request.push(Part::new(
PartKind::ClientInfo,
Argument::ClientInfo(guard.get_client_info_for_sending()),
));
}
}
request.push(Part::new(PartKind::Command, Argument::Command(stmt)));
request.send_and_get_hdbresponse(None, None, am_conn_core, None, SkipLastSpace::Soft)
}