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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
use crate::cassandra::batch::CustomPayload;
use crate::cassandra::collection::List;
use crate::cassandra::collection::Map;
// use decimal::d128;
use crate::cassandra::collection::Set;
use crate::cassandra::consistency::Consistency;
use crate::cassandra::error::*;
use crate::cassandra::inet::Inet;
use crate::cassandra::policy::retry::RetryPolicy;
use crate::cassandra::result::CassResult;
use crate::cassandra::tuple::Tuple;
use crate::cassandra::user_type::UserType;
use crate::cassandra::util::Protected;
use crate::cassandra::uuid::Uuid;

use crate::cassandra_sys::cass_false;
use crate::cassandra_sys::cass_statement_add_key_index;
use crate::cassandra_sys::cass_statement_bind_bool;
use crate::cassandra_sys::cass_statement_bind_bool_by_name;
use crate::cassandra_sys::cass_statement_bind_bytes;
use crate::cassandra_sys::cass_statement_bind_bytes_by_name;
use crate::cassandra_sys::cass_statement_bind_collection;
use crate::cassandra_sys::cass_statement_bind_collection_by_name;
use crate::cassandra_sys::cass_statement_bind_decimal;
use crate::cassandra_sys::cass_statement_bind_decimal_by_name;
use crate::cassandra_sys::cass_statement_bind_double;
use crate::cassandra_sys::cass_statement_bind_double_by_name;
use crate::cassandra_sys::cass_statement_bind_float;
use crate::cassandra_sys::cass_statement_bind_float_by_name;
use crate::cassandra_sys::cass_statement_bind_inet;
use crate::cassandra_sys::cass_statement_bind_inet_by_name;
use crate::cassandra_sys::cass_statement_bind_int16;
use crate::cassandra_sys::cass_statement_bind_int16_by_name;
use crate::cassandra_sys::cass_statement_bind_int32;
use crate::cassandra_sys::cass_statement_bind_int32_by_name;
use crate::cassandra_sys::cass_statement_bind_int64;
use crate::cassandra_sys::cass_statement_bind_int64_by_name;
use crate::cassandra_sys::cass_statement_bind_int8;
use crate::cassandra_sys::cass_statement_bind_int8_by_name;
use crate::cassandra_sys::cass_statement_bind_null;
use crate::cassandra_sys::cass_statement_bind_null_by_name;
use crate::cassandra_sys::cass_statement_bind_string;
use crate::cassandra_sys::cass_statement_bind_string_by_name;
use crate::cassandra_sys::cass_statement_bind_tuple;
use crate::cassandra_sys::cass_statement_bind_tuple_by_name;
use crate::cassandra_sys::cass_statement_bind_uint32;
use crate::cassandra_sys::cass_statement_bind_uint32_by_name;
use crate::cassandra_sys::cass_statement_bind_user_type;
use crate::cassandra_sys::cass_statement_bind_user_type_by_name;
use crate::cassandra_sys::cass_statement_bind_uuid;
use crate::cassandra_sys::cass_statement_bind_uuid_by_name;
use crate::cassandra_sys::cass_statement_free;
use crate::cassandra_sys::cass_statement_new;
use crate::cassandra_sys::cass_statement_set_consistency;
use crate::cassandra_sys::cass_statement_set_custom_payload;
use crate::cassandra_sys::cass_statement_set_keyspace;
use crate::cassandra_sys::cass_statement_set_paging_size;
use crate::cassandra_sys::cass_statement_set_paging_state;
use crate::cassandra_sys::cass_statement_set_paging_state_token;
use crate::cassandra_sys::cass_statement_set_request_timeout;
use crate::cassandra_sys::cass_statement_set_retry_policy;
use crate::cassandra_sys::cass_statement_set_serial_consistency;
use crate::cassandra_sys::cass_statement_set_timestamp;
use crate::cassandra_sys::cass_true;
use crate::cassandra_sys::CassStatement as _Statement;
use crate::cassandra_sys::CASS_UINT64_MAX;

use std::ffi::CString;
use time::Duration;
/// A statement object is an executable query. It represents either a regular
/// (adhoc) statement or a prepared statement. It maintains the queries' parameter
/// values along with query options (consistency level, paging state, etc.)
///
/// <b>Note:</b> Parameters for regular queries are not supported by the binary protocol
/// version 1.
#[derive(Debug)]
pub struct Statement(*mut _Statement);

// 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 Statement {}

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

#[macro_export]
/// Create a Statement from a query string, automatically counting `?`
/// characters to determine the number of parameters.
macro_rules! stmt {
    ( $( $x:expr ),*) => {
        {
            $(
            let query = $x;
            let param_count = query.matches("?").count();
            let statement = $crate::Statement::new(query, param_count);
            )*
            statement
        }
    };
}

// statement,
//  key,
//  i % 2 == 0,
//  i as f32 / 2.0f32,
//  i as f64 / 200.0,
//  i as i32 * 10,
//  i as i64 * 100);

impl Drop for Statement {
    /// Frees a statement instance. Statements can be immediately freed after
    /// being prepared, executed or added to a batch.
    fn drop(&mut self) {
        unsafe { self.free() }
    }
}

/// All Rust types that can be bound to a cassandra statement
/// //FIXME not yet implemented
// pub enum CassBindable {
//
// }
/// Any rust type that can have a default bind implementation
pub trait BindRustType<T> {
    /// binds a rust type to C* by index
    fn bind(&mut self, index: usize, value: T) -> Result<&mut Statement>;
    /// binds a rust type to C* by name
    fn bind_by_name(&mut self, col: &str, value: T) -> Result<&mut Statement>;
}

impl BindRustType<bool> for Statement {
    fn bind(&mut self, index: usize, value: bool) -> Result<&mut Self> {
        self.bind_bool(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: bool) -> Result<&mut Self> {
        self.bind_bool_by_name(col, value)
    }
}

impl BindRustType<f32> for Statement {
    fn bind(&mut self, index: usize, value: f32) -> Result<&mut Self> {
        self.bind_float(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: f32) -> Result<&mut Self> {
        self.bind_float_by_name(col, value)
    }
}

impl BindRustType<f64> for Statement {
    fn bind(&mut self, index: usize, value: f64) -> Result<&mut Self> {
        self.bind_double(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: f64) -> Result<&mut Self> {
        self.bind_double_by_name(col, value)
    }
}

impl BindRustType<i8> for Statement {
    fn bind(&mut self, index: usize, value: i8) -> Result<&mut Self> {
        self.bind_int8(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: i8) -> Result<&mut Self> {
        self.bind_int8_by_name(col, value)
    }
}

impl BindRustType<i16> for Statement {
    fn bind(&mut self, index: usize, value: i16) -> Result<&mut Self> {
        self.bind_int16(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: i16) -> Result<&mut Self> {
        self.bind_int16_by_name(col, value)
    }
}

impl BindRustType<i32> for Statement {
    fn bind(&mut self, index: usize, value: i32) -> Result<&mut Self> {
        self.bind_int32(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: i32) -> Result<&mut Self> {
        self.bind_int32_by_name(col, value)
    }
}

impl BindRustType<i64> for Statement {
    fn bind(&mut self, index: usize, value: i64) -> Result<&mut Self> {
        self.bind_int64(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: i64) -> Result<&mut Self> {
        self.bind_int64_by_name(col, value)
    }
}

impl BindRustType<u32> for Statement {
    fn bind(&mut self, index: usize, value: u32) -> Result<&mut Self> {
        self.bind_uint32(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: u32) -> Result<&mut Self> {
        self.bind_uint32_by_name(col, value)
    }
}

impl<'a> BindRustType<&'a str> for Statement {
    fn bind(&mut self, index: usize, value: &str) -> Result<&mut Self> {
        self.bind_string(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: &str) -> Result<&mut Self> {
        self.bind_string_by_name(col, value)
    }
}

impl BindRustType<Set> for Statement {
    fn bind(&mut self, index: usize, value: Set) -> Result<&mut Self> {
        self.bind_set(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: Set) -> Result<&mut Self> {
        self.bind_set_by_name(col, value)
    }
}

impl BindRustType<List> for Statement {
    fn bind(&mut self, index: usize, value: List) -> Result<&mut Self> {
        self.bind_list(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: List) -> Result<&mut Self> {
        self.bind_list_by_name(col, value)
    }
}

impl BindRustType<Uuid> for Statement {
    fn bind(&mut self, index: usize, value: Uuid) -> Result<&mut Self> {
        self.bind_uuid(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: Uuid) -> Result<&mut Self> {
        self.bind_uuid_by_name(col, value)
    }
}

impl BindRustType<Inet> for Statement {
    fn bind(&mut self, index: usize, value: Inet) -> Result<&mut Self> {
        self.bind_inet(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: Inet) -> Result<&mut Self> {
        self.bind_inet_by_name(col, value)
    }
}

impl BindRustType<Map> for Statement {
    fn bind(&mut self, index: usize, value: Map) -> Result<&mut Self> {
        self.bind_map(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: Map) -> Result<&mut Self> {
        self.bind_map_by_name(col, value)
    }
}

impl BindRustType<Tuple> for Statement {
    fn bind(&mut self, index: usize, value: Tuple) -> Result<&mut Self> {
        self.bind_tuple(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: Tuple) -> Result<&mut Self> {
        self.bind_tuple_by_name(col, value)
    }
}

impl BindRustType<&UserType> for Statement {
    fn bind(&mut self, index: usize, value: &UserType) -> Result<&mut Self> {
        self.bind_user_type(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: &UserType) -> Result<&mut Self> {
        self.bind_user_type_by_name(col, value)
    }
}

impl BindRustType<Vec<u8>> for Statement {
    fn bind(&mut self, index: usize, value: Vec<u8>) -> Result<&mut Self> {
        self.bind_bytes(index, value)
    }

    fn bind_by_name(&mut self, col: &str, value: Vec<u8>) -> Result<&mut Self> {
        self.bind_bytes_by_name(col, value)
    }
}

impl Statement {
    /// Creates a new query statement.
    pub fn new(query: &str, parameter_count: usize) -> Self {
        unsafe {
            let query_cstr = CString::new(query).expect("must be utf8");
            Statement(cass_statement_new(query_cstr.as_ptr(), parameter_count))
        }
    }

    unsafe fn free(&mut self) {
        cass_statement_free(self.0)
    }

    //    ///Binds an arbitrary CassBindable type to a cassandra statement
    //    ///FIXME not yet implemented
    //    pub fn bind(&mut self, params: Vec<CassBindable>) {
    //        let _ = params;
    //        unimplemented!();
    //    }

    /// Adds a key index specifier to this a statement.
    /// When using token-aware routing, this can be used to tell the driver which
    /// parameters within a non-prepared, parameterized statement are part of
    /// the partition key.
    ///
    /// Use consecutive calls for composite partition keys.
    ///
    /// This is not necessary for prepared statements, as the key
    /// parameters are determined in the metadata processed in the prepare phase.
    pub fn add_key_index(&mut self, index: usize) -> Result<&mut Self> {
        unsafe { cass_statement_add_key_index(self.0, index).to_result(self) }
    }

    /// Sets the statement's keyspace for use with token-aware routing.
    ///
    /// This is not necessary for prepared statements, as the keyspace
    /// is determined in the metadata processed in the prepare phase.
    pub fn set_keyspace(&mut self, keyspace: String) -> Result<&mut Self> {
        unsafe {
            let keyspace_cstr = CString::new(keyspace)?;
            cass_statement_set_keyspace(self.0, keyspace_cstr.as_ptr()).to_result(self)
        }
    }

    /// Sets the statement's consistency level.
    ///
    /// <b>Default:</b> CASS_CONSISTENCY_LOCAL_ONE
    pub fn set_consistency(&mut self, consistency: Consistency) -> Result<&mut Self> {
        unsafe { cass_statement_set_consistency(self.0, consistency.inner()).to_result(self) }
    }

    /// Sets the statement's serial consistency level.
    ///
    /// <b>Default:</b> Not set
    pub fn set_serial_consistency(&mut self, serial_consistency: Consistency) -> Result<&mut Self> {
        unsafe {
            cass_statement_set_serial_consistency(self.0, serial_consistency.inner())
                .to_result(self)
        }
    }

    /// Sets the statement's page size.
    ///
    /// <b>Default:</b> -1 (Disabled)
    pub fn set_paging_size(&mut self, page_size: i32) -> Result<&mut Self> {
        unsafe { cass_statement_set_paging_size(self.0, page_size).to_result(self) }
    }

    /// Sets the statement's paging state. This can be used to get the next page of
    /// data in a multi-page query.
    pub fn set_paging_state(&mut self, result: CassResult) -> Result<&mut Self> {
        unsafe { cass_statement_set_paging_state(self.0, result.inner()).to_result(self) }
    }

    /// Sets the statement's paging state.  This can be used to get the next page of
    /// data in a multi-page query.
    ///
    /// <b>Warning:</b> The paging state should not be exposed to or come from
    /// untrusted environments. The paging state could be spoofed and potentially
    /// used to gain access to other data.
    pub fn set_paging_state_token(&mut self, paging_state: &str) -> Result<&mut Self> {
        unsafe {
            cass_statement_set_paging_state_token(
                self.0,
                paging_state.as_ptr() as *const i8,
                paging_state.len(),
            )
            .to_result(self)
        }
    }

    /// Sets the statement's timestamp.
    pub fn set_timestamp(&mut self, timestamp: i64) -> Result<&mut Self> {
        unsafe { cass_statement_set_timestamp(self.0, timestamp).to_result(self) }
    }

    /// Sets the statement's timeout for waiting for a response from a node.
    /// Some(Duration::milliseconds(0)) sets no timeout, and None disables it
    /// (to use the cluster-level request timeout).
    pub fn set_statement_request_timeout(&mut self, timeout: Option<Duration>) -> &mut Self {
        unsafe {
            let timeout_millis = match timeout {
                None => CASS_UINT64_MAX as u64,
                Some(time) => time.num_milliseconds() as u64,
            };
            cass_statement_set_request_timeout(self.0, timeout_millis);
        }
        self
    }

    /// Sets the statement's retry policy.
    pub fn set_retry_policy(&mut self, retry_policy: RetryPolicy) -> Result<&mut Self> {
        unsafe { cass_statement_set_retry_policy(self.0, retry_policy.inner()).to_result(self) }
    }

    /// Sets the statement's custom payload.
    pub fn set_custom_payload(&mut self, payload: CustomPayload) -> Result<&mut Self> {
        unsafe { cass_statement_set_custom_payload(self.0, payload.inner()).to_result(self) }
    }

    /// Binds null to a query or bound statement at the specified index.
    pub fn bind_null(&mut self, index: usize) -> Result<&mut Self> {
        unsafe { cass_statement_bind_null(self.0, index).to_result(self) }
    }

    /// Binds a null to all the values with the specified name.
    ///
    /// This can only be used with statements created by
    /// cass_prepared_bind().
    pub fn bind_null_by_name(&mut self, name: &str) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_null_by_name(self.0, name_cstr.as_ptr()).to_result(self)
        }
    }

    /// Binds a "tinyint" to a query or bound statement at the specified index.
    pub fn bind_int8(&mut self, index: usize, value: i8) -> Result<&mut Self> {
        unsafe { cass_statement_bind_int8(self.0, index, value).to_result(self) }
    }

    /// Binds a "tinyint" to all the values with the specified name.
    pub fn bind_int8_by_name(&mut self, name: &str, value: i8) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_int8_by_name(self.0, name_cstr.as_ptr(), value).to_result(self)
        }
    }

    /// Binds an "smallint" to a query or bound statement at the specified index.
    pub fn bind_int16(&mut self, index: usize, value: i16) -> Result<&mut Self> {
        unsafe { cass_statement_bind_int16(self.0, index, value).to_result(self) }
    }

    /// Binds a "smallint" to all the values with the specified name.
    pub fn bind_int16_by_name(&mut self, name: &str, value: i16) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_int16_by_name(self.0, name_cstr.as_ptr(), value).to_result(self)
        }
    }

    /// Binds an "int" to a query or bound statement at the specified index.
    pub fn bind_int32(&mut self, index: usize, value: i32) -> Result<&mut Self> {
        unsafe { cass_statement_bind_int32(self.0, index, value).to_result(self) }
    }

    /// Binds an "int" to all the values with the specified name.
    pub fn bind_int32_by_name(&mut self, name: &str, value: i32) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_int32_by_name(self.0, name_cstr.as_ptr(), value).to_result(self)
        }
    }

    /// Binds a "date" to a query or bound statement at the specified index.
    pub fn bind_uint32(&mut self, index: usize, value: u32) -> Result<&mut Self> {
        unsafe { cass_statement_bind_uint32(self.0, index, value).to_result(self) }
    }

    /// Binds a "date" to all the values with the specified name.
    ///
    /// This can only be used with statements created by
    /// cass_prepared_bind().
    pub fn bind_uint32_by_name(&mut self, name: &str, value: u32) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_uint32_by_name(self.0, name_cstr.as_ptr(), value).to_result(self)
        }
    }

    /// Binds a "bigint", "counter", "timestamp" or "time" to a query or
    /// bound statement at the specified index.
    pub fn bind_int64(&mut self, index: usize, value: i64) -> Result<&mut Self> {
        unsafe { cass_statement_bind_int64(self.0, index, value).to_result(self) }
    }

    /// Binds a "bigint", "counter", "timestamp" or "time" to all values
    /// with the specified name.
    pub fn bind_int64_by_name(&mut self, name: &str, value: i64) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_int64_by_name(self.0, name_cstr.as_ptr(), value).to_result(self)
        }
    }

    /// Binds a "float" to a query or bound statement at the specified index.
    pub fn bind_float(&mut self, index: usize, value: f32) -> Result<&mut Self> {
        unsafe { cass_statement_bind_float(self.0, index, value).to_result(self) }
    }

    /// Binds a "float" to all the values with the specified name.
    ///
    /// This can only be used with statements created by
    /// cass_prepared_bind().
    pub fn bind_float_by_name(&mut self, name: &str, value: f32) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_float_by_name(self.0, name_cstr.as_ptr(), value).to_result(self)
        }
    }

    /// Binds a "double" to a query or bound statement at the specified index.
    pub fn bind_double(&mut self, index: usize, value: f64) -> Result<&mut Self> {
        unsafe { cass_statement_bind_double(self.0, index, value).to_result(self) }
    }

    /// Binds a "double" to all the values with the specified name.
    ///
    /// This can only be used with statements created by
    /// cass_prepared_bind().
    pub fn bind_double_by_name(&mut self, name: &str, value: f64) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_double_by_name(self.0, name_cstr.as_ptr(), value).to_result(self)
        }
    }

    /// Binds a "boolean" to a query or bound statement at the specified index.
    pub fn bind_bool(&mut self, index: usize, value: bool) -> Result<&mut Self> {
        unsafe {
            cass_statement_bind_bool(self.0, index, if value { cass_true } else { cass_false })
                .to_result(self)
        }
    }

    /// Binds a "boolean" to all the values with the specified name.
    ///
    /// This can only be used with statements created by
    /// cass_prepared_bind().
    pub fn bind_bool_by_name(&mut self, name: &str, value: bool) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_bool_by_name(
                self.0,
                name_cstr.as_ptr(),
                if value { cass_true } else { cass_false },
            )
            .to_result(self)
        }
    }

    /// Binds an "ascii", "text" or "varchar" to a query or bound statement
    /// at the specified index.
    pub fn bind_string(&mut self, index: usize, value: &str) -> Result<&mut Self> {
        unsafe {
            let value_cstr = CString::new(value)?;
            cass_statement_bind_string(self.0, index, value_cstr.as_ptr()).to_result(self)
        }
    }

    /// Binds an "ascii", "text" or "varchar" to all the values
    /// with the specified name.
    ///
    /// This can only be used with statements created by
    /// cass_prepared_bind().
    pub fn bind_string_by_name(&mut self, name: &str, value: &str) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            let value_cstr = CString::new(value)?;
            cass_statement_bind_string_by_name(self.0, name_cstr.as_ptr(), value_cstr.as_ptr())
                .to_result(self)
        }
    }

    /// Binds a "blob", "varint" or "custom" to a query or bound statement at the specified index.
    pub fn bind_bytes(&mut self, index: usize, value: Vec<u8>) -> Result<&mut Self> {
        unsafe {
            cass_statement_bind_bytes(self.0, index, value.as_ptr(), value.len()).to_result(self)
        }
    }

    /// Binds a "blob", "varint" or "custom" to all the values with the
    /// specified name.
    ///
    /// This can only be used with statements created by
    /// cass_prepared_bind().
    pub fn bind_bytes_by_name(&mut self, name: &str, mut value: Vec<u8>) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_bytes_by_name(
                self.0,
                name_cstr.as_ptr(),
                value.as_mut_ptr(),
                value.len(),
            )
            .to_result(self)
        }
    }

    /// Binds a "uuid" or "timeuuid" to a query or bound statement at the specified index.
    pub fn bind_uuid(&mut self, index: usize, value: Uuid) -> Result<&mut Self> {
        unsafe { cass_statement_bind_uuid(self.0, index, value.inner()).to_result(self) }
    }

    /// Binds a "uuid" or "timeuuid" to all the values
    /// with the specified name.
    ///
    /// This can only be used with statements created by
    /// cass_prepared_bind().
    pub fn bind_uuid_by_name(&mut self, name: &str, value: Uuid) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_uuid_by_name(self.0, name_cstr.as_ptr(), value.inner())
                .to_result(self)
        }
    }

    /// Binds an "inet" to a query or bound statement at the specified index.
    pub fn bind_inet(&mut self, index: usize, value: Inet) -> Result<&mut Self> {
        unsafe { cass_statement_bind_inet(self.0, index, value.inner()).to_result(self) }
    }

    /// Binds an "inet" to all the values with the specified name.
    pub fn bind_inet_by_name(&mut self, name: &str, value: Inet) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_inet_by_name(self.0, name_cstr.as_ptr(), value.inner())
                .to_result(self)
        }
    }

    //  ///Bind a "decimal" to a query or bound statement at the specified index.
    //    pub fn bind_decimal(&self,
    //                                index: i32,
    //                                value: d128)
    //                                -> Result<&mut Self, CassError> {
    //            unsafe {
    //                CassError::build(
    //                    cass_statement_bind_decimal(
    //                        self.0,
    //                        index,
    //                        value
    //                    )
    //                ).wrap(&mut self)
    //            }
    //        }

    // Binds a "decimal" to all the values with the specified name.
    //
    // This can only be used with statements created by
    // cass_prepared_bind().
    //    pub fn bind_decimal_by_name<'a>(&'a self,
    //                                    name: &str,
    //                                    value: String)
    //                                    -> Result<&'a Self, CassError> {
    //        unsafe {
    //            let name = CString::new(name).unwrap();
    //            CassError::build(
    //            cass_statement_bind_decimal_by_name(
    //                self.0,
    //                name.as_ptr(),
    //                value
    //            )
    //        ).wrap(&self)
    //        }
    //    }

    /// Bind a "map" to a query or bound statement at the specified index.
    pub fn bind_map(&mut self, index: usize, map: Map) -> Result<&mut Self> {
        unsafe { cass_statement_bind_collection(self.0, index, map.inner()).to_result(self) }
    }

    /// Bind a "map" to all the values with the
    /// specified name.
    ///
    /// This can only be used with statements created by
    /// cass_prepared_bind().
    pub fn bind_map_by_name(&mut self, name: &str, map: Map) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_collection_by_name(self.0, name_cstr.as_ptr(), map.inner())
                .to_result(self)
        }
    }
    /// Bind a "set" to a query or bound statement at the specified index.
    pub fn bind_set(&mut self, index: usize, collection: Set) -> Result<&mut Self> {
        unsafe { cass_statement_bind_collection(self.0, index, collection.inner()).to_result(self) }
    }

    /// Bind a "set" to all the values with the
    /// specified name.
    ///
    /// This can only be used with statements created by
    /// cass_prepared_bind().
    pub fn bind_set_by_name(&mut self, name: &str, collection: Set) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_collection_by_name(self.0, name_cstr.as_ptr(), collection.inner())
                .to_result(self)
        }
    }

    /// Bind a "list" to a query or bound statement at the specified index.
    pub fn bind_list(&mut self, index: usize, collection: List) -> Result<&mut Self> {
        unsafe { cass_statement_bind_collection(self.0, index, collection.inner()).to_result(self) }
    }

    /// Bind a "list" to all the values with the
    /// specified name.
    ///
    /// This can only be used with statements created by
    /// cass_prepared_bind().
    pub fn bind_list_by_name(&mut self, name: &str, collection: List) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_collection_by_name(self.0, name_cstr.as_ptr(), collection.inner())
                .to_result(self)
        }
    }

    /// Bind a "tuple" to a query or bound statement at the specified index.
    pub fn bind_tuple(&mut self, index: usize, value: Tuple) -> Result<&mut Self> {
        unsafe { cass_statement_bind_tuple(self.0, index, value.inner()).to_result(self) }
    }

    /// Bind a "tuple" to all the values with the specified name.
    ///
    /// This can only be used with statements created by
    /// cass_prepared_bind().
    pub fn bind_tuple_by_name(&mut self, name: &str, value: Tuple) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_tuple_by_name(self.0, name_cstr.as_ptr(), value.inner())
                .to_result(self)
        }
    }

    /// Bind a user defined type to a query or bound statement at the
    /// specified index.
    pub fn bind_user_type(&mut self, index: usize, value: &UserType) -> Result<&mut Self> {
        unsafe { cass_statement_bind_user_type(self.0, index, value.inner()).to_result(self) }
    }

    /// Bind a user defined type to a query or bound statement with the
    /// specified name.
    pub fn bind_user_type_by_name(&mut self, name: &str, value: &UserType) -> Result<&mut Self> {
        unsafe {
            let name_cstr = CString::new(name)?;
            cass_statement_bind_user_type_by_name(self.0, name_cstr.as_ptr(), value.inner())
                .to_result(self)
        }
    }
}