Skip to main content

sql_fun_sqlast/sem/create_table/
column_def.rs

1use crate::sem::{ColumnName, SemScalarExpr, TypeReference};
2
3/// column definitions
4#[derive(Debug, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
5pub struct ColumnDefinition {
6    name: Option<ColumnName>,
7    type_ref: Option<TypeReference>,
8    is_not_null: Option<bool>,
9    default: Option<Box<SemScalarExpr>>,
10}
11
12impl Clone for ColumnDefinition {
13    fn clone(&self) -> Self {
14        Self {
15            name: self.name.clone(),
16            type_ref: self.type_ref.clone(),
17            is_not_null: self.is_not_null,
18            default: Default::default(),
19        }
20    }
21}
22
23impl ColumnDefinition {
24    /// create instance
25    #[must_use]
26    pub fn new(
27        name: &Option<ColumnName>,
28        type_ref: Option<&TypeReference>,
29        is_not_null: Option<bool>,
30    ) -> Self {
31        Self {
32            name: name.clone(),
33            type_ref: type_ref.cloned(),
34            is_not_null,
35            default: Default::default(),
36        }
37    }
38
39    /// get name of column
40    #[must_use]
41    pub fn name(&self) -> &Option<ColumnName> {
42        &self.name
43    }
44
45    /// get [`TypeReference`]
46    #[must_use]
47    pub fn get_type(&self) -> Option<TypeReference> {
48        self.type_ref.clone()
49    }
50
51    /// column nullability
52    #[must_use]
53    pub fn is_not_null(&self) -> Option<bool> {
54        self.is_not_null
55    }
56
57    /// apply default expression to column
58    pub fn set_default(&mut self, scalar_expr: &SemScalarExpr) {
59        self.default = Some(Box::new(scalar_expr.clone()));
60    }
61}