Skip to main content

toasty_core/stmt/
expr_like.rs

1use super::Expr;
2
3/// A SQL `LIKE` pattern-match expression: `expr LIKE pattern`.
4///
5/// Returns `true` if `expr` matches `pattern`. The user is responsible for
6/// including any `%` or `_` wildcard characters in `pattern`.
7///
8/// # Examples
9///
10/// ```text
11/// name LIKE 'Al%'   // true if name starts with "Al"
12/// name LIKE '%son'  // true if name ends with "son"
13/// ```
14#[derive(Debug, Clone, PartialEq)]
15pub struct ExprLike {
16    /// The attribute to test.
17    pub expr: Box<Expr>,
18
19    /// The LIKE pattern (including any % or _ wildcards).
20    pub pattern: Box<Expr>,
21
22    /// Optional escape character. When `Some(c)`, occurrences of `c` in the
23    /// pattern make the following `%`, `_`, or `c` match literally, and the
24    /// serializer emits an `ESCAPE 'c'` clause.
25    pub escape: Option<char>,
26}
27
28impl Expr {
29    /// Creates a `expr LIKE pattern` expression with no escape character.
30    pub fn like(expr: impl Into<Self>, pattern: impl Into<Self>) -> Self {
31        ExprLike {
32            expr: Box::new(expr.into()),
33            pattern: Box::new(pattern.into()),
34            escape: None,
35        }
36        .into()
37    }
38}
39
40impl From<ExprLike> for Expr {
41    fn from(value: ExprLike) -> Self {
42        Self::Like(value)
43    }
44}