Skip to main content

Rows

Struct Rows 

Source
pub struct Rows { /* private fields */ }
Expand description

Iterator of typed Row values produced by a SELECT query.

Today Rows is backed by an eager Vec<Vec<Value>> — the cursor abstraction in Phase 5a’s follow-up will swap this for a lazy walker that streams rows off the B-Tree without materializing them upfront. The Rows::next API is designed for that: it returns Result<Option<Row>> rather than Option<Result<Row>>, so a mid-stream I/O error surfaces cleanly.

Implementations§

Source§

impl Rows

Source

pub fn columns(&self) -> &[String]

Column names in projection order.

Source

pub fn next(&mut self) -> Result<Option<Row<'_>>>

Advances to the next row. Returns Ok(None) when the query is exhausted, Ok(Some(row)) otherwise, Err(_) on an I/O or decode failure (relevant once Phase 5a’s cursor work lands — today this is always Ok(_)).

Examples found in repository?
examples/rust/quickstart.rs (line 25)
14fn main() -> Result<()> {
15    let mut conn = Connection::open_in_memory()?;
16
17    conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);")?;
18    conn.execute("INSERT INTO users (name, age) VALUES ('alice', 30);")?;
19    conn.execute("INSERT INTO users (name, age) VALUES ('bob', 25);")?;
20    conn.execute("INSERT INTO users (name, age) VALUES ('charlie', 40);")?;
21
22    println!("All users:");
23    let stmt = conn.prepare("SELECT id, name, age FROM users;")?;
24    let mut rows = stmt.query()?;
25    while let Some(row) = rows.next()? {
26        let id: i64 = row.get_by_name("id")?;
27        let name: String = row.get_by_name("name")?;
28        // `Option<i64>` wraps NULL cleanly — `age` is declared
29        // nullable so the typed accessor surfaces None when absent.
30        let age: Option<i64> = row.get_by_name("age")?;
31        println!(
32            "  {} — {} ({})",
33            id,
34            name,
35            age.map(|a| a.to_string())
36                .unwrap_or_else(|| "NULL".to_string())
37        );
38    }
39
40    // Transactions: BEGIN + INSERT + ROLLBACK leaves the table untouched.
41    conn.execute("BEGIN;")?;
42    conn.execute("INSERT INTO users (name, age) VALUES ('will_vanish', 99);")?;
43    println!("\nMid-transaction row count: {}", count_users(&mut conn)?);
44    conn.execute("ROLLBACK;")?;
45    println!(
46        "Post-rollback row count:   {} (unchanged)",
47        count_users(&mut conn)?
48    );
49
50    Ok(())
51}
Source

pub fn collect_all(self) -> Result<Vec<OwnedRow>>

Collects every remaining row into a Vec<Row>. Convenient for small result sets; avoid on large queries — that’s what the streaming Rows::next API is for.

Examples found in repository?
examples/rust/quickstart.rs (line 55)
53fn count_users(conn: &mut Connection) -> Result<usize> {
54    let stmt = conn.prepare("SELECT id FROM users;")?;
55    let rows = stmt.query()?.collect_all()?;
56    Ok(rows.len())
57}

Trait Implementations§

Source§

impl Debug for Rows

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Rows

§

impl RefUnwindSafe for Rows

§

impl Send for Rows

§

impl Sync for Rows

§

impl Unpin for Rows

§

impl UnsafeUnpin for Rows

§

impl UnwindSafe for Rows

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.