|
1 | 1 | use super::schema::{self, Columns, Element}; |
| 2 | +use crate::db::main::area_element::schema::{self as area_element_schema}; |
2 | 3 | use crate::{service::overpass::OverpassElement, Result}; |
3 | 4 | use rusqlite::{named_params, params, Connection}; |
4 | 5 | use serde_json::{Map, Value}; |
@@ -177,6 +178,40 @@ pub fn select_with_opening_hours_without_humanization( |
177 | 178 | .map_err(Into::into) |
178 | 179 | } |
179 | 180 |
|
| 181 | +pub fn select_with_opening_hours_without_humanization_by_area( |
| 182 | + area_id: i64, |
| 183 | + limit: i64, |
| 184 | + conn: &Connection, |
| 185 | +) -> Result<Vec<Element>> { |
| 186 | + let table = schema::TABLE_NAME; |
| 187 | + let sql = format!( |
| 188 | + r#" |
| 189 | + SELECT {table}.{id}, {table}.{overpass_data}, {table}.{tags}, {table}.{lat}, {table}.{lon}, {table}.{created_at}, {table}.{updated_at}, {table}.{deleted_at} |
| 190 | + FROM {table} |
| 191 | + INNER JOIN {area_element_table} ae ON ae.element_id = {table}.id AND ae.area_id = ?1 AND ae.deleted_at IS NULL |
| 192 | + WHERE json_extract({table}.{overpass_data}, '$.tags.opening_hours') IS NOT NULL |
| 193 | + AND json_extract({table}.{tags}, '$.opening_hours:en:human_readable') IS NULL |
| 194 | + AND {table}.{deleted_at} IS NULL |
| 195 | + ORDER BY RANDOM() |
| 196 | + LIMIT ?2 |
| 197 | + "#, |
| 198 | + table = table, |
| 199 | + id = Columns::Id.as_str(), |
| 200 | + overpass_data = Columns::OverpassData.as_str(), |
| 201 | + tags = Columns::Tags.as_str(), |
| 202 | + lat = Columns::Lat.as_str(), |
| 203 | + lon = Columns::Lon.as_str(), |
| 204 | + created_at = Columns::CreatedAt.as_str(), |
| 205 | + updated_at = Columns::UpdatedAt.as_str(), |
| 206 | + deleted_at = Columns::DeletedAt.as_str(), |
| 207 | + area_element_table = area_element_schema::TABLE_NAME, |
| 208 | + ); |
| 209 | + conn.prepare(&sql)? |
| 210 | + .query_map(params![area_id, limit], Element::mapper())? |
| 211 | + .collect::<Result<Vec<_>, _>>() |
| 212 | + .map_err(Into::into) |
| 213 | +} |
| 214 | + |
180 | 215 | pub fn select_by_id_or_osm_id(id: impl Into<String>, conn: &Connection) -> Result<Element> { |
181 | 216 | let id: String = id.into(); |
182 | 217 | let id = id.as_str(); |
|
0 commit comments