Skip to main content

gluesql_core/
store.rs

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