no_mistakes_postgres_sql/
bound_expr.rs1use sqlparser::ast::{visit_expressions_mut, BinaryOperator, Expr, ObjectNamePart, Value};
2use std::ops::ControlFlow;
3
4pub fn conjuncts_of(expr: &Expr, out: &mut Vec<Expr>) {
5 match expr {
6 Expr::Nested(inner) => conjuncts_of(inner, out),
7 Expr::BinaryOp {
8 left,
9 op: BinaryOperator::And,
10 right,
11 } => {
12 conjuncts_of(left, out);
13 conjuncts_of(right, out);
14 }
15 other => out.push(unnest(other)),
16 }
17}
18
19pub fn unnest(expr: &Expr) -> Expr {
20 match expr {
21 Expr::Nested(inner) => unnest(inner),
22 other => other.clone(),
23 }
24}
25
26pub fn canonical_bound_expr(expr: &Expr) -> Expr {
27 let mut normalized = unnest(expr);
28 let _: ControlFlow<()> = visit_expressions_mut(&mut normalized, |expr| {
29 if let Expr::Nested(inner) = expr {
30 let inner = std::mem::replace(inner, Box::new(Expr::Value(Value::Null.into())));
32 *expr = *inner;
33 return ControlFlow::Continue(());
34 }
35 match expr {
36 Expr::Identifier(ident) => {
37 ident.value = crate::idents::ident_key(ident);
38 ident.quote_style = ident
39 .value
40 .bytes()
41 .any(|byte| byte.is_ascii_uppercase())
42 .then_some('"');
43 }
44 Expr::Function(function) => {
45 for part in &mut function.name.0 {
46 match part {
47 ObjectNamePart::Identifier(ident) if ident.quote_style.is_none() => {
48 ident.value.make_ascii_lowercase();
49 }
50 _ => {}
51 }
52 }
53 }
54 _ => {}
55 }
56 ControlFlow::Continue(())
57 });
58 normalized
59}