quaint_forked/ast/
column.rs1use 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#[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#[derive(Clone, Debug, PartialEq)]
39pub enum DefaultValue<'a> {
40 Provided(Value<'a>),
42 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 pub(crate) fn into_bare(self) -> Self {
71 Self {
72 name: self.name,
73 ..Default::default()
74 }
75 }
76
77 pub fn default<V>(mut self, value: V) -> Self
79 where
80 V: Into<DefaultValue<'a>>,
81 {
82 self.default = Some(value.into());
83 self
84 }
85
86 pub fn type_family(mut self, type_family: TypeFamily) -> Self {
88 self.type_family = Some(type_family);
89 self
90 }
91
92 pub fn default_autogen(&self) -> bool {
95 self.default
96 .as_ref()
97 .map(|d| d == &DefaultValue::Generated)
98 .unwrap_or(false)
99 }
100}
101
102impl<'a> From<Column<'a>> for Expression<'a> {
103 fn from(col: Column<'a>) -> Self {
104 Expression {
105 kind: ExpressionKind::Column(Box::new(col)),
106 alias: None,
107 }
108 }
109}
110
111impl<'a> Column<'a> {
112 pub fn new<S>(name: S) -> Self
114 where
115 S: Into<Cow<'a, str>>,
116 {
117 Column {
118 name: name.into(),
119 ..Default::default()
120 }
121 }
122
123 pub fn table<T>(mut self, table: T) -> Self
125 where
126 T: Into<Table<'a>>,
127 {
128 self.table = Some(table.into());
129 self
130 }
131
132 pub fn opt_table<T>(mut self, table: Option<T>) -> Self
134 where
135 T: Into<Table<'a>>,
136 {
137 if let Some(table) = table {
138 self.table = Some(table.into());
139 }
140
141 self
142 }
143}
144
145impl<'a> Aliasable<'a> for Column<'a> {
146 type Target = Column<'a>;
147
148 fn alias<T>(mut self, alias: T) -> Self::Target
149 where
150 T: Into<Cow<'a, str>>,
151 {
152 self.alias = Some(alias.into());
153 self
154 }
155}
156
157impl<'a> From<&'a str> for Column<'a> {
158 fn from(s: &'a str) -> Self {
159 Column {
160 name: s.into(),
161 ..Default::default()
162 }
163 }
164}
165
166impl<'a, 'b> From<&'a &'b str> for Column<'b> {
167 fn from(s: &'a &'b str) -> Self {
168 Column::from(*s)
169 }
170}
171
172impl<'a> From<String> for Column<'a> {
173 fn from(s: String) -> Self {
174 Column {
175 name: s.into(),
176 ..Default::default()
177 }
178 }
179}
180
181impl<'a, T, C> From<(T, C)> for Column<'a>
182where
183 T: Into<Table<'a>>,
184 C: Into<Column<'a>>,
185{
186 fn from(t: (T, C)) -> Column<'a> {
187 let mut column: Column<'a> = t.1.into();
188 column = column.table(t.0);
189
190 column
191 }
192}