discord_cassandra_cpp/cassandra/
collection.rs

1use crate::cassandra::data_type::ConstDataType;
2use crate::cassandra::data_type::DataType;
3use crate::cassandra::error::*;
4use crate::cassandra::inet::Inet;
5use crate::cassandra::tuple::Tuple;
6use crate::cassandra::user_type::UserType;
7use crate::cassandra::util::{Protected, ProtectedInner};
8use crate::cassandra::uuid::Uuid;
9
10use crate::cassandra_sys::cass_collection_append_bool;
11use crate::cassandra_sys::cass_collection_append_bytes;
12use crate::cassandra_sys::cass_collection_append_collection;
13use crate::cassandra_sys::cass_collection_append_decimal;
14use crate::cassandra_sys::cass_collection_append_double;
15use crate::cassandra_sys::cass_collection_append_float;
16use crate::cassandra_sys::cass_collection_append_inet;
17use crate::cassandra_sys::cass_collection_append_int16;
18use crate::cassandra_sys::cass_collection_append_int32;
19use crate::cassandra_sys::cass_collection_append_int64;
20use crate::cassandra_sys::cass_collection_append_int8;
21use crate::cassandra_sys::cass_collection_append_string_n;
22use crate::cassandra_sys::cass_collection_append_tuple;
23use crate::cassandra_sys::cass_collection_append_uint32;
24use crate::cassandra_sys::cass_collection_append_user_type;
25use crate::cassandra_sys::cass_collection_append_uuid;
26use crate::cassandra_sys::cass_collection_data_type;
27use crate::cassandra_sys::cass_collection_free;
28use crate::cassandra_sys::cass_collection_new;
29use crate::cassandra_sys::cass_collection_new_from_data_type;
30use crate::cassandra_sys::cass_false;
31use crate::cassandra_sys::cass_true;
32use crate::cassandra_sys::CassCollection as _CassCollection;
33use crate::cassandra_sys::CASS_COLLECTION_TYPE_LIST;
34use crate::cassandra_sys::CASS_COLLECTION_TYPE_MAP;
35use crate::cassandra_sys::CASS_COLLECTION_TYPE_SET;
36
37use std::os::raw::c_char;
38
39// #[repr(C)]
40// #[derive(Debug,Copy,Clone)]
41// pub enum CassCollectionType {
42//    CASS_COLLECTION_TYPE_LIST,
43//    CASS_COLLECTION_TYPE_MAP,
44//    CASS_COLLECTION_TYPE_SET,
45// }
46
47/// A generic Cassandra collection that needs to go away
48pub trait CassCollection {
49    /// The type of value held by this collection
50    type Value;
51
52    /// Creates a new collection.
53    fn new(item_count: usize) -> Self;
54
55    /// Creates a new collection from an existing data type.
56    fn new_from_data_type(value: DataType, item_count: usize) -> Self;
57
58    /// Gets the data type of a collection.
59    fn data_type(&self) -> ConstDataType;
60
61    /// Appends a "tinyint" to the collection.
62    fn append_int8(&mut self, value: i8) -> Result<&mut Self>;
63
64    /// Appends an "smallint" to the collection.
65    fn append_int16(&mut self, value: i16) -> Result<&mut Self>;
66
67    /// Appends an "int" to the collection.
68    fn append_int32(&mut self, value: i32) -> Result<&mut Self>;
69
70    /// Appends a "date" to the collection.
71    fn append_uint32(&mut self, value: u32) -> Result<&mut Self>;
72
73    /// Appends a "bigint", "counter", "timestamp" or "time" to the
74    /// collection.
75    fn append_int64(&mut self, value: i64) -> Result<&mut Self>;
76
77    /// Appends a "float" to the collection.
78    fn append_float(&mut self, value: f32) -> Result<&mut Self>;
79
80    /// Appends a "double" to the collection.
81    fn append_double(&mut self, value: f64) -> Result<&mut Self>;
82
83    /// Appends a "boolean" to the collection.
84    fn append_bool(&mut self, value: bool) -> Result<&mut Self>;
85
86    /// Appends an "ascii", "text" or "varchar" to the collection.
87    fn append_string(&mut self, value: &str) -> Result<&mut Self>;
88
89    /// Appends a "blob", "varint" or "custom" to the collection.
90    fn append_bytes(&mut self, value: Vec<u8>) -> Result<&mut Self>;
91
92    /// Appends a "uuid" or "timeuuid"  to the collection.
93    fn append_uuid(&mut self, value: Uuid) -> Result<&mut Self>;
94
95    /// Appends an "inet" to the collection.
96    fn append_inet(&mut self, value: Inet) -> Result<&mut Self>;
97
98    /// Appends a "list" to the collection.
99    fn append_list(&mut self, value: List) -> Result<&mut Self>;
100
101    /// Appends a "set" to the collection.
102    fn append_set(&mut self, value: Set) -> Result<&mut Self>;
103
104    /// Appends a "map" to the collection.
105    fn append_map(&mut self, value: Map) -> Result<&mut Self>;
106
107    /// Appends a "tuple" to the collection.
108    fn append_tuple(&mut self, value: Tuple) -> Result<&mut Self>;
109
110    /// Appends a "udt" to the collection.
111    fn append_user_type(&mut self, value: &UserType) -> Result<&mut Self>;
112}
113
114/// A cassandra list collection
115#[derive(Debug)]
116pub struct List(*mut _CassCollection);
117
118// The underlying C type has no thread-local state, but does not support access
119// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
120unsafe impl Send for List {}
121
122impl ProtectedInner<*mut _CassCollection> for List {
123    fn inner(&self) -> *mut _CassCollection {
124        self.0
125    }
126}
127
128impl Protected<*mut _CassCollection> for List {
129    fn build(inner: *mut _CassCollection) -> Self {
130        if inner.is_null() {
131            panic!("Unexpected null pointer")
132        };
133        List(inner)
134    }
135}
136
137impl ProtectedInner<*mut _CassCollection> for Map {
138    fn inner(&self) -> *mut _CassCollection {
139        self.0
140    }
141}
142
143impl Protected<*mut _CassCollection> for Map {
144    fn build(inner: *mut _CassCollection) -> Self {
145        if inner.is_null() {
146            panic!("Unexpected null pointer")
147        };
148        Map(inner)
149    }
150}
151
152impl ProtectedInner<*mut _CassCollection> for Set {
153    fn inner(&self) -> *mut _CassCollection {
154        self.0
155    }
156}
157
158impl Protected<*mut _CassCollection> for Set {
159    fn build(inner: *mut _CassCollection) -> Self {
160        if inner.is_null() {
161            panic!("Unexpected null pointer")
162        };
163        Set(inner)
164    }
165}
166
167impl Drop for List {
168    fn drop(&mut self) {
169        unsafe { cass_collection_free(self.0) }
170    }
171}
172
173impl CassCollection for List {
174    type Value = _CassCollection;
175
176    /// create a new list
177    fn new(item_count: usize) -> Self {
178        unsafe { List::build(cass_collection_new(CASS_COLLECTION_TYPE_LIST, item_count)) }
179    }
180
181    fn new_from_data_type(value: DataType, item_count: usize) -> Self {
182        unsafe {
183            List(cass_collection_new_from_data_type(
184                value.inner(),
185                item_count,
186            ))
187        }
188    }
189
190    /// Gets the data type of a collection.
191    fn data_type(&self) -> ConstDataType {
192        unsafe { ConstDataType::build(cass_collection_data_type(self.inner())) }
193    }
194
195    /// Appends a "tinyint" to the collection.
196    fn append_int8(&mut self, value: i8) -> Result<&mut Self> {
197        unsafe { cass_collection_append_int8(self.inner(), value).to_result(self) }
198    }
199
200    /// Appends an "smallint" to the collection.
201    fn append_int16(&mut self, value: i16) -> Result<&mut Self> {
202        unsafe { cass_collection_append_int16(self.inner(), value).to_result(self) }
203    }
204
205    /// Appends an "int" to the collection.
206    fn append_int32(&mut self, value: i32) -> Result<&mut Self> {
207        unsafe { cass_collection_append_int32(self.inner(), value).to_result(self) }
208    }
209
210    /// Appends a "date" to the collection.
211    fn append_uint32(&mut self, value: u32) -> Result<&mut Self> {
212        unsafe { cass_collection_append_uint32(self.inner(), value).to_result(self) }
213    }
214
215    /// Appends a "bigint", "counter", "timestamp" or "time" to the
216    /// collection.
217    fn append_int64(&mut self, value: i64) -> Result<&mut Self> {
218        unsafe { cass_collection_append_int64(self.inner(), value).to_result(self) }
219    }
220
221    /// Appends a "float" to the collection.
222    fn append_float(&mut self, value: f32) -> Result<&mut Self> {
223        unsafe { cass_collection_append_float(self.inner(), value).to_result(self) }
224    }
225
226    /// Appends a "double" to the collection.
227    fn append_double(&mut self, value: f64) -> Result<&mut Self> {
228        unsafe { cass_collection_append_double(self.inner(), value).to_result(self) }
229    }
230
231    /// Appends a "boolean" to the collection.
232    fn append_bool(&mut self, value: bool) -> Result<&mut Self> {
233        unsafe {
234            cass_collection_append_bool(self.inner(), if value { cass_true } else { cass_false })
235                .to_result(self)
236        }
237    }
238
239    /// Appends an "ascii", "text" or "varchar" to the collection.
240    fn append_string(&mut self, value: &str) -> Result<&mut Self> {
241        unsafe {
242            let value_ptr = value.as_ptr() as *const c_char;
243            let result = cass_collection_append_string_n(self.inner(), value_ptr, value.len());
244            result.to_result(self)
245        }
246    }
247
248    /// Appends a "blob", "varint" or "custom" to the collection.
249    fn append_bytes(&mut self, value: Vec<u8>) -> Result<&mut Self> {
250        unsafe {
251            let bytes = cass_collection_append_bytes(self.inner(), value[..].as_ptr(), value.len());
252            bytes.to_result(self)
253        }
254    }
255
256    /// Appends a "uuid" or "timeuuid"  to the collection.
257    fn append_uuid(&mut self, value: Uuid) -> Result<&mut Self> {
258        unsafe { cass_collection_append_uuid(self.inner(), value.inner()).to_result(self) }
259    }
260
261    /// Appends an "inet" to the collection.
262    fn append_inet(&mut self, value: Inet) -> Result<&mut Self> {
263        unsafe { cass_collection_append_inet(self.inner(), value.inner()).to_result(self) }
264    }
265
266    /// Appends a "list" to the collection.
267    fn append_list(&mut self, value: List) -> Result<&mut Self> {
268        unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
269    }
270
271    /// Appends a "set" to the collection.
272    fn append_set(&mut self, value: Set) -> Result<&mut Self> {
273        unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
274    }
275
276    /// Appends a "map" to the collection.
277    fn append_map(&mut self, value: Map) -> Result<&mut Self> {
278        unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
279    }
280
281    /// Appends a "tuple" to the collection.
282    fn append_tuple(&mut self, value: Tuple) -> Result<&mut Self> {
283        unsafe { cass_collection_append_tuple(self.inner(), value.inner()).to_result(self) }
284    }
285
286    /// Appends a "udt" to the collection.
287    fn append_user_type(&mut self, value: &UserType) -> Result<&mut Self> {
288        unsafe { cass_collection_append_user_type(self.inner(), value.inner()).to_result(self) }
289    }
290}
291
292/// A Cassandra set
293#[derive(Debug)]
294pub struct Set(*mut _CassCollection);
295
296// The underlying C type has no thread-local state, but does not support access
297// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
298unsafe impl Send for Set {}
299
300impl Drop for Set {
301    fn drop(&mut self) {
302        unsafe { cass_collection_free(self.inner()) }
303    }
304}
305
306// impl CassIterator for Set{
307//
308// }
309
310impl CassCollection for Set {
311    type Value = _CassCollection;
312
313    /// create a new list
314    fn new(item_count: usize) -> Self {
315        unsafe { Set(cass_collection_new(CASS_COLLECTION_TYPE_SET, item_count)) }
316    }
317
318    fn new_from_data_type(value: DataType, item_count: usize) -> Self {
319        unsafe {
320            Set(cass_collection_new_from_data_type(
321                value.inner(),
322                item_count,
323            ))
324        }
325    }
326    /// Gets the data type of a collection.
327    fn data_type(&self) -> ConstDataType {
328        unsafe { ConstDataType::build(cass_collection_data_type(self.inner())) }
329    }
330
331    /// Appends a "tinyint" to the collection.
332    fn append_int8(&mut self, value: i8) -> Result<&mut Self> {
333        unsafe { cass_collection_append_int8(self.inner(), value).to_result(self) }
334    }
335
336    /// Appends an "smallint" to the collection.
337    fn append_int16(&mut self, value: i16) -> Result<&mut Self> {
338        unsafe { cass_collection_append_int16(self.inner(), value).to_result(self) }
339    }
340
341    /// Appends an "int" to the collection.
342    fn append_int32(&mut self, value: i32) -> Result<&mut Self> {
343        unsafe { cass_collection_append_int32(self.inner(), value).to_result(self) }
344    }
345
346    /// Appends a "date" to the collection.
347    fn append_uint32(&mut self, value: u32) -> Result<&mut Self> {
348        unsafe { cass_collection_append_uint32(self.inner(), value).to_result(self) }
349    }
350
351    /// Appends a "bigint", "counter", "timestamp" or "time" to the
352    /// collection.
353    fn append_int64(&mut self, value: i64) -> Result<&mut Self> {
354        unsafe { cass_collection_append_int64(self.inner(), value).to_result(self) }
355    }
356
357    /// Appends a "float" to the collection.
358    fn append_float(&mut self, value: f32) -> Result<&mut Self> {
359        unsafe { cass_collection_append_float(self.inner(), value).to_result(self) }
360    }
361
362    /// Appends a "double" to the collection.
363    fn append_double(&mut self, value: f64) -> Result<&mut Self> {
364        unsafe { cass_collection_append_double(self.inner(), value).to_result(self) }
365    }
366
367    /// Appends a "boolean" to the collection.
368    fn append_bool(&mut self, value: bool) -> Result<&mut Self> {
369        unsafe {
370            cass_collection_append_bool(self.inner(), if value { cass_true } else { cass_false })
371                .to_result(self)
372        }
373    }
374
375    /// Appends an "ascii", "text" or "varchar" to the collection.
376    fn append_string(&mut self, value: &str) -> Result<&mut Self> {
377        unsafe {
378            let value_ptr = value.as_ptr() as *const c_char;
379            let result = cass_collection_append_string_n(self.inner(), value_ptr, value.len());
380            result.to_result(self)
381        }
382    }
383
384    /// Appends a "blob", "varint" or "custom" to the collection.
385    fn append_bytes(&mut self, value: Vec<u8>) -> Result<&mut Self> {
386        unsafe {
387            let bytes = cass_collection_append_bytes(self.inner(), value[..].as_ptr(), value.len());
388            bytes.to_result(self)
389        }
390    }
391
392    /// Appends a "uuid" or "timeuuid"  to the collection.
393    fn append_uuid(&mut self, value: Uuid) -> Result<&mut Self> {
394        unsafe { cass_collection_append_uuid(self.inner(), value.inner()).to_result(self) }
395    }
396
397    /// Appends an "inet" to the collection.
398    fn append_inet(&mut self, value: Inet) -> Result<&mut Self> {
399        unsafe { cass_collection_append_inet(self.inner(), value.inner()).to_result(self) }
400    }
401
402    /// Appends a "list" to the collection.
403    fn append_list(&mut self, value: List) -> Result<&mut Self> {
404        unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
405    }
406
407    /// Appends a "set" to the collection.
408    fn append_set(&mut self, value: Set) -> Result<&mut Self> {
409        unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
410    }
411
412    /// Appends a "map" to the collection.
413    fn append_map(&mut self, value: Map) -> Result<&mut Self> {
414        unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
415    }
416
417    /// Appends a "tuple" to the collection.
418    fn append_tuple(&mut self, value: Tuple) -> Result<&mut Self> {
419        unsafe { cass_collection_append_tuple(self.inner(), value.inner()).to_result(self) }
420    }
421
422    /// Appends a "udt" to the collection.
423    fn append_user_type(&mut self, value: &UserType) -> Result<&mut Self> {
424        unsafe { cass_collection_append_user_type(self.inner(), value.inner()).to_result(self) }
425    }
426}
427
428/// A Cassandra Map
429#[derive(Debug)]
430pub struct Map(*mut _CassCollection);
431
432// The underlying C type has no thread-local state, but does not support access
433// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
434unsafe impl Send for Map {}
435
436impl Drop for Map {
437    fn drop(&mut self) {
438        unsafe { cass_collection_free(self.0) }
439    }
440}
441
442impl CassCollection for Map {
443    type Value = _CassCollection;
444    /// create a new list
445    fn new(item_count: usize) -> Self {
446        unsafe { Map(cass_collection_new(CASS_COLLECTION_TYPE_MAP, item_count)) }
447    }
448
449    fn new_from_data_type(value: DataType, item_count: usize) -> Self {
450        unsafe {
451            Map(cass_collection_new_from_data_type(
452                value.inner(),
453                item_count,
454            ))
455        }
456    }
457
458    /// Gets the data type of a collection.
459    fn data_type(&self) -> ConstDataType {
460        unsafe { ConstDataType::build(cass_collection_data_type(self.inner())) }
461    }
462
463    /// Appends a "tinyint" to the collection.
464    fn append_int8(&mut self, value: i8) -> Result<&mut Self> {
465        unsafe { cass_collection_append_int8(self.inner(), value).to_result(self) }
466    }
467
468    /// Appends an "smallint" to the collection.
469    fn append_int16(&mut self, value: i16) -> Result<&mut Self> {
470        unsafe { cass_collection_append_int16(self.inner(), value).to_result(self) }
471    }
472
473    /// Appends an "int" to the collection.
474    fn append_int32(&mut self, value: i32) -> Result<&mut Self> {
475        unsafe { cass_collection_append_int32(self.inner(), value).to_result(self) }
476    }
477
478    /// Appends a "date" to the collection.
479    fn append_uint32(&mut self, value: u32) -> Result<&mut Self> {
480        unsafe { cass_collection_append_uint32(self.inner(), value).to_result(self) }
481    }
482
483    /// Appends a "bigint", "counter", "timestamp" or "time" to the
484    /// collection.
485    fn append_int64(&mut self, value: i64) -> Result<&mut Self> {
486        unsafe { cass_collection_append_int64(self.inner(), value).to_result(self) }
487    }
488
489    /// Appends a "float" to the collection.
490    fn append_float(&mut self, value: f32) -> Result<&mut Self> {
491        unsafe { cass_collection_append_float(self.inner(), value).to_result(self) }
492    }
493
494    /// Appends a "double" to the collection.
495    fn append_double(&mut self, value: f64) -> Result<&mut Self> {
496        unsafe { cass_collection_append_double(self.inner(), value).to_result(self) }
497    }
498
499    /// Appends a "boolean" to the collection.
500    fn append_bool(&mut self, value: bool) -> Result<&mut Self> {
501        unsafe {
502            cass_collection_append_bool(self.inner(), if value { cass_true } else { cass_false })
503                .to_result(self)
504        }
505    }
506
507    /// Appends an "ascii", "text" or "varchar" to the collection.
508    fn append_string(&mut self, value: &str) -> Result<&mut Self> {
509        unsafe {
510            let value_ptr = value.as_ptr() as *const c_char;
511            let result = cass_collection_append_string_n(self.inner(), value_ptr, value.len());
512            result.to_result(self)
513        }
514    }
515
516    /// Appends a "blob", "varint" or "custom" to the collection.
517    fn append_bytes(&mut self, value: Vec<u8>) -> Result<&mut Self> {
518        unsafe {
519            let bytes = cass_collection_append_bytes(self.inner(), value[..].as_ptr(), value.len());
520            bytes.to_result(self)
521        }
522    }
523
524    /// Appends a "uuid" or "timeuuid"  to the collection.
525    fn append_uuid(&mut self, value: Uuid) -> Result<&mut Self> {
526        unsafe { cass_collection_append_uuid(self.inner(), value.inner()).to_result(self) }
527    }
528
529    /// Appends an "inet" to the collection.
530    fn append_inet(&mut self, value: Inet) -> Result<&mut Self> {
531        unsafe { cass_collection_append_inet(self.inner(), value.inner()).to_result(self) }
532    }
533
534    /// Appends a "list" to the collection.
535    fn append_list(&mut self, value: List) -> Result<&mut Self> {
536        unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
537    }
538
539    /// Appends a "set" to the collection.
540    fn append_set(&mut self, value: Set) -> Result<&mut Self> {
541        unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
542    }
543
544    /// Appends a "map" to the collection.
545    fn append_map(&mut self, value: Map) -> Result<&mut Self> {
546        unsafe { cass_collection_append_collection(self.inner(), value.0).to_result(self) }
547    }
548
549    /// Appends a "tuple" to the collection.
550    fn append_tuple(&mut self, value: Tuple) -> Result<&mut Self> {
551        unsafe { cass_collection_append_tuple(self.inner(), value.inner()).to_result(self) }
552    }
553
554    /// Appends a "udt" to the collection.
555    fn append_user_type(&mut self, value: &UserType) -> Result<&mut Self> {
556        unsafe { cass_collection_append_user_type(self.inner(), value.inner()).to_result(self) }
557    }
558}