Struct oracle::sql_type::RefCursor

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

Result set output by or returned by a PL/SQL block or a stored procedure

This struct has four query methods, which are similar to Statement’s query methods excluding params arguments. The latter methods internally execute statements with specified params and then get query results. On the other hand, the former get query results executed in a PL/SQL block or a stored procedure. So there are no params arguments in this struct.

When settings about queries are set to StatementBuilder, they are also applied to ref cursors output by a PL/SQL. It is worth to call StatementBuilder::fetch_array_size(1) in order to reduce memory usage when a ref cursor contains at most one row.

§Examples

Ref cursor as an output parameter

let sql = r#"
begin
  open :cursor for select IntCol, StringCol from TestStrings order by IntCol;
end;
"#;
let mut stmt = conn.statement(sql).build()?;
stmt.execute(&[&None::<RefCursor>])?;

let mut cursor: RefCursor = stmt.bind_value(1)?;
let mut n = 1;
for row_result in cursor.query_as::<(i32, String)>()? {
    let (int_col, string_col) = row_result?;
    assert_eq!(int_col, n);
    assert_eq!(string_col, format!("String {}", n));
    n += 1;
}

Ref cursor returned by a PL/SQL block

let sql = r#"
declare
  cursor1 SYS_REFCURSOR;
begin
  open cursor1 for select IntCol, StringCol from TestStrings order by IntCol;
  dbms_sql.return_result(cursor1);
end;
"#;
let mut stmt = conn.statement(sql).build()?;
stmt.execute(&[])?;

// Get the result set.
let mut opt_cursor = stmt.implicit_result()?;
assert!(opt_cursor.is_some());
let mut cursor = opt_cursor.unwrap();
let mut n = 1;
for row_result in cursor.query_as::<(i32, String)>()? {
    let (int_col, string_col) = row_result?;
    assert_eq!(int_col, n);
    assert_eq!(string_col, format!("String {}", n));
    n += 1;
}

Implementations§

source§

impl RefCursor

source

pub fn query(&mut self) -> Result<ResultSet<'_, Row>>

Gets rows as an iterator of Rows.

§Examples
let sql = r#"
begin
  open :cursor for select IntCol, StringCol from TestStrings order by IntCol;
end;
"#;
let mut stmt = conn.statement(sql).build()?;
stmt.execute(&[&None::<RefCursor>])?;

let mut cursor: RefCursor = stmt.bind_value(1)?;
let mut n = 1;
for row_result in cursor.query()? {
    let row = row_result?;
    let int_col: i32 = row.get(0)?;
    let string_col: String = row.get(1)?;
    assert_eq!(int_col, n);
    assert_eq!(string_col, format!("String {}", n));
    n += 1;
}
source

pub fn query_as<T>(&mut self) -> Result<ResultSet<'_, T>>
where T: RowValue,

Gets rows as an itertor of the specified type.

§Examples
let sql = r#"
begin
  open :cursor for select IntCol, StringCol from TestStrings order by IntCol;
end;
"#;
let mut stmt = conn.statement(sql).build()?;
stmt.execute(&[&None::<RefCursor>])?;

let mut cursor: RefCursor = stmt.bind_value(1)?;
let mut n = 1;
for row_result in cursor.query_as::<(i32, String)>()? {
    let (int_col, string_col) = row_result?;
    assert_eq!(int_col, n);
    assert_eq!(string_col, format!("String {}", n));
    n += 1;
}
source

pub fn query_row(&mut self) -> Result<Row>

Gets one row as Row.

§Examples
let sql = r#"
begin
  open :cursor for select StringCol from TestStrings where IntCol = :IntCol;
end;
"#;
let mut stmt = conn.statement(sql).fetch_array_size(1).build()?;
stmt.execute(&[&None::<RefCursor>, &1])?;

let mut cursor: RefCursor = stmt.bind_value(1)?;
let string_col: String = cursor.query_row()?.get(0)?;
assert_eq!(string_col, "String 1");
source

pub fn query_row_as<T>(&mut self) -> Result<T>
where T: RowValue,

Gets one row as the specified type.

§Examples
let sql = r#"
begin
  open :cursor for select StringCol from TestStrings where IntCol = :IntCol;
end;
"#;
let mut stmt = conn.statement(sql).fetch_array_size(1).build()?;
stmt.execute(&[&None::<RefCursor>, &1])?;

let mut cursor: RefCursor = stmt.bind_value(1)?;
assert_eq!(cursor.query_row_as::<String>()?, "String 1");

Trait Implementations§

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, 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.