Debian Codegraph @

third_party/rust/rusqlite/examples/owning_statement.rs

raw
1 extern crate rusqlite;
2 use ouroboros::self_referencing;
3 use rusqlite::{CachedStatement, Connection, Result, Rows};
4
5 /// Caveat: single statement at a time for one connection.
6 /// But if you need multiple statements, you can still create your own struct
7 /// with multiple fields (one for each statement).
8 #[self_referencing]
9 struct OwningStatement {
10 conn: Connection,
11 #[borrows(conn)]
12 #[covariant]
13 stmt: CachedStatement<'this>,
14 }
15
16 fn main() -> Result<()> {
17 let conn = Connection::open_in_memory()?;
18
19 let mut os = OwningStatementTryBuilder {
20 conn,
21 stmt_builder: |c| c.prepare_cached("SELECT 1"),
22 }
23 .try_build()?;
24
25 let mut rows = os.with_stmt_mut(|stmt| -> Result<Rows<'_>> { stmt.query([]) })?;
26 while let Some(row) = rows.next()? {
27 assert_eq!(Ok(1), row.get(0));
28 }
29 Ok(())
30 }