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
use cassandra::data_type::ConstDataType;
use cassandra::statement::Statement;
use cassandra::util::Protected;
use cassandra_sys::CassPrepared as _PreparedStatement;
use cassandra_sys::cass_prepared_bind;
use cassandra_sys::cass_prepared_free;
use cassandra_sys::cass_prepared_parameter_data_type;
use cassandra_sys::cass_prepared_parameter_data_type_by_name;
use cassandra_sys::cass_prepared_parameter_name;
use std::{mem, slice, str};
use std::ffi::CString;
#[derive(Debug)]
pub struct PreparedStatement(*const _PreparedStatement);
unsafe impl Sync for PreparedStatement {}
unsafe impl Send for PreparedStatement {}
impl Drop for PreparedStatement {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe { cass_prepared_free(self.0) }
}
}
}
impl Protected<*const _PreparedStatement> for PreparedStatement {
fn inner(&self) -> *const _PreparedStatement { self.0 }
fn build(inner: *const _PreparedStatement) -> Self { PreparedStatement(inner) }
}
impl PreparedStatement {
pub fn bind(&self) -> Statement { unsafe { Statement::build(cass_prepared_bind(self.0)) } }
#[allow(cast_possible_truncation)]
pub fn parameter_name(&self, index: usize) -> Result<&str, str::Utf8Error> {
unsafe {
let mut name = mem::zeroed();
let mut name_length = mem::zeroed();
cass_prepared_parameter_name(self.0, index, &mut name, &mut name_length);
str::from_utf8(slice::from_raw_parts(name as *const u8, name_length as usize))
}
}
pub fn parameter_data_type(&self, index: usize) -> ConstDataType {
unsafe { ConstDataType(cass_prepared_parameter_data_type(self.0, index)) }
}
pub fn parameter_data_type_by_name(&self, name: &str) -> ConstDataType {
unsafe {
ConstDataType(cass_prepared_parameter_data_type_by_name(self.0,
CString::new(name)
.expect("must be utf8")
.as_ptr()))
}
}
}