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

impl fmt::Display for Expr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Expr::Identifier(s) => write!(f, "{}", s),
            Expr::MapAccess(x) => write!(f, "{}", x),
            Expr::Wildcard => f.write_str("*"),
            Expr::QualifiedWildcard(q) => write!(f, "{}.*", display_separated(q, ".")),
            Expr::CompoundIdentifier(s) => write!(f, "{}", display_separated(s, ".")),
            Expr::IsNull(ast) => write!(f, "{} IS NULL", ast),
            Expr::IsNotNull(ast) => write!(f, "{} IS NOT NULL", ast),
            Expr::InList(x) => write!(f, "{}", x),
            Expr::InSubquery(x) => write!(f, "{}", x),
            Expr::Between(x) => write!(f, "{}", x),
            Expr::BinaryOp(x) => write!(f, "{}", x),
            Expr::UnaryOp(x) => write!(f, "{}", x),
            Expr::Cast(x) => write!(f, "{}", x),
            Expr::TryCast(x) => write!(f, "{}", x),
            Expr::Extract(x) => write!(f, "{}", x),
            Expr::Collate(x) => write!(f, "{}", x),
            Expr::Nested(ast) => write!(f, "({})", ast),
            Expr::Value(v) => write!(f, "{}", v),
            Expr::TypedString(x) => write!(f, "{}", x),
            Expr::Function(fun) => write!(f, "{}", fun),
            Expr::Case(x) => write!(f, "{}", x),
            Expr::Exists(s) => write!(f, "EXISTS ({})", s),
            Expr::Subquery(s) => write!(f, "({})", s),
            Expr::ListAgg(listagg) => write!(f, "{}", listagg),
            Expr::Substring(x) => write!(f, "{}", x),
            Expr::Trim(x) => write!(f, "{}", x),
        }
    }
}

impl fmt::Display for Trim {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "TRIM(")?;
        if let Some((ref ident, ref trim_char)) = self.trim_where {
            write!(f, "{} {} FROM {}", ident, trim_char, self.expr)?;
        } else {
            write!(f, "{}", self.expr)?;
        }
        write!(f, ")")
    }
}

impl fmt::Display for Substring {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SUBSTRING({}", self.expr)?;
        if let Some(ref from_part) = self.substring_from {
            write!(f, " FROM {}", from_part)?;
        }
        if let Some(ref from_part) = self.substring_for {
            write!(f, " FOR {}", from_part)?;
        }

        write!(f, ")")
    }
}

impl fmt::Display for Case {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "CASE")?;
        if let Some(ref operand) = self.operand {
            write!(f, " {}", operand)?;
        }
        for (c, r) in self.conditions.iter().zip(&self.results) {
            write!(f, " WHEN {} THEN {}", c, r)?;
        }

        if let Some(ref else_result) = self.else_result {
            write!(f, " ELSE {}", else_result)?;
        }
        write!(f, " END")
    }
}

impl fmt::Display for TypedString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.data_type)?;
        write!(f, " '{}'", &value::escape_single_quote_string(&self.value))
    }
}

impl fmt::Display for Collate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} COLLATE {}", self.expr, self.collation)
    }
}
impl fmt::Display for UnaryOp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.op == UnaryOperator::PGPostfixFactorial {
            write!(f, "{}{}", self.expr, self.op)
        } else {
            write!(f, "{} {}", self.op, self.expr)
        }
    }
}
impl fmt::Display for InSubquery {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {}IN ({})",
            self.expr,
            if self.negated { "NOT " } else { "" },
            self.subquery
        )
    }
}
impl fmt::Display for Between {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {}BETWEEN {} AND {}",
            self.expr,
            if self.negated { "NOT " } else { "" },
            self.low,
            self.high
        )
    }
}
impl fmt::Display for InList {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {}IN ({})",
            self.expr,
            if self.negated { "NOT " } else { "" },
            display_comma_separated(&self.list)
        )
    }
}
impl fmt::Display for BinaryOp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} {} {}", self.left, self.op, self.right)
    }
}
impl fmt::Display for MapAccess {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}[\"{}\"]", self.column, self.key)
    }
}

impl fmt::Display for Cast {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "CAST({} AS {})", self.expr, self.data_type)
    }
}

impl fmt::Display for TryCast {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "TRY_CAST({} AS {})", self.expr, self.data_type)
    }
}

impl fmt::Display for Extract {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "EXTRACT({} FROM {})", self.field, self.expr)
    }
}