pub trait RowValue: Sized {
// Required method
fn get(row: &Row) -> Result<Self>;
}
Expand description
A trait to get a row as specified type
This is the return type of Connection::query_row_as
,
Connection::query_row_as_named
and Row::get_as
.
The trait was added to fetch column values as a tuple.
The oracle crate provides implementations for a type
implementing FromSql
and tuples of types implementing
FromSql
. The number of elements in a tuple should
be 1 through 50.
let conn = Connection::connect("scott", "tiger", "")?;
let sql = "select * from emp where empno = :1";
// Gets the first column value in a row.
// Values after the second column are ignored.
let empno = conn.query_row_as::<u32>(sql, &[&7369])?;
// Gets the first two column values in a row.
// Values after the third column are ignored.
let tuple_of_empno_and_ename = conn.query_row_as::<(i32, String)>(sql, &[&7499])?;
You can implement the trait for your own types. For example
when you have a struct whose members are empno
and ename
,
you can make the struct from empno
and ename
column values
as follows:
struct Emp {
empno: i32,
ename: String,
}
impl RowValue for Emp {
fn get(row: &Row) -> std::result::Result<Emp, Error> {
Ok(Emp {
empno: row.get("empno")?,
ename: row.get("ename")?,
})
}
}
let conn = Connection::connect("scott", "tiger", "")?;
let mut stmt = conn.statement("select * from emp").build()?;
// Gets rows as Emp
for result in stmt.query_as::<Emp>(&[])? {
let emp = result?;
println!("{},{}", emp.empno, emp.ename);
}
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.