otter_sql/
schema.rs

1//! Schemas (as in a namespace for tables, not a database schema).
2
3use crate::{vm::TableIndex, BoundedString};
4
5/// A namespace in a database.
6///
7/// The schema only holds a reference to the actual tables, which are owned by the VM.
8#[derive(Debug)]
9pub struct Schema {
10    name: BoundedString,
11    tables: Vec<TableIndex>,
12}
13
14impl Schema {
15    pub fn new(name: BoundedString) -> Self {
16        Self {
17            name,
18            tables: Vec::new(),
19        }
20    }
21
22    /// The name of the schema.
23    pub fn name(&self) -> &BoundedString {
24        &self.name
25    }
26
27    /// All the tables in the schema.
28    pub fn tables(&self) -> &Vec<TableIndex> {
29        &self.tables
30    }
31
32    /// Add a new table to the schema.
33    pub fn add_table(&mut self, table: TableIndex) -> &mut Self {
34        self.tables.push(table);
35        self
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use crate::vm::TableIndex;
42
43    use super::Schema;
44
45    #[test]
46    fn create_schema() {
47        let mut schema = Schema::new("test".into());
48        assert_eq!(schema.name(), "test");
49        assert_eq!(schema.tables().len(), 0);
50
51        schema.add_table(TableIndex::default());
52        assert_eq!(schema.tables().len(), 1);
53        assert_eq!(schema.tables()[0], TableIndex::default());
54        assert_ne!(schema.tables()[0], TableIndex::default().next_index());
55    }
56}