substreams_database_change/
helpers.rs1use std::collections::HashMap;
2
3use crate::change::ToField;
4use crate::pb::database::{
5 table_change::{Operation, PrimaryKey},
6 CompositePrimaryKey, DatabaseChanges, TableChange,
7};
8
9impl DatabaseChanges {
10 pub fn push_change<T: AsRef<str>, K: AsRef<str>>(
11 &mut self,
12 table: T,
13 pk: K,
14 ordinal: u64,
15 operation: Operation,
16 ) -> &mut TableChange {
17 let table_change = TableChange::new(table, pk, ordinal, operation);
18 self.table_changes.push(table_change);
19 return self.table_changes.last_mut().unwrap();
20 }
21
22 pub fn push_change_composite<T: AsRef<str>>(
23 &mut self,
24 table: T,
25 keys: HashMap<String, String>,
26 ordinal: u64,
27 operation: Operation,
28 ) -> &mut TableChange {
29 let table_change = TableChange::new_composite(table, keys, ordinal, operation);
30 self.table_changes.push(table_change);
31 return self.table_changes.last_mut().unwrap();
32 }
33}
34
35impl TableChange {
36 pub fn new<T: AsRef<str>, K: AsRef<str>>(
37 entity: T,
38 pk: K,
39 ordinal: u64,
40 operation: Operation,
41 ) -> TableChange {
42 TableChange {
43 table: entity.as_ref().to_string(),
44 primary_key: Some(PrimaryKey::Pk(pk.as_ref().to_string())),
45 ordinal,
46 operation: operation as i32,
47 fields: vec![],
48 }
49 }
50
51 pub fn new_composite<T: AsRef<str>>(
52 entity: T,
53 keys: HashMap<String, String>,
54 ordinal: u64,
55 operation: Operation,
56 ) -> TableChange {
57 TableChange {
58 table: entity.as_ref().to_string(),
59 primary_key: Some(PrimaryKey::CompositePk(CompositePrimaryKey { keys })),
60 ordinal,
61 operation: operation as i32,
62 fields: vec![],
63 }
64 }
65
66 pub fn change<N: AsRef<str>, T: ToField>(&mut self, name: N, change: T) -> &mut TableChange {
67 self.fields.push(change.to_field(name));
68 self
69 }
70}