gluesql_core/
store.rs

1mod alter_table;
2mod data_row;
3mod function;
4mod index;
5mod metadata;
6mod planner;
7mod transaction;
8
9pub trait GStore: Store + Index + Metadata + CustomFunction {}
10impl<S: Store + Index + Metadata + CustomFunction> GStore for S {}
11
12pub trait GStoreMut:
13    StoreMut + IndexMut + AlterTable + Transaction + CustomFunction + CustomFunctionMut
14{
15}
16impl<S: StoreMut + IndexMut + AlterTable + Transaction + CustomFunction + CustomFunctionMut>
17    GStoreMut for S
18{
19}
20
21pub use {
22    alter_table::{AlterTable, AlterTableError},
23    data_row::DataRow,
24    function::{CustomFunction, CustomFunctionMut},
25    index::{Index, IndexError, IndexMut},
26    metadata::{MetaIter, Metadata},
27    planner::Planner,
28    transaction::Transaction,
29};
30
31use {
32    crate::{
33        data::{Key, Schema},
34        executor::Referencing,
35        result::{Error, Result},
36    },
37    async_trait::async_trait,
38    futures::stream::Stream,
39    std::pin::Pin,
40};
41
42pub type RowIter<'a> = Pin<Box<dyn Stream<Item = Result<(Key, DataRow)>> + Send + 'a>>;
43
44/// By implementing `Store` trait, you can run `SELECT` query.
45#[async_trait]
46pub trait Store: Send + Sync {
47    async fn fetch_schema(&self, table_name: &str) -> Result<Option<Schema>>;
48
49    async fn fetch_all_schemas(&self) -> Result<Vec<Schema>>;
50
51    async fn fetch_data(&self, table_name: &str, key: &Key) -> Result<Option<DataRow>>;
52
53    async fn scan_data<'a>(&'a self, table_name: &str) -> Result<RowIter<'a>>;
54
55    async fn fetch_referencings(&self, table_name: &str) -> Result<Vec<Referencing>> {
56        let schemas = self.fetch_all_schemas().await?;
57
58        Ok(schemas
59            .into_iter()
60            .flat_map(|schema| {
61                let Schema {
62                    table_name: referencing_table_name,
63                    foreign_keys,
64                    ..
65                } = schema;
66
67                foreign_keys.into_iter().filter_map(move |foreign_key| {
68                    (foreign_key.referenced_table_name == table_name
69                        && referencing_table_name != table_name)
70                        .then_some(Referencing {
71                            table_name: referencing_table_name.clone(),
72                            foreign_key,
73                        })
74                })
75            })
76            .collect())
77    }
78}
79
80/// By implementing `StoreMut` trait,
81/// you can run `INSERT`, `CREATE TABLE`, `DELETE`, `UPDATE` and `DROP TABLE` queries.
82#[async_trait]
83pub trait StoreMut: Send + Sync {
84    async fn insert_schema(&mut self, _schema: &Schema) -> Result<()> {
85        let msg = "[Storage] StoreMut::insert_schema is not supported".to_owned();
86
87        Err(Error::StorageMsg(msg))
88    }
89
90    async fn delete_schema(&mut self, _table_name: &str) -> Result<()> {
91        let msg = "[Storage] StoreMut::delete_schema is not supported".to_owned();
92
93        Err(Error::StorageMsg(msg))
94    }
95
96    async fn append_data(&mut self, _table_name: &str, _rows: Vec<DataRow>) -> Result<()> {
97        let msg = "[Storage] StoreMut::append_data is not supported".to_owned();
98
99        Err(Error::StorageMsg(msg))
100    }
101
102    async fn insert_data(&mut self, _table_name: &str, _rows: Vec<(Key, DataRow)>) -> Result<()> {
103        let msg = "[Storage] StoreMut::insert_data is not supported".to_owned();
104
105        Err(Error::StorageMsg(msg))
106    }
107
108    async fn delete_data(&mut self, _table_name: &str, _keys: Vec<Key>) -> Result<()> {
109        let msg = "[Storage] StoreMut::delete_data is not supported".to_owned();
110
111        Err(Error::StorageMsg(msg))
112    }
113}