Skip to main content

gen_models/
block_group_lineage.rs

1use gen_core::HashId;
2use rusqlite::{Row, params};
3
4use crate::{block_group::BlockGroup, db::GraphConnection, lineage::SqlLineage, traits::Query};
5
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub struct BlockGroupLineage {
8    pub parent_block_group_id: HashId,
9    pub child_block_group_id: HashId,
10}
11
12impl Query for BlockGroupLineage {
13    type Model = BlockGroupLineage;
14
15    const PRIMARY_KEY: &'static str = "id";
16    const TABLE_NAME: &'static str = "block_groups";
17
18    fn process_row(row: &Row) -> Self::Model {
19        BlockGroupLineage {
20            parent_block_group_id: row.get(0).unwrap(),
21            child_block_group_id: row.get(1).unwrap(),
22        }
23    }
24}
25
26impl SqlLineage for BlockGroupLineage {
27    type Id = HashId;
28
29    const CHILD_COLUMN: &'static str = "id";
30    const CHILD_ID_COLUMN: &'static str = "id";
31    const CHILD_TABLE_NAME: &'static str = "block_groups";
32    const PARENT_COLUMN: &'static str = "parent_block_group_id";
33    const PARENT_ID_COLUMN: &'static str = "id";
34    const PARENT_TABLE_NAME: &'static str = "block_groups";
35
36    fn parent_id(&self) -> &Self::Id {
37        &self.parent_block_group_id
38    }
39
40    fn child_id(&self) -> &Self::Id {
41        &self.child_block_group_id
42    }
43}
44
45impl BlockGroupLineage {
46    pub fn get_parents(conn: &GraphConnection, child_block_group_id: &HashId) -> Vec<HashId> {
47        BlockGroupLineage::query(
48            conn,
49            "SELECT parent_block_group_id, id
50             FROM block_groups
51             WHERE id = ?1 AND parent_block_group_id IS NOT NULL;",
52            params![child_block_group_id],
53        )
54        .into_iter()
55        .map(|lineage| lineage.parent_block_group_id)
56        .collect()
57    }
58
59    pub fn get_children(conn: &GraphConnection, parent_block_group_id: &HashId) -> Vec<HashId> {
60        BlockGroupLineage::query(
61            conn,
62            "SELECT parent_block_group_id, id
63             FROM block_groups
64             WHERE parent_block_group_id = ?1
65             ORDER BY created_on, id;",
66            params![parent_block_group_id],
67        )
68        .into_iter()
69        .map(|lineage| lineage.child_block_group_id)
70        .collect()
71    }
72
73    pub fn get_parent_block_groups(
74        conn: &GraphConnection,
75        child_block_group_id: &HashId,
76    ) -> Vec<BlockGroup> {
77        let parent_ids = BlockGroupLineage::get_parents(conn, child_block_group_id);
78        BlockGroup::query_by_ids(conn, &parent_ids)
79    }
80
81    pub fn get_ancestor_block_groups(
82        conn: &GraphConnection,
83        child_block_group_id: &HashId,
84        max_depth: Option<usize>,
85    ) -> Vec<BlockGroup> {
86        let ancestor_ids = BlockGroupLineage::get_ancestors(conn, child_block_group_id, max_depth);
87        BlockGroup::query_by_ids(conn, &ancestor_ids)
88    }
89
90    pub fn get_descendant_block_groups(
91        conn: &GraphConnection,
92        parent_block_group_id: &HashId,
93        max_depth: Option<usize>,
94    ) -> Vec<BlockGroup> {
95        let descendant_ids =
96            BlockGroupLineage::get_descendants(conn, parent_block_group_id, max_depth);
97        BlockGroup::query_by_ids(conn, &descendant_ids)
98    }
99}