Struct oracle::sql_type::Collection

source ·
pub struct Collection { /* private fields */ }
Expand description

Oracle-specific collection data type

This type corresponds to varray and nested table data types. See Oracle manual.

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)");

Note: Methods in the type may be changed in future.

Implementations§

source§

impl Collection

source

pub fn object_type(&self) -> &ObjectType

Returns type information.

source

pub fn size(&self) -> Result<i32>

Returns the number of elements.

This counts also deleted elements. See “Comments” about OCICollSize().

source

pub fn iter<T>(&self) -> Iter<'_, T>
where T: FromSql,

Returns an iterator visiting all values with indices in the collection.

// Creates VARRAY type and gets the type information
conn.execute("create or replace type string_varray is varray(20) of varchar2(60)", &[])?;
let objtype = conn.object_type("STRING_VARRAY")?;

// Creates a VARRAY instance and appends three elements.
let mut coll = objtype.new_collection()?;
coll.push(&"First Element")?;
coll.push(&"Second Element")?;
coll.push(&"Third Element")?;

let vec = coll
    .iter::<String>() // iterator returning Result<(i32, String)>
    .collect::<Result<Vec<_>>>()?;
assert_eq!(vec[0], (0, "First Element".to_string()));
assert_eq!(vec[1], (1, "Second Element".to_string()));
assert_eq!(vec[2], (2, "Third Element".to_string()));

// Creates Table type and gets the type information
conn.execute("create or replace type string_table is table of varchar2(60)", &[])?;
let objtype = conn.object_type("STRING_TABLE")?;

// Creates a TABLE instance, appends four elements and makes a hole.
let mut coll = objtype.new_collection()?;
coll.push(&"First Element")?;
coll.push(&"Second Element")?;
coll.push(&"Third Element")?;
coll.push(&"Fourth Element")?;
coll.remove(2)?; // Remove "Third Element"

// iterator returning Result<(i32, String)>
let mut iter = coll.iter::<String>();
assert_eq!(iter.next().unwrap()?, (0, "First Element".to_string()));
assert_eq!(iter.next().unwrap()?, (1, "Second Element".to_string()));
assert_eq!(iter.next().unwrap()?, (3, "Fourth Element".to_string()));
assert!(iter.next().is_none());
source

pub fn indices(&self) -> Indices<'_>

Returns an iterator visiting all indices in the collection.

// Creates Table type and gets the type information
conn.execute("create or replace type string_table is table of varchar2(60)", &[])?;
let objtype = conn.object_type("STRING_TABLE")?;

// Creates a TABLE instance, appends four elements and makes a hole.
let mut coll = objtype.new_collection()?;
coll.push(&"First Element")?; // index 0
coll.push(&"Second Element")?; // index 1
coll.push(&"Third Element")?; // index 2
coll.push(&"Fourth Element")?; // index 3
coll.remove(2)?; // remote index 2
// coll's indices are 0, 1 and 3.

let mut indices = coll.indices();
assert_eq!(indices.next().unwrap().unwrap(), 0);
assert_eq!(indices.next().unwrap().unwrap(), 1);
assert_eq!(indices.next().unwrap().unwrap(), 3);
assert!(indices.next().is_none());
source

pub fn values<T>(&self) -> Values<'_, T>
where T: FromSql,

Returns an iterator visiting all values in the collection.

// Creates VARRAY type and gets the type information
conn.execute("create or replace type string_varray3 is varray(20) of varchar2(60)", &[])?;
let objtype = conn.object_type("STRING_VARRAY3")?;

// Creates a VARRAY instance and appends three elements.
let mut coll = objtype.new_collection()?;
coll.push(&"First Element");
coll.push(&"Second Element");
coll.push(&"Third Element");

let mut iter = coll.values::<String>();
assert_eq!(iter.next().unwrap()?, "First Element".to_string());
assert_eq!(iter.next().unwrap()?, "Second Element".to_string());
assert_eq!(iter.next().unwrap()?, "Third Element".to_string());
assert!(iter.next().is_none());
source

pub fn first_index(&self) -> Result<i32>

Returns the first index.

Use this method if indexes of the collection isn’t continuous.

source

pub fn last_index(&self) -> Result<i32>

Returns the last index.

Use this method if indexes of the collection isn’t continuous.

source

pub fn next_index(&self, index: i32) -> Result<i32>

Returns the next index following the specified index.

Use this method if indexes of the collection isn’t continuous.

source

pub fn prev_index(&self, index: i32) -> Result<i32>

Returns the previous index following the specified index.

Use this method if indexes of the collection isn’t continuous.

source

pub fn exist(&self, index: i32) -> Result<bool>

Returns whether an element exists at the specified index.

source

pub fn get<T>(&self, index: i32) -> Result<T>
where T: FromSql,

Returns the value of the element at the specified index.

source

pub fn set(&mut self, index: i32, value: &dyn ToSql) -> Result<()>

Sets the value to the element at the specified index.

source

pub fn push(&mut self, value: &dyn ToSql) -> Result<()>

Appends an element to the end of the collection.

source

pub fn remove(&mut self, index: i32) -> Result<()>

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.

source

pub fn trim(&mut self, len: usize) -> Result<()>

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.

Trait Implementations§

source§

impl Clone for Collection

source§

fn clone(&self) -> Collection

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Collection

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Collection

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl FromSql for Collection

source§

impl ToSql for Collection

source§

fn oratype(&self, _conn: &Connection) -> Result<OracleType>

source§

fn to_sql(&self, val: &mut SqlValue<'_>) -> Result<()>

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> RowValue for T
where T: FromSql,

source§

fn get(row: &Row) -> Result<T, Error>

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.