covid19db 2.0.6

Utility for building and accessing COVID-19 datasets
Documentation
/* Database schema

Copyright (c) 2020 John Goerzen

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

use crate::dateutil::*;
use chrono::NaiveDate;
use julianday::JulianDay;
use sqlx::Query;

/** The `CDataSet` struct represents a row in the `cdataset` table.  It is an instance
of `sqlx::FromRow` for the benefit of users of `sqlx::query_as`. */
#[derive(PartialEq, Clone, Debug, sqlx::FromRow)]
pub struct CDataSet {
    // from the schema
    // sed -e 's/ *\([^ ]*\)/pub \1:/' -e 's/integer not null/i64/' -e "s/text not null/String/" -e "s/text,/Option<String>,/" -e 's/real,/Option<f64>,/' -e 's/integer,/Option<i64>,/'
    //
    pub dataset: String,
    pub locid: i64,
    pub location_lat: Option<f64>,
    pub location_long: Option<f64>,
    pub date_julian: i32,
    pub day_index_0: i32,
    pub day_index_1: i32,
    pub day_index_10: Option<i32>,
    pub day_index_100: Option<i32>,
    pub day_index_1k: Option<i32>,
    pub day_index_10k: Option<i32>,
    pub day_index_peak: Option<i32>,
    pub day_index_peak_confirmed: Option<i32>,
    pub day_index_peak_deaths: Option<i32>,
    pub absolute_confirmed: i64,
    pub absolute_deaths: i64,
    pub absolute_recovered: i64,
    pub absolute_infected: i64,
    pub absolute_pop100k_confirmed: Option<f64>,
    pub absolute_pop100k_deaths: Option<f64>,
    pub absolute_pop100k_recovered: Option<f64>,
    pub absolute_pop100k_infected: Option<f64>,
    pub relative_deaths: Option<f64>,
    pub relative_recovered: Option<f64>,
    pub relative_infected: Option<f64>,
    pub delta_confirmed: i64,
    pub delta_deaths: i64,
    pub delta_recovered: i64,
    pub delta_infected: i64,
    pub delta_pct_confirmed: Option<f64>,
    pub delta_pct_deaths: Option<f64>,
    pub delta_pct_recovered: Option<f64>,
    pub delta_pct_infected: Option<f64>,
    pub delta_pop100k_confirmed: Option<f64>,
    pub delta_pop100k_deaths: Option<f64>,
    pub delta_pop100k_recovered: Option<f64>,
    pub delta_pop100k_infected: Option<f64>,
    pub peak_pct_confirmed: Option<f64>,
    pub peak_pct_deaths: Option<f64>,
    pub peak_pct_recovered: Option<f64>,
    pub peak_pct_infected: Option<f64>,
    pub factbook_area: Option<f64>,
    pub factbook_population: Option<i64>,
    pub factbook_death_rate: Option<f64>,
    pub factbook_median_age: Option<f64>,
}

impl CDataSet {
    /// Bind all the parameters to a query, perhaps as generated by [`insert_str`].
    pub fn bind_query<'q>(self, query: Query<'q, sqlx::Sqlite>) -> Query<'q, sqlx::Sqlite> {
        // from schema
        // sed -e 's/ *\([^ ]*\).*/.bind(self.\1)/'
        query
            .bind(self.dataset)
            .bind(self.locid)
            .bind(self.location_lat)
            .bind(self.location_long)
            .bind(self.date_julian)
            .bind(self.day_index_0)
            .bind(self.day_index_1)
            .bind(self.day_index_10)
            .bind(self.day_index_100)
            .bind(self.day_index_1k)
            .bind(self.day_index_10k)
            .bind(self.day_index_peak)
            .bind(self.day_index_peak_confirmed)
            .bind(self.day_index_peak_deaths)
            .bind(self.absolute_confirmed)
            .bind(self.absolute_deaths)
            .bind(self.absolute_recovered)
            .bind(self.absolute_infected)
            .bind(self.absolute_pop100k_confirmed)
            .bind(self.absolute_pop100k_deaths)
            .bind(self.absolute_pop100k_recovered)
            .bind(self.absolute_pop100k_infected)
            .bind(self.relative_deaths)
            .bind(self.relative_recovered)
            .bind(self.relative_infected)
            .bind(self.delta_confirmed)
            .bind(self.delta_deaths)
            .bind(self.delta_recovered)
            .bind(self.delta_infected)
            .bind(self.delta_pct_confirmed)
            .bind(self.delta_pct_deaths)
            .bind(self.delta_pct_recovered)
            .bind(self.delta_pct_infected)
            .bind(self.delta_pop100k_confirmed)
            .bind(self.delta_pop100k_deaths)
            .bind(self.delta_pop100k_recovered)
            .bind(self.delta_pop100k_infected)
            .bind(self.peak_pct_confirmed)
            .bind(self.peak_pct_deaths)
            .bind(self.peak_pct_recovered)
            .bind(self.peak_pct_infected)
            .bind(self.factbook_area)
            .bind(self.factbook_population)
            .bind(self.factbook_death_rate)
            .bind(self.factbook_median_age)
    }

    /// Gets an INSERT INTO string representing all the values in the table.
    pub fn insert_str() -> &'static str {
        "INSERT INTO cdataset_raw VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
    }

    /// Zeroes out the delta parameters so that this can reflect a duplicate day
    pub fn dup_day(self) -> Self {
        CDataSet {
            delta_confirmed: 0,
            delta_deaths: 0,
            delta_recovered: 0,
            delta_infected: 0,
            delta_pct_confirmed: None,
            delta_pct_deaths: None,
            delta_pct_recovered: None,
            delta_pct_infected: None,
            delta_pop100k_confirmed: None,
            delta_pop100k_deaths: None,
            delta_pop100k_recovered: None,
            delta_pop100k_infected: None,
            ..self
        }
    }

    /// Sets all date fields in the struct to appropriate representations of the
    /// given Julian date.
    pub fn set_date(&mut self, julian: i32) {
        self.date_julian = julian;
    }

    #[allow(dead_code)]
    /// Sets all date fields in the struct to the appropriate representation of
    /// the given `JulianDay`.
    pub fn set_date_julianday(&mut self, jd: &JulianDay) {
        self.set_date(jd_to_day(jd));
    }

    #[allow(dead_code)]
    /// Sets all date fields in the struct to the appropriate representation of
    /// the given `NaiveDate` from the `chrono` package.
    pub fn set_date_naivedate(&mut self, nd: &NaiveDate) {
        self.set_date(nd_to_day(nd));
    }
}