pub struct Statement<'c> { /* private fields */ }Expand description
A prepared statement bound to a specific connection lifetime. Today this is a thin wrapper around the raw SQL; Phase 5’s cursor work will grow it into a real prepared-plan cache.
Implementations§
Source§impl<'c> Statement<'c>
impl<'c> Statement<'c>
Sourcepub fn run(&mut self) -> Result<String>
pub fn run(&mut self) -> Result<String>
Executes a prepared non-query statement. Equivalent to
Connection::execute — included for parity with the
typed-row query() so callers who want Statement::run / Statement::query
symmetry get it.
Sourcepub fn query(&self) -> Result<Rows>
pub fn query(&self) -> Result<Rows>
Runs a SELECT and returns a Rows iterator over typed rows.
Errors if the prepared statement isn’t a SELECT.
Examples found in repository?
examples/rust/quickstart.rs (line 24)
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}
52
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}Sourcepub fn column_names(&self) -> Option<Vec<String>>
pub fn column_names(&self) -> Option<Vec<String>>
Column names this statement will produce, in projection order.
None for non-SELECT statements.
Trait Implementations§
Auto Trait Implementations§
impl<'c> Freeze for Statement<'c>
impl<'c> RefUnwindSafe for Statement<'c>
impl<'c> Send for Statement<'c>
impl<'c> Sync for Statement<'c>
impl<'c> Unpin for Statement<'c>
impl<'c> UnsafeUnpin for Statement<'c>
impl<'c> !UnwindSafe for Statement<'c>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more