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
use crate::change::ToField;
use crate::pb::database::{table_change::Operation, DatabaseChanges, TableChange};

impl DatabaseChanges {
    pub fn push_change<V: AsRef<str>>(
        &mut self,
        table: V,
        pk: V,
        ordinal: u64,
        operation: Operation,
    ) -> &mut TableChange {
        let table_change = TableChange::new(table, pk, ordinal, operation);
        self.table_changes.push(table_change);
        return self.table_changes.last_mut().unwrap();
    }
}

impl TableChange {
    pub fn new<V: AsRef<str>>(entity: V, pk: V, ordinal: u64, operation: Operation) -> TableChange {
        TableChange {
            table: entity.as_ref().to_string(),
            pk: pk.as_ref().to_string(),
            ordinal,
            operation: operation as i32,
            fields: vec![],
        }
    }

    pub fn change<N: AsRef<str>, T: ToField>(&mut self, name: N, change: T) -> &mut TableChange {
        self.fields.push(change.to_field(name));
        self
    }
}