feophantlib/engine/objects/
table.rs

1//! Postgres doc: https://www.postgresql.org/docs/current/catalog-pg-class.html
2
3use std::sync::Arc;
4
5use super::{types::SqlTypeDefinition, Attribute, Constraint, Index};
6use thiserror::Error;
7use uuid::Uuid;
8
9#[derive(Clone, Debug, PartialEq)]
10pub struct Table {
11    pub id: Uuid,
12    pub name: String,
13    pub attributes: Vec<Attribute>,
14    pub constraints: Vec<Constraint>,
15    pub indexes: Vec<Arc<Index>>,
16    pub sql_type: Arc<SqlTypeDefinition>,
17}
18
19impl Table {
20    pub fn new(
21        id: Uuid,
22        name: String,
23        attributes: Vec<Attribute>,
24        constraints: Vec<Constraint>,
25        indexes: Vec<Arc<Index>>,
26    ) -> Table {
27        let sql_type = Arc::new(SqlTypeDefinition::new(&attributes));
28        Table {
29            id,
30            name,
31            attributes,
32            constraints,
33            indexes,
34            sql_type,
35        }
36    }
37
38    //TODO might not be need any more with the type
39    pub fn get_column_index(&self, name: &str) -> Result<usize, TableError> {
40        for i in 0..self.attributes.len() {
41            if self.attributes[i].name == name {
42                return Ok(i);
43            }
44        }
45
46        Err(TableError::ColumnDoesNotExist(name.to_string()))
47    }
48}
49
50#[derive(Error, Debug)]
51pub enum TableError {
52    #[error("Column named {0} does not exist")]
53    ColumnDoesNotExist(String),
54}