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