1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use cfg_if::cfg_if;

cfg_if! {
    if #[cfg(feature = "alter-table")] {
        mod alter_table;
        pub use alter_table::{AlterTable, AlterTableError};
    }
}

cfg_if! {
    if #[cfg(feature = "index")] {
        mod index;
        pub use index::{Index, IndexError, IndexMut};
    }
}

cfg_if! {
    if #[cfg(feature = "transaction")] {
        mod transaction;
        pub use transaction::Transaction;
    }
}

cfg_if! {
    if #[cfg(feature = "index")] {
        pub trait GStore: Store + Index {}
        impl<S: Store + Index> GStore for S {}
    } else {
        pub trait GStore: Store {}
        impl<S: Store> GStore for S {}
    }
}

cfg_if! {
    if #[cfg(all(feature = "alter-table", feature = "index", feature = "transaction"))] {
        pub trait GStoreMut: StoreMut + IndexMut + AlterTable + Transaction {}
        impl<S: StoreMut + IndexMut + AlterTable+ Transaction> GStoreMut for S {}
    } else if #[cfg(all(feature = "alter-table", feature = "index"))] {
        pub trait GStoreMut: StoreMut + IndexMut + AlterTable {}
        impl<S: StoreMut + IndexMut + AlterTable> GStoreMut for S {}
    } else if #[cfg(all(feature = "alter-table", feature = "transaction"))] {
        pub trait GStoreMut: StoreMut + Transaction + AlterTable {}
        impl<S: StoreMut + Transaction + AlterTable> GStoreMut for S {}
    } else if #[cfg(all(feature = "index", feature = "transaction"))] {
        pub trait GStoreMut: StoreMut + IndexMut + Transaction {}
        impl<S: StoreMut + IndexMut + Transaction> GStoreMut for S {}
    } else if #[cfg(feature = "alter-table")] {
        pub trait GStoreMut: StoreMut + AlterTable {}
        impl<S: StoreMut+ AlterTable> GStoreMut for S {}
    } else if #[cfg(feature = "index")] {
        pub trait GStoreMut: StoreMut + IndexMut {}
        impl<S: StoreMut + IndexMut> GStoreMut for S {}
    } else if #[cfg(feature = "transaction")] {
        pub trait GStoreMut: StoreMut + Transaction {}
        impl<S: StoreMut + Transaction> GStoreMut for S {}
    } else {
        pub trait GStoreMut: StoreMut {}
        impl<S: StoreMut> GStoreMut for S {}
    }
}

use {
    crate::{
        data::{Key, Row, Schema},
        result::{MutResult, Result},
    },
    async_trait::async_trait,
};

pub type RowIter = Box<dyn Iterator<Item = Result<(Key, Row)>>>;

/// By implementing `Store` trait, you can run `SELECT` query.
#[async_trait(?Send)]
pub trait Store {
    async fn fetch_schema(&self, table_name: &str) -> Result<Option<Schema>>;

    async fn fetch_all_schemas(&self) -> Result<Vec<Schema>>;

    async fn fetch_data(&self, table_name: &str, key: &Key) -> Result<Option<Row>>;

    async fn scan_data(&self, table_name: &str) -> Result<RowIter>;
}

/// By implementing `StoreMut` trait,
/// you can run `INSERT`, `CREATE TABLE`, `DELETE`, `UPDATE` and `DROP TABLE` queries.
#[async_trait(?Send)]
pub trait StoreMut
where
    Self: Sized,
{
    async fn insert_schema(self, schema: &Schema) -> MutResult<Self, ()>;

    async fn delete_schema(self, table_name: &str) -> MutResult<Self, ()>;

    async fn append_data(self, table_name: &str, rows: Vec<Row>) -> MutResult<Self, ()>;

    async fn insert_data(self, table_name: &str, rows: Vec<(Key, Row)>) -> MutResult<Self, ()>;

    async fn delete_data(self, table_name: &str, keys: Vec<Key>) -> MutResult<Self, ()>;
}