1
//! Tag link writes on a caller's connection; the caller holds the owner lock.
2

            
3
use finance::tag::Tag;
4
use sqlx::PgConnection;
5
use sqlx::types::Uuid;
6

            
7
use super::db_err;
8
use crate::error::ServerError;
9

            
10
5358
fn ensure_not_blank(tag: &Tag) -> Result<(), ServerError> {
11
5358
    if tag.is_blank() {
12
9
        return Err(ServerError::Creation);
13
5349
    }
14
5349
    Ok(())
15
5358
}
16

            
17
203
async fn delete_orphan(conn: &mut PgConnection, tag_id: Uuid) -> Result<(), ServerError> {
18
203
    super::delete_tag_if_orphaned(conn, tag_id)
19
203
        .await
20
203
        .map_err(db_err)
21
203
}
22

            
23
/// Link `tag` to the transaction, replacing another value of its name; a
24
/// replaced tag row nothing references any more is deleted.
25
1356
pub(crate) async fn set_transaction_tag(
26
1356
    conn: &mut PgConnection,
27
1356
    tx_id: Uuid,
28
1356
    tag: &Tag,
29
1356
) -> Result<(), ServerError> {
30
1356
    ensure_not_blank(tag)?;
31
1350
    let (name, value) = tag.normalized();
32
1350
    let detached = sqlx::query_file!(
33
1350
        "sql/set/transactions/tag.sql",
34
        &tx_id,
35
        name,
36
        value,
37
        tag.description
38
    )
39
1350
    .fetch_all(&mut *conn)
40
1350
    .await
41
1350
    .map_err(db_err)?;
42
1350
    for row in detached {
43
186
        delete_orphan(conn, row.tag_id).await?;
44
    }
45
1350
    Ok(())
46
1356
}
47

            
48
/// Link `tag` to the split, replacing another value of its name; a replaced
49
/// tag row nothing references any more is deleted.
50
4002
pub(crate) async fn set_split_tag(
51
4002
    conn: &mut PgConnection,
52
4002
    split_id: Uuid,
53
4002
    tag: &Tag,
54
4002
) -> Result<(), ServerError> {
55
4002
    ensure_not_blank(tag)?;
56
3999
    let (name, value) = tag.normalized();
57
3999
    let detached = sqlx::query_file!(
58
3999
        "sql/set/splits/tag.sql",
59
        &split_id,
60
        name,
61
        value,
62
        tag.description
63
    )
64
3999
    .fetch_all(&mut *conn)
65
3999
    .await
66
3999
    .map_err(db_err)?;
67
3998
    for row in detached {
68
4
        delete_orphan(conn, row.tag_id).await?;
69
    }
70
3998
    Ok(())
71
4002
}
72

            
73
/// Unlink the tag from the transaction; a tag row nothing references any
74
/// more is deleted.
75
9
pub(crate) async fn detach_transaction_tag(
76
9
    conn: &mut PgConnection,
77
9
    tx_id: Uuid,
78
9
    tag_id: Uuid,
79
9
) -> Result<(), ServerError> {
80
9
    sqlx::query_file!("sql/delete/transaction_tags/by_pair.sql", &tx_id, &tag_id)
81
9
        .execute(&mut *conn)
82
9
        .await
83
9
        .map_err(db_err)?;
84
9
    delete_orphan(conn, tag_id).await
85
9
}
86

            
87
/// Unlink the tag from the split; a tag row nothing references any more is
88
/// deleted.
89
4
pub(crate) async fn detach_split_tag(
90
4
    conn: &mut PgConnection,
91
4
    split_id: Uuid,
92
4
    tag_id: Uuid,
93
4
) -> Result<(), ServerError> {
94
4
    sqlx::query_file!("sql/delete/split_tags/by_pair.sql", &split_id, &tag_id)
95
4
        .execute(&mut *conn)
96
4
        .await
97
4
        .map_err(db_err)?;
98
4
    delete_orphan(conn, tag_id).await
99
4
}