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
use crate::prelude::*;

#[derive(Debug)]
pub struct Column {
    name: String,
    alias: Option<String>,
}

impl Column {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            alias: None,
        }
    }

    pub fn with_alias(mut self, alias: impl Into<String>) -> Self {
        self.alias = Some(alias.into());
        self
    }
}

impl Sql for Column {
    fn sql(&self, mut s: String, _ctx: &Context) -> Result<String> {
        s.push_str(&self.name);

        if let Some(alias) = &self.alias {
            s.push_str(" AS ");
            s.push_str(alias)
        }

        Ok(s)
    }
}

impl From<&str> for Column {
    fn from(value: &str) -> Self {
        Column::new(value)
    }
}