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
//! Columns in a table.
use sqlparser::ast::{ColumnOptionDef, DataType};

use crate::BoundedString;

/// A column's metadata.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Column {
    name: BoundedString,
    data_type: DataType,
    options: Vec<ColumnOptionDef>,
    /// Whether this is a hidden, internal column.
    internal: bool,
}

impl Column {
    pub fn new(
        name: BoundedString,
        data_type: DataType,
        options: Vec<ColumnOptionDef>,
        internal: bool,
    ) -> Self {
        Self {
            name,
            data_type,
            options,
            internal,
        }
    }

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

    /// Data type of the column.
    pub fn data_type(&self) -> &DataType {
        &self.data_type
    }

    /// Column's options (attributes, constraints, etc.).
    pub fn options(&self) -> &Vec<ColumnOptionDef> {
        &self.options
    }

    /// Add a new column option.
    pub fn add_column_option(&mut self, option: ColumnOptionDef) {
        self.options.push(option)
    }

    /// Whether the column is a hidden, internal-only column.
    pub fn is_internal(&self) -> bool {
        self.internal
    }
}

#[cfg(test)]
mod tests {
    use sqlparser::ast::{ColumnOption, ColumnOptionDef, DataType};

    use super::Column;

    #[test]
    fn create_column() {
        let column = Column::new(
            "test".into(),
            DataType::Int(None),
            vec![ColumnOptionDef {
                name: None,
                option: ColumnOption::NotNull,
            }],
            false,
        );

        assert_eq!(column.name(), "test");
        assert_eq!(column.data_type(), &DataType::Int(None));
        assert_eq!(
            column.options(),
            &vec![ColumnOptionDef {
                name: None,
                option: ColumnOption::NotNull,
            }],
        );
    }
}