Skip to main content

geonames/
sqlitewriter.rs

1use crate::parser::Parser;
2
3use rusqlite::{Result, NO_PARAMS};
4
5macro_rules! getopts {
6    ($input:expr) => {
7        $input.unwrap_or("-".to_string())
8    };
9}
10
11macro_rules! getoptf {
12    ($input:expr) => {
13        $input.unwrap_or(0.0).to_string()
14    };
15}
16
17macro_rules! getopti {
18    ($input:expr) => {
19        $input.unwrap_or(0).to_string()
20    };
21}
22
23pub fn write_sqlite(input: Parser, output: std::path::PathBuf) -> Result<()> {
24    let connection = rusqlite::Connection::open(output)?;
25    connection.execute(
26        "CREATE TABLE IF NOT EXISTS cities (
27            geonameid INT, 
28            name TEXT, 
29            asciiname TEXT, 
30            alternatenames TEXT,
31            latitude REAL,
32            longitude REAL,
33            feature_class TEXT,
34            feature_code TEXT,
35            country_code TEXT,
36            cc2 TEXT,
37            admin1_code TEXT,
38            admin2_code TEXT,
39            admin3_code TEXT,
40            admin4_code TEXT,
41            population NUMERIC,
42            elevation INT,
43            dem REAL,
44            timezone TEXT,
45            modification_date TEXT
46        )",
47        NO_PARAMS,
48    )?;
49    for result in input.into_iter() {
50        //let record: Geoname = result;
51        connection.execute(
52            "INSERT INTO cities (
53                geonameid, 
54                name, 
55                asciiname, 
56                alternatenames,
57                latitude,
58                longitude,
59                feature_class,
60                feature_code,
61                country_code,
62                cc2,
63                admin1_code,
64                admin2_code,
65                admin3_code,
66                admin4_code,
67                population,
68                elevation,
69                dem,
70                timezone,
71                modification_date
72            ) values (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19)",
73            &[
74                &result.geonameid.to_string(),
75                &result.name,
76                &getopts!(result.asciiname),
77                &getopts!(result.alternatenames),
78                &result.latitude.to_string(),
79                &result.longitude.to_string(),
80                &result.feature_class.to_string(),
81                &result.feature_code,
82                &getopts!(result.country_code),
83                &getopts!(result.cc2),
84                &getopts!(result.admin1_code),
85                &getopts!(result.admin2_code),
86                &getopts!(result.admin3_code),
87                &getopts!(result.admin4_code),
88                &getoptf!(result.population),
89                &getopti!(result.elevation),
90                &getoptf!(result.dem),
91                &getopts!(result.timezone),
92                &result.modification_date.to_string(),
93            ],
94        )?;
95    }
96    Ok(())
97}