artifactize-core 0.27.0

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
Documentation
use super::Reader;
use rusqlite::Connection;
use std::cell::RefCell;

thread_local! { static STATEMENTS: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) }; }

pub(super) fn capture(connection: &mut Connection) {
    connection.trace_v2(
        rusqlite::trace::TraceEventCodes::SQLITE_TRACE_STMT,
        Some(trace),
    );
}

fn trace(event: rusqlite::trace::TraceEvent<'_>) {
    let rusqlite::trace::TraceEvent::Stmt(statement, _) = event else {
        return;
    };
    let sql = statement.expanded_sql().unwrap();
    if sql.starts_with("SELECT") || sql.starts_with("WITH") {
        STATEMENTS.with(|statements| statements.borrow_mut().push(sql.to_owned()));
    }
}

fn take() -> Vec<String> {
    STATEMENTS.with(|statements| std::mem::take(&mut *statements.borrow_mut()))
}

impl Reader {
    pub fn traced_statements(&self) -> Vec<String> {
        take()
    }
}