grafbase_sql_ast/ast/
column.rs1use super::Aliasable;
2use crate::ast::{Expression, ExpressionKind, Table};
3use std::borrow::Cow;
4
5#[derive(Debug, Clone, Copy)]
6pub enum TypeDataLength {
7 Constant(u16),
8 Maximum,
9}
10
11#[derive(Clone, Debug, Default)]
13pub struct Column<'a> {
14 pub name: Cow<'a, str>,
15 pub(crate) table: Option<Table<'a>>,
16 pub(crate) alias: Option<Cow<'a, str>>,
17}
18
19impl<'a> PartialEq for Column<'a> {
21 fn eq(&self, other: &Column) -> bool {
22 self.name == other.name && self.table == other.table
23 }
24}
25
26impl<'a> Column<'a> {
27 pub fn into_bare(self) -> Self {
30 Self {
31 name: self.name,
32 ..Default::default()
33 }
34 }
35}
36
37impl<'a> From<Column<'a>> for Expression<'a> {
38 fn from(col: Column<'a>) -> Self {
39 Expression {
40 kind: ExpressionKind::Column(Box::new(col)),
41 alias: None,
42 }
43 }
44}
45
46impl<'a> Column<'a> {
47 pub fn new<S>(name: S) -> Self
49 where
50 S: Into<Cow<'a, str>>,
51 {
52 Column {
53 name: name.into(),
54 ..Default::default()
55 }
56 }
57
58 pub fn table<T>(mut self, table: T) -> Self
60 where
61 T: Into<Table<'a>>,
62 {
63 self.table = Some(table.into());
64 self
65 }
66
67 pub fn opt_table<T>(mut self, table: Option<T>) -> Self
69 where
70 T: Into<Table<'a>>,
71 {
72 if let Some(table) = table {
73 self.table = Some(table.into());
74 }
75
76 self
77 }
78}
79
80impl<'a> Aliasable<'a> for Column<'a> {
81 type Target = Column<'a>;
82
83 fn alias<T>(mut self, alias: T) -> Self::Target
84 where
85 T: Into<Cow<'a, str>>,
86 {
87 self.alias = Some(alias.into());
88 self
89 }
90}
91
92impl<'a> From<&'a str> for Column<'a> {
93 fn from(s: &'a str) -> Self {
94 Column {
95 name: s.into(),
96 ..Default::default()
97 }
98 }
99}
100
101impl<'a, 'b> From<&'a &'b str> for Column<'b> {
102 fn from(s: &'a &'b str) -> Self {
103 Column::from(*s)
104 }
105}
106
107impl<'a> From<String> for Column<'a> {
108 fn from(s: String) -> Self {
109 Column {
110 name: s.into(),
111 ..Default::default()
112 }
113 }
114}
115
116impl<'a, T, C> From<(T, C)> for Column<'a>
117where
118 T: Into<Table<'a>>,
119 C: Into<Column<'a>>,
120{
121 fn from(t: (T, C)) -> Column<'a> {
122 let mut column: Column<'a> = t.1.into();
123 column = column.table(t.0);
124
125 column
126 }
127}