Lines
99.2 %
Functions
80 %
Branches
100 %
//! Setting and clearing one tag name of an account or a commodity on the tag
//! lock protocol. Each attempt is one DB transaction: the tag keys, then the
//! owner row, then the writes. Keys come before the row as in a merge, which
//! holds `from`'s keys while it waits for `from`'s row; the touched tags are
//! reread under the row lock, and any change makes the attempt retry. Stored
//! names match by their trimmed form.
use finance::tag::Tag;
use sqlx::{PgConnection, types::Uuid};
use super::lock::{Key, KeyChanged, begin, lock, retry, sorted_keys};
use super::user::{delete_tag_if_orphaned, lock_account};
#[derive(Clone, Copy)]
pub(crate) enum Owner {
Account,
Commodity,
}
impl Owner {
pub(crate) async fn lock_row(self, conn: &mut PgConnection, id: Uuid) -> sqlx::Result<()> {
match self {
Self::Account => lock_account(conn, id).await,
Self::Commodity => {
sqlx::query_file!("sql/select/commodities/by_id_locked.sql", id)
.fetch_one(conn)
.await?;
Ok(())
/// Link `tag` in place of any other value of its name, then drop the
/// detached tag rows nothing references any more.
pub(crate) async fn set(
self,
conn: &mut PgConnection,
id: Uuid,
tag: &Tag,
) -> sqlx::Result<Result<(), KeyChanged>> {
let (name, value) = tag.normalized();
let mut tx = begin(conn).await?;
let touched = self.lockable_for_set(&mut tx, id, name, value).await?;
if lock(&mut tx, &touched, &[tag]).await?.is_err() {
return retry(tx).await;
self.lock_row(&mut tx, id).await?;
if self.lockable_for_set(&mut tx, id, name, value).await? != touched {
self.write_set(&mut tx, id, tag).await?;
tx.commit().await?;
Ok(Ok(()))
/// Unlink every tag named `name`, then drop the released tag rows nothing
/// references any more.
pub(crate) async fn clear(
name: &str,
let released = self.linked_of_name(&mut tx, id, name).await?;
if lock(&mut tx, &released, &[]).await?.is_err() {
if self.linked_of_name(&mut tx, id, name).await? != released {
self.write_clear(&mut tx, id, name, &released).await?;
/// Link `tag` in place of the owner's other values of its name and drop
/// the detached tag rows nothing references any more. The caller holds
/// the locks.
pub(crate) async fn write_set(
) -> sqlx::Result<()> {
let detached = match self {
Self::Account => {
let rows =
sqlx::query_file!("sql/set/accounts/tag.sql", id, name, value, tag.description)
.fetch_all(&mut *conn)
rows.into_iter()
.map(|row| (row.id, (row.tag_name, row.tag_value)))
.collect::<Vec<_>>()
let rows = sqlx::query_file!(
"sql/set/commodities/tag.sql",
id,
name,
value,
tag.description
)
};
for (tag_id, _) in &sorted_keys(detached.into_iter()) {
delete_tag_if_orphaned(conn, *tag_id).await?;
/// Unlink the owner's tags named `name`, the `released` rows, and drop the
/// ones nothing references any more. The caller holds the locks.
pub(crate) async fn write_clear(
released: &[(Uuid, Key)],
sqlx::query_file!("sql/delete/account_tags/by_account_and_name.sql", id, name)
.execute(&mut *conn)
.await?
sqlx::query_file!(
"sql/delete/commodity_tags/by_commodity_and_name.sql",
name
for (tag_id, _) in released {
/// The owner's tags named `name` and the row of `(name, value)`, if stored.
async fn lockable_for_set(
value: &str,
) -> sqlx::Result<Vec<(Uuid, Key)>> {
let keys = match self {
Self::Account => sqlx::query_file!(
"sql/select/tags/lockable_for_account_set.sql",
value
.fetch_all(conn)
.into_iter()
.collect::<Vec<_>>(),
Self::Commodity => sqlx::query_file!(
"sql/select/tags/lockable_for_commodity_set.sql",
Ok(sorted_keys(keys.into_iter()))
/// The owner's tags named `name`.
async fn linked_of_name(
Self::Account => sqlx::query_file!("sql/select/tags/by_account_and_name.sql", id, name)
sqlx::query_file!("sql/select/tags/by_commodity_and_name.sql", id, name)