aboutsummaryrefslogtreecommitdiffstats
path: root/src/update.rs
blob: ebed2178fe528e50feb43a3dd5399b93f0f50f84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2022  Teddy Wing
//
// This file is part of Yaqlite.
//
// Yaqlite is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Yaqlite is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Yaqlite. If not, see <https://www.gnu.org/licenses/>.

/// Update a YAML record in the given database.
pub fn update(
    dbconn: &mut rusqlite::Connection,
    table_name: &str,
    record_id: &str,
    data: &mut yaml_rust::Yaml,
) -> Result<(), crate::Error> {
    let table_columns = crate::sqlite::get_column_names(&dbconn, table_name)?;

    let tx = dbconn.transaction()?;

    crate::yaml::db_update(
        data,
        &tx,
        &table_name,
        &table_columns,
        // TODO: dynamic or user-supplied
        "id",
        record_id,
    )?;

    tx.commit()?;

    Ok(())
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn updates_database_record_from_yaml() {
        #[derive(Debug, PartialEq)]
        struct TestRecord {
            id: i8,
            count: i16,
            weight: f32,
            description: String,
        }

        let mut conn = rusqlite::Connection::open_in_memory().unwrap();

        conn.execute(
            r#"
                CREATE TABLE "test" (
                    id INTEGER PRIMARY KEY,
                    count INTEGER,
                    weight REAL,
                    description TEXT
                );
            "#,
            [],
        ).unwrap();

        {
            let mut stmt = conn.prepare(r#"
                INSERT INTO "test"
                    (count, weight, description)
                VALUES
                    (?, ?, ?);
            "#).unwrap();

            stmt.insert(
                rusqlite::params![
                    55_i16,
                    0.8_f32,
                    "Ounces or grams?",
                ],
            ).unwrap();
        }

        let expected = TestRecord {
            id: 1,
            count: 28,
            weight: 1.2,
            description: r#"This is a multiline

description."#.to_owned(),
        };

        let mut yaml_data = yaml_rust::YamlLoader::load_from_str(
            &format!(
r#"count: {}
weight: {}
description: |-
  This is a multiline

  description.
"#,
                expected.count,
                expected.weight,
            ),
        ).unwrap();
        let mut yaml_record = yaml_data.get_mut(0).unwrap();

        update(&mut conn, "test", "1", &mut yaml_record).unwrap();

        {
            let mut stmt = conn.prepare(r#"
                SELECT
                    "id", "count", "weight", "description"
                FROM "test"
                WHERE "id" = 1;
            "#).unwrap();

            let got = stmt.query_row(
                [],
                |row| {
                    Ok(
                        TestRecord {
                            id: row.get(0).unwrap(),
                            count: row.get(1).unwrap(),
                            weight: row.get(2).unwrap(),
                            description: row.get(3).unwrap(),
                        }
                    )
                }
            ).unwrap();

            assert_eq!(expected, got);
        }

        conn.close().unwrap();
    }
}