osm_io/osm/apidb_dump/
sql.rs

1use std::ops::{Shl, Shr};
2
3use anyhow::anyhow;
4use chrono::{DateTime, NaiveDateTime, ParseError, SecondsFormat};
5
6pub(crate) fn parse_sql_time(s: &str) -> Result<NaiveDateTime, ParseError> {
7    NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S%.f")
8}
9
10pub(crate) fn parse_sql_bool(s: &str) -> Result<bool, anyhow::Error> {
11    match s {
12        "t" => { Ok(true) }
13        "f" => { Ok(false) }
14        _ => {
15            Err(anyhow!("Wrong boolean literal: {}", s))
16        }
17    }
18}
19
20pub(crate) fn to_sql_bool(v: bool) -> char {
21    match v {
22        true => { 't' }
23        false => { 'f' }
24    }
25}
26
27pub(crate) fn parse_sql_null_string(s: &str) -> Option<String> {
28    match s {
29        "\\N" => {
30            None
31        }
32        _ => {
33            Some(s.to_string())
34        }
35    }
36}
37
38pub(crate) fn to_sql_time_millis(t: i64) -> String {
39    let datetime = DateTime::from_timestamp_millis(t).unwrap();
40    let sql_time: String = datetime.to_rfc3339_opts(SecondsFormat::Secs, true);
41    sql_time.replace('T', " ").replace('Z', "")
42}
43
44pub(crate) fn to_sql_time_micros(t: i64) -> String {
45    let datetime = DateTime::from_timestamp_micros(t).unwrap();
46    let sql_time: String = datetime.to_rfc3339_opts(SecondsFormat::Micros, true);
47    sql_time.replace('T', " ").replace('Z', "")
48}
49
50pub(crate) fn calculate_tile(lat: f64, lon: f64) -> u64 {
51    let x = ((lon + 180.0) * 65535.0 / 360.0).round() as u64;
52    let y = ((lat + 90.0) * 65535.0 / 180.0).round() as u64;
53
54    let mut tile = 0_u64;
55
56    for i in (0..16).rev() {
57        tile = tile.shl(1) | (x.shr(i) & 1_u64);
58        tile = tile.shl(1) | (y.shr(i) & 1_u64);
59    }
60    tile
61}