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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
use crate::cassandra::collection::Set;
use crate::cassandra::data_type::ConstDataType;
use crate::cassandra::error::*;
use crate::cassandra::inet::Inet;
use crate::cassandra::tuple::Tuple;
use crate::cassandra::util::Protected;
use crate::cassandra::uuid::Uuid;

use crate::cassandra_sys::cass_false;
use crate::cassandra_sys::cass_true;
use crate::cassandra_sys::cass_user_type_data_type;
use crate::cassandra_sys::cass_user_type_free;
use crate::cassandra_sys::cass_user_type_set_bool;
use crate::cassandra_sys::cass_user_type_set_bool_by_name;
use crate::cassandra_sys::cass_user_type_set_bytes;
use crate::cassandra_sys::cass_user_type_set_bytes_by_name;
use crate::cassandra_sys::cass_user_type_set_collection;
use crate::cassandra_sys::cass_user_type_set_collection_by_name;
use crate::cassandra_sys::cass_user_type_set_decimal;
use crate::cassandra_sys::cass_user_type_set_decimal_by_name;
use crate::cassandra_sys::cass_user_type_set_double;
use crate::cassandra_sys::cass_user_type_set_double_by_name;
use crate::cassandra_sys::cass_user_type_set_float;
use crate::cassandra_sys::cass_user_type_set_float_by_name;
use crate::cassandra_sys::cass_user_type_set_inet;
use crate::cassandra_sys::cass_user_type_set_inet_by_name;
use crate::cassandra_sys::cass_user_type_set_int16;
use crate::cassandra_sys::cass_user_type_set_int16_by_name;
use crate::cassandra_sys::cass_user_type_set_int32;
use crate::cassandra_sys::cass_user_type_set_int32_by_name;
use crate::cassandra_sys::cass_user_type_set_int64;
use crate::cassandra_sys::cass_user_type_set_int64_by_name;
use crate::cassandra_sys::cass_user_type_set_int8;
use crate::cassandra_sys::cass_user_type_set_int8_by_name;
use crate::cassandra_sys::cass_user_type_set_null;
use crate::cassandra_sys::cass_user_type_set_null_by_name;
use crate::cassandra_sys::cass_user_type_set_string;
use crate::cassandra_sys::cass_user_type_set_string_by_name;
use crate::cassandra_sys::cass_user_type_set_tuple;
use crate::cassandra_sys::cass_user_type_set_tuple_by_name;
use crate::cassandra_sys::cass_user_type_set_uint32;
use crate::cassandra_sys::cass_user_type_set_uint32_by_name;
use crate::cassandra_sys::cass_user_type_set_user_type;
use crate::cassandra_sys::cass_user_type_set_user_type_by_name;
use crate::cassandra_sys::cass_user_type_set_uuid;
use crate::cassandra_sys::cass_user_type_set_uuid_by_name;
use crate::cassandra_sys::CassUserType as _UserType;

use std::ffi::CString;
// use cassandra::iterator::FieldIterator;

/// A user defined type
#[derive(Debug)]
pub struct UserType(*mut _UserType);

// The underlying C type has no thread-local state, but does not support access
// from multiple threads: https://datastax.github.io/cpp-driver/topics/#thread-safety
unsafe impl Send for UserType {}

impl Protected<*mut _UserType> for UserType {
    fn inner(&self) -> *mut _UserType {
        self.0
    }
    fn build(inner: *mut _UserType) -> Self {
        if inner.is_null() {
            panic!("Unexpected null pointer")
        };
        UserType(inner)
    }
}

impl Drop for UserType {
    fn drop(&mut self) {
        unsafe { cass_user_type_free(self.0) }
    }
}

// impl Drop for UserType {
//    fn drop(&mut self) {unsafe{
//        cass_user_type_free(self.0)
//    }}
// }

impl UserType {
    /// Gets the data type of a user defined type.
    pub fn data_type(&self) -> ConstDataType {
        unsafe { ConstDataType::build(cass_user_type_data_type(self.0)) }
    }

    /// Sets a null in a user defined type at the specified index.
    pub fn set_null(&mut self, index: usize) -> Result<&mut Self> {
        unsafe { cass_user_type_set_null(self.0, index).to_result(self) }
    }

    /// Sets a null in a user defined type at the specified name.
    pub fn set_null_by_name<S>(&mut self, name: S) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name_cstr = CString::new(name.into())?;
            cass_user_type_set_null_by_name(self.0, name_cstr.as_ptr()).to_result(self)
        }
    }

    /// Sets a "tinyint" in a user defined type at the specified index.
    pub fn set_int8(&mut self, index: usize, value: i8) -> Result<&mut Self> {
        unsafe { cass_user_type_set_int8(self.0, index, value).to_result(self) }
    }

    /// Sets a "tinyint" in a user defined type at the specified name.
    pub fn set_int8_by_name<S>(&mut self, name: S, value: i8) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_int8_by_name(self.0, name.as_ptr(), value).to_result(self)
        }
    }

    /// Sets an "smallint" in a user defined type at the specified index.
    pub fn set_int16(&mut self, index: usize, value: i16) -> Result<&mut Self> {
        unsafe { cass_user_type_set_int16(self.0, index, value).to_result(self) }
    }

    /// Sets an "smallint" in a user defined type at the specified name.
    pub fn set_int16_by_name<S>(&mut self, name: S, value: i16) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_int16_by_name(self.0, name.as_ptr(), value).to_result(self)
        }
    }

    /// Sets an "int" in a user defined type at the specified index.
    pub fn set_int32(&mut self, index: usize, value: i32) -> Result<&mut Self> {
        unsafe { cass_user_type_set_int32(self.0, index, value).to_result(self) }
    }

    /// Sets an "int" in a user defined type at the specified name.
    pub fn set_int32_by_name<S>(&mut self, name: S, value: i32) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_int32_by_name(self.0, name.as_ptr(), value).to_result(self)
        }
    }

    /// Sets a "date" in a user defined type at the specified index.
    pub fn set_uint32(&mut self, index: usize, value: u32) -> Result<&mut Self> {
        unsafe { cass_user_type_set_uint32(self.0, index, value).to_result(self) }
    }

    /// Sets a "date" in a user defined type at the specified name.
    pub fn set_uint32_by_name<S>(&mut self, name: S, value: u32) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_uint32_by_name(self.0, name.as_ptr(), value).to_result(self)
        }
    }

    /// Sets an "bigint", "counter", "timestamp" or "time" in a
    /// user defined type at the specified index.
    pub fn set_int64(&mut self, index: usize, value: i64) -> Result<&mut Self> {
        unsafe { cass_user_type_set_int64(self.0, index, value).to_result(self) }
    }

    /// Sets an "bigint", "counter", "timestamp" or "time" in a
    /// user defined type at the specified name.
    pub fn set_int64_by_name<S>(&mut self, name: S, value: i64) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_int64_by_name(self.0, name.as_ptr(), value).to_result(self)
        }
    }

    /// Sets a "float" in a user defined type at the specified index.
    pub fn set_float(&mut self, index: usize, value: f32) -> Result<&mut Self> {
        unsafe { cass_user_type_set_float(self.0, index, value).to_result(self) }
    }

    /// Sets a "float" in a user defined type at the specified name.
    pub fn set_float_by_name<S>(&mut self, name: S, value: f32) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_float_by_name(self.0, name.as_ptr(), value).to_result(self)
        }
    }

    /// Sets an "double" in a user defined type at the specified index.
    pub fn set_double(&mut self, index: usize, value: f64) -> Result<&mut Self> {
        unsafe { cass_user_type_set_double(self.0, index, value).to_result(self) }
    }

    /// Sets an "double" in a user defined type at the specified name.

    pub fn set_double_by_name<S>(&mut self, name: S, value: f64) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_double_by_name(self.0, name.as_ptr(), value).to_result(self)
        }
    }

    /// Sets a "boolean" in a user defined type at the specified index.
    pub fn set_bool(&mut self, index: usize, value: bool) -> Result<&mut Self> {
        unsafe {
            cass_user_type_set_bool(self.0, index, if value { cass_true } else { cass_false })
                .to_result(self)
        }
    }

    /// Sets a "boolean" in a user defined type at the specified name.
    pub fn set_bool_by_name<S>(&mut self, name: S, value: bool) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_bool_by_name(
                self.0,
                name.as_ptr(),
                if value { cass_true } else { cass_false },
            )
            .to_result(self)
        }
    }

    /// Sets an "ascii", "text" or "varchar" in a user defined type at the
    /// specified index.
    pub fn set_stringl<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let value = CString::new(value.into())?;
            cass_user_type_set_string(self.0, index, value.as_ptr()).to_result(self)
        }
    }

    /// Sets an "ascii", "text" or "varchar" in a user defined type at the
    /// specified name.
    pub fn set_string_by_name<S>(&mut self, name: S, value: S) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            let value = CString::new(value.into())?;
            cass_user_type_set_string_by_name(self.0, name.as_ptr(), value.as_ptr()).to_result(self)
        }
    }

    // FIXME. right way to pass the vec?
    /// Sets a "blob" "varint" or "custom" in a user defined type at the specified index.
    pub fn set_bytes(&mut self, index: usize, value: Vec<u8>) -> Result<&mut Self> {
        unsafe {
            cass_user_type_set_bytes(self.0, index, value.as_ptr(), value.len()).to_result(self)
        }
    }

    /// Sets a "blob", "varint" or "custom" in a user defined type at the specified name.
    pub fn set_bytes_by_name<S>(&mut self, name: S, value: Vec<u8>) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_bytes_by_name(self.0, name.as_ptr(), value.as_ptr(), value.len())
                .to_result(self)
        }
    }

    /// Sets a "uuid" or "timeuuid" in a user defined type at the specified index.
    pub fn set_uuid<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
    where
        S: Into<Uuid>,
    {
        unsafe { cass_user_type_set_uuid(self.0, index, value.into().inner()).to_result(self) }
    }

    /// Sets a "uuid" or "timeuuid" in a user defined type at the specified name.
    pub fn set_uuid_by_name<S, U>(&mut self, name: S, value: U) -> Result<&mut Self>
    where
        S: Into<String>,
        U: Into<Uuid>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_uuid_by_name(self.0, name.as_ptr(), value.into().inner())
                .to_result(self)
        }
    }

    /// Sets a "inet" in a user defined type at the specified index.
    pub fn set_inet<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
    where
        S: Into<Inet>,
    {
        unsafe { cass_user_type_set_inet(self.0, index, value.into().inner()).to_result(self) }
    }

    /// Sets a "inet" in a user defined type at the specified name.
    pub fn set_inet_by_name<S, U>(&mut self, name: S, value: U) -> Result<&mut Self>
    where
        S: Into<String>,
        U: Into<Inet>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_inet_by_name(self.0, name.as_ptr(), value.into().inner())
                .to_result(self)
        }
    }

    /// Sets a "list", "map" or "set" in a user defined type at the specified index.
    pub fn set_collection<S>(&mut self, index: usize, value: S) -> Result<&mut Self>
    where
        S: Into<Set>,
    {
        unsafe {
            cass_user_type_set_collection(self.0, index, value.into().inner()).to_result(self)
        }
    }

    /// Sets a "list", "map" or "set" in a user defined type at the
    /// specified name.
    pub fn set_collection_by_name<S>(&mut self, name: S, value: Set) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_collection_by_name(self.0, name.as_ptr(), value.inner())
                .to_result(self)
        }
    }

    /// Sets a "tuple" in a user defined type at the specified index.
    pub fn set_tuple(&mut self, index: usize, value: Tuple) -> Result<&mut Self> {
        unsafe { cass_user_type_set_tuple(self.0, index, value.inner()).to_result(self) }
    }

    /// Sets a "tuple" in a user defined type at the specified name.
    pub fn set_tuple_by_name<S>(&mut self, name: S, value: Tuple) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_tuple_by_name(self.0, name.as_ptr(), value.inner()).to_result(self)
        }
    }

    /// Sets a user defined type in a user defined type at the specified index.
    pub fn set_user_type(&mut self, index: usize, value: UserType) -> Result<&mut Self> {
        unsafe { cass_user_type_set_user_type(self.0, index, value.0).to_result(self) }
    }

    /// Sets a user defined type in a user defined type at the specified name.
    pub fn set_user_type_by_name<S>(&mut self, name: S, value: UserType) -> Result<&mut Self>
    where
        S: Into<String>,
    {
        unsafe {
            let name = CString::new(name.into())?;
            cass_user_type_set_user_type_by_name(self.0, name.as_ptr(), value.0).to_result(self)
        }
    }
}