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
impl RefCursor
Sourcepub fn query(&mut self) -> Result<ResultSet<'_, Row>>
pub fn query(&mut self) -> Result<ResultSet<'_, Row>>
Gets rows as an iterator of Row
s.
§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;
}
Sourcepub fn query_as<T>(&mut self) -> Result<ResultSet<'_, T>>where
T: RowValue,
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;
}
Sourcepub fn query_row(&mut self) -> Result<Row>
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");
Sourcepub fn query_row_as<T>(&mut self) -> Result<T>where
T: RowValue,
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");