1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use super::{values::NativeColumnType, Aliasable};
use crate::{
    ast::{Expression, ExpressionKind, Table},
    Value,
};
use std::borrow::Cow;

#[derive(Debug, Clone, Copy)]
pub enum TypeDataLength {
    Constant(u16),
    Maximum,
}

#[derive(Debug, Clone, Copy)]
pub enum TypeFamily {
    Text(Option<TypeDataLength>),
    Int,
    Float,
    Double,
    Boolean,
    Uuid,
    DateTime,
    Decimal(Option<(u8, u8)>),
    Bytes(Option<TypeDataLength>),
}

/// A column definition.
#[derive(Clone, Debug, Default)]
pub struct Column<'a> {
    pub name: Cow<'a, str>,
    pub(crate) table: Option<Table<'a>>,
    pub(crate) alias: Option<Cow<'a, str>>,
    pub(crate) default: Option<DefaultValue<'a>>,
    pub(crate) type_family: Option<TypeFamily>,
    /// The underlying native type of the column.
    pub(crate) native_type: Option<NativeColumnType<'a>>,
    /// Whether the column is an enum.
    pub(crate) is_enum: bool,
    /// Whether the column is a (scalar) list.
    pub(crate) is_list: bool,
    /// Whether the column is part of a SELECT or RETURNING clause.
    pub(crate) is_selected: bool,
}

/// Defines a default value for a `Column`.
#[derive(Clone, Debug, PartialEq)]
pub enum DefaultValue<'a> {
    /// A static value.
    Provided(Value<'a>),
    /// Generated in the database.
    Generated,
}

impl<'a> Default for DefaultValue<'a> {
    fn default() -> Self {
        Self::Generated
    }
}

impl<'a, V> From<V> for DefaultValue<'a>
where
    V: Into<Value<'a>>,
{
    fn from(v: V) -> Self {
        Self::Provided(v.into())
    }
}

impl<'a> PartialEq for Column<'a> {
    fn eq(&self, other: &Column) -> bool {
        self.name == other.name && self.table == other.table
    }
}

impl<'a> Column<'a> {
    /// Create a bare version of the column, stripping out all other information
    /// other than the name.
    pub(crate) fn into_bare(self) -> Self {
        Self {
            name: self.name,
            ..Default::default()
        }
    }

    /// Sets the default value for the column.
    pub fn default<V>(mut self, value: V) -> Self
    where
        V: Into<DefaultValue<'a>>,
    {
        self.default = Some(value.into());
        self
    }

    /// Sets a type family, used mainly for SQL Server `OUTPUT` hack.
    pub fn type_family(mut self, type_family: TypeFamily) -> Self {
        self.type_family = Some(type_family);
        self
    }

    /// Sets whether the column points to an enum type.
    pub fn set_is_enum(mut self, is_enum: bool) -> Self {
        self.is_enum = is_enum;
        self
    }

    /// Sets whether the column points to an scalar list.
    pub fn set_is_list(mut self, is_list: bool) -> Self {
        self.is_list = is_list;
        self
    }

    /// Sets whether the column is selected.
    ///
    /// On Postgres, this defines whether an enum column should be casted to `TEXT` when rendered.
    ///
    /// Since enums are user-defined custom types, `tokio-postgres` fires an additional query
    /// when selecting columns of type enum to know which custom type the column refers to.
    /// Casting the enum column to `TEXT` avoid this roundtrip since `TEXT` is a builtin type.
    ///
    /// We don't want to cast every single enum columns to text though, as this would prevent indexes from being used,
    /// so we use this additional field to granularly pick which columns we cast.
    pub fn set_is_selected(mut self, is_selected: bool) -> Self {
        self.is_selected = is_selected;
        self
    }

    /// True when the default value is set and automatically generated in the
    /// database.
    pub fn default_autogen(&self) -> bool {
        self.default
            .as_ref()
            .map(|d| d == &DefaultValue::Generated)
            .unwrap_or(false)
    }

    pub fn native_column_type<T: Into<NativeColumnType<'a>>>(mut self, native_type: Option<T>) -> Column<'a> {
        self.native_type = native_type.map(|nt| nt.into());
        self
    }
}

impl<'a> From<Column<'a>> for Expression<'a> {
    fn from(col: Column<'a>) -> Self {
        Expression {
            kind: ExpressionKind::Column(Box::new(col)),
            alias: None,
        }
    }
}

impl<'a> Column<'a> {
    /// Create a column definition.
    pub fn new<S>(name: S) -> Self
    where
        S: Into<Cow<'a, str>>,
    {
        Column {
            name: name.into(),
            ..Default::default()
        }
    }

    /// Include the table name in the column expression.
    pub fn table<T>(mut self, table: T) -> Self
    where
        T: Into<Table<'a>>,
    {
        self.table = Some(table.into());
        self
    }

    /// Include the table name in the column expression, if table is defined.
    pub fn opt_table<T>(mut self, table: Option<T>) -> Self
    where
        T: Into<Table<'a>>,
    {
        if let Some(table) = table {
            self.table = Some(table.into());
        }

        self
    }
}

impl<'a> Aliasable<'a> for Column<'a> {
    type Target = Column<'a>;

    fn alias<T>(mut self, alias: T) -> Self::Target
    where
        T: Into<Cow<'a, str>>,
    {
        self.alias = Some(alias.into());
        self
    }
}

impl<'a> From<&'a str> for Column<'a> {
    fn from(s: &'a str) -> Self {
        Column {
            name: s.into(),
            ..Default::default()
        }
    }
}

impl<'a, 'b> From<&'a &'b str> for Column<'b> {
    fn from(s: &'a &'b str) -> Self {
        Column::from(*s)
    }
}

impl<'a> From<String> for Column<'a> {
    fn from(s: String) -> Self {
        Column {
            name: s.into(),
            ..Default::default()
        }
    }
}

impl<'a, T, C> From<(T, C)> for Column<'a>
where
    T: Into<Table<'a>>,
    C: Into<Column<'a>>,
{
    fn from(t: (T, C)) -> Column<'a> {
        let mut column: Column<'a> = t.1.into();
        column = column.table(t.0);

        column
    }
}