ic_dbms_api/dbms/
foreign_fetcher.rs

1use crate::dbms::table::TableColumns;
2use crate::dbms::value::Value;
3use crate::prelude::{Database, IcDbmsResult};
4
5/// This trait defines the behavior of a foreign fetcher, which is responsible for
6/// fetching data from foreign sources or databases.
7///
8/// It takes a table name and returns the values associated with that table.
9pub trait ForeignFetcher: Default {
10    /// Fetches the data for the specified table and primary key values.
11    ///
12    /// # Arguments
13    ///
14    /// * `database` - The database from which to fetch the data.
15    /// * `table` - The name of the table to fetch data from.
16    /// * `local_column` - The local column that references the foreign key.
17    /// * `pk_value` - The primary key to look for.
18    ///
19    /// # Returns
20    ///
21    /// A result containing the fetched table columns or an error.
22    fn fetch(
23        &self,
24        database: &impl Database,
25        table: &str,
26        local_column: &'static str,
27        pk_value: Value,
28    ) -> IcDbmsResult<TableColumns>;
29}
30
31/// A no-op foreign fetcher that does not perform any fetching.
32#[derive(Default)]
33pub struct NoForeignFetcher;
34
35impl ForeignFetcher for NoForeignFetcher {
36    fn fetch(
37        &self,
38        _database: &impl Database,
39        _table: &str,
40        _local_column: &'static str,
41        _pk_value: Value,
42    ) -> IcDbmsResult<TableColumns> {
43        unimplemented!("NoForeignFetcher should have a table without foreign keys");
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    #[should_panic(expected = "NoForeignFetcher should have a table without foreign keys")]
53    fn test_no_foreign_fetcher() {
54        let fetcher = NoForeignFetcher;
55        let _ = fetcher.fetch(
56            &MockDatabase,
57            "some_table",
58            "some_column",
59            Value::Uint32(1.into()),
60        );
61    }
62
63    struct MockDatabase;
64
65    impl Database for MockDatabase {
66        fn select<T>(&self, _query: crate::prelude::Query<T>) -> IcDbmsResult<Vec<T::Record>>
67        where
68            T: crate::prelude::TableSchema,
69        {
70            unimplemented!()
71        }
72
73        fn insert<T>(&self, _record: T::Insert) -> IcDbmsResult<()>
74        where
75            T: crate::prelude::TableSchema,
76            T::Insert: crate::prelude::InsertRecord<Schema = T>,
77        {
78            unimplemented!()
79        }
80
81        fn update<T>(&self, _patch: T::Update) -> IcDbmsResult<u64>
82        where
83            T: crate::prelude::TableSchema,
84            T::Update: crate::prelude::UpdateRecord<Schema = T>,
85        {
86            unimplemented!()
87        }
88
89        fn delete<T>(
90            &self,
91            _behaviour: crate::prelude::DeleteBehavior,
92            _filter: Option<crate::prelude::Filter>,
93        ) -> IcDbmsResult<u64>
94        where
95            T: crate::prelude::TableSchema,
96        {
97            unimplemented!()
98        }
99
100        fn commit(&mut self) -> IcDbmsResult<()> {
101            unimplemented!()
102        }
103
104        fn rollback(&mut self) -> IcDbmsResult<()> {
105            unimplemented!()
106        }
107    }
108}