gen_models/
block_group_lineage.rs1use gen_core::HashId;
2use rusqlite::Row;
3
4use crate::{
5 Direction,
6 block_group::{BlockGroup, BlockGroupSelect},
7 db::GraphConnection,
8 lineage::SqlLineage,
9};
10
11#[derive(Clone, Debug, Eq, PartialEq)]
12pub struct BlockGroupLineage {
13 pub parent_block_group_id: HashId,
14 pub child_block_group_id: HashId,
15}
16
17impl SqlLineage for BlockGroupLineage {
18 type Id = HashId;
19
20 const CHILD_COLUMN: &'static str = "id";
21 const CHILD_ID_COLUMN: &'static str = "id";
22 const CHILD_TABLE_NAME: &'static str = "block_groups";
23 const PARENT_COLUMN: &'static str = "parent_block_group_id";
24 const PARENT_ID_COLUMN: &'static str = "id";
25 const PARENT_TABLE_NAME: &'static str = "block_groups";
26 const TABLE_NAME: &'static str = "block_groups";
27
28 fn parent_id(&self) -> &Self::Id {
29 &self.parent_block_group_id
30 }
31
32 fn child_id(&self) -> &Self::Id {
33 &self.child_block_group_id
34 }
35
36 fn process_row(row: &Row) -> rusqlite::Result<Self> {
37 Ok(Self {
38 parent_block_group_id: row.get(0)?,
39 child_block_group_id: row.get(1)?,
40 })
41 }
42}
43
44impl BlockGroupLineage {
45 pub fn get_parents(conn: &GraphConnection, child_block_group_id: &HashId) -> Vec<HashId> {
46 BlockGroup::select(conn)
47 .id(*child_block_group_id)
48 .only(BlockGroupSelect::ParentBlockGroupId)
49 .load()
50 .expect("should load parent block group ids")
51 .into_iter()
52 .flatten()
53 .collect()
54 }
55
56 pub fn get_children(conn: &GraphConnection, parent_block_group_id: &HashId) -> Vec<HashId> {
57 BlockGroup::select(conn)
58 .parent_block_group_id(*parent_block_group_id)
59 .order_by(BlockGroupSelect::CreatedOn, Direction::Asc)
60 .order_by(BlockGroupSelect::Id, Direction::Asc)
61 .only(BlockGroupSelect::Id)
62 .load()
63 .expect("should load child block group ids")
64 }
65
66 pub fn get_parent_block_groups(
67 conn: &GraphConnection,
68 child_block_group_id: &HashId,
69 ) -> Vec<BlockGroup> {
70 let parent_ids = BlockGroupLineage::get_parents(conn, child_block_group_id);
71 BlockGroup::select(conn)
72 .query_by_ids(parent_ids)
73 .expect("should load parent block groups by id")
74 }
75
76 pub fn get_ancestor_block_groups(
77 conn: &GraphConnection,
78 child_block_group_id: &HashId,
79 max_depth: Option<usize>,
80 ) -> Vec<BlockGroup> {
81 let ancestor_ids =
82 BlockGroupLineage::get_ancestors(conn, child_block_group_id, max_depth, None);
83 BlockGroup::select(conn)
84 .query_by_ids(ancestor_ids)
85 .expect("should load ancestor block groups by id")
86 }
87
88 pub fn get_descendant_block_groups(
89 conn: &GraphConnection,
90 parent_block_group_id: &HashId,
91 max_depth: Option<usize>,
92 ) -> Vec<BlockGroup> {
93 let descendant_ids =
94 BlockGroupLineage::get_descendants(conn, parent_block_group_id, max_depth, None);
95 BlockGroup::select(conn)
96 .query_by_ids(descendant_ids)
97 .expect("should load descendant block groups by id")
98 }
99}