discord_cassandra_cpp/cassandra/
user_type.rs

1use crate::cassandra::collection::{List, Map, Set};
2use crate::cassandra::data_type::ConstDataType;
3use crate::cassandra::error::*;
4use crate::cassandra::inet::Inet;
5use crate::cassandra::tuple::Tuple;
6use crate::cassandra::util::{Protected, ProtectedInner};
7use crate::cassandra::uuid::Uuid;
8
9use crate::cassandra_sys::cass_false;
10use crate::cassandra_sys::cass_true;
11use crate::cassandra_sys::cass_user_type_data_type;
12use crate::cassandra_sys::cass_user_type_free;
13use crate::cassandra_sys::cass_user_type_set_bool;
14use crate::cassandra_sys::cass_user_type_set_bool_by_name_n;
15use crate::cassandra_sys::cass_user_type_set_bytes;
16use crate::cassandra_sys::cass_user_type_set_bytes_by_name_n;
17use crate::cassandra_sys::cass_user_type_set_collection;
18use crate::cassandra_sys::cass_user_type_set_collection_by_name_n;
19use crate::cassandra_sys::cass_user_type_set_decimal;
20use crate::cassandra_sys::cass_user_type_set_decimal_by_name_n;
21use crate::cassandra_sys::cass_user_type_set_double;
22use crate::cassandra_sys::cass_user_type_set_double_by_name_n;
23use crate::cassandra_sys::cass_user_type_set_float;
24use crate::cassandra_sys::cass_user_type_set_float_by_name_n;
25use crate::cassandra_sys::cass_user_type_set_inet;
26use crate::cassandra_sys::cass_user_type_set_inet_by_name_n;
27use crate::cassandra_sys::cass_user_type_set_int16;
28use crate::cassandra_sys::cass_user_type_set_int16_by_name_n;
29use crate::cassandra_sys::cass_user_type_set_int32;
30use crate::cassandra_sys::cass_user_type_set_int32_by_name_n;
31use crate::cassandra_sys::cass_user_type_set_int64;
32use crate::cassandra_sys::cass_user_type_set_int64_by_name_n;
33use crate::cassandra_sys::cass_user_type_set_int8;
34use crate::cassandra_sys::cass_user_type_set_int8_by_name_n;
35use crate::cassandra_sys::cass_user_type_set_null;
36use crate::cassandra_sys::cass_user_type_set_null_by_name_n;
37use crate::cassandra_sys::cass_user_type_set_string_by_name_n;
38use crate::cassandra_sys::cass_user_type_set_string_n;
39use crate::cassandra_sys::cass_user_type_set_tuple;
40use crate::cassandra_sys::cass_user_type_set_tuple_by_name_n;
41use crate::cassandra_sys::cass_user_type_set_uint32;
42use crate::cassandra_sys::cass_user_type_set_uint32_by_name_n;
43use crate::cassandra_sys::cass_user_type_set_user_type;
44use crate::cassandra_sys::cass_user_type_set_user_type_by_name_n;
45use crate::cassandra_sys::cass_user_type_set_uuid;
46use crate::cassandra_sys::cass_user_type_set_uuid_by_name_n;
47use crate::cassandra_sys::CassUserType as _UserType;
48
49use std::os::raw::c_char;
50// use cassandra::iterator::FieldIterator;
51
52/// A user defined type
53#[derive(Debug)]
54pub struct UserType(*mut _UserType);
55
56// The underlying C type has no thread-local state, but does not support access
57// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
58unsafe impl Send for UserType {}
59
60impl ProtectedInner<*mut _UserType> for UserType {
61    fn inner(&self) -> *mut _UserType {
62        self.0
63    }
64}
65
66impl Protected<*mut _UserType> for UserType {
67    fn build(inner: *mut _UserType) -> Self {
68        if inner.is_null() {
69            panic!("Unexpected null pointer")
70        };
71        UserType(inner)
72    }
73}
74
75impl Drop for UserType {
76    fn drop(&mut self) {
77        unsafe { cass_user_type_free(self.0) }
78    }
79}
80
81// impl Drop for UserType {
82//    fn drop(&mut self) {unsafe{
83//        cass_user_type_free(self.0)
84//    }}
85// }
86
87impl UserType {
88    /// Gets the data type of a user defined type.
89    pub fn data_type(&self) -> ConstDataType {
90        unsafe { ConstDataType::build(cass_user_type_data_type(self.0)) }
91    }
92
93    /// Sets a null in a user defined type at the specified index.
94    pub fn set_null(&mut self, index: usize) -> Result<&mut Self> {
95        unsafe { cass_user_type_set_null(self.0, index).to_result(self) }
96    }
97
98    /// Sets a null in a user defined type at the specified name.
99    pub fn set_null_by_name<S>(&mut self, name: S) -> Result<&mut Self>
100    where
101        S: Into<String>,
102    {
103        unsafe {
104            let name_str = name.into();
105            let name_ptr = name_str.as_ptr() as *const c_char;
106            cass_user_type_set_null_by_name_n(self.0, name_ptr, name_str.len()).to_result(self)
107        }
108    }
109
110    /// Sets a "tinyint" in a user defined type at the specified index.
111    pub fn set_int8(&mut self, index: usize, value: i8) -> Result<&mut Self> {
112        unsafe { cass_user_type_set_int8(self.0, index, value).to_result(self) }
113    }
114
115    /// Sets a "tinyint" in a user defined type at the specified name.
116    pub fn set_int8_by_name<S>(&mut self, name: S, value: i8) -> Result<&mut Self>
117    where
118        S: Into<String>,
119    {
120        unsafe {
121            let name_str = name.into();
122            let name_ptr = name_str.as_ptr() as *const c_char;
123            cass_user_type_set_int8_by_name_n(self.0, name_ptr, name_str.len(), value)
124                .to_result(self)
125        }
126    }
127
128    /// Sets an "smallint" in a user defined type at the specified index.
129    pub fn set_int16(&mut self, index: usize, value: i16) -> Result<&mut Self> {
130        unsafe { cass_user_type_set_int16(self.0, index, value).to_result(self) }
131    }
132
133    /// Sets an "smallint" in a user defined type at the specified name.
134    pub fn set_int16_by_name<S>(&mut self, name: S, value: i16) -> Result<&mut Self>
135    where
136        S: Into<String>,
137    {
138        unsafe {
139            let name_str = name.into();
140            let name_ptr = name_str.as_ptr() as *const c_char;
141            cass_user_type_set_int16_by_name_n(self.0, name_ptr, name_str.len(), value)
142                .to_result(self)
143        }
144    }
145
146    /// Sets an "int" in a user defined type at the specified index.
147    pub fn set_int32(&mut self, index: usize, value: i32) -> Result<&mut Self> {
148        unsafe { cass_user_type_set_int32(self.0, index, value).to_result(self) }
149    }
150
151    /// Sets an "int" in a user defined type at the specified name.
152    pub fn set_int32_by_name<S>(&mut self, name: S, value: i32) -> Result<&mut Self>
153    where
154        S: Into<String>,
155    {
156        unsafe {
157            let name_str = name.into();
158            let name_ptr = name_str.as_ptr() as *const c_char;
159            cass_user_type_set_int32_by_name_n(self.0, name_ptr, name_str.len(), value)
160                .to_result(self)
161        }
162    }
163
164    /// Sets a "date" in a user defined type at the specified index.
165    pub fn set_uint32(&mut self, index: usize, value: u32) -> Result<&mut Self> {
166        unsafe { cass_user_type_set_uint32(self.0, index, value).to_result(self) }
167    }
168
169    /// Sets a "date" in a user defined type at the specified name.
170    pub fn set_uint32_by_name<S>(&mut self, name: S, value: u32) -> Result<&mut Self>
171    where
172        S: Into<String>,
173    {
174        unsafe {
175            let name_str = name.into();
176            let name_ptr = name_str.as_ptr() as *const c_char;
177            cass_user_type_set_uint32_by_name_n(self.0, name_ptr, name_str.len(), value)
178                .to_result(self)
179        }
180    }
181
182    /// Sets an "bigint", "counter", "timestamp" or "time" in a
183    /// user defined type at the specified index.
184    pub fn set_int64(&mut self, index: usize, value: i64) -> Result<&mut Self> {
185        unsafe { cass_user_type_set_int64(self.0, index, value).to_result(self) }
186    }
187
188    /// Sets an "bigint", "counter", "timestamp" or "time" in a
189    /// user defined type at the specified name.
190    pub fn set_int64_by_name<S>(&mut self, name: S, value: i64) -> Result<&mut Self>
191    where
192        S: Into<String>,
193    {
194        unsafe {
195            let name_str = name.into();
196            let name_ptr = name_str.as_ptr() as *const c_char;
197            cass_user_type_set_int64_by_name_n(self.0, name_ptr, name_str.len(), value)
198                .to_result(self)
199        }
200    }
201
202    /// Sets a "float" in a user defined type at the specified index.
203    pub fn set_float(&mut self, index: usize, value: f32) -> Result<&mut Self> {
204        unsafe { cass_user_type_set_float(self.0, index, value).to_result(self) }
205    }
206
207    /// Sets a "float" in a user defined type at the specified name.
208    pub fn set_float_by_name<S>(&mut self, name: S, value: f32) -> Result<&mut Self>
209    where
210        S: Into<String>,
211    {
212        unsafe {
213            let name_str = name.into();
214            let name_ptr = name_str.as_ptr() as *const c_char;
215            cass_user_type_set_float_by_name_n(self.0, name_ptr, name_str.len(), value)
216                .to_result(self)
217        }
218    }
219
220    /// Sets an "double" in a user defined type at the specified index.
221    pub fn set_double(&mut self, index: usize, value: f64) -> Result<&mut Self> {
222        unsafe { cass_user_type_set_double(self.0, index, value).to_result(self) }
223    }
224
225    /// Sets an "double" in a user defined type at the specified name.
226
227    pub fn set_double_by_name<S>(&mut self, name: S, value: f64) -> Result<&mut Self>
228    where
229        S: Into<String>,
230    {
231        unsafe {
232            let name_str = name.into();
233            let name_ptr = name_str.as_ptr() as *const c_char;
234            cass_user_type_set_double_by_name_n(self.0, name_ptr, name_str.len(), value)
235                .to_result(self)
236        }
237    }
238
239    /// Sets a "boolean" in a user defined type at the specified index.
240    pub fn set_bool(&mut self, index: usize, value: bool) -> Result<&mut Self> {
241        unsafe {
242            cass_user_type_set_bool(self.0, index, if value { cass_true } else { cass_false })
243                .to_result(self)
244        }
245    }
246
247    /// Sets a "boolean" in a user defined type at the specified name.
248    pub fn set_bool_by_name<S>(&mut self, name: S, value: bool) -> Result<&mut Self>
249    where
250        S: Into<String>,
251    {
252        unsafe {
253            let name_str = name.into();
254            let name_ptr = name_str.as_ptr() as *const c_char;
255            cass_user_type_set_bool_by_name_n(
256                self.0,
257                name_ptr,
258                name_str.len(),
259                if value { cass_true } else { cass_false },
260            )
261            .to_result(self)
262        }
263    }
264
265    /// Sets an "ascii", "text" or "varchar" in a user defined type at the
266    /// specified index.
267    pub fn set_stringl<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
268    where
269        S: Into<String>,
270    {
271        unsafe {
272            let value_str = value.into();
273            let value_ptr = value_str.as_ptr() as *const c_char;
274            cass_user_type_set_string_n(self.0, index, value_ptr, value_str.len()).to_result(self)
275        }
276    }
277
278    /// Sets an "ascii", "text" or "varchar" in a user defined type at the
279    /// specified name.
280    pub fn set_string_by_name<S>(&mut self, name: S, value: S) -> Result<&mut Self>
281    where
282        S: Into<String>,
283    {
284        unsafe {
285            let name_str = name.into();
286            let name_ptr = name_str.as_ptr() as *const c_char;
287            let value_str = value.into();
288            let value_ptr = value_str.as_ptr() as *const c_char;
289            cass_user_type_set_string_by_name_n(
290                self.0,
291                name_ptr,
292                name_str.len(),
293                value_ptr,
294                value_str.len(),
295            )
296            .to_result(self)
297        }
298    }
299
300    // FIXME. right way to pass the vec?
301    /// Sets a "blob" "varint" or "custom" in a user defined type at the specified index.
302    pub fn set_bytes(&mut self, index: usize, value: Vec<u8>) -> Result<&mut Self> {
303        unsafe {
304            cass_user_type_set_bytes(self.0, index, value.as_ptr(), value.len()).to_result(self)
305        }
306    }
307
308    /// Sets a "blob", "varint" or "custom" in a user defined type at the specified name.
309    pub fn set_bytes_by_name<S>(&mut self, name: S, value: Vec<u8>) -> Result<&mut Self>
310    where
311        S: Into<String>,
312    {
313        unsafe {
314            let name_str = name.into();
315            let name_ptr = name_str.as_ptr() as *const c_char;
316            cass_user_type_set_bytes_by_name_n(
317                self.0,
318                name_ptr,
319                name_str.len(),
320                value.as_ptr(),
321                value.len(),
322            )
323            .to_result(self)
324        }
325    }
326
327    /// Sets a "uuid" or "timeuuid" in a user defined type at the specified index.
328    pub fn set_uuid<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
329    where
330        S: Into<Uuid>,
331    {
332        unsafe { cass_user_type_set_uuid(self.0, index, value.into().inner()).to_result(self) }
333    }
334
335    /// Sets a "uuid" or "timeuuid" in a user defined type at the specified name.
336    pub fn set_uuid_by_name<S, U>(&mut self, name: S, value: U) -> Result<&mut Self>
337    where
338        S: Into<String>,
339        U: Into<Uuid>,
340    {
341        unsafe {
342            let name_str = name.into();
343            let name_ptr = name_str.as_ptr() as *const c_char;
344            cass_user_type_set_uuid_by_name_n(
345                self.0,
346                name_ptr,
347                name_str.len(),
348                value.into().inner(),
349            )
350            .to_result(self)
351        }
352    }
353
354    /// Sets a "inet" in a user defined type at the specified index.
355    pub fn set_inet<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
356    where
357        S: Into<Inet>,
358    {
359        unsafe { cass_user_type_set_inet(self.0, index, value.into().inner()).to_result(self) }
360    }
361
362    /// Sets a "inet" in a user defined type at the specified name.
363    pub fn set_inet_by_name<S, U>(&mut self, name: S, value: U) -> Result<&mut Self>
364    where
365        S: Into<String>,
366        U: Into<Inet>,
367    {
368        unsafe {
369            let name_str = name.into();
370            let name_ptr = name_str.as_ptr() as *const c_char;
371            cass_user_type_set_inet_by_name_n(
372                self.0,
373                name_ptr,
374                name_str.len(),
375                value.into().inner(),
376            )
377            .to_result(self)
378        }
379    }
380
381    /// Sets a list in a user defined type at the specified index.
382    pub fn set_list<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
383    where
384        S: Into<List>,
385    {
386        unsafe {
387            cass_user_type_set_collection(self.0, index, value.into().inner()).to_result(self)
388        }
389    }
390
391    /// Sets a list in a user defined type at the specified name.
392    pub fn set_list_by_name<S, V>(&mut self, name: S, value: V) -> Result<&mut Self>
393    where
394        S: Into<String>,
395        V: Into<List>,
396    {
397        unsafe {
398            let name_str = name.into();
399            let name_ptr = name_str.as_ptr() as *const c_char;
400            cass_user_type_set_collection_by_name_n(
401                self.0,
402                name_ptr,
403                name_str.len(),
404                value.into().inner(),
405            )
406            .to_result(self)
407        }
408    }
409
410    /// Sets a map in a user defined type at the specified index.
411    pub fn set_map<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
412    where
413        S: Into<Map>,
414    {
415        unsafe {
416            cass_user_type_set_collection(self.0, index, value.into().inner()).to_result(self)
417        }
418    }
419
420    /// Sets a map in a user defined type at the specified name.
421    pub fn set_map_by_name<S, V>(&mut self, name: S, value: V) -> Result<&mut Self>
422    where
423        S: Into<String>,
424        V: Into<Map>,
425    {
426        unsafe {
427            let name_str = name.into();
428            let name_ptr = name_str.as_ptr() as *const c_char;
429            cass_user_type_set_collection_by_name_n(
430                self.0,
431                name_ptr,
432                name_str.len(),
433                value.into().inner(),
434            )
435            .to_result(self)
436        }
437    }
438
439    /// Sets a "set" in a user defined type at the specified index.
440    pub fn set_set<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
441    where
442        S: Into<Set>,
443    {
444        unsafe {
445            cass_user_type_set_collection(self.0, index, value.into().inner()).to_result(self)
446        }
447    }
448
449    /// Sets a "set" in a user defined type at the specified name.
450    pub fn set_set_by_name<S, V>(&mut self, name: S, value: V) -> Result<&mut Self>
451    where
452        S: Into<String>,
453        V: Into<Set>,
454    {
455        unsafe {
456            let name_str = name.into();
457            let name_ptr = name_str.as_ptr() as *const c_char;
458            cass_user_type_set_collection_by_name_n(
459                self.0,
460                name_ptr,
461                name_str.len(),
462                value.into().inner(),
463            )
464            .to_result(self)
465        }
466    }
467
468    /// Sets a "tuple" in a user defined type at the specified index.
469    pub fn set_tuple(&mut self, index: usize, value: Tuple) -> Result<&mut Self> {
470        unsafe { cass_user_type_set_tuple(self.0, index, value.inner()).to_result(self) }
471    }
472
473    /// Sets a "tuple" in a user defined type at the specified name.
474    pub fn set_tuple_by_name<S>(&mut self, name: S, value: Tuple) -> Result<&mut Self>
475    where
476        S: Into<String>,
477    {
478        unsafe {
479            let name_str = name.into();
480            let name_ptr = name_str.as_ptr() as *const c_char;
481            cass_user_type_set_tuple_by_name_n(self.0, name_ptr, name_str.len(), value.inner())
482                .to_result(self)
483        }
484    }
485
486    /// Sets a user defined type in a user defined type at the specified index.
487    pub fn set_user_type(&mut self, index: usize, value: UserType) -> Result<&mut Self> {
488        unsafe { cass_user_type_set_user_type(self.0, index, value.0).to_result(self) }
489    }
490
491    /// Sets a user defined type in a user defined type at the specified name.
492    pub fn set_user_type_by_name<S>(&mut self, name: S, value: UserType) -> Result<&mut Self>
493    where
494        S: Into<String>,
495    {
496        unsafe {
497            let name_str = name.into();
498            let name_ptr = name_str.as_ptr() as *const c_char;
499            cass_user_type_set_user_type_by_name_n(self.0, name_ptr, name_str.len(), value.0)
500                .to_result(self)
501        }
502    }
503}