Using files instead of strings for queries.
This commit is contained in:
parent
1821d5e482
commit
4fde4bea30
3
sql/maps/get_all_maps.sql
Normal file
3
sql/maps/get_all_maps.sql
Normal file
|
@ -0,0 +1,3 @@
|
|||
SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles
|
||||
FROM record_maps
|
||||
JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map
|
4
sql/maps/get_map_by_name.sql
Normal file
4
sql/maps/get_map_by_name.sql
Normal file
|
@ -0,0 +1,4 @@
|
|||
SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles
|
||||
FROM record_maps
|
||||
JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map
|
||||
WHERE record_maps.map = $1
|
4
sql/maps/get_maps_by_category.sql
Normal file
4
sql/maps/get_maps_by_category.sql
Normal file
|
@ -0,0 +1,4 @@
|
|||
SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles
|
||||
FROM record_maps
|
||||
JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map
|
||||
WHERE category = $1
|
4
sql/maps/get_maps_by_mapper.sql
Normal file
4
sql/maps/get_maps_by_mapper.sql
Normal file
|
@ -0,0 +1,4 @@
|
|||
SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles
|
||||
FROM record_maps
|
||||
JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map
|
||||
WHERE $1 = ANY(mappers)
|
18
sql/races/get_races_by_map.sql
Normal file
18
sql/races/get_races_by_map.sql
Normal file
|
@ -0,0 +1,18 @@
|
|||
SELECT
|
||||
name,
|
||||
map,
|
||||
time,
|
||||
timestamp,
|
||||
server,
|
||||
ARRAY[cp1, cp2, cp3, cp4, cp5,
|
||||
cp6, cp7, cp8, cp9, cp10,
|
||||
cp11, cp12, cp13, cp14, cp15,
|
||||
cp16, cp17, cp18, cp19, cp20,
|
||||
cp21, cp22, cp23, cp24, cp25
|
||||
] AS checkpoints,
|
||||
gameid,
|
||||
ddnet7
|
||||
FROM record_race WHERE map = $1
|
||||
ORDER BY name, time
|
||||
OFFSET (($2 - 1) * $3)
|
||||
FETCH NEXT $3 ROWS ONLY
|
18
sql/races/get_races_by_player.sql
Normal file
18
sql/races/get_races_by_player.sql
Normal file
|
@ -0,0 +1,18 @@
|
|||
SELECT
|
||||
name,
|
||||
map,
|
||||
time,
|
||||
timestamp,
|
||||
server,
|
||||
ARRAY[cp1, cp2, cp3, cp4, cp5,
|
||||
cp6, cp7, cp8, cp9, cp10,
|
||||
cp11, cp12, cp13, cp14, cp15,
|
||||
cp16, cp17, cp18, cp19, cp20,
|
||||
cp21, cp22, cp23, cp24, cp25
|
||||
] AS checkpoints,
|
||||
gameid,
|
||||
ddnet7
|
||||
FROM record_race WHERE name = $1
|
||||
ORDER BY map, time
|
||||
OFFSET (($2 - 1) * $3)
|
||||
FETCH NEXT $3 ROWS ONLY
|
5
sql/teamraces/get_teamraces_by_map.sql
Normal file
5
sql/teamraces/get_teamraces_by_map.sql
Normal file
|
@ -0,0 +1,5 @@
|
|||
SELECT players, map, time, timestamp
|
||||
FROM record_teamrace_array
|
||||
WHERE map = $1
|
||||
OFFSET (($2 - 1) * $3)
|
||||
FETCH NEXT $3 ROWS ONLY
|
5
sql/teamraces/get_teamraces_by_player.sql
Normal file
5
sql/teamraces/get_teamraces_by_player.sql
Normal file
|
@ -0,0 +1,5 @@
|
|||
SELECT players, map, time, timestamp
|
||||
FROM record_teamrace_array
|
||||
WHERE $1 = ANY(players)
|
||||
OFFSET (($2 - 1) * $3)
|
||||
FETCH NEXT $3 ROWS ONLY
|
|
@ -15,7 +15,7 @@ async fn get_teamrace_by_player(
|
|||
page: Option<&str>,
|
||||
) -> Result<Json<SearchResult<Teamrace>>, String> {
|
||||
let page_num = get_page_number_as_i32_from_str(page);
|
||||
match Teamrace::get_teamrace_by_player(db, player, page_num).await {
|
||||
match Teamrace::get_teamraces_by_player(db, player, page_num).await {
|
||||
Ok(maps) => Ok(Json(maps)),
|
||||
Err(err) => Err(format!("Error: {}", err)),
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ async fn get_teamrace_by_map(
|
|||
page: Option<&str>,
|
||||
) -> Result<Json<SearchResult<Teamrace>>, String> {
|
||||
let page_num = get_page_number_as_i32_from_str(page);
|
||||
match Teamrace::get_teamrace_by_map(db, map, page_num).await {
|
||||
match Teamrace::get_teamraces_by_map(db, map, page_num).await {
|
||||
Ok(maps) => Ok(Json(maps)),
|
||||
Err(err) => Err(format!("Error: {}", err)),
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use chrono::NaiveDateTime;
|
||||
use rocket::futures::StreamExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::database::DatabaseHandler;
|
||||
|
@ -19,67 +18,33 @@ pub struct Map {
|
|||
|
||||
impl Map {
|
||||
pub async fn get_all_maps(db: &DatabaseHandler) -> Result<Vec<Map>, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Map,
|
||||
"
|
||||
SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles
|
||||
FROM record_maps
|
||||
JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map;
|
||||
"
|
||||
)
|
||||
.fetch_all(&db.pool)
|
||||
.await
|
||||
sqlx::query_file_as!(Map, "sql/maps/get_all_maps.sql")
|
||||
.fetch_all(&db.pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_map_by_name(db: &DatabaseHandler, map: &str) -> Result<Map, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Map,
|
||||
"
|
||||
SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles
|
||||
FROM record_maps
|
||||
JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map
|
||||
WHERE record_maps.map = $1;
|
||||
",
|
||||
map
|
||||
)
|
||||
.fetch_one(&db.pool)
|
||||
.await
|
||||
sqlx::query_file_as!(Map, "sql/maps/get_map_by_name.sql", map)
|
||||
.fetch_one(&db.pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_maps_by_category(
|
||||
db: &DatabaseHandler,
|
||||
category: &str,
|
||||
) -> Result<Vec<Map>, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Map,
|
||||
"
|
||||
SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles
|
||||
FROM record_maps
|
||||
JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map
|
||||
WHERE category = $1;
|
||||
",
|
||||
category
|
||||
)
|
||||
.fetch_all(&db.pool)
|
||||
.await
|
||||
sqlx::query_file_as!(Map, "sql/maps/get_maps_by_category.sql", category)
|
||||
.fetch_all(&db.pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_maps_by_mapper(
|
||||
db: &DatabaseHandler,
|
||||
mapper: &str,
|
||||
) -> Result<Vec<Map>, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
Map,
|
||||
"
|
||||
SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles
|
||||
FROM record_maps
|
||||
JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map
|
||||
WHERE $1 = ANY(mappers)
|
||||
",
|
||||
mapper
|
||||
)
|
||||
.fetch_all(&db.pool)
|
||||
.await
|
||||
sqlx::query_file_as!(Map, "sql/maps/get_maps_by_mapper.sql", mapper)
|
||||
.fetch_all(&db.pool)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -23,27 +23,9 @@ impl Race {
|
|||
player: &str,
|
||||
page: i32,
|
||||
) -> Result<SearchResult<Race>, sqlx::Error> {
|
||||
let results = sqlx::query_as!(
|
||||
let results = sqlx::query_file_as!(
|
||||
Race,
|
||||
"
|
||||
SELECT name,
|
||||
map,
|
||||
time,
|
||||
timestamp,
|
||||
server,
|
||||
ARRAY[cp1, cp2, cp3, cp4, cp5,
|
||||
cp6, cp7, cp8, cp9, cp10,
|
||||
cp11, cp12, cp13, cp14, cp15,
|
||||
cp16, cp17, cp18, cp19, cp20,
|
||||
cp21, cp22, cp23, cp24, cp25
|
||||
] AS checkpoints,
|
||||
gameid,
|
||||
ddnet7
|
||||
FROM record_race WHERE name = $1
|
||||
ORDER BY map, time
|
||||
OFFSET (($2 - 1) * $3)
|
||||
FETCH NEXT $3 ROWS ONLY
|
||||
",
|
||||
"sql/races/get_races_by_player.sql",
|
||||
player,
|
||||
page,
|
||||
ROWS_PER_PAGE
|
||||
|
@ -73,28 +55,9 @@ impl Race {
|
|||
map: &str,
|
||||
page: i32,
|
||||
) -> Result<SearchResult<Race>, sqlx::Error> {
|
||||
let results = sqlx::query_as!(
|
||||
let results = sqlx::query_file_as!(
|
||||
Race,
|
||||
"
|
||||
SELECT
|
||||
name,
|
||||
map,
|
||||
time,
|
||||
timestamp,
|
||||
server,
|
||||
ARRAY[cp1, cp2, cp3, cp4, cp5,
|
||||
cp6, cp7, cp8, cp9, cp10,
|
||||
cp11, cp12, cp13, cp14, cp15,
|
||||
cp16, cp17, cp18, cp19, cp20,
|
||||
cp21, cp22, cp23, cp24, cp25
|
||||
] AS checkpoints,
|
||||
gameid,
|
||||
ddnet7
|
||||
FROM record_race WHERE map = $1
|
||||
ORDER BY name, time
|
||||
OFFSET (($2 - 1) * $3)
|
||||
FETCH NEXT $3 ROWS ONLY
|
||||
",
|
||||
"sql/races/get_races_by_map.sql",
|
||||
map,
|
||||
page,
|
||||
ROWS_PER_PAGE
|
||||
|
|
|
@ -14,20 +14,14 @@ pub struct Teamrace {
|
|||
}
|
||||
|
||||
impl Teamrace {
|
||||
pub async fn get_teamrace_by_player(
|
||||
pub async fn get_teamraces_by_player(
|
||||
db: &DatabaseHandler,
|
||||
player: &str,
|
||||
page: i32,
|
||||
) -> Result<SearchResult<Teamrace>, sqlx::Error> {
|
||||
let results = sqlx::query_as!(
|
||||
let results = sqlx::query_file_as!(
|
||||
Teamrace,
|
||||
"
|
||||
SELECT players, map, time, timestamp
|
||||
FROM record_teamrace_array
|
||||
WHERE $1 = ANY(players)
|
||||
OFFSET (($2 - 1) * $3)
|
||||
FETCH NEXT $3 ROWS ONLY
|
||||
",
|
||||
"sql/teamraces/get_teamraces_by_player.sql",
|
||||
player,
|
||||
page,
|
||||
ROWS_PER_PAGE
|
||||
|
@ -52,20 +46,14 @@ impl Teamrace {
|
|||
})
|
||||
}
|
||||
|
||||
pub async fn get_teamrace_by_map(
|
||||
pub async fn get_teamraces_by_map(
|
||||
db: &DatabaseHandler,
|
||||
map: &str,
|
||||
page: i32,
|
||||
) -> Result<SearchResult<Teamrace>, sqlx::Error> {
|
||||
let results = sqlx::query_as!(
|
||||
let results = sqlx::query_file_as!(
|
||||
Teamrace,
|
||||
"
|
||||
SELECT players, map, time, timestamp
|
||||
FROM record_teamrace_array
|
||||
WHERE map = $1
|
||||
OFFSET (($2 - 1) * $3)
|
||||
FETCH NEXT $3 ROWS ONLY
|
||||
",
|
||||
"sql/teamraces/get_teamraces_by_map.sql",
|
||||
map,
|
||||
page,
|
||||
ROWS_PER_PAGE
|
||||
|
|
Loading…
Reference in New Issue
Block a user