diff --git a/src/main.rs b/src/main.rs index 2439bb7..5fb38ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,39 @@ -use rusqlite::NO_PARAMS; -use rusqlite::Connection as DbConnection; use color_eyre::eyre::Result; +use rusqlite::Connection as DbConnection; +use rusqlite::NO_PARAMS; -mod section_interface; -mod model; mod db; mod migrations; +mod model; +mod section_interface; + +use model::Section; -fn setup_db() -> Result<()> { +fn setup_db() -> Result { // let conn = DbConnection::open_in_memory()?; let mut conn = DbConnection::open("test.db")?; let migs = db::create_migrations(); migs.apply(&mut conn)?; - Ok(()) + Ok(conn) +} + +fn query_sections(conn: &DbConnection) -> Result> { + let mut statement = conn.prepare_cached("SELECT id, name, interface_id FROM sections;")?; + let rows = statement.query_map(NO_PARAMS, Section::from_sql)?; + Ok(rows.collect::, _>>()?) } fn main() -> Result<()> { env_logger::init(); color_eyre::install()?; - println!("Hello, world!"); - setup_db().unwrap(); + let conn = setup_db()?; + + let sections = query_sections(&conn); + for sec in sections { + println!("section: {:?}", sec); + } Ok(()) } diff --git a/src/model/mod.rs b/src/model/mod.rs index 029ef3e..cabb658 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -1 +1,3 @@ mod section; + +pub use section::Section; diff --git a/src/model/section.rs b/src/model/section.rs index c2b71b6..cbb4141 100644 --- a/src/model/section.rs +++ b/src/model/section.rs @@ -1,21 +1,23 @@ use crate::section_interface::SectionId; -use rusqlite::Row; -use std::convert::TryFrom; +use rusqlite::{Row as SqlRow, Error as SqlError, ToSql}; +#[derive(Debug, Clone)] pub struct Section { pub id: u32, pub name: String, pub interface_id: SectionId, } -impl<'a> TryFrom<&Row<'a>> for Section { - type Error = rusqlite::Error; - fn try_from(row: &Row<'a>) -> Result { +impl Section { + pub fn from_sql<'a>(row: &SqlRow<'a>) -> Result { Ok(Section { id: row.get(0)?, name: row.get(1)?, interface_id: row.get(2)?, }) } -} + pub fn as_sql(&self) -> Vec<&dyn ToSql> { + vec![&self.id, &self.name, &self.interface_id] + } +}