1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* 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));
    }
}