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
use crate::ast::{
    Column,
    Function,
    Operator,
    Value,
};
use sql_ast::ast as sql;

//TODO: Should be able to do math operations
// such as: *, +, -, /, %
#[derive(Debug, PartialEq, Clone)]
pub enum Expr {
    Column(Column),
    Function(Function),
    Value(Value),
    BinaryOperation(Box<BinaryOperation>),
    /// The expressions is explicitly
    /// grouped in a parenthesis
    Nested(Box<Expr>),
}

#[derive(Debug, PartialEq, Clone)]
pub struct ExprRename {
    pub expr: Expr,
    pub rename: Option<String>,
}

#[derive(Debug, PartialEq, Clone)]
pub struct BinaryOperation {
    pub left: Expr,
    pub operator: Operator,
    pub right: Expr,
}

impl Into<sql::Expr> for &Expr {
    fn into(self) -> sql::Expr {
        match self {
            Expr::Column(column) => {
                sql::Expr::Identifier(sql::Ident::new(&column.name))
            }
            Expr::Function(function) => {
                sql::Expr::Function(Into::into(function))
            }
            Expr::Value(value) => sql::Expr::Value(Into::into(value)),
            Expr::BinaryOperation(binop) => {
                sql::Expr::BinaryOp {
                    left: Box::new(Into::into(&binop.left)),
                    op: Into::into(&binop.operator),
                    right: Box::new(Into::into(&binop.right)),
                }
            }
            Expr::Nested(expr) => {
                sql::Expr::Nested(Box::new(Into::into(expr.as_ref())))
            }
        }
    }
}