sqlint/ast/
column.rs

1use super::Aliasable;
2use crate::{
3    ast::{Expression, ExpressionKind, Table},
4    Value,
5};
6use std::borrow::Cow;
7
8#[derive(Debug, Clone, Copy)]
9pub enum TypeDataLength {
10    Constant(u16),
11    Maximum,
12}
13
14#[derive(Debug, Clone, Copy)]
15pub enum TypeFamily {
16    Text(Option<TypeDataLength>),
17    Int,
18    Float,
19    Double,
20    Boolean,
21    Uuid,
22    DateTime,
23    Decimal(Option<(u8, u8)>),
24    Bytes(Option<TypeDataLength>),
25}
26
27/// A column definition.
28#[derive(Clone, Debug, Default)]
29pub struct Column<'a> {
30    pub name: Cow<'a, str>,
31    pub(crate) table: Option<Table<'a>>,
32    pub(crate) alias: Option<Cow<'a, str>>,
33    pub(crate) default: Option<DefaultValue<'a>>,
34    pub(crate) type_family: Option<TypeFamily>,
35}
36
37/// Defines a default value for a `Column`.
38#[derive(Clone, Debug, PartialEq)]
39pub enum DefaultValue<'a> {
40    /// A static value.
41    Provided(Value<'a>),
42    /// Generated in the database.
43    Generated,
44}
45
46impl<'a> Default for DefaultValue<'a> {
47    fn default() -> Self {
48        Self::Generated
49    }
50}
51
52impl<'a, V> From<V> for DefaultValue<'a>
53where
54    V: Into<Value<'a>>,
55{
56    fn from(v: V) -> Self {
57        Self::Provided(v.into())
58    }
59}
60
61impl<'a> PartialEq for Column<'a> {
62    fn eq(&self, other: &Column) -> bool {
63        self.name == other.name && self.table == other.table
64    }
65}
66
67impl<'a> Column<'a> {
68    /// Create a bare version of the column, stripping out all other information
69    /// other than the name.
70    pub(crate) fn into_bare(self) -> Self {
71        Self { name: self.name, ..Default::default() }
72    }
73
74    /// Sets the default value for the column.
75    pub fn default<V>(mut self, value: V) -> Self
76    where
77        V: Into<DefaultValue<'a>>,
78    {
79        self.default = Some(value.into());
80        self
81    }
82
83    /// Sets a type family, used mainly for SQL Server `OUTPUT` hack.
84    pub fn type_family(mut self, type_family: TypeFamily) -> Self {
85        self.type_family = Some(type_family);
86        self
87    }
88
89    /// True when the default value is set and automatically generated in the
90    /// database.
91    pub fn default_autogen(&self) -> bool {
92        self.default.as_ref().map(|d| d == &DefaultValue::Generated).unwrap_or(false)
93    }
94}
95
96impl<'a> From<Column<'a>> for Expression<'a> {
97    fn from(col: Column<'a>) -> Self {
98        Expression { kind: ExpressionKind::Column(Box::new(col)), alias: None }
99    }
100}
101
102impl<'a> Column<'a> {
103    /// Create a column definition.
104    pub fn new<S>(name: S) -> Self
105    where
106        S: Into<Cow<'a, str>>,
107    {
108        Column { name: name.into(), ..Default::default() }
109    }
110
111    /// Include the table name in the column expression.
112    pub fn table<T>(mut self, table: T) -> Self
113    where
114        T: Into<Table<'a>>,
115    {
116        self.table = Some(table.into());
117        self
118    }
119
120    /// Include the table name in the column expression, if table is defined.
121    pub fn opt_table<T>(mut self, table: Option<T>) -> Self
122    where
123        T: Into<Table<'a>>,
124    {
125        if let Some(table) = table {
126            self.table = Some(table.into());
127        }
128
129        self
130    }
131}
132
133impl<'a> Aliasable<'a> for Column<'a> {
134    type Target = Column<'a>;
135
136    fn alias<T>(mut self, alias: T) -> Self::Target
137    where
138        T: Into<Cow<'a, str>>,
139    {
140        self.alias = Some(alias.into());
141        self
142    }
143}
144
145impl<'a> From<&'a str> for Column<'a> {
146    fn from(s: &'a str) -> Self {
147        Column { name: s.into(), ..Default::default() }
148    }
149}
150
151impl<'a, 'b> From<&'a &'b str> for Column<'b> {
152    fn from(s: &'a &'b str) -> Self {
153        Column::from(*s)
154    }
155}
156
157impl<'a> From<String> for Column<'a> {
158    fn from(s: String) -> Self {
159        Column { name: s.into(), ..Default::default() }
160    }
161}
162
163impl<'a, T, C> From<(T, C)> for Column<'a>
164where
165    T: Into<Table<'a>>,
166    C: Into<Column<'a>>,
167{
168    fn from(t: (T, C)) -> Column<'a> {
169        let mut column: Column<'a> = t.1.into();
170        column = column.table(t.0);
171
172        column
173    }
174}