How to store SQLite prepared statements for later?

You are right, indeed, that sibling references are awkward in Rust. There is a good reason though, they are not easily modeled by the ownership system.

In this particular case, I would advise you to split the structure: you can keep the prepared statements in a dedicated cache also parametrized on the lifetime of the db for example; the db instead should be instantiated at the top of your program and passed down (think dependency injection) so that the cache that depends on it can outlive the program main function.

This does mean that the db will remain borrowed, obviously.


The Statement struct has a lifetime parameter, Statement<'conn>. When you prepare the statement, you must have a reference to the Connection that outlives the statement.

extern crate rusqlite;

use rusqlite::{Connection, Statement};

struct MyAppState {
    db: Connection,
}

impl MyAppState {
    fn new() -> MyAppState {
        let db = Connection::open(":memory:").unwrap();
        MyAppState { db: db }
    }
}

struct PreparedStatement<'conn> {
    statement: Statement<'conn>,
}

impl<'conn> PreparedStatement<'conn> {
    pub fn new<'a>(conn: &'a Connection, sql: &str) -> PreparedStatement<'a> {
        PreparedStatement {
            statement: conn.prepare(sql).unwrap(),
        }
    }

    fn query_some_info(&mut self, arg: i64) -> i64 {
        let mut result_iter = self.statement.query(&[&arg]).unwrap();
        let result = result_iter.next().unwrap().unwrap().get(0);

        result
    }
}

fn main() {
    let app = MyAppState::new();
    let mut prepared_stmt = PreparedStatement::new(&app.db, "SELECT ? + 1");
    for i in 0..100 {
        let result = prepared_stmt.query_some_info(i);
        println!("{}", result);
    }
}

In Rust, unlike some other languages, I have found that factoring something out into a function changes its meaning. It introduces new lifetimes, which usually works against you. But in this case, that's exactly what was needed.

Tags:

Sqlite

Rust