Lines
100 %
Functions
81.82 %
Branches
//! Tag link writes on a caller's connection; the caller holds the owner lock.
use finance::tag::Tag;
use sqlx::PgConnection;
use sqlx::types::Uuid;
use super::db_err;
use crate::error::ServerError;
fn ensure_not_blank(tag: &Tag) -> Result<(), ServerError> {
if tag.is_blank() {
return Err(ServerError::Creation);
}
Ok(())
async fn delete_orphan(conn: &mut PgConnection, tag_id: Uuid) -> Result<(), ServerError> {
super::delete_tag_if_orphaned(conn, tag_id)
.await
.map_err(db_err)
/// Link `tag` to the transaction, replacing another value of its name; a
/// replaced tag row nothing references any more is deleted.
pub(crate) async fn set_transaction_tag(
conn: &mut PgConnection,
tx_id: Uuid,
tag: &Tag,
) -> Result<(), ServerError> {
ensure_not_blank(tag)?;
let (name, value) = tag.normalized();
let detached = sqlx::query_file!(
"sql/set/transactions/tag.sql",
&tx_id,
name,
value,
tag.description
)
.fetch_all(&mut *conn)
.map_err(db_err)?;
for row in detached {
delete_orphan(conn, row.tag_id).await?;
/// Link `tag` to the split, replacing another value of its name; a replaced
/// tag row nothing references any more is deleted.
pub(crate) async fn set_split_tag(
split_id: Uuid,
"sql/set/splits/tag.sql",
&split_id,
/// Unlink the tag from the transaction; a tag row nothing references any
/// more is deleted.
pub(crate) async fn detach_transaction_tag(
tag_id: Uuid,
sqlx::query_file!("sql/delete/transaction_tags/by_pair.sql", &tx_id, &tag_id)
.execute(&mut *conn)
delete_orphan(conn, tag_id).await
/// Unlink the tag from the split; a tag row nothing references any more is
/// deleted.
pub(crate) async fn detach_split_tag(
sqlx::query_file!("sql/delete/split_tags/by_pair.sql", &split_id, &tag_id)