#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc = include_str!(concat!(env!("OUT_DIR"), "/readme_for_rustdoc.md"))]
use rusqlite::{params, Connection};
pub fn read_all_rows(conn: &Connection, table: &str) -> tabled::Table {
use rusqlite::types::Value;
use tabled::builder::Builder;
use tabled::settings::{Panel, Style};
let query = format!("SELECT * FROM {table}");
let mut stmt = conn.prepare(&query).unwrap();
let count = stmt.column_count();
let mut table = Builder::default();
table.push_record(
stmt.column_names()
.into_iter()
.map(|s| s.to_string())
.collect::<Vec<_>>(),
);
stmt.query(params![])
.unwrap()
.mapped(|row| {
Ok((0..count)
.into_iter()
.map(|i| format!("{:?}", row.get_unwrap::<_, Value>(i)))
.collect::<Vec<_>>())
})
.for_each(|s| table.push_record(s.unwrap()));
let mut table = table.build();
table.with(Style::psql()).with(Panel::header(query));
table
}
#[cfg(test)]
mod tests {
use super::*;
use rusqlite::Connection;
#[test]
fn basic_feature() {
let conn = Connection::open_in_memory().unwrap();
conn.execute("CREATE TABLE friend (name, year_of_birth INTEGER);", ())
.unwrap();
insta::assert_snapshot!("empty_table", read_all_rows(&conn, "friend"));
let mut stmt = conn
.prepare("INSERT INTO friend (name, year_of_birth) VALUES (?1, ?2)")
.unwrap();
for (year, name) in &[
("alice", 1977),
("bob", 1987),
("charlie", 2000),
("daphne", 1950),
("eve", 1984),
] {
stmt.execute((name, year)).unwrap();
}
insta::assert_snapshot!("with_data", read_all_rows(&conn, "friend"));
stmt.execute((1337, 2013)).unwrap();
insta::assert_snapshot!(
"with_data_of_inconsistent_types",
read_all_rows(&conn, "friend")
);
}
}