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/>.
*/
#![allow(non_snake_case)]

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

/** The `CovidTracking` struct represents a row in the `covid19tracking` 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 CovidTracking {
    pub date_julian: i32,
    pub state: String,
    pub positive: Option<i64>,
    pub negative: Option<i64>,
    pub pending: Option<i64>,
    pub hospitalizedCurrently: Option<i64>,
    pub hospitalizedCumulative: Option<i64>,
    pub incluCurrently: Option<i64>,
    pub incluCumulative: Option<i64>,
    pub onVentilatorCurrently: Option<i64>,
    pub onVentilatorCumulative: Option<i64>,
    pub recovered: Option<i64>,
    pub dataQualityGrade: Option<String>,
    pub lastUpdateEt: Option<String>,
    pub dateModified: Option<String>,
    pub checkTimeEt: Option<String>,
    pub death: Option<i64>,
    pub hospitalized: Option<i64>,
    pub dateChecked: Option<String>,
    pub totalTestsViral: Option<i64>,
    pub positiveTestsViral: Option<i64>,
    pub negativeTestsViral: Option<i64>,
    pub positiveCasesViral: Option<i64>,
    pub deathConfirmed: Option<i64>,
    pub deathProbable: Option<i64>,
    pub totalTestEncountersViral: Option<i64>,
    pub totalTestsPeopleViral: Option<i64>,
    pub totalTestsAntibody: Option<i64>,
    pub positiveTestsAntibody: Option<i64>,
    pub negativeTestsAntibody: Option<i64>,
    pub totalTestsPeopleAntibody: Option<i64>,
    pub positiveTestsPeopleAntibody: Option<i64>,
    pub negativeTestsPeopleAntibody: Option<i64>,
    pub totalTestsPeopleAntigen: Option<i64>,
    pub positiveTestsPeopleAntigen: Option<i64>,
    pub totalTestsAntigen: Option<i64>,
    pub positiveTestsAntigen: Option<i64>,
    pub fips: i64,
    pub positiveIncrease: Option<i64>,
    pub negativeIncrease: Option<i64>,
    pub total: Option<i64>,
    pub totalTestResultsSource: String,
    pub totalTestResults: Option<i64>,
    pub totalTestResultsIncrease: Option<i64>,
    pub posNeg: Option<i64>,
    pub deathIncrease: Option<i64>,
    pub hospitalizedIncrease: Option<i64>,
    pub commercialScore: Option<i64>,
    pub negativeRegularScore: Option<i64>,
    pub negativeScore: Option<i64>,
    pub positiveScore: Option<i64>,
    pub score: Option<i64>,
    pub grade: Option<String>,
}

impl CovidTracking {
    /// 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.date_julian)
            .bind(self.state)
            .bind(self.positive)
            .bind(self.negative)
            .bind(self.pending)
            .bind(self.hospitalizedCurrently)
            .bind(self.hospitalizedCumulative)
            .bind(self.incluCurrently)
            .bind(self.incluCumulative)
            .bind(self.onVentilatorCurrently)
            .bind(self.onVentilatorCumulative)
            .bind(self.recovered)
            .bind(self.dataQualityGrade)
            .bind(self.lastUpdateEt)
            .bind(self.dateModified)
            .bind(self.checkTimeEt)
            .bind(self.death)
            .bind(self.hospitalized)
            .bind(self.dateChecked)
            .bind(self.totalTestsViral)
            .bind(self.positiveTestsViral)
            .bind(self.negativeTestsViral)
            .bind(self.positiveCasesViral)
            .bind(self.deathConfirmed)
            .bind(self.deathProbable)
            .bind(self.totalTestEncountersViral)
            .bind(self.totalTestsPeopleViral)
            .bind(self.totalTestsAntibody)
            .bind(self.positiveTestsAntibody)
            .bind(self.negativeTestsAntibody)
            .bind(self.totalTestsPeopleAntibody)
            .bind(self.positiveTestsPeopleAntibody)
            .bind(self.negativeTestsPeopleAntibody)
            .bind(self.totalTestsPeopleAntigen)
            .bind(self.positiveTestsPeopleAntigen)
            .bind(self.totalTestsAntigen)
            .bind(self.positiveTestsAntigen)
            .bind(self.fips)
            .bind(self.positiveIncrease)
            .bind(self.negativeIncrease)
            .bind(self.total)
            .bind(self.totalTestResultsSource)
            .bind(self.totalTestResults)
            .bind(self.totalTestResultsIncrease)
            .bind(self.posNeg)
            .bind(self.deathIncrease)
            .bind(self.hospitalizedIncrease)
            .bind(self.commercialScore)
            .bind(self.negativeRegularScore)
            .bind(self.negativeScore)
            .bind(self.positiveScore)
            .bind(self.score)
            .bind(self.grade)
    }

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

    /// 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));
    }
}