Files
biggus-dickus/crates/db/src/vocab.rs
T

146 lines
4.3 KiB
Rust

//! Controlled vocabularies and terms.
use domain::{LocalizedLabel, NewTerm, Term, TermId, TermRef, Vocabulary, VocabularyId};
use sqlx::Row;
/// Labels aggregated per row as JSON, to read a term and its labels in one query.
const LABELS_JSON: &str = "COALESCE(json_agg(json_build_object('lang', tl.lang, 'label', tl.label) \
ORDER BY tl.lang) FILTER (WHERE tl.term_id IS NOT NULL), '[]'::json)";
/// Create a vocabulary with the given key.
pub async fn create_vocabulary<'e, E>(executor: E, key: &str) -> Result<Vocabulary, sqlx::Error>
where
E: sqlx::PgExecutor<'e>,
{
let id = VocabularyId::new();
sqlx::query("INSERT INTO vocabulary (id, key) VALUES ($1, $2)")
.bind(id.to_uuid())
.bind(key)
.execute(executor)
.await?;
Ok(Vocabulary {
id,
key: key.to_owned(),
})
}
/// Look up a vocabulary by its key.
pub async fn vocabulary_by_key<'e, E>(
executor: E,
key: &str,
) -> Result<Option<Vocabulary>, sqlx::Error>
where
E: sqlx::PgExecutor<'e>,
{
let row = sqlx::query("SELECT id, key FROM vocabulary WHERE key = $1")
.bind(key)
.fetch_optional(executor)
.await?;
row.map(map_vocabulary).transpose()
}
/// Insert a term and its labels. Multiple statements — pass a transaction
/// connection (`&mut *tx`) so the term and its labels commit atomically.
pub async fn add_term(conn: &mut sqlx::PgConnection, new: &NewTerm) -> Result<TermId, sqlx::Error> {
let id = TermId::new();
sqlx::query("INSERT INTO term (id, vocabulary_id, external_uri) VALUES ($1, $2, $3)")
.bind(id.to_uuid())
.bind(new.vocabulary_id.to_uuid())
.bind(new.external_uri.as_deref())
.execute(&mut *conn)
.await?;
for label in &new.labels {
sqlx::query("INSERT INTO term_label (term_id, lang, label) VALUES ($1, $2, $3)")
.bind(id.to_uuid())
.bind(&label.lang)
.bind(&label.label)
.execute(&mut *conn)
.await?;
}
Ok(id)
}
/// Fetch one term (with its labels).
pub async fn term_by_id<'e, E>(executor: E, id: TermId) -> Result<Option<Term>, sqlx::Error>
where
E: sqlx::PgExecutor<'e>,
{
let sql = format!(
"SELECT t.id, t.vocabulary_id, t.external_uri, {LABELS_JSON} AS labels \
FROM term t LEFT JOIN term_label tl ON tl.term_id = t.id \
WHERE t.id = $1 GROUP BY t.id"
);
let row = sqlx::query(&sql)
.bind(id.to_uuid())
.fetch_optional(executor)
.await?;
row.map(map_term).transpose()
}
/// List all terms in a vocabulary (with labels), ordered by id.
pub async fn list_terms<'e, E>(
executor: E,
vocabulary_id: VocabularyId,
) -> Result<Vec<Term>, sqlx::Error>
where
E: sqlx::PgExecutor<'e>,
{
let sql = format!(
"SELECT t.id, t.vocabulary_id, t.external_uri, {LABELS_JSON} AS labels \
FROM term t LEFT JOIN term_label tl ON tl.term_id = t.id \
WHERE t.vocabulary_id = $1 GROUP BY t.id ORDER BY t.id"
);
let rows = sqlx::query(&sql)
.bind(vocabulary_id.to_uuid())
.fetch_all(executor)
.await?;
rows.into_iter().map(map_term).collect()
}
/// Resolve a term to a [`TermRef`], confirming it belongs to `vocabulary_id`.
pub async fn resolve_term<'e, E>(
executor: E,
vocabulary_id: VocabularyId,
term_id: TermId,
) -> Result<Option<TermRef>, sqlx::Error>
where
E: sqlx::PgExecutor<'e>,
{
let found =
sqlx::query_scalar::<_, i32>("SELECT 1 FROM term WHERE id = $1 AND vocabulary_id = $2")
.bind(term_id.to_uuid())
.bind(vocabulary_id.to_uuid())
.fetch_optional(executor)
.await?;
Ok(found.map(|_| TermRef::new(term_id, vocabulary_id)))
}
fn map_vocabulary(row: sqlx::postgres::PgRow) -> Result<Vocabulary, sqlx::Error> {
Ok(Vocabulary {
id: VocabularyId::from_uuid(row.try_get("id")?),
key: row.try_get("key")?,
})
}
fn map_term(row: sqlx::postgres::PgRow) -> Result<Term, sqlx::Error> {
let labels: sqlx::types::Json<Vec<LocalizedLabel>> = row.try_get("labels")?;
Ok(Term {
id: TermId::from_uuid(row.try_get("id")?),
vocabulary_id: VocabularyId::from_uuid(row.try_get("vocabulary_id")?),
external_uri: row.try_get("external_uri")?,
labels: labels.0,
})
}