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
#![allow(non_camel_case_types)]
#![allow(dead_code)]
#![allow(missing_copy_implementations)]
use crate::cassandra::data_type::ConstDataType;
use crate::cassandra::error::*;
use crate::cassandra::row::Row;
use crate::cassandra::util::Protected;
use crate::cassandra::value::ValueType;
use crate::cassandra_sys::cass_false;
use crate::cassandra_sys::cass_iterator_free;
use crate::cassandra_sys::cass_iterator_from_result;
use crate::cassandra_sys::cass_iterator_get_row;
use crate::cassandra_sys::cass_iterator_next;
use crate::cassandra_sys::cass_result_column_count;
use crate::cassandra_sys::cass_result_column_data_type;
use crate::cassandra_sys::cass_result_column_name;
use crate::cassandra_sys::cass_result_column_type;
use crate::cassandra_sys::cass_result_first_row;
use crate::cassandra_sys::cass_result_free;
use crate::cassandra_sys::cass_result_has_more_pages;
use crate::cassandra_sys::cass_result_paging_state_token;
use crate::cassandra_sys::cass_result_row_count;
use crate::cassandra_sys::cass_true;
use crate::cassandra_sys::CassIterator as _CassIterator;
use crate::cassandra_sys::CassResult as _CassResult;
use std::ffi::CString;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::marker::PhantomData;
use std::mem;
use std::slice;
use std::str;
pub struct CassResult(*const _CassResult);
unsafe impl Sync for CassResult {}
unsafe impl Send for CassResult {}
impl Protected<*const _CassResult> for CassResult {
fn inner(&self) -> *const _CassResult {
self.0
}
fn build(inner: *const _CassResult) -> Self {
if inner.is_null() {
panic!("Unexpected null pointer")
};
CassResult(inner)
}
}
impl Debug for CassResult {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Result row count: {:?}\n", self.row_count())?;
for row in self.iter() {
write!(f, "{:?}\n", row)?;
}
Ok(())
}
}
impl Display for CassResult {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Result row count: {}\n", self.row_count())?;
for row in self.iter() {
write!(f, "{}\n", row)?;
}
Ok(())
}
}
impl Drop for CassResult {
fn drop(&mut self) {
unsafe { cass_result_free(self.0) }
}
}
impl CassResult {
pub fn row_count(&self) -> u64 {
unsafe { cass_result_row_count(self.0) as u64 }
}
pub fn column_count(&self) -> u64 {
unsafe { cass_result_column_count(self.0) as u64 }
}
pub fn column_name(&self, index: usize) -> Result<&str> {
unsafe {
let mut name = mem::zeroed();
let mut name_length = mem::zeroed();
cass_result_column_name(self.0, index, &mut name, &mut name_length)
.to_result(())
.and_then(|_| {
let slice = slice::from_raw_parts(name as *const u8, name_length as usize);
Ok(str::from_utf8(slice)?)
})
}
}
pub fn column_type(&self, index: usize) -> ValueType {
unsafe { ValueType::build(cass_result_column_type(self.0, index)) }
}
pub fn column_data_type(&self, index: usize) -> ConstDataType {
unsafe { ConstDataType::build(cass_result_column_data_type(self.0, index)) }
}
pub fn first_row(&self) -> Option<Row> {
unsafe {
match self.row_count() {
0 => None,
_ => Some(Row::build(cass_result_first_row(self.0))),
}
}
}
pub fn has_more_pages(&self) -> bool {
unsafe { cass_result_has_more_pages(self.0) == cass_true }
}
pub fn set_paging_state_token(&mut self, paging_state: &str) -> Result<&mut Self> {
unsafe {
let state = CString::new(paging_state)?;
cass_result_paging_state_token(
self.0,
&mut state.as_ptr(),
&mut (state.to_bytes().len()),
)
.to_result(self)
}
}
pub fn iter(&self) -> ResultIterator {
unsafe { ResultIterator(cass_iterator_from_result(self.0), PhantomData) }
}
}
#[derive(Debug)]
pub struct ResultIterator<'a>(pub *mut _CassIterator, PhantomData<&'a CassResult>);
unsafe impl<'a> Send for ResultIterator<'a> {}
impl<'a> Drop for ResultIterator<'a> {
fn drop(&mut self) {
unsafe { cass_iterator_free(self.0) }
}
}
impl<'a> Iterator for ResultIterator<'a> {
type Item = Row;
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe {
match cass_iterator_next(self.0) {
cass_false => None,
cass_true => Some(self.get_row()),
}
}
}
}
impl<'a> ResultIterator<'a> {
pub fn get_row(&mut self) -> Row {
unsafe { Row::build(cass_iterator_get_row(self.0)) }
}
}
impl<'a> IntoIterator for &'a CassResult {
type Item = Row;
type IntoIter = ResultIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}