diff --git a/fantoir-datasource/src/services/query.rs b/fantoir-datasource/src/services/query.rs --- a/fantoir-datasource/src/services/query.rs +++ b/fantoir-datasource/src/services/query.rs @@ -9,7 +9,7 @@ //! code doesn't contain the code direction, and it can't be computed. use std::fmt::{Display, Formatter}; -use sqlx::{Error, FromRow, PgPool}; +use sqlx::PgPool; /* ------------------------------------------------------------- Search a fantoir code from INSEE code, identifiant communal. @@ -18,19 +18,15 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ pub async fn search_fantoir_code(pool: &PgPool, code_insee: &str, identifiant_communal_voie: &str) -> Option<String> { - let result = sqlx::query!(r#" + sqlx::query_scalar( r#" SELECT code_fantoir FROM fantoir -WHERE code_insee = $1 AND identifiant_communal_voie = $2 - "#, code_insee, identifiant_communal_voie) - .fetch_one(pool) - .await; - - if let Err(Error::RowNotFound) = result { - return None; - } - - result.unwrap().code_fantoir +WHERE code_insee = $1 AND identifiant_communal_voie = $2"#) + .bind(code_insee) + .bind(identifiant_communal_voie) + .fetch_optional(pool) + .await + .unwrap() } /* ------------------------------------------------------------- @@ -72,48 +68,24 @@ } pub async fn query_fantoir_code(pool: &PgPool, code_fantoir: &str) -> Option<FantoirVoieResult> { - let result = sqlx::query!(r#" -SELECT code_insee, identifiant_communal_voie, code_nature_voie, libelle_voie + sqlx::query_as(r#" +SELECT code_fantoir, code_insee, identifiant_communal_voie, code_nature_voie, libelle_voie FROM fantoir -WHERE code_fantoir = $1; - "#, code_fantoir) - .fetch_one(pool) - .await; - - if let Err(Error::RowNotFound) = result { - return None; - } - - let result = result.unwrap(); - - Some( - FantoirVoieResult { - code_fantoir: code_fantoir.to_string(), - code_insee: result.code_insee.unwrap(), - identifiant_communal_voie: result.identifiant_communal_voie.unwrap(), - code_nature_voie: result.code_nature_voie, - libelle_voie: result.libelle_voie.unwrap(), - } - ) +WHERE code_fantoir = $1;"#) + .bind(code_fantoir) + .fetch_optional(pool) + .await + .unwrap() } pub async fn query_libelle (pool: &PgPool, libelle: &str) -> Vec<FantoirVoieResult> { - let result = sqlx::query(r#" + sqlx::query_as(r#" SELECT code_fantoir, code_insee, identifiant_communal_voie, code_nature_voie, libelle_voie FROM fantoir WHERE libelle_voie ILIKE CONCAT('%', $1, '%'); "#) .bind(libelle) .fetch_all(pool) - .await; - - if let Err(Error::RowNotFound) = result { - return Vec::new(); - } - - result + .await .unwrap() - .iter() - .map(|row| FantoirVoieResult::from_row(row).unwrap()) - .collect() }