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
// Rust-oracle - Rust binding for Oracle database
//
// URL: https://github.com/kubo/rust-oracle
//
//-----------------------------------------------------------------------------
// Copyright (c) 2017-2018 Kubo Takehiro <kubo@jiubao.org>. All rights reserved.
// This program is free software: you can modify it and/or redistribute it
// under the terms of:
//
// (i)  the Universal Permissive License v 1.0 or at your option, any
//      later version (http://oss.oracle.com/licenses/upl); and/or
//
// (ii) the Apache License v 2.0. (http://www.apache.org/licenses/LICENSE-2.0)
//-----------------------------------------------------------------------------

use std::cmp;
use std::fmt;
use std::ptr;
use std::sync::Arc;

use crate::binding::*;
use crate::chkerr;
use crate::sql_type::FromSql;
use crate::sql_type::OracleType;
use crate::sql_type::ToSql;
use crate::to_rust_str;
use crate::util::write_literal;
use crate::Connection;
use crate::Context;
use crate::DpiObjectAttr;
use crate::DpiObjectType;
use crate::Error;
use crate::Result;
use crate::SqlValue;

unsafe fn release_dpi_data(data: &dpiData, native_type_num: u32) {
    if data.isNull == 0 {
        match native_type_num {
            DPI_NATIVE_TYPE_LOB => {
                dpiLob_release(data.value.asLOB);
            }
            DPI_NATIVE_TYPE_OBJECT => {
                dpiObject_release(data.value.asObject);
            }
            DPI_NATIVE_TYPE_ROWID => {
                dpiRowid_release(data.value.asRowid);
            }
            _ => (),
        }
    }
}

/// Oracle-specific collection data type
///
/// This type corresponds to varray and nested table data types.
/// See [Oracle manual](https://docs.oracle.com/database/122/ADOBJ/collection-data-types.htm).
///
/// ```no_run
/// # use oracle::*; fn try_main() -> Result<()> {
/// let conn = Connection::connect("scott", "tiger", "")?;
///
/// // MDSYS.SDO_ELEM_INFO_ARRAY is defined as VARRAY (1048576) of NUMBER.
/// let objtype = conn.object_type("MDSYS.SDO_ELEM_INFO_ARRAY")?;
///
/// // Create a new collection
/// let mut obj = objtype.new_collection()?;
/// obj.push(&1);
/// obj.push(&3);
/// assert_eq!(obj.to_string(), "MDSYS.SDO_ELEM_INFO_ARRAY(1, 3)");
/// # Ok(())} fn main() { try_main().unwrap(); }
/// ```
///
/// Note: Methods in the type may be changed in future.
pub struct Collection {
    ctxt: &'static Context,
    pub(crate) handle: *mut dpiObject,
    objtype: ObjectType,
}

impl Collection {
    pub(crate) fn new(
        ctxt: &'static Context,
        handle: *mut dpiObject,
        objtype: ObjectType,
    ) -> Collection {
        Collection {
            ctxt: ctxt,
            handle: handle,
            objtype: objtype,
        }
    }

    /// Returns type information.
    pub fn object_type(&self) -> &ObjectType {
        &self.objtype
    }

    /// Returns the number of elements.
    ///
    /// This counts also deleted elements. See "Comments" about [OCICollSize()][].
    ///
    /// [OCICollSize()]: https://docs.oracle.com/en/database/oracle/oracle-database/12.2/lnoci/oci-collection-and-iterator-functions.html#GUID-B8F6665F-12F1-43DB-A27E-82A2A655D701
    pub fn size(&self) -> Result<i32> {
        let mut size = 0;
        chkerr!(self.ctxt, dpiObject_getSize(self.handle, &mut size));
        Ok(size)
    }

    /// Returns the first index.
    ///
    /// Use this method if indexes of the collection isn't continuous.
    pub fn first_index(&self) -> Result<i32> {
        let mut index = 0;
        let mut exists = 0;
        chkerr!(
            self.ctxt,
            dpiObject_getFirstIndex(self.handle, &mut index, &mut exists)
        );
        if exists != 0 {
            Ok(index)
        } else {
            Err(Error::NoDataFound)
        }
    }

    /// Returns the last index.
    ///
    /// Use this method if indexes of the collection isn't continuous.
    pub fn last_index(&self) -> Result<i32> {
        let mut index = 0;
        let mut exists = 0;
        chkerr!(
            self.ctxt,
            dpiObject_getLastIndex(self.handle, &mut index, &mut exists)
        );
        if exists != 0 {
            Ok(index)
        } else {
            Err(Error::NoDataFound)
        }
    }

    /// Returns the next index following the specified index.
    ///
    /// Use this method if indexes of the collection isn't continuous.
    pub fn next_index(&self, index: i32) -> Result<i32> {
        let mut next = 0;
        let mut exists = 0;
        chkerr!(
            self.ctxt,
            dpiObject_getNextIndex(self.handle, index, &mut next, &mut exists)
        );
        if exists != 0 {
            Ok(next)
        } else {
            Err(Error::NoDataFound)
        }
    }

    /// Returns the previous index following the specified index.
    ///
    /// Use this method if indexes of the collection isn't continuous.
    pub fn prev_index(&self, index: i32) -> Result<i32> {
        let mut prev = 0;
        let mut exists = 0;
        chkerr!(
            self.ctxt,
            dpiObject_getPrevIndex(self.handle, index, &mut prev, &mut exists)
        );
        if exists != 0 {
            Ok(prev)
        } else {
            Err(Error::NoDataFound)
        }
    }

    /// Returns whether an element exists at the specified index.
    pub fn exist(&self, index: i32) -> Result<bool> {
        let mut exists = 0;
        chkerr!(
            self.ctxt,
            dpiObject_getElementExistsByIndex(self.handle, index, &mut exists)
        );
        Ok(exists != 0)
    }

    /// Returns the value of the element at the specified index.
    pub fn get<T>(&self, index: i32) -> Result<T>
    where
        T: FromSql,
    {
        let oratype = self.objtype.element_oracle_type().unwrap();
        let mut data = Default::default();
        let mut buf = [0i8; 172]; // DPI_NUMBER_AS_TEXT_CHARS in odpi/src/dpiImpl.h
        if let OracleType::Number(_, _) = *oratype {
            unsafe {
                dpiData_setBytes(&mut data, buf.as_mut_ptr(), buf.len() as u32);
            }
        }
        let sql_value = SqlValue::from_oratype(self.ctxt, oratype, &mut data)?;
        let native_type_num = sql_value.native_type_num();
        chkerr!(
            self.ctxt,
            dpiObject_getElementValueByIndex(self.handle, index, native_type_num, &mut data)
        );
        let res = sql_value.get();
        unsafe { release_dpi_data(&data, native_type_num) };
        res
    }

    /// Sets the value to the element at the specified index.
    pub fn set(&mut self, index: i32, value: &dyn ToSql) -> Result<()> {
        let oratype = self.objtype.element_oracle_type().unwrap();
        let mut data = Default::default();
        let mut sql_value = SqlValue::from_oratype(self.ctxt, oratype, &mut data)?;
        sql_value.set(value)?;
        chkerr!(
            self.ctxt,
            dpiObject_setElementValueByIndex(
                self.handle,
                index,
                sql_value.native_type_num(),
                &mut data
            )
        );
        Ok(())
    }

    /// Appends an element to the end of the collection.
    pub fn push(&mut self, value: &dyn ToSql) -> Result<()> {
        let oratype = self.objtype.element_oracle_type().unwrap();
        let mut data = Default::default();
        let mut sql_value = SqlValue::from_oratype(self.ctxt, oratype, &mut data)?;
        sql_value.set(value)?;
        chkerr!(
            self.ctxt,
            dpiObject_appendElement(self.handle, sql_value.native_type_num(), &mut data)
        );
        Ok(())
    }

    /// Remove the element at the specified index.
    /// Note that the position ordinals of the remaining elements are not changed.
    /// The operation creates **holes** in the collection.
    pub fn remove(&mut self, index: i32) -> Result<()> {
        chkerr!(
            self.ctxt,
            dpiObject_deleteElementByIndex(self.handle, index)
        );
        Ok(())
    }

    /// Trims a number of elements from the end of a collection.
    ///
    /// If the number of of elements to trim exceeds the current size
    /// of the collection an error is returned.
    pub fn trim(&mut self, len: usize) -> Result<()> {
        chkerr!(self.ctxt, dpiObject_trim(self.handle, len as u32));
        Ok(())
    }
}

impl Clone for Collection {
    fn clone(&self) -> Collection {
        unsafe { dpiObject_addRef(self.handle) };
        Collection::new(self.ctxt, self.handle, self.objtype.clone())
    }
}

impl Drop for Collection {
    fn drop(&mut self) {
        let _ = unsafe { dpiObject_release(self.handle) };
    }
}

impl FromSql for Collection {
    fn from_sql(val: &SqlValue) -> Result<Collection> {
        val.to_collection()
    }
}

impl ToSql for Collection {
    fn oratype(&self, _conn: &Connection) -> Result<OracleType> {
        Ok(OracleType::Object(self.object_type().clone()))
    }
    fn to_sql(&self, val: &mut SqlValue) -> Result<()> {
        val.set_collection(self)
    }
}

impl fmt::Display for Collection {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}(", self.objtype)?;
        if let Ok(index) = self.first_index() {
            let mut idx = index;
            let oratype = self.objtype.element_oracle_type().unwrap();
            loop {
                write_literal(f, &self.get(idx), oratype)?;
                if let Ok(index) = self.next_index(idx) {
                    idx = index;
                    write!(f, ", ")?;
                } else {
                    break;
                }
            }
        }
        write!(f, ")")
    }
}

impl fmt::Debug for Collection {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let oratype = self.objtype.element_oracle_type().unwrap();
        write!(f, "Collection({} collection of {}: ", self.objtype, oratype)?;
        if let Ok(index) = self.first_index() {
            let mut idx = index;
            loop {
                write_literal(f, &self.get(idx), oratype)?;
                if let Ok(index) = self.next_index(idx) {
                    idx = index;
                    write!(f, ", ")?;
                } else {
                    break;
                }
            }
        }
        write!(f, ")")
    }
}

/// Oracle-specific object data type
///
/// ```no_run
/// # use oracle::*; fn try_main() -> Result<()> {
/// let conn = Connection::connect("scott", "tiger", "")?;
///
/// // MDSYS.SDO_GEOMETRY
/// // https://docs.oracle.com/en/database/oracle/oracle-database/12.2/spatl/spatial-datatypes-metadata.html#GUID-683FF8C5-A773-4018-932D-2AF6EC8BC119
/// let geom_type = conn.object_type("MDSYS.SDO_GEOMETRY")?;
/// let point_type = conn.object_type("MDSYS.SDO_POINT_TYPE")?;
///
/// // Create a new object
/// let mut obj = geom_type.new_object()?;
/// let mut point = point_type.new_object()?;
/// point.set("X", &-79)?;
/// point.set("Y", &37)?;
/// obj.set("SDO_GTYPE", &2001)?;
/// obj.set("SDO_POINT", &point)?;
/// assert_eq!(obj.to_string(), "MDSYS.SDO_GEOMETRY(2001, NULL, MDSYS.SDO_POINT_TYPE(-79, 37, NULL), NULL, NULL)");
///
/// // Gets an attribute value.
/// let gtype: i32 = obj.get("SDO_GTYPE")?;
/// assert_eq!(gtype, 2001);
/// # Ok(())} fn main() { try_main().unwrap(); }
/// ```
///
/// Note: Methods in the type may be changed in future.
pub struct Object {
    ctxt: &'static Context,
    pub(crate) handle: *mut dpiObject,
    objtype: ObjectType,
}

impl Object {
    pub(crate) fn new(
        ctxt: &'static Context,
        handle: *mut dpiObject,
        objtype: ObjectType,
    ) -> Object {
        Object {
            ctxt: ctxt,
            handle: handle,
            objtype: objtype,
        }
    }

    /// Returns type information.
    pub fn object_type(&self) -> &ObjectType {
        &self.objtype
    }

    fn type_attr(&self, name: &str) -> Result<&ObjectTypeAttr> {
        for attr in self.objtype.attributes() {
            if attr.name() == name {
                return Ok(attr);
            }
        }
        Err(Error::InvalidAttributeName(name.to_string()))
    }

    pub(crate) fn get_by_attr<T>(&self, attr: &ObjectTypeAttr) -> Result<T>
    where
        T: FromSql,
    {
        let mut data = Default::default();
        let mut buf = [0i8; 172]; // DPI_NUMBER_AS_TEXT_CHARS in odpi/src/dpiImpl.h
        if let OracleType::Number(_, _) = attr.oratype {
            unsafe {
                dpiData_setBytes(&mut data, buf.as_mut_ptr(), buf.len() as u32);
            }
        }
        let sql_value = SqlValue::from_oratype(self.ctxt, &attr.oratype, &mut data)?;
        let native_type_num = sql_value.native_type_num();
        chkerr!(
            self.ctxt,
            dpiObject_getAttributeValue(self.handle, attr.handle.raw(), native_type_num, &mut data)
        );
        let res = sql_value.get();
        unsafe { release_dpi_data(&data, native_type_num) };
        res
    }

    /// Gets an value at the specified attribute.
    pub fn get<T>(&self, name: &str) -> Result<T>
    where
        T: FromSql,
    {
        self.get_by_attr(self.type_attr(name)?)
    }

    /// Sets the value to the specified attribute.
    pub fn set(&mut self, name: &str, value: &dyn ToSql) -> Result<()> {
        let attrtype = self.type_attr(name)?;
        let mut data = Default::default();
        let mut sql_value = SqlValue::from_oratype(self.ctxt, &attrtype.oratype, &mut data)?;
        sql_value.set(value)?;
        chkerr!(
            self.ctxt,
            dpiObject_setAttributeValue(
                self.handle,
                attrtype.handle.raw(),
                sql_value.native_type_num(),
                &mut data
            )
        );
        Ok(())
    }
}

impl Clone for Object {
    fn clone(&self) -> Object {
        unsafe { dpiObject_addRef(self.handle) };
        Object::new(self.ctxt, self.handle, self.objtype.clone())
    }
}

impl Drop for Object {
    fn drop(&mut self) {
        let _ = unsafe { dpiObject_release(self.handle) };
    }
}

impl FromSql for Object {
    fn from_sql(val: &SqlValue) -> Result<Object> {
        val.to_object()
    }
}

impl ToSql for Object {
    fn oratype(&self, _conn: &Connection) -> Result<OracleType> {
        Ok(OracleType::Object(self.object_type().clone()))
    }
    fn to_sql(&self, val: &mut SqlValue) -> Result<()> {
        val.set_object(self)
    }
}

impl fmt::Display for Object {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}(", self.objtype)?;
        let mut first = true;
        for attr in self.objtype.attributes() {
            if first {
                first = false;
            } else {
                write!(f, ", ")?;
            }
            write_literal(f, &self.get_by_attr(attr), &attr.oratype)?;
        }
        write!(f, ")")
    }
}

impl fmt::Debug for Object {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Object({}(", self.objtype)?;
        let mut first = true;
        for attr in self.objtype.attributes() {
            if first {
                first = false;
            } else {
                write!(f, ", ")?;
            }
            write!(f, "{}({}): ", attr.name(), attr.oracle_type())?;
            write_literal(f, &self.get_by_attr(attr), &attr.oratype)?;
        }
        write!(f, "))")
    }
}

/// Type information about Object or Collection data type
///
/// This is for not only Object type information but also
/// collection type information.
///
/// # Examples
///
/// Gets MDSYS.SDO_GEOMETRY object type information.
///
/// ```no_run
/// # use oracle::*; fn try_main() -> Result<()> {
/// let conn = Connection::connect("scott", "tiger", "")?;
/// let objtype = conn.object_type("MDSYS.SDO_GEOMETRY");
/// # Ok(())} fn main() { try_main().unwrap(); }
/// ```
///
/// Gets object type infomration in query.
///
/// ```no_run
/// # use oracle::*; use oracle::sql_type::*; fn try_main() -> Result<()> {
/// let conn = Connection::connect("scott", "tiger", "")?;
/// // conn.execute("create table location (name varchar2(60), loc sdo_geometry)", &[]);
/// let mut stmt = conn.prepare("select loc from location where name = '...'", &[])?;
/// let rows = stmt.query(&[])?;
/// let objtype = if let OracleType::Object(ref objtype) = *rows.column_info()[0].oracle_type() {
///     objtype
/// } else {
///     panic!("Not an object type")
/// };
/// # Ok(())} fn main() { try_main().unwrap(); }
/// ```
#[derive(Clone)]
pub struct ObjectType {
    pub(crate) internal: Arc<ObjectTypeInternal>,
}

impl ObjectType {
    pub(crate) fn from_dpi_object_type(
        ctxt: &'static Context,
        handle: DpiObjectType,
    ) -> Result<ObjectType> {
        Ok(ObjectType {
            internal: Arc::new(ObjectTypeInternal::from_dpi_object_type(ctxt, handle)?),
        })
    }

    pub(crate) fn handle(&self) -> &DpiObjectType {
        &self.internal.handle
    }

    /// Gets schema name
    pub fn schema(&self) -> &str {
        &self.internal.schema
    }

    /// Gets object name
    pub fn name(&self) -> &str {
        &self.internal.name
    }

    /// True when it is a collectoin. Otherwise false.
    pub fn is_collection(&self) -> bool {
        self.internal.elem_oratype.is_some()
    }

    /// Gets the Oracle type of elements if it is a collection.
    /// Otherwise, `None`.
    pub fn element_oracle_type(&self) -> Option<&OracleType> {
        if let Some(ref oratype) = self.internal.elem_oratype {
            Some(oratype)
        } else {
            None
        }
    }

    /// Gets the number of attributes if it isn't a collection.
    /// Otherwise, 0.
    pub fn num_attributes(&self) -> usize {
        self.internal.attrs.len()
    }

    /// Gets a vector of attribute information if it isn't a collection.
    /// Otherwise, a zero-length vector.
    ///
    /// # Examples
    ///
    /// Prints attribute information of `MDSYS.SDO_GEOMETRY`.
    ///
    /// ```no_run
    /// # use oracle::*; fn try_main() -> Result<()> {
    /// let conn = Connection::connect("scott", "tiger", "")?;
    /// let objtype = conn.object_type("MDSYS.SDO_GEOMETRY")?;
    /// for attr in objtype.attributes() {
    ///     println!("{:-20} {}", attr.name(), attr.oracle_type());
    /// }
    /// # Ok(())} fn main() { try_main().unwrap(); }
    /// ```
    pub fn attributes(&self) -> &[ObjectTypeAttr] {
        &self.internal.attrs
    }

    /// Create a new Oracle object.
    pub fn new_object(&self) -> Result<Object> {
        if self.is_collection() {
            return Err(Error::InvalidOperation(format!(
                "{}.{} isn't object type.",
                self.schema(),
                self.name()
            )));
        }
        let ctxt = self.internal.ctxt;
        let mut handle = ptr::null_mut();
        chkerr!(
            ctxt,
            dpiObjectType_createObject(self.internal.handle.raw(), &mut handle)
        );
        Ok(Object::new(ctxt, handle, self.clone()))
    }

    /// Create a new collection.
    pub fn new_collection(&self) -> Result<Collection> {
        if !self.is_collection() {
            return Err(Error::InvalidOperation(format!(
                "{}.{} isn't collection type.",
                self.schema(),
                self.name()
            )));
        }
        let ctxt = self.internal.ctxt;
        let mut handle = ptr::null_mut();
        chkerr!(
            ctxt,
            dpiObjectType_createObject(self.internal.handle.raw(), &mut handle)
        );
        Ok(Collection::new(ctxt, handle, self.clone()))
    }
}

impl cmp::PartialEq for ObjectType {
    fn eq(&self, other: &Self) -> bool {
        self.internal == other.internal
    }
}

impl fmt::Display for ObjectType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.internal)
    }
}

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

/// Object type attribute information
///
/// See [ObjectType.attributes()](struct.ObjectType.html#method.attributes)
pub struct ObjectTypeAttr {
    ctxt: &'static Context,
    handle: DpiObjectAttr,
    name: String,
    oratype: OracleType,
}

impl ObjectTypeAttr {
    fn new(ctxt: &'static Context, handle: DpiObjectAttr) -> Result<ObjectTypeAttr> {
        let mut info = Default::default();
        chkerr!(ctxt, dpiObjectAttr_getInfo(handle.raw(), &mut info));
        Ok(ObjectTypeAttr {
            ctxt: ctxt,
            handle: handle,
            name: to_rust_str(info.name, info.nameLength),
            oratype: OracleType::from_type_info(ctxt, &info.typeInfo)?,
        })
    }

    /// Gets the attribute name
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Gets the attribute type
    pub fn oracle_type(&self) -> &OracleType {
        &self.oratype
    }
}

impl Clone for ObjectTypeAttr {
    fn clone(&self) -> ObjectTypeAttr {
        ObjectTypeAttr {
            ctxt: self.ctxt,
            handle: self.handle.clone(),
            name: self.name.clone(),
            oratype: self.oratype.clone(),
        }
    }
}

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

//
// ObjectTypeInternal
//

pub(crate) struct ObjectTypeInternal {
    ctxt: &'static Context,
    handle: DpiObjectType,
    schema: String,
    name: String,
    elem_oratype: Option<OracleType>,
    attrs: Vec<ObjectTypeAttr>,
}

impl ObjectTypeInternal {
    fn from_dpi_object_type(
        ctxt: &'static Context,
        handle: DpiObjectType,
    ) -> Result<ObjectTypeInternal> {
        let mut info = Default::default();
        chkerr!(ctxt, dpiObjectType_getInfo(handle.raw(), &mut info));
        let (elem_oratype, attrs) = if info.isCollection != 0 {
            match OracleType::from_type_info(ctxt, &info.elementTypeInfo) {
                Ok(oratype) => (Some(oratype), Vec::new()),
                Err(err) => return Err(err),
            }
        } else {
            let attrnum = info.numAttributes as usize;
            let mut attr_handles = vec![ptr::null_mut(); attrnum];
            chkerr!(
                ctxt,
                dpiObjectType_getAttributes(
                    handle.raw(),
                    info.numAttributes,
                    attr_handles.as_mut_ptr()
                )
            );
            let mut attrs = Vec::with_capacity(attrnum);
            for i in 0..attrnum {
                match ObjectTypeAttr::new(ctxt, DpiObjectAttr::new(attr_handles[i])) {
                    Ok(attr) => attrs.push(attr),
                    Err(err) => {
                        for j in (i + 1)..attrnum {
                            unsafe {
                                dpiObjectAttr_release(attr_handles[j]);
                            }
                        }
                        return Err(err);
                    }
                }
            }
            (None, attrs)
        };
        Ok(ObjectTypeInternal {
            ctxt: ctxt,
            handle: handle,
            schema: to_rust_str(info.schema, info.schemaLength),
            name: to_rust_str(info.name, info.nameLength),
            elem_oratype: elem_oratype,
            attrs: attrs,
        })
    }
}

impl cmp::PartialEq for ObjectTypeInternal {
    fn eq(&self, other: &Self) -> bool {
        self.handle.raw() == other.handle.raw()
    }
}

impl fmt::Display for ObjectTypeInternal {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}.{}", self.schema, self.name)
    }
}

impl fmt::Debug for ObjectTypeInternal {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.elem_oratype.is_some() {
            write!(
                f,
                "ObjectType({}.{} collection of {})",
                self.schema,
                self.name,
                self.elem_oratype.as_ref().unwrap()
            )
        } else {
            write!(f, "ObjectType({}.{}(", self.schema, self.name)?;
            let mut first = true;
            for attr in &self.attrs {
                if first {
                    first = false;
                } else {
                    write!(f, ", ")?;
                }
                write!(f, "{} {}", attr.name(), attr.oracle_type())?;
            }
            write!(f, "))")
        }
    }
}