otter_sql/
column.rs

1//! Columns in a table.
2use sqlparser::ast::{ColumnOptionDef, DataType};
3
4use crate::BoundedString;
5
6/// A column's metadata.
7#[derive(Debug, Clone, PartialEq, Eq, Hash)]
8pub struct Column {
9    name: BoundedString,
10    data_type: DataType,
11    options: Vec<ColumnOptionDef>,
12    /// Whether this is a hidden, internal column.
13    internal: bool,
14}
15
16impl Column {
17    pub fn new(
18        name: BoundedString,
19        data_type: DataType,
20        options: Vec<ColumnOptionDef>,
21        internal: bool,
22    ) -> Self {
23        Self {
24            name,
25            data_type,
26            options,
27            internal,
28        }
29    }
30
31    /// Name of the column.
32    pub fn name(&self) -> &BoundedString {
33        &self.name
34    }
35
36    /// Data type of the column.
37    pub fn data_type(&self) -> &DataType {
38        &self.data_type
39    }
40
41    /// Column's options (attributes, constraints, etc.).
42    pub fn options(&self) -> &Vec<ColumnOptionDef> {
43        &self.options
44    }
45
46    /// Add a new column option.
47    pub fn add_column_option(&mut self, option: ColumnOptionDef) {
48        self.options.push(option)
49    }
50
51    /// Whether the column is a hidden, internal-only column.
52    pub fn is_internal(&self) -> bool {
53        self.internal
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use sqlparser::ast::{ColumnOption, ColumnOptionDef, DataType};
60
61    use super::Column;
62
63    #[test]
64    fn create_column() {
65        let column = Column::new(
66            "test".into(),
67            DataType::Int(None),
68            vec![ColumnOptionDef {
69                name: None,
70                option: ColumnOption::NotNull,
71            }],
72            false,
73        );
74
75        assert_eq!(column.name(), "test");
76        assert_eq!(column.data_type(), &DataType::Int(None));
77        assert_eq!(
78            column.options(),
79            &vec![ColumnOptionDef {
80                name: None,
81                option: ColumnOption::NotNull,
82            }],
83        );
84    }
85}