-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,93 @@ | ||
pub fn insert_tile(z: i16, x: i32, y: i32) { | ||
use diesel_async::pooled_connection::AsyncDieselConnectionManager; | ||
use diesel_async::AsyncPgConnection; | ||
use diesel_async::RunQueryDsl; | ||
use diesel::ExpressionMethods; | ||
use diesel::QueryDsl; | ||
use crate::models::TileStorage; | ||
use diesel::SelectableHelper; | ||
|
||
pub enum TileCategory { | ||
IntercityRailShapesRaw, | ||
LocalRailShapesRaw, | ||
BusShapesRaw, | ||
} | ||
|
||
fn tile_enum_to_i16(x: TileCategory) -> i16 { | ||
match x { | ||
TileCategory::IntercityRailShapesRaw => 0, | ||
TileCategory::LocalRailShapesRaw => 1, | ||
TileCategory::BusShapesRaw => 2, | ||
} | ||
} | ||
|
||
pub async fn insert_tile( | ||
conn: &mut bb8::PooledConnection<'_, AsyncDieselConnectionManager<AsyncPgConnection>>, | ||
category: TileCategory, | ||
z: i16, | ||
x: i32, | ||
y: i32, | ||
data: Vec<u8>, | ||
) -> Result<(), anyhow::Error> { | ||
let category_i16 = tile_enum_to_i16(category); | ||
|
||
let _ = diesel::insert_into(crate::schema::gtfs::tile_storage::dsl::tile_storage) | ||
.values(crate::models::TileStorage { | ||
category: category_i16, | ||
z: z, | ||
x: x, | ||
y: y, | ||
mvt_data: data, | ||
added_time: chrono::Utc::now() | ||
}) | ||
.execute(conn) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn delete_tile( | ||
conn: &mut bb8::PooledConnection<'_, AsyncDieselConnectionManager<AsyncPgConnection>>, | ||
category: TileCategory, | ||
z: i16, | ||
x: i32, | ||
y: i32, | ||
) -> Result<(), anyhow::Error> { | ||
let category_i16 = tile_enum_to_i16(category); | ||
|
||
let _ = diesel::delete( | ||
crate::schema::gtfs::tile_storage::dsl::tile_storage | ||
.filter(crate::schema::gtfs::tile_storage::dsl::category.eq(category_i16) | ||
) | ||
.filter(crate::schema::gtfs::tile_storage::dsl::z.eq(z) | ||
) | ||
.filter(crate::schema::gtfs::tile_storage::dsl::x.eq(x) | ||
) | ||
.filter(crate::schema::gtfs::tile_storage::dsl::y.eq(y) | ||
) | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn get_tile( | ||
conn: &mut bb8::PooledConnection<'_, AsyncDieselConnectionManager<AsyncPgConnection>>, | ||
category: TileCategory, | ||
z: i16, | ||
x: i32, | ||
y: i32, | ||
) -> diesel::result::QueryResult<crate::models::TileStorage> { | ||
let category_i16 = tile_enum_to_i16(category); | ||
|
||
crate::schema::gtfs::tile_storage::dsl::tile_storage | ||
.filter(crate::schema::gtfs::tile_storage::dsl::category.eq(category_i16) | ||
) | ||
.filter(crate::schema::gtfs::tile_storage::dsl::z.eq(z) | ||
) | ||
.filter(crate::schema::gtfs::tile_storage::dsl::x.eq(x) | ||
) | ||
.filter(crate::schema::gtfs::tile_storage::dsl::y.eq(y) | ||
) | ||
.select(crate::models::TileStorage::as_select()) | ||
.first::<crate::models::TileStorage>(conn) | ||
.await | ||
} |