Alex Mikhalev
5 years ago
3 changed files with 30 additions and 14 deletions
@ -1,27 +1,39 @@ |
|||||||
use rusqlite::NO_PARAMS; |
|
||||||
use rusqlite::Connection as DbConnection; |
|
||||||
use color_eyre::eyre::Result; |
use color_eyre::eyre::Result; |
||||||
|
use rusqlite::Connection as DbConnection; |
||||||
|
use rusqlite::NO_PARAMS; |
||||||
|
|
||||||
mod section_interface; |
|
||||||
mod model; |
|
||||||
mod db; |
mod db; |
||||||
mod migrations; |
mod migrations; |
||||||
|
mod model; |
||||||
|
mod section_interface; |
||||||
|
|
||||||
|
use model::Section; |
||||||
|
|
||||||
fn setup_db() -> Result<()> { |
fn setup_db() -> Result<DbConnection> { |
||||||
// let conn = DbConnection::open_in_memory()?;
|
// let conn = DbConnection::open_in_memory()?;
|
||||||
let mut conn = DbConnection::open("test.db")?; |
let mut conn = DbConnection::open("test.db")?; |
||||||
|
|
||||||
let migs = db::create_migrations(); |
let migs = db::create_migrations(); |
||||||
migs.apply(&mut conn)?; |
migs.apply(&mut conn)?; |
||||||
|
|
||||||
Ok(()) |
Ok(conn) |
||||||
|
} |
||||||
|
|
||||||
|
fn query_sections(conn: &DbConnection) -> Result<Vec<Section>> { |
||||||
|
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::<Result<Vec<_>, _>>()?) |
||||||
} |
} |
||||||
|
|
||||||
fn main() -> Result<()> { |
fn main() -> Result<()> { |
||||||
env_logger::init(); |
env_logger::init(); |
||||||
color_eyre::install()?; |
color_eyre::install()?; |
||||||
println!("Hello, world!"); |
let conn = setup_db()?; |
||||||
setup_db().unwrap(); |
|
||||||
|
let sections = query_sections(&conn); |
||||||
|
for sec in sections { |
||||||
|
println!("section: {:?}", sec); |
||||||
|
} |
||||||
|
|
||||||
Ok(()) |
Ok(()) |
||||||
} |
} |
||||||
|
@ -1 +1,3 @@ |
|||||||
mod section; |
mod section; |
||||||
|
|
||||||
|
pub use section::Section; |
||||||
|
@ -1,21 +1,23 @@ |
|||||||
use crate::section_interface::SectionId; |
use crate::section_interface::SectionId; |
||||||
use rusqlite::Row; |
use rusqlite::{Row as SqlRow, Error as SqlError, ToSql}; |
||||||
use std::convert::TryFrom; |
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)] |
||||||
pub struct Section { |
pub struct Section { |
||||||
pub id: u32, |
pub id: u32, |
||||||
pub name: String, |
pub name: String, |
||||||
pub interface_id: SectionId, |
pub interface_id: SectionId, |
||||||
} |
} |
||||||
|
|
||||||
impl<'a> TryFrom<&Row<'a>> for Section { |
impl Section { |
||||||
type Error = rusqlite::Error; |
pub fn from_sql<'a>(row: &SqlRow<'a>) -> Result<Section, SqlError> { |
||||||
fn try_from(row: &Row<'a>) -> Result<Section, Self::Error> { |
|
||||||
Ok(Section { |
Ok(Section { |
||||||
id: row.get(0)?, |
id: row.get(0)?, |
||||||
name: row.get(1)?, |
name: row.get(1)?, |
||||||
interface_id: row.get(2)?, |
interface_id: row.get(2)?, |
||||||
}) |
}) |
||||||
} |
} |
||||||
} |
|
||||||
|
|
||||||
|
pub fn as_sql(&self) -> Vec<&dyn ToSql> { |
||||||
|
vec![&self.id, &self.name, &self.interface_id] |
||||||
|
} |
||||||
|
} |
||||||
|
Loading…
Reference in new issue