Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3755023
D2749.id6981.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D2749.id6981.diff
View Options
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
@@ -1,8 +1,15 @@
-//! Service to search imported FANTOIR table
-//! This is intended to be exposed to the tool, and used internally to fix FANTOIR codes.
+//! Service to search imported FANTOIR table.
+//!
+//! This is intended to be exposed to the tool, and used internally to fix FANTOIR codes:
+//!
+//! - the `fantoir-datasource query` command can so be used to check
+//! if an import contains expected values to validate before promotion.
+//!
+//! - the Wikidata import code uses `search_fantoir_code` when the FANTOIR
+//! 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.
@@ -11,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)
+WHERE code_insee = $1 AND identifiant_communal_voie = $2"#)
+ .bind(code_insee)
+ .bind(identifiant_communal_voie)
.fetch_one(pool)
- .await;
-
- if let Err(Error::RowNotFound) = result {
- return None;
- }
-
- result.unwrap().code_fantoir
+ .await
+ .ok()
}
/* -------------------------------------------------------------
@@ -65,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)
+WHERE code_fantoir = $1;"#)
+ .bind(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(),
- }
- )
+ .await
+ .ok()
}
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
- .unwrap()
- .iter()
- .map(|row| FantoirVoieResult::from_row(row).unwrap())
- .collect()
+ .await
+ .unwrap_or(Vec::new())
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Nov 19, 11:56 (21 h, 16 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2252461
Default Alt Text
D2749.id6981.diff (3 KB)
Attached To
Mode
D2749: Don't require PostgreSQL schema at build time
Attached
Detach File
Event Timeline
Log In to Comment