story-kit: merge 260_refactor_upgrade_libsqlite3_sys
This commit is contained in:
30
vendor/rusqlite/examples/owning_statement.rs
vendored
Normal file
30
vendor/rusqlite/examples/owning_statement.rs
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
extern crate rusqlite;
|
||||
use rusqlite::{CachedStatement, Connection, Result, Rows};
|
||||
use self_cell::{self_cell, MutBorrow};
|
||||
|
||||
type CachedStatementRef<'a> = CachedStatement<'a>;
|
||||
|
||||
// Caveat: single statement at a time for one connection.
|
||||
// But if you need multiple statements, you can still create your own struct
|
||||
// with multiple fields (one for each statement).
|
||||
self_cell!(
|
||||
struct OwningStatement {
|
||||
owner: MutBorrow<Connection>,
|
||||
#[covariant]
|
||||
dependent: CachedStatementRef,
|
||||
}
|
||||
);
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let conn = Connection::open_in_memory()?;
|
||||
|
||||
let mut os = OwningStatement::try_new(MutBorrow::new(conn), |s| {
|
||||
s.borrow_mut().prepare_cached("SELECT 1")
|
||||
})?;
|
||||
|
||||
let mut rows = os.with_dependent_mut(|_conn, stmt| -> Result<Rows<'_>> { stmt.query([]) })?;
|
||||
while let Some(row) = rows.next()? {
|
||||
assert_eq!(Ok(1), row.get(0));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user