sql_builder/query/condition/
constant.rs1use super::{Condition, ConditionExpression, ConditionType};
4use crate::{
5 BinOper, Expr, UnOper, Value,
6 lexer::{Dialect, Kind, tokens},
7};
8
9impl Condition {
10 pub fn is_trivially_true(&self) -> bool {
25 self.constant() == Some(true)
26 }
27
28 fn constant(&self) -> Option<bool> {
30 let values = self.conditions.iter().map(|condition| match condition {
31 ConditionExpression::Condition(condition) => condition.constant(),
32 ConditionExpression::Expr(expr) => expr_constant(expr),
33 });
34 let folded = match self.condition_type {
35 ConditionType::All => all(values),
36 ConditionType::Any => any(values),
37 };
38 folded.map(|value| value != self.negate)
39 }
40}
41
42fn all(values: impl IntoIterator<Item = Option<bool>>) -> Option<bool> {
44 let mut known = Some(true);
45 for value in values {
46 match value {
47 Some(false) => return Some(false),
48 Some(true) => {}
49 None => known = None,
50 }
51 }
52 known
53}
54
55fn any(values: impl IntoIterator<Item = Option<bool>>) -> Option<bool> {
57 all(values.into_iter().map(|value| value.map(|value| !value))).map(|value| !value)
58}
59
60fn expr_constant(expr: &Expr) -> Option<bool> {
61 match expr {
62 Expr::Value(Value::Bool(Some(value))) | Expr::Constant(Value::Bool(Some(value))) => {
63 Some(*value)
64 }
65 Expr::Unary(UnOper::Not, inner) => expr_constant(inner).map(|value| !value),
66 Expr::Binary(left, operator, right) => match (operator, right.as_ref()) {
67 (BinOper::In, Expr::Tuple(list)) if list.is_empty() => Some(false),
68 (BinOper::NotIn, Expr::Tuple(list)) if list.is_empty() => Some(true),
69 (BinOper::And, _) => all([expr_constant(left), expr_constant(right)]),
70 (BinOper::Or, _) => any([expr_constant(left), expr_constant(right)]),
71 (BinOper::Equal, _) => equal(left, right),
72 (BinOper::NotEqual, _) => equal(left, right).map(|value| !value),
73 _ => None,
74 },
75 Expr::Custom(sql) => custom_constant(sql),
76 _ => None,
77 }
78}
79
80fn equal(left: &Expr, right: &Expr) -> Option<bool> {
83 let literal = |expr: &Expr| match expr {
84 Expr::Value(value) | Expr::Constant(value) if *value != value.as_null() => {
85 Some(value.clone())
86 }
87 _ => None,
88 };
89 let (left, right) = (literal(left)?, literal(right)?);
90 (std::mem::discriminant(&left) == std::mem::discriminant(&right)).then(|| left == right)
91}
92
93fn custom_constant(sql: &str) -> Option<bool> {
97 let mut spelled = String::new();
98 for token in tokens(sql, Dialect::Any) {
99 match token.kind {
100 Kind::Space => {}
101 Kind::Symbol if matches!(token.text, "(" | ")") => {}
102 Kind::Word | Kind::Symbol => spelled.push_str(&token.text.to_ascii_uppercase()),
103 _ => return None,
104 }
105 }
106 match spelled.as_str() {
107 "TRUE" | "1" | "1=1" | "TRUE=TRUE" => Some(true),
108 "FALSE" | "0" | "0=1" | "1=0" | "1=2" | "FALSE=TRUE" | "TRUE=FALSE" => Some(false),
109 _ => None,
110 }
111}
112
113#[cfg(test)]
114#[path = "../../../tests/unit/condition_constant_tests.rs"]
115mod tests;