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
use crate::stmt::{Expr, Node, Statement, Visit, VisitMut};
/// A guard condition on an [`Update`](super::Update) statement.
///
/// Unlike a [`Filter`](super::Filter), a condition does not select which rows
/// to operate on. Instead, it is evaluated after the filter and determines
/// whether the update should actually be applied. If the condition is not met,
/// the update is silently skipped.
///
/// When `expr` is `None`, no condition is applied (the update always proceeds).
///
/// # Examples
///
/// ```
/// use toasty_core::stmt::Condition;
///
/// let cond = Condition::default();
/// assert!(cond.is_none());
/// ```
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Condition {
/// The condition expression, or `None` for unconditional updates.
pub expr: Option<Expr>,
}
impl Condition {
/// Creates a condition from an expression.
pub fn new(expr: impl Into<Expr>) -> Condition {
Condition {
expr: Some(expr.into()),
}
}
/// Returns `true` if a condition expression is set.
pub fn is_some(&self) -> bool {
self.expr.is_some()
}
/// Returns `true` if no condition expression is set.
pub fn is_none(&self) -> bool {
self.expr.is_none()
}
}
impl Statement {
/// Returns a reference to this statement's condition, if it has one and it
/// is set. Only `Update` statements support conditions.
pub fn condition(&self) -> Option<&Condition> {
match self {
Statement::Update(update) if update.condition.is_some() => Some(&update.condition),
_ => None,
}
}
/// Returns a mutable reference to the statement's condition.
///
/// Returns `None` for statements that do not support conditions.
pub fn condition_mut(&mut self) -> Option<&mut Condition> {
match self {
Statement::Update(update) => Some(&mut update.condition),
_ => None,
}
}
/// Returns a mutable reference to the statement's condition.
///
/// # Panics
///
/// Panics if the statement does not support conditions.
#[track_caller]
pub fn condition_mut_unwrap(&mut self) -> &mut Condition {
match self {
Statement::Update(update) => &mut update.condition,
_ => panic!("expected Statement with condition"),
}
}
}
impl Node for Condition {
fn visit<V: Visit>(&self, mut visit: V) {
visit.visit_condition(self);
}
fn visit_mut<V: VisitMut>(&mut self, mut visit: V) {
visit.visit_condition_mut(self);
}
}
impl<T> From<T> for Condition
where
Expr: From<T>,
{
fn from(value: T) -> Self {
Condition {
expr: Some(value.into()),
}
}
}