Browse Source

Move most db stuff to database module

master
Alex Mikhalev 4 years ago
parent
commit
10f1003b56
  1. 48
      src/database/mod.rs
  2. 69
      src/main.rs

48
src/database/mod.rs

@ -3,3 +3,51 @@ mod migrations;
pub use migration::*; pub use migration::*;
pub use migrations::create_migrations; pub use migrations::create_migrations;
pub use rusqlite::Connection as DbConn;
use eyre::Result;
use crate::model::{Program, Programs, Section, Sections};
use rusqlite::NO_PARAMS;
pub fn setup_db() -> Result<DbConn> {
// let conn = DbConn::open_in_memory()?;
let mut conn = DbConn::open("test.db")?;
let migs = create_migrations();
migs.apply(&mut conn)?;
Ok(conn)
}
pub fn query_sections(conn: &DbConn) -> Result<Sections> {
let mut statement = conn.prepare_cached(
"SELECT s.id, s.name, s.interface_id \
FROM sections AS s;",
)?;
let rows = statement.query_map(NO_PARAMS, Section::from_sql)?;
let mut sections = Sections::new();
for row in rows {
let section = row?;
sections.insert(section.id, section.into());
}
Ok(sections)
}
pub fn query_programs(conn: &DbConn) -> Result<Programs> {
let mut statement = conn.prepare_cached(
"
SELECT p.id, p.name, ps.sequence, p.enabled, p.schedule
FROM programs AS p
INNER JOIN program_sequences AS ps
ON ps.program_id = p.id;",
)?;
let rows = statement.query_map(NO_PARAMS, Program::from_sql)?;
let mut programs = Programs::new();
for row in rows {
let program = row?;
programs.insert(program.id, program.into());
}
Ok(programs)
}

69
src/main.rs

@ -1,12 +1,6 @@
#![warn(clippy::all)] #![warn(clippy::all)]
#![warn(clippy::print_stdout)] #![warn(clippy::print_stdout)]
use color_eyre::eyre::Result;
use rusqlite::Connection as DbConnection;
use rusqlite::NO_PARAMS;
use tracing::info;
use tracing_subscriber::EnvFilter;
mod database; mod database;
mod model; mod model;
mod option_future; mod option_future;
@ -17,49 +11,10 @@ mod section_runner;
#[cfg(test)] #[cfg(test)]
mod trace_listeners; mod trace_listeners;
use model::{Program, Programs, Section, Sections}; use color_eyre::eyre::Result;
use std::sync::Arc; use std::sync::Arc;
use tracing::info;
fn setup_db() -> Result<DbConnection> { use tracing_subscriber::EnvFilter;
// let conn = DbConnection::open_in_memory()?;
let mut conn = DbConnection::open("test.db")?;
let migs = database::create_migrations();
migs.apply(&mut conn)?;
Ok(conn)
}
fn query_sections(conn: &DbConnection) -> Result<Sections> {
let mut statement = conn.prepare_cached(
"SELECT s.id, s.name, s.interface_id \
FROM sections AS s;",
)?;
let rows = statement.query_map(NO_PARAMS, Section::from_sql)?;
let mut sections = Sections::new();
for row in rows {
let section = row?;
sections.insert(section.id, section.into());
}
Ok(sections)
}
fn query_programs(conn: &DbConnection) -> Result<Programs> {
let mut statement = conn.prepare_cached(
"
SELECT p.id, p.name, ps.sequence, p.enabled, p.schedule
FROM programs AS p
INNER JOIN program_sequences AS ps
ON ps.program_id = p.id;",
)?;
let rows = statement.query_map(NO_PARAMS, Program::from_sql)?;
let mut programs = Programs::new();
for row in rows {
let program = row?;
programs.insert(program.id, program.into());
}
Ok(programs)
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
@ -71,9 +26,9 @@ async fn main() -> Result<()> {
.init(); .init();
color_eyre::install()?; color_eyre::install()?;
let conn = setup_db()?; let conn = database::setup_db()?;
let sections = query_sections(&conn)?; let sections = database::query_sections(&conn)?;
for sec in sections.values() { for sec in sections.values() {
info!(section = debug(&sec), "read section"); info!(section = debug(&sec), "read section");
} }
@ -83,7 +38,19 @@ async fn main() -> Result<()> {
let mut section_runner = section_runner::SectionRunner::new(section_interface); let mut section_runner = section_runner::SectionRunner::new(section_interface);
let mut program_runner = program_runner::ProgramRunner::new(section_runner.clone()); let mut program_runner = program_runner::ProgramRunner::new(section_runner.clone());
let programs = query_programs(&conn)?; let mut programs = database::query_programs(&conn)?;
// TEMP: just so the program runs right away
if let Some(prog) = programs.get_mut(&1) {
let prog_mut = Arc::make_mut(prog);
if let Some(first_time) = prog_mut.schedule.times.get_mut(0) {
*first_time = chrono::Local::now().time() + chrono::Duration::seconds(5);
}
}
for prog in programs.values() {
info!(program = debug(&prog), "read program");
}
program_runner.update_sections(sections.clone()).await?; program_runner.update_sections(sections.clone()).await?;
program_runner.update_programs(programs.clone()).await?; program_runner.update_programs(programs.clone()).await?;

Loading…
Cancel
Save