Using files instead of strings for queries.

This commit is contained in:
BurnyLlama 2023-04-16 14:22:11 +02:00
parent 1821d5e482
commit 4fde4bea30
12 changed files with 85 additions and 108 deletions

View 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

View 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

View 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

View 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)

View 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

View 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

View 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

View 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

View File

@ -15,7 +15,7 @@ async fn get_teamrace_by_player(
page: Option<&str>, page: Option<&str>,
) -> Result<Json<SearchResult<Teamrace>>, String> { ) -> Result<Json<SearchResult<Teamrace>>, String> {
let page_num = get_page_number_as_i32_from_str(page); 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)), Ok(maps) => Ok(Json(maps)),
Err(err) => Err(format!("Error: {}", err)), Err(err) => Err(format!("Error: {}", err)),
} }
@ -28,7 +28,7 @@ async fn get_teamrace_by_map(
page: Option<&str>, page: Option<&str>,
) -> Result<Json<SearchResult<Teamrace>>, String> { ) -> Result<Json<SearchResult<Teamrace>>, String> {
let page_num = get_page_number_as_i32_from_str(page); 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)), Ok(maps) => Ok(Json(maps)),
Err(err) => Err(format!("Error: {}", err)), Err(err) => Err(format!("Error: {}", err)),
} }

View File

@ -1,5 +1,4 @@
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use rocket::futures::StreamExt;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::database::DatabaseHandler; use crate::database::DatabaseHandler;
@ -19,67 +18,33 @@ pub struct Map {
impl Map { impl Map {
pub async fn get_all_maps(db: &DatabaseHandler) -> Result<Vec<Map>, sqlx::Error> { pub async fn get_all_maps(db: &DatabaseHandler) -> Result<Vec<Map>, sqlx::Error> {
sqlx::query_as!( sqlx::query_file_as!(Map, "sql/maps/get_all_maps.sql")
Map, .fetch_all(&db.pool)
" .await
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
} }
pub async fn get_map_by_name(db: &DatabaseHandler, map: &str) -> Result<Map, sqlx::Error> { pub async fn get_map_by_name(db: &DatabaseHandler, map: &str) -> Result<Map, sqlx::Error> {
sqlx::query_as!( sqlx::query_file_as!(Map, "sql/maps/get_map_by_name.sql", map)
Map, .fetch_one(&db.pool)
" .await
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
} }
pub async fn get_maps_by_category( pub async fn get_maps_by_category(
db: &DatabaseHandler, db: &DatabaseHandler,
category: &str, category: &str,
) -> Result<Vec<Map>, sqlx::Error> { ) -> Result<Vec<Map>, sqlx::Error> {
sqlx::query_as!( sqlx::query_file_as!(Map, "sql/maps/get_maps_by_category.sql", category)
Map, .fetch_all(&db.pool)
" .await
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
} }
pub async fn get_maps_by_mapper( pub async fn get_maps_by_mapper(
db: &DatabaseHandler, db: &DatabaseHandler,
mapper: &str, mapper: &str,
) -> Result<Vec<Map>, sqlx::Error> { ) -> Result<Vec<Map>, sqlx::Error> {
sqlx::query_as!( sqlx::query_file_as!(Map, "sql/maps/get_maps_by_mapper.sql", mapper)
Map, .fetch_all(&db.pool)
" .await
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
} }
} }

View File

@ -23,27 +23,9 @@ impl Race {
player: &str, player: &str,
page: i32, page: i32,
) -> Result<SearchResult<Race>, sqlx::Error> { ) -> Result<SearchResult<Race>, sqlx::Error> {
let results = sqlx::query_as!( let results = sqlx::query_file_as!(
Race, Race,
" "sql/races/get_races_by_player.sql",
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
",
player, player,
page, page,
ROWS_PER_PAGE ROWS_PER_PAGE
@ -73,28 +55,9 @@ impl Race {
map: &str, map: &str,
page: i32, page: i32,
) -> Result<SearchResult<Race>, sqlx::Error> { ) -> Result<SearchResult<Race>, sqlx::Error> {
let results = sqlx::query_as!( let results = sqlx::query_file_as!(
Race, Race,
" "sql/races/get_races_by_map.sql",
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
",
map, map,
page, page,
ROWS_PER_PAGE ROWS_PER_PAGE

View File

@ -14,20 +14,14 @@ pub struct Teamrace {
} }
impl Teamrace { impl Teamrace {
pub async fn get_teamrace_by_player( pub async fn get_teamraces_by_player(
db: &DatabaseHandler, db: &DatabaseHandler,
player: &str, player: &str,
page: i32, page: i32,
) -> Result<SearchResult<Teamrace>, sqlx::Error> { ) -> Result<SearchResult<Teamrace>, sqlx::Error> {
let results = sqlx::query_as!( let results = sqlx::query_file_as!(
Teamrace, Teamrace,
" "sql/teamraces/get_teamraces_by_player.sql",
SELECT players, map, time, timestamp
FROM record_teamrace_array
WHERE $1 = ANY(players)
OFFSET (($2 - 1) * $3)
FETCH NEXT $3 ROWS ONLY
",
player, player,
page, page,
ROWS_PER_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, db: &DatabaseHandler,
map: &str, map: &str,
page: i32, page: i32,
) -> Result<SearchResult<Teamrace>, sqlx::Error> { ) -> Result<SearchResult<Teamrace>, sqlx::Error> {
let results = sqlx::query_as!( let results = sqlx::query_file_as!(
Teamrace, Teamrace,
" "sql/teamraces/get_teamraces_by_map.sql",
SELECT players, map, time, timestamp
FROM record_teamrace_array
WHERE map = $1
OFFSET (($2 - 1) * $3)
FETCH NEXT $3 ROWS ONLY
",
map, map,
page, page,
ROWS_PER_PAGE ROWS_PER_PAGE