sql_from_models_parser/ast/expression/
display.rs

1use super::*;
2
3impl fmt::Display for Expr {
4    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5        match self {
6            Expr::Identifier(s) => write!(f, "{}", s),
7            Expr::MapAccess(x) => write!(f, "{}", x),
8            Expr::Wildcard => f.write_str("*"),
9            Expr::QualifiedWildcard(q) => write!(f, "{}.*", display_separated(q, ".")),
10            Expr::CompoundIdentifier(s) => write!(f, "{}", display_separated(s, ".")),
11            Expr::IsNull(ast) => write!(f, "{} IS NULL", ast),
12            Expr::IsNotNull(ast) => write!(f, "{} IS NOT NULL", ast),
13            Expr::InList(x) => write!(f, "{}", x),
14            Expr::InSubquery(x) => write!(f, "{}", x),
15            Expr::Between(x) => write!(f, "{}", x),
16            Expr::BinaryOp(x) => write!(f, "{}", x),
17            Expr::UnaryOp(x) => write!(f, "{}", x),
18            Expr::Cast(x) => write!(f, "{}", x),
19            Expr::TryCast(x) => write!(f, "{}", x),
20            Expr::Extract(x) => write!(f, "{}", x),
21            Expr::Collate(x) => write!(f, "{}", x),
22            Expr::Nested(ast) => write!(f, "({})", ast),
23            Expr::Value(v) => write!(f, "{}", v),
24            Expr::TypedString(x) => write!(f, "{}", x),
25            Expr::Function(fun) => write!(f, "{}", fun),
26            Expr::Case(x) => write!(f, "{}", x),
27            Expr::Exists(s) => write!(f, "EXISTS ({})", s),
28            Expr::Subquery(s) => write!(f, "({})", s),
29            Expr::ListAgg(listagg) => write!(f, "{}", listagg),
30            Expr::Substring(x) => write!(f, "{}", x),
31            Expr::Trim(x) => write!(f, "{}", x),
32        }
33    }
34}
35
36impl fmt::Display for Trim {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        write!(f, "TRIM(")?;
39        if let Some((ref ident, ref trim_char)) = self.trim_where {
40            write!(f, "{} {} FROM {}", ident, trim_char, self.expr)?;
41        } else {
42            write!(f, "{}", self.expr)?;
43        }
44        write!(f, ")")
45    }
46}
47
48impl fmt::Display for Substring {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        write!(f, "SUBSTRING({}", self.expr)?;
51        if let Some(ref from_part) = self.substring_from {
52            write!(f, " FROM {}", from_part)?;
53        }
54        if let Some(ref from_part) = self.substring_for {
55            write!(f, " FOR {}", from_part)?;
56        }
57
58        write!(f, ")")
59    }
60}
61
62impl fmt::Display for Case {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        write!(f, "CASE")?;
65        if let Some(ref operand) = self.operand {
66            write!(f, " {}", operand)?;
67        }
68        for (c, r) in self.conditions.iter().zip(&self.results) {
69            write!(f, " WHEN {} THEN {}", c, r)?;
70        }
71
72        if let Some(ref else_result) = self.else_result {
73            write!(f, " ELSE {}", else_result)?;
74        }
75        write!(f, " END")
76    }
77}
78
79impl fmt::Display for TypedString {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        write!(f, "{}", self.data_type)?;
82        write!(f, " '{}'", &value::escape_single_quote_string(&self.value))
83    }
84}
85
86impl fmt::Display for Collate {
87    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88        write!(f, "{} COLLATE {}", self.expr, self.collation)
89    }
90}
91impl fmt::Display for UnaryOp {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        if self.op == UnaryOperator::PGPostfixFactorial {
94            write!(f, "{}{}", self.expr, self.op)
95        } else {
96            write!(f, "{} {}", self.op, self.expr)
97        }
98    }
99}
100impl fmt::Display for InSubquery {
101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102        write!(
103            f,
104            "{} {}IN ({})",
105            self.expr,
106            if self.negated { "NOT " } else { "" },
107            self.subquery
108        )
109    }
110}
111impl fmt::Display for Between {
112    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113        write!(
114            f,
115            "{} {}BETWEEN {} AND {}",
116            self.expr,
117            if self.negated { "NOT " } else { "" },
118            self.low,
119            self.high
120        )
121    }
122}
123impl fmt::Display for InList {
124    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125        write!(
126            f,
127            "{} {}IN ({})",
128            self.expr,
129            if self.negated { "NOT " } else { "" },
130            display_comma_separated(&self.list)
131        )
132    }
133}
134impl fmt::Display for BinaryOp {
135    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136        write!(f, "{} {} {}", self.left, self.op, self.right)
137    }
138}
139impl fmt::Display for MapAccess {
140    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141        write!(f, "{}[\"{}\"]", self.column, self.key)
142    }
143}
144
145impl fmt::Display for Cast {
146    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147        write!(f, "CAST({} AS {})", self.expr, self.data_type)
148    }
149}
150
151impl fmt::Display for TryCast {
152    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153        write!(f, "TRY_CAST({} AS {})", self.expr, self.data_type)
154    }
155}
156
157impl fmt::Display for Extract {
158    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
159        write!(f, "EXTRACT({} FROM {})", self.field, self.expr)
160    }
161}