Schrodinger's SQL

This commit is contained in:
2023-12-18 19:40:02 +02:00
parent b011f3f972
commit 7393638559

View File

@@ -14,6 +14,7 @@ pub struct Arrival {
tram_line: i32, tram_line: i32,
direction: bool, direction: bool,
} }
use web::Data; use web::Data;
use sqlx::{PgPool, query_as, query}; use sqlx::{PgPool, query_as, query};
#[get("all")] #[get("all")]
@@ -29,21 +30,34 @@ async fn show_arrivals(db_pool: Data<PgPool>) -> impl Responder {
} }
#[get("specific")] #[get("specific")]
async fn show_specific(db_pool: Data<PgPool>, type_arr: Vec<u8>) -> impl Responder { async fn show_specific(db_pool: Data<PgPool>, t_line: Vec<u8>, week_day: Some(u8)) -> impl Responder {
// Query Logic // Query Logic
// Idea: Construct a query bit by bit depending on input // Idea: Construct a query bit by bit depending on input
let mut dyn_query = String::from("SELECT * FROM arrivals WHERE "); let mut dyn_query = QueryBuilder::new("SELECT * FROM arrivals WHERE tram_line = ");
if type_arr.len() == 1 { if t_line.len() == 1 {
dyn_query.push_str(&format!("tram_line = {}", type_arr[0])); dyn_query.push_bind(t_line[0]);
} else {
dyn_query.push_bind(t_line[0]);
dyn_query.push("OR tram_line = ");
dyn_query.push_bind(t_line[1]);
} // Should be fine for tramline?
if let Some(week_day) {
dyn_query.push("AND week_day = ");
dyn_query.push_bind(week_day);
} }
dyn_query.build().sql().into();
// Check: https://stackoverflow.com/questions/74956100/how-to-build-safe-dynamic-query-with-sqlx-in-rust // We don't talk about this
// lappy ded ;-; // and idk if `query_as!()` will take a QueryBuilder
let arrivals = String::from("AIDS"); let arrivals = String::from("AIDS");
// Idk what to do, can't test with db cuz no .env xdxd
// linly pls i never made an api before and royal son doesn't count
dbg!(&arrivals); dbg!(&arrivals);
HttpResponse::Ok() HttpResponse::Ok()
.content_type("application/json") .content_type("application/json")