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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
//! Oracle Advanced Queuing (available when `aq_unstable` feature is enabled.)
//!
//! **Warning:** Any type in this module is unstable. It may be changed incompatibly by minor version upgrades.
//!
//! # Examples
//!
//! ## Object type queue
//!
//! ```
//! # use oracle::Error;
//! # use oracle::test_util;
//! # use oracle::aq;
//! # use oracle::sql_type::Object;
//! # let conn = test_util::connect()?;
//!
//! // Create a queue
//! let objtype = conn.object_type("UDT_BOOK")?;
//! let mut queue = aq::Queue::<Object>::new(&conn, "BOOK_QUEUE", &objtype)?;
//!
//! // Create a message
//! let mut payload = objtype.new_object()?;
//! payload.set("TITLE", &"Pebble in the Sky")?;
//! payload.set("AUTHORS", &"Isaac Asimov")?;
//! payload.set("PRICE", &17.0)?;
//! let mut msg = aq::MsgProps::<Object>::new(&conn)?;
//! msg.set_payload(&payload);
//!
//! // Enqueue the message to the queue
//! queue.enqueue(&msg)?;
//!
//! // Dequeue a message from the queue
//! let new_msg = queue.dequeue()?;
//! let new_payload = new_msg.payload()?;
//!
//! // Compare message payloads.
//! assert_eq!(payload.get::<String>("TITLE")?, new_payload.get::<String>("TITLE")?);
//! assert_eq!(payload.get::<String>("AUTHORS")?, new_payload.get::<String>("AUTHORS")?);
//! assert_eq!(payload.get::<f32>("PRICE")?, new_payload.get::<f32>("PRICE")?);
//! # Ok::<(), Error>(())
//! ```
//!
//! ## RAW data queue
//!
//! ```
//! # use oracle::Error;
//! # use oracle::test_util;
//! # use oracle::aq;
//! # let conn = test_util::connect()?;
//!
//! // Create a queue
//! let mut queue = aq::Queue::<[u8]>::new(&conn, "RAW_QUEUE", &())?;
//!
//! // Create a message
//! let payload = b"\xde\xad\xbe\xef";
//! let mut msg = aq::MsgProps::<[u8]>::new(&conn)?;
//! msg.set_payload(payload.as_ref());
//!
//! // Enqueue the message to the queue
//! queue.enqueue(&msg)?;
//!
//! // Dequeue a message from the queue
//! let new_msg = queue.dequeue()?;
//! let new_payload = new_msg.payload()?; // returns Vec<u8>
//!
//! // Compare message payloads.
//! assert_eq!(payload, new_payload.as_slice());
//! # Ok::<(), Error>(())
//! ```
//!
//! # Enqueue and dequeue more than one message in one call
//!
//! ```
//! # use oracle::Error;
//! # use oracle::test_util;
//! # use oracle::aq;
//! # let conn = test_util::connect()?;
//!
//! // Create a queue
//! let mut queue = aq::Queue::<[u8]>::new(&conn, "RAW_QUEUE", &())?;
//!
//! // Create messages
//! let payloads = [b"\xde\xad\xbe\xef", b"\xba\xce\xba\x11"];
//! let mut messages = vec![];
//! for payload in &payloads {
//!     let mut msg = aq::MsgProps::<[u8]>::new(&conn)?;
//!     msg.set_payload(payload.as_ref())?;
//!     messages.push(msg);
//! }
//!
//! // Enqueue the messages
//! queue.enqueue_many(&messages)?;
//!
//! // Dequeue messages from the queue
//! let new_messages = queue.dequeue_many(10)?;
//!
//! // Compare message payloads.
//! assert_eq!(new_messages.len(), 2);
//! assert_eq!(new_messages[0].payload()?, payloads[0]);
//! assert_eq!(new_messages[1].payload()?, payloads[1]);
//! # Ok::<(), Error>(())
//! ```

use crate::binding::*;
use crate::chkerr;
use crate::connection::Conn;
use crate::new_odpi_str;
use crate::sql_type::Object;
use crate::sql_type::ObjectType;
use crate::sql_type::OracleType;
use crate::sql_type::Timestamp;
use crate::to_odpi_str;
use crate::to_rust_slice;
use crate::Connection;
use crate::Context;
use crate::DpiMsgProps;
use crate::DpiObject;
use crate::DpiQueue;
use crate::Error;
use crate::Result;
use std::borrow::ToOwned;
use std::fmt;
use std::marker::PhantomData;
use std::os::raw::c_char;
use std::ptr;
use std::time::Duration;

/// A trait for payload type
///
/// **Warning:** The type is unstable. It may be changed incompatibly by minor version upgrades.
pub trait Payload: ToOwned {
    type TypeInfo;
    fn payload_type(payload_type: &Self::TypeInfo) -> Result<Option<ObjectType>>;
    fn get(props: &MsgProps<Self>) -> Result<Self::Owned>;
    fn set(&self, props: &mut MsgProps<Self>) -> Result<()>;
}

impl Payload for [u8] {
    type TypeInfo = ();

    fn payload_type(_payload_type: &Self::TypeInfo) -> Result<Option<ObjectType>> {
        Ok(None)
    }

    fn get(props: &MsgProps<Self>) -> Result<Vec<u8>> {
        let mut ptr = ptr::null();
        let mut len = 0;
        chkerr!(
            props.ctxt(),
            dpiMsgProps_getPayload(props.handle.raw, ptr::null_mut(), &mut ptr, &mut len)
        );
        Ok(to_rust_slice(ptr, len).to_vec())
    }

    fn set(&self, props: &mut MsgProps<Self>) -> Result<()> {
        chkerr!(
            props.ctxt(),
            dpiMsgProps_setPayloadBytes(
                props.handle.raw,
                self.as_ptr() as *const c_char,
                self.len() as u32
            )
        );
        props.payload_type = None;
        Ok(())
    }
}

impl Payload for Object {
    type TypeInfo = ObjectType;

    fn payload_type(payload_type: &Self::TypeInfo) -> Result<Option<ObjectType>> {
        Ok(Some(payload_type.clone()))
    }

    fn get(props: &MsgProps<Self>) -> Result<Object> {
        let objtype = props
            .payload_type
            .as_ref()
            .ok_or_else(Error::no_data_found)?;
        let mut obj_handle = DpiObject::null();
        chkerr!(
            props.ctxt(),
            dpiMsgProps_getPayload(
                props.handle.raw,
                &mut obj_handle.raw,
                ptr::null_mut(),
                ptr::null_mut()
            )
        );
        Ok(Object::new(props.conn.clone(), obj_handle, objtype.clone()))
    }

    fn set(&self, props: &mut MsgProps<Self>) -> Result<()> {
        chkerr!(
            props.ctxt(),
            dpiMsgProps_setPayloadObject(props.handle.raw, self.handle())
        );
        props.payload_type = Some(self.object_type().clone());
        Ok(())
    }
}

/// Advanced Queueing (AQ) queue which may be used to enqueue and dequeue messages
///
/// **Warning:** The type is unstable. It may be changed incompatibly by minor version upgrades.
pub struct Queue<T>
where
    T: Payload + ?Sized,
{
    conn: Conn,
    handle: DpiQueue,
    payload_type: Option<ObjectType>,
    enq_options: Option<EnqOptions>,
    deq_options: Option<DeqOptions>,
    phantom: PhantomData<T>,
}

impl<T> Queue<T>
where
    T: Payload + ?Sized,
{
    fn handle(&self) -> *mut dpiQueue {
        self.handle.raw
    }

    fn ctxt(&self) -> &Context {
        self.conn.ctxt()
    }

    /// Creates a new queue which may be used to enqueue and dequeue messages
    /// from Advanced Queuing (AQ) queues.
    pub fn new(
        conn: &Connection,
        queue_name: &str,
        payload_type: &T::TypeInfo,
    ) -> Result<Queue<T>> {
        let mut handle = ptr::null_mut();
        let name = to_odpi_str(queue_name);
        let payload_type = T::payload_type(payload_type)?;
        let objtype = payload_type
            .as_ref()
            .map(|t| t.handle().raw)
            .unwrap_or(ptr::null_mut());
        chkerr!(
            conn.ctxt(),
            dpiConn_newQueue(conn.handle(), name.ptr, name.len, objtype, &mut handle)
        );
        Ok(Queue {
            conn: conn.conn.clone(),
            handle: DpiQueue::new(handle),
            payload_type,
            enq_options: None,
            deq_options: None,
            phantom: PhantomData,
        })
    }

    /// Dequeues a single message from the queue.
    pub fn dequeue(&self) -> Result<MsgProps<T>> {
        let mut props = ptr::null_mut();
        chkerr!(self.ctxt(), dpiQueue_deqOne(self.handle(), &mut props));
        Ok(MsgProps::from_dpi_msg_props(
            self.conn.clone(),
            DpiMsgProps::new(props),
            self.payload_type.clone(),
        ))
    }

    /// Dequeues multiple messages from the queue.
    pub fn dequeue_many(&self, max_size: u32) -> Result<Vec<MsgProps<T>>> {
        let mut num_props = max_size;
        let mut handles = Vec::<DpiMsgProps>::with_capacity(max_size as usize);
        chkerr!(
            self.ctxt(),
            dpiQueue_deqMany(
                self.handle(),
                &mut num_props,
                // The following code works only when
                // the size of `*mut dpiMsgProps` equals to that of `DpiMsgProps`.
                handles.as_mut_ptr() as *mut *mut dpiMsgProps
            )
        );
        let num_props = num_props as usize;
        unsafe {
            handles.set_len(num_props);
        }
        let props: Vec<_> = handles
            .into_iter()
            .map(|handle| {
                MsgProps::from_dpi_msg_props(self.conn.clone(), handle, self.payload_type.clone())
            })
            .collect();
        Ok(props)
    }

    /// Enqueues a single mesasge into the queue.
    pub fn enqueue(&self, props: &MsgProps<T>) -> Result<()> {
        chkerr!(self.ctxt(), dpiQueue_enqOne(self.handle(), props.handle()));
        Ok(())
    }

    /// Enqueues multiple messages into the queue.
    ///
    /// **Warning:** calling this function in parallel on different connections
    /// acquired from the same pool may fail due to Oracle bug 29928074. Ensure
    /// that this function is not run in parallel, use standalone connections or
    /// connections from different pools, or make multiple calls to
    /// [`Queue.enqueue`] instead. The function [`Queue.dequeue_many`]
    /// call is not affected.
    ///
    /// [`Queue.enqueue`]: #method.enqueue
    /// [`Queue.dequeue_many`]: #method.dequeue_many
    pub fn enqueue_many<'a, I>(&'a self, props: I) -> Result<()>
    where
        I: IntoIterator<Item = &'a MsgProps<T>>,
    {
        let iter = props.into_iter();
        let (lower, _) = iter.size_hint();
        let mut raw_props = Vec::with_capacity(lower);
        for msg in iter {
            let handle = msg.handle();
            raw_props.push(handle);
            unsafe {
                dpiMsgProps_addRef(handle);
            }
        }
        chkerr!(
            self.ctxt(),
            dpiQueue_enqMany(
                self.handle(),
                raw_props.len() as u32,
                raw_props.as_mut_ptr()
            ),
            for handle in raw_props {
                unsafe {
                    dpiMsgProps_release(handle);
                }
            }
        );
        for handle in raw_props {
            unsafe {
                dpiMsgProps_release(handle);
            }
        }
        Ok(())
    }

    /// Returns a reference to the dequeue options associated with the queue. These
    /// options affect how messages are dequeued.
    pub fn deq_options(&mut self) -> Result<&mut DeqOptions> {
        if self.deq_options.is_none() {
            let mut handle = ptr::null_mut();
            chkerr!(
                self.ctxt(),
                dpiQueue_getDeqOptions(self.handle(), &mut handle)
            );
            self.deq_options = Some(DeqOptions::new(self.ctxt().clone(), handle));
        }
        Ok(self.deq_options.as_mut().unwrap())
    }

    /// Returns a reference to the enqueue options associated with the queue. These
    /// options affect how messages are enqueued.
    pub fn enq_options(&mut self) -> Result<&mut EnqOptions> {
        if self.enq_options.is_none() {
            let mut handle = ptr::null_mut();
            chkerr!(
                self.ctxt(),
                dpiQueue_getEnqOptions(self.handle(), &mut handle)
            );
            self.enq_options = Some(EnqOptions::new(self.ctxt().clone(), handle));
        }
        Ok(self.enq_options.as_mut().unwrap())
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
/// Delivery mode used for filtering messages when dequeuing messages from a queue
///
/// **Warning:** The type is unstable. It may be changed incompatibly by minor version upgrades.
pub enum MessageDeliveryMode {
    /// Dequeue only persistent messages from the queue. This is the default mode.
    Persistent,
    /// Dequeue only buffered messages from the queue.
    Buffered,
    /// Dequeue both persistent and buffered messages from the queue.
    PersistentOrBuffered,
}

impl MessageDeliveryMode {
    fn from_dpi_value(val: dpiMessageDeliveryMode) -> Result<MessageDeliveryMode> {
        match val as u32 {
            DPI_MODE_MSG_PERSISTENT => Ok(MessageDeliveryMode::Persistent),
            DPI_MODE_MSG_BUFFERED => Ok(MessageDeliveryMode::Buffered),
            DPI_MODE_MSG_PERSISTENT_OR_BUFFERED => Ok(MessageDeliveryMode::PersistentOrBuffered),
            _ => Err(Error::internal_error(format!(
                "unknown dpiMessageDeliveryMode {}",
                val
            ))),
        }
    }

    fn to_dpi_value(&self) -> dpiMessageDeliveryMode {
        match self {
            MessageDeliveryMode::Persistent => DPI_MODE_MSG_PERSISTENT as dpiMessageDeliveryMode,
            MessageDeliveryMode::Buffered => DPI_MODE_MSG_PERSISTENT as dpiMessageDeliveryMode,
            MessageDeliveryMode::PersistentOrBuffered => {
                DPI_MODE_MSG_PERSISTENT as dpiMessageDeliveryMode
            }
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
/// Possible states for messages in a queue
///
/// **Warning:** The type is unstable. It may be changed incompatibly by minor version upgrades.
pub enum MessageState {
    /// The message is ready to be processed.
    Ready,
    /// The message is waiting for the delay time to expire.
    Waiting,
    /// The message has already been processed and is retained.
    Processed,
    /// The message has been moved to the exception queue.
    Expired,
}

impl MessageState {
    fn from_dpi_value(val: dpiMessageState) -> Result<MessageState> {
        match val {
            DPI_MSG_STATE_READY => Ok(MessageState::Ready),
            DPI_MSG_STATE_WAITING => Ok(MessageState::Waiting),
            DPI_MSG_STATE_PROCESSED => Ok(MessageState::Processed),
            DPI_MSG_STATE_EXPIRED => Ok(MessageState::Expired),
            _ => Err(Error::internal_error(format!(
                "unknown dpiMessageState {}",
                val
            ))),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
/// Modes that are possible when dequeuing messages from a queue
///
/// **Warning:** The type is unstable. It may be changed incompatibly by minor version upgrades.
pub enum DeqMode {
    /// Read the message without acquiring a lock on the
    ///  message(equivalent to a SELECT statement).
    Browse,
    /// Read the message and obtain a write lock on the
    /// message (equivalent to a SELECT FOR UPDATE
    /// statement).
    Locked,
    /// Read the message and update or delete it. This is
    /// the default mode. Note that the message may be
    /// retained in the queue table based on retention
    /// properties.
    Remove,
    /// Confirms receipt of the message but does not
    /// deliver the actual message content.
    RemoveNoData,
}

impl DeqMode {
    fn from_dpi_value(val: dpiDeqMode) -> Result<DeqMode> {
        match val {
            DPI_MODE_DEQ_BROWSE => Ok(DeqMode::Browse),
            DPI_MODE_DEQ_LOCKED => Ok(DeqMode::Locked),
            DPI_MODE_DEQ_REMOVE => Ok(DeqMode::Remove),
            DPI_MODE_DEQ_REMOVE_NO_DATA => Ok(DeqMode::RemoveNoData),
            _ => Err(Error::internal_error(format!("unknown dpiDeqMode {}", val))),
        }
    }

    fn to_dpi_value(&self) -> dpiDeqMode {
        match self {
            DeqMode::Browse => DPI_MODE_DEQ_BROWSE,
            DeqMode::Locked => DPI_MODE_DEQ_LOCKED,
            DeqMode::Remove => DPI_MODE_DEQ_REMOVE,
            DeqMode::RemoveNoData => DPI_MODE_DEQ_REMOVE_NO_DATA,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
/// method used for determining which message is to be dequeued from a queue
///
/// **Warning:** The type is unstable. It may be changed incompatibly by minor version upgrades.
pub enum DeqNavigation {
    /// Retrieves the first available message that
    /// matches the search criteria. This resets the
    /// position to the beginning of the queue.
    FirstMessage,
    /// Skips the remainder of the current transaction
    /// group (if any) and retrieves the first message of
    /// the next transaction group. This option can only
    /// be used if message grouping is enabled for the
    /// queue.
    NextTransaction,
    /// Retrieves the next available message that matches
    /// the search criteria. This is the default method.
    NextMessage,
}

impl DeqNavigation {
    fn from_dpi_value(val: dpiDeqNavigation) -> Result<DeqNavigation> {
        match val {
            DPI_DEQ_NAV_FIRST_MSG => Ok(DeqNavigation::FirstMessage),
            DPI_DEQ_NAV_NEXT_TRANSACTION => Ok(DeqNavigation::NextTransaction),
            DPI_DEQ_NAV_NEXT_MSG => Ok(DeqNavigation::NextMessage),
            _ => Err(Error::internal_error(format!(
                "unknown dpiDeqNavigation {}",
                val
            ))),
        }
    }

    fn to_dpi_value(&self) -> dpiDeqNavigation {
        match self {
            DeqNavigation::FirstMessage => DPI_DEQ_NAV_FIRST_MSG,
            DeqNavigation::NextTransaction => DPI_DEQ_NAV_NEXT_TRANSACTION,
            DeqNavigation::NextMessage => DPI_DEQ_NAV_NEXT_MSG,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
/// visibility of messages in advanced queuing
///
/// **Warning:** The type is unstable. It may be changed incompatibly by minor version upgrades.
pub enum Visibility {
    /// The message is not part of the current transaction
    /// but constitutes a transaction of its own.
    Immediate,
    /// The message is part of the current transaction.
    /// This is the default value.
    OnCommit,
}

impl Visibility {
    fn from_dpi_value(val: dpiVisibility) -> Result<Visibility> {
        match val {
            DPI_VISIBILITY_IMMEDIATE => Ok(Visibility::Immediate),
            DPI_VISIBILITY_ON_COMMIT => Ok(Visibility::OnCommit),
            _ => Err(Error::internal_error(format!(
                "unknown dpiVisibility {}",
                val
            ))),
        }
    }

    fn to_dpi_value(&self) -> dpiVisibility {
        match self {
            Visibility::Immediate => DPI_VISIBILITY_IMMEDIATE,
            Visibility::OnCommit => DPI_VISIBILITY_ON_COMMIT,
        }
    }
}

/// Options when dequeuing messages using advanced queueing
///
/// **Warning:** The type is unstable. It may be changed incompatibly by minor version upgrades.
pub struct DeqOptions {
    ctxt: Context,
    handle: *mut dpiDeqOptions,
}

impl DeqOptions {
    fn new(ctxt: Context, handle: *mut dpiDeqOptions) -> DeqOptions {
        DeqOptions { ctxt, handle }
    }

    fn ctxt(&self) -> &Context {
        &self.ctxt
    }

    /// Returns the condition that must be satisfied in order for a message to be
    /// dequeued.
    ///
    /// See [`set_condition`](#method.set_condition) method for more information
    pub fn condition(&self) -> Result<String> {
        let mut s = new_odpi_str();
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_getCondition(self.handle, &mut s.ptr, &mut s.len)
        );
        Ok(s.to_string())
    }

    /// Returns the name of the consumer that is dequeuing messages.
    ///
    /// see [`set_consumer_name`](#method.set_consumer_name) method for more information.
    pub fn consumer_name(&self) -> Result<String> {
        let mut s = new_odpi_str();
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_getConsumerName(self.handle, &mut s.ptr, &mut s.len)
        );
        Ok(s.to_string())
    }

    ///  Returns the correlation of the message to be dequeued.
    ///
    ///  See [`set_correlation`](#method.set_correlation) method for more information.
    pub fn correlation(&self) -> Result<String> {
        let mut s = new_odpi_str();
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_getCorrelation(self.handle, &mut s.ptr, &mut s.len)
        );
        Ok(s.to_string())
    }

    /// Returns the mode that is to be used when dequeuing messages.
    pub fn mode(&self) -> Result<DeqMode> {
        let mut val = 0;
        chkerr!(self.ctxt(), dpiDeqOptions_getMode(self.handle, &mut val));
        DeqMode::from_dpi_value(val)
    }

    /// Returns the identifier of the specific message that is to be dequeued.
    pub fn message_id(&self) -> Result<Vec<u8>> {
        let mut msg = new_odpi_str();
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_getMsgId(self.handle, &mut msg.ptr, &mut msg.len)
        );
        Ok(msg.to_vec())
    }

    /// Returns the position of the message that is to be dequeued.
    pub fn navigation(&self) -> Result<DeqNavigation> {
        let mut val = 0;
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_getNavigation(self.handle, &mut val)
        );
        DeqNavigation::from_dpi_value(val)
    }

    /// Returns the transformation of the message to be dequeued.
    ///
    /// See [`set_transformation`](#method.set_transformation) method for more information.
    pub fn transformation(&self) -> Result<String> {
        let mut s = new_odpi_str();
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_getTransformation(self.handle, &mut s.ptr, &mut s.len)
        );
        Ok(s.to_string())
    }

    /// Returns whether the message being dequeued is part of the current
    /// transaction or constitutes a transaction on its own.
    pub fn visibility(&self) -> Result<Visibility> {
        let mut val = 0;
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_getVisibility(self.handle, &mut val)
        );
        Visibility::from_dpi_value(val)
    }

    /// Returns the time to wait for a message matching the search
    /// criteria.
    pub fn wait(&self) -> Result<Duration> {
        let mut val = 0;
        chkerr!(self.ctxt(), dpiDeqOptions_getWait(self.handle, &mut val));
        Ok(Duration::from_secs(val as u64))
    }

    /// Sets the condition which must be true for messages to be dequeued.
    ///
    /// The condition must be a valid boolean expression similar to the where clause
    /// of a SQL query. The expression can include conditions on message
    /// properties, user data properties and PL/SQL or SQL functions. User data
    /// properties must be prefixed with tab.user_data as a qualifier to indicate
    /// the specific column of the queue table that stores the message payload.
    pub fn set_condition(&mut self, val: &str) -> Result<()> {
        let val = to_odpi_str(val);
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_setCondition(self.handle, val.ptr, val.len)
        );
        Ok(())
    }

    /// Sets the name of the consumer which will be dequeuing messages. This value
    /// should only be set if the queue is set up for multiple consumers.
    pub fn set_consumer_name(&mut self, val: &str) -> Result<()> {
        let val = to_odpi_str(val);
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_setConsumerName(self.handle, val.ptr, val.len)
        );
        Ok(())
    }

    /// Sets the correlation of the message to be dequeued.
    ///
    /// Special pattern  matching characters such as the percent
    /// sign (`%`) and the underscore (`_`)
    /// can be used. If multiple messages satisfy the pattern, the order of
    /// dequeuing is undetermined.
    pub fn set_correlation(&mut self, val: &str) -> Result<()> {
        let val = to_odpi_str(val);
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_setCorrelation(self.handle, val.ptr, val.len)
        );
        Ok(())
    }

    /// Sets the message delivery mode that is to be used when dequeuing messages.
    pub fn set_delivery_mode(&mut self, val: &MessageDeliveryMode) -> Result<()> {
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_setDeliveryMode(self.handle, val.to_dpi_value())
        );
        Ok(())
    }

    /// Sets the mode that is to be used when dequeuing messages.
    pub fn set_mode(&mut self, val: &DeqMode) -> Result<()> {
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_setMode(self.handle, val.to_dpi_value())
        );
        Ok(())
    }

    /// Sets the identifier of the specific message to be dequeued.
    pub fn set_message_id(&mut self, val: &[u8]) -> Result<()> {
        let ptr = if val.is_empty() {
            ptr::null()
        } else {
            val.as_ptr() as *const c_char
        };
        let len = val.len() as u32;
        chkerr!(self.ctxt(), dpiDeqOptions_setMsgId(self.handle, ptr, len));
        Ok(())
    }

    /// Sets the position in the queue of the message that is to be dequeued.
    pub fn set_navigation(&mut self, val: &DeqNavigation) -> Result<()> {
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_setNavigation(self.handle, val.to_dpi_value())
        );
        Ok(())
    }

    /// Sets the transformation of the message to be dequeued.
    ///
    /// The transformation
    /// is applied after the message is dequeued but before it is returned to the
    /// application. It must be created using DBMS_TRANSFORM.
    pub fn set_transformation(&mut self, val: &str) -> Result<()> {
        let val = to_odpi_str(val);
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_setTransformation(self.handle, val.ptr, val.len)
        );
        Ok(())
    }

    /// Sets whether the message being dequeued is part of the current transaction
    /// or constitutes a transaction on its own.
    pub fn set_visibility(&mut self, val: &Visibility) -> Result<()> {
        chkerr!(
            self.ctxt(),
            dpiDeqOptions_setVisibility(self.handle, val.to_dpi_value())
        );
        Ok(())
    }

    /// Set the time to wait for a message matching the search
    /// criteria.
    pub fn set_wait(&mut self, val: &Duration) -> Result<()> {
        let secs = val.as_secs();
        let secs = if secs > u32::max_value().into() {
            u32::max_value()
        } else {
            secs as u32
        };
        chkerr!(self.ctxt(), dpiDeqOptions_setWait(self.handle, secs));
        Ok(())
    }
}

impl fmt::Debug for DeqOptions {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "DeqOptions {{ handle: {:?} }}", self.handle)
    }
}

/// Options when enqueuing messages using advanced queueing
///
/// **Warning:** The type is unstable. It may be changed incompatibly by minor version upgrades.
pub struct EnqOptions {
    ctxt: Context,
    handle: *mut dpiEnqOptions,
}

impl EnqOptions {
    fn new(ctxt: Context, handle: *mut dpiEnqOptions) -> EnqOptions {
        EnqOptions { ctxt, handle }
    }

    fn ctxt(&self) -> &Context {
        &self.ctxt
    }

    /// Returns the transformation of the message to be enqueued.
    ///
    /// See [`set_transformation`](#method.set_transformation) method for more information.
    pub fn transformation(&self) -> Result<String> {
        let mut s = new_odpi_str();
        chkerr!(
            self.ctxt(),
            dpiEnqOptions_getTransformation(self.handle, &mut s.ptr, &mut s.len)
        );
        Ok(s.to_string())
    }

    /// Returns whether the message being enqueued is part of the current
    /// transaction or constitutes a transaction on its own.
    pub fn visibility(&self) -> Result<Visibility> {
        let mut val = 0;
        chkerr!(
            self.ctxt(),
            dpiEnqOptions_getVisibility(self.handle, &mut val)
        );
        Visibility::from_dpi_value(val)
    }

    /// Sets the message delivery mode that is to be used when enqueuing messages.
    pub fn set_delivery_mode(&mut self, val: &MessageDeliveryMode) -> Result<()> {
        chkerr!(
            self.ctxt(),
            dpiEnqOptions_setDeliveryMode(self.handle, val.to_dpi_value())
        );
        Ok(())
    }

    /// Sets the transformation of the message to be enqueued.
    ///
    /// The transformation
    /// is applied after the message is enqueued but before it is returned to the
    /// application. It must be created using DBMS_TRANSFORM.
    pub fn set_transformation(&mut self, val: &str) -> Result<()> {
        let val = to_odpi_str(val);
        chkerr!(
            self.ctxt(),
            dpiEnqOptions_setTransformation(self.handle, val.ptr, val.len)
        );
        Ok(())
    }

    /// Sets whether the message being enqueued is part of the current transaction
    /// or constitutes a transaction on its own.
    pub fn set_visibility(&mut self, val: &Visibility) -> Result<()> {
        chkerr!(
            self.ctxt(),
            dpiEnqOptions_setVisibility(self.handle, val.to_dpi_value())
        );
        Ok(())
    }
}

impl fmt::Debug for EnqOptions {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "EnqOptions {{ handle: {:?} }}", self.handle)
    }
}

/// Properties of messages that are enqueued and dequeued using advanced queuing
///
/// **Warning:** The type is unstable. It may be changed incompatibly by minor version upgrades.
#[derive(Clone)]
pub struct MsgProps<T>
where
    T: Payload + ?Sized,
{
    conn: Conn,
    handle: DpiMsgProps,
    payload_type: Option<ObjectType>,
    phantom: PhantomData<T>,
}

impl<T> MsgProps<T>
where
    T: Payload + ?Sized,
{
    fn handle(&self) -> *mut dpiMsgProps {
        self.handle.raw()
    }

    fn ctxt(&self) -> &Context {
        self.conn.ctxt()
    }

    /// Creates a new message properties
    pub fn new(conn: &Connection) -> Result<MsgProps<T>> {
        let mut handle = ptr::null_mut();
        chkerr!(conn.ctxt(), dpiConn_newMsgProps(conn.handle(), &mut handle));
        Ok(MsgProps {
            conn: conn.conn.clone(),
            handle: DpiMsgProps::new(handle),
            payload_type: None,
            phantom: PhantomData,
        })
    }

    fn from_dpi_msg_props(
        conn: Conn,
        handle: DpiMsgProps,
        payload_type: Option<ObjectType>,
    ) -> MsgProps<T> {
        MsgProps {
            conn,
            handle,
            payload_type,
            phantom: PhantomData,
        }
    }

    /// Returns the number of attempts that have been made to dequeue a message.
    pub fn num_attempts(&self) -> Result<i32> {
        let mut val = 0;
        chkerr!(
            self.ctxt(),
            dpiMsgProps_getNumAttempts(self.handle(), &mut val)
        );
        Ok(val)
    }

    /// Returns the correlation supplied by the producer when the message was
    /// enqueued.
    pub fn correlation(&self) -> Result<String> {
        let mut s = new_odpi_str();
        chkerr!(
            self.ctxt(),
            dpiMsgProps_getCorrelation(self.handle(), &mut s.ptr, &mut s.len)
        );
        Ok(s.to_string())
    }

    /// Returns the duration the enqueued message will be delayed.
    pub fn delay(&self) -> Result<Duration> {
        let mut secs = 0;
        chkerr!(self.ctxt(), dpiMsgProps_getDelay(self.handle(), &mut secs));
        Ok(Duration::from_secs(secs as u64))
    }

    /// Returns the mode that was used to deliver the message.
    pub fn delivery_mode(&self) -> Result<MessageDeliveryMode> {
        let mut val = 0;
        chkerr!(
            self.ctxt(),
            dpiMsgProps_getDeliveryMode(self.handle(), &mut val)
        );
        MessageDeliveryMode::from_dpi_value(val)
    }

    /// Returns the time that the message was enqueued.
    pub fn enq_time(&self) -> Result<Timestamp> {
        let mut val = Default::default();
        chkerr!(self.ctxt(), dpiMsgProps_getEnqTime(self.handle(), &mut val));
        Ok(Timestamp::from_dpi_timestamp(&val, &OracleType::Date))
    }

    /// Returns the name of the queue to which the message is moved if it cannot be
    /// processed successfully.
    ///
    /// See [`set_exception_queue`](#method.set_exception_queue) method for more information.
    pub fn exception_queue(&self) -> Result<String> {
        let mut s = new_odpi_str();
        chkerr!(
            self.ctxt(),
            dpiMsgProps_getExceptionQ(self.handle(), &mut s.ptr, &mut s.len)
        );
        Ok(s.to_string())
    }

    /// Returns the duration the message is available to be dequeued.
    ///
    /// See [`set_expiration`](#method.set_expiration) method for more information.
    pub fn expiration(&self) -> Result<Duration> {
        let mut val = 0;
        chkerr!(
            self.ctxt(),
            dpiMsgProps_getExpiration(self.handle(), &mut val)
        );
        Ok(Duration::from_secs(val as u64))
    }

    /// Returns the id of the message in the queue that generated this message. No
    /// value is available until the message has been enqueued or dequeued.
    pub fn message_id(&self) -> Result<Vec<u8>> {
        let mut msg = new_odpi_str();
        chkerr!(
            self.ctxt(),
            dpiMsgProps_getMsgId(self.handle(), &mut msg.ptr, &mut msg.len)
        );
        Ok(msg.to_vec())
    }

    /// Returns the id of the message in the last queue that generated this
    /// message.
    ///
    /// See [`set_original_message_id`](#method.set_original_message_id) for more information.
    pub fn original_message_id(&self) -> Result<Vec<u8>> {
        let mut msg = new_odpi_str();
        chkerr!(
            self.ctxt(),
            dpiMsgProps_getOriginalMsgId(self.handle(), &mut msg.ptr, &mut msg.len)
        );
        Ok(msg.to_vec())
    }

    /// Returns the payload associated with the message properties.
    ///
    /// The payload is available after the a call to [`Queue.dequeue`] or
    /// [`Queue.dequeue_many`]
    ///
    /// [`Queue.dequeue`]: Queue#method.dequeue
    /// [`Queue.dequeue_many`]: Queue#method.dequeue_many
    pub fn payload(&self) -> Result<T::Owned> {
        T::get(self)
    }

    /// Returns the priority assigned to the message.
    ///
    /// See [`set_priority`](#method.set_priority) method for more information.
    pub fn priority(&self) -> Result<i32> {
        let mut val = 0;
        chkerr!(
            self.ctxt(),
            dpiMsgProps_getPriority(self.handle(), &mut val)
        );
        Ok(val)
    }

    /// Returns the state of the message at the time of dequeue.
    pub fn state(&self) -> Result<MessageState> {
        let mut val = 0;
        chkerr!(self.ctxt(), dpiMsgProps_getState(self.handle(), &mut val));
        MessageState::from_dpi_value(val)
    }

    /// Sets the correlation of the message to be dequeued.
    ///
    /// Special pattern matching characters such as the percent
    /// sign (`%`) and the underscore (`_`) can be used. If multiple
    /// messages satisfy the pattern, the order of dequeuing is
    /// undetermined.
    pub fn set_correlation(&mut self, val: &str) -> Result<()> {
        let val = to_odpi_str(val);
        chkerr!(
            self.ctxt(),
            dpiMsgProps_setCorrelation(self.handle(), val.ptr, val.len)
        );
        Ok(())
    }

    /// Sets the number of seconds to delay the message before it can be dequeued.
    ///
    /// Messages enqueued with a delay are put into the [`MessageState::Waiting`]
    /// state. When the delay expires the message is put into the
    /// [`MessageState::Ready`] state. Dequeuing directly by message id overrides this
    /// delay specification. Note that delay processing requires the queue monitor
    /// to be started.
    ///
    /// [`MessageState::Waiting`]: MessageState#variant.Waiting
    /// [`MessageState::Ready`]: MessageState#variant.Ready
    pub fn set_delay(&mut self, val: &Duration) -> Result<()> {
        let secs = val.as_secs();
        if secs > i32::max_value() as u64 {
            Err(Error::out_of_range(format!("too long duration {:?}", val)))
        } else {
            chkerr!(
                self.ctxt(),
                dpiMsgProps_setDelay(self.handle(), secs as i32)
            );
            Ok(())
        }
    }

    /// Sets the name of the queue to which the message is moved if it cannot be
    /// processed successfully.
    ///
    /// Messages are moved if the number of unsuccessful
    /// dequeue attempts has reached the maximum allowed number or if the message
    /// has expired. All messages in the exception queue are in the
    /// [`MessageState::Expired`] state.
    ///
    /// [`MessageState::Expired`]: MessageState#variant.Expired
    pub fn set_exception_queue(&mut self, val: &str) -> Result<()> {
        let val = to_odpi_str(val);
        chkerr!(
            self.ctxt(),
            dpiMsgProps_setExceptionQ(self.handle(), val.ptr, val.len)
        );
        Ok(())
    }

    /// Sets the number of seconds the message is available to be dequeued.
    ///
    /// This value is an offset from the delay. Expiration processing requires the queue
    /// monitor to be running. Until this time elapses, the messages are in the
    /// queue in the state [`MessageState::Ready`]. After this time elapses messages
    /// are moved to the exception queue in the [`MessageState::Expired`] state.
    ///
    /// [`MessageState::Ready`]: MessageState#variant.Ready
    /// [`MessageState::Expired`]: MessageState#variant.Expired
    pub fn set_expiration(&mut self, val: &Duration) -> Result<()> {
        let secs = val.as_secs();
        if secs > i32::max_value() as u64 {
            Err(Error::out_of_range(format!("too long duration {:?}", val)))
        } else {
            chkerr!(
                self.ctxt(),
                dpiMsgProps_setExpiration(self.handle(), secs as i32)
            );
            Ok(())
        }
    }

    /// Sets the id of the message in the last queue that generated this
    /// message.
    pub fn set_original_message_id(&mut self, val: &[u8]) -> Result<()> {
        let ptr = if val.is_empty() {
            ptr::null()
        } else {
            val.as_ptr() as *const c_char
        };
        let len = val.len() as u32;
        chkerr!(
            self.ctxt(),
            dpiMsgProps_setOriginalMsgId(self.handle(), ptr, len)
        );
        Ok(())
    }

    /// Sets the payload for the message.
    ///
    /// This value will be used when the message is enqueued using
    /// [`Queue.enqueue`] or [`Queue.enqueue_many`].
    ///
    /// [`Queue.enqueue`]: Queue#method.enqueue
    /// [`Queue.enqueue_many`]: Queue#method.enqueue_many
    pub fn set_payload(&mut self, val: &T) -> Result<()> {
        val.set(self)
    }

    /// Sets the priority assigned to the message.
    ///
    /// A smaller number indicates a higher priority. The priority can
    /// be any number, including negative numbers.
    pub fn set_priority(&mut self, val: i32) -> Result<()> {
        chkerr!(self.ctxt(), dpiMsgProps_setPriority(self.handle(), val));
        Ok(())
    }
}

impl<T> fmt::Debug for MsgProps<T>
where
    T: Payload,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "MsgProps {{ handle: {:?} }}", self.handle())
    }
}