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
//! Schemas (as in a namespace for tables, not a database schema).

use crate::{vm::TableIndex, BoundedString};

/// A namespace in a database.
///
/// The schema only holds a reference to the actual tables, which are owned by the VM.
#[derive(Debug)]
pub struct Schema {
    name: BoundedString,
    tables: Vec<TableIndex>,
}

impl Schema {
    pub fn new(name: BoundedString) -> Self {
        Self {
            name,
            tables: Vec::new(),
        }
    }

    /// The name of the schema.
    pub fn name(&self) -> &BoundedString {
        &self.name
    }

    /// All the tables in the schema.
    pub fn tables(&self) -> &Vec<TableIndex> {
        &self.tables
    }

    /// Add a new table to the schema.
    pub fn add_table(&mut self, table: TableIndex) -> &mut Self {
        self.tables.push(table);
        self
    }
}

#[cfg(test)]
mod tests {
    use crate::vm::TableIndex;

    use super::Schema;

    #[test]
    fn create_schema() {
        let mut schema = Schema::new("test".into());
        assert_eq!(schema.name(), "test");
        assert_eq!(schema.tables().len(), 0);

        schema.add_table(TableIndex::default());
        assert_eq!(schema.tables().len(), 1);
        assert_eq!(schema.tables()[0], TableIndex::default());
        assert_ne!(schema.tables()[0], TableIndex::default().next_index());
    }
}