Skip to main content

uqa_sql/ast/
pattern_operator.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Native pattern syntax retains its operator identity independently of routine names.
8
9use serde::{Deserialize, Serialize};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12pub enum PatternOperator {
13    Like,
14    NotLike,
15    ILike,
16    NotILike,
17    Regex,
18    NotRegex,
19    RegexInsensitive,
20    NotRegexInsensitive,
21    StartsWith,
22}
23
24impl PatternOperator {
25    #[must_use]
26    pub fn from_symbol(symbol: &str) -> Option<Self> {
27        Some(match symbol {
28            "~~" => Self::Like,
29            "!~~" => Self::NotLike,
30            "~~*" => Self::ILike,
31            "!~~*" => Self::NotILike,
32            "~" => Self::Regex,
33            "!~" => Self::NotRegex,
34            "~*" => Self::RegexInsensitive,
35            "!~*" => Self::NotRegexInsensitive,
36            "^@" => Self::StartsWith,
37            _ => return None,
38        })
39    }
40
41    /// The selected binary operator after any SQL escape helper.
42    #[must_use]
43    pub const fn symbol(self) -> &'static str {
44        match self {
45            Self::Like => "~~",
46            Self::NotLike => "!~~",
47            Self::ILike => "~~*",
48            Self::NotILike => "!~~*",
49            Self::Regex => "~",
50            Self::NotRegex => "!~",
51            Self::RegexInsensitive => "~*",
52            Self::NotRegexInsensitive => "!~*",
53            Self::StartsWith => "^@",
54        }
55    }
56
57    #[must_use]
58    pub const fn syntax(self) -> &'static str {
59        match self {
60            Self::Like => "LIKE",
61            Self::NotLike => "NOT LIKE",
62            Self::ILike => "ILIKE",
63            Self::NotILike => "NOT ILIKE",
64            _ => self.symbol(),
65        }
66    }
67
68    #[must_use]
69    pub const fn is_like(self) -> bool {
70        matches!(
71            self,
72            Self::Like | Self::NotLike | Self::ILike | Self::NotILike
73        )
74    }
75
76    #[must_use]
77    pub const fn case_insensitive(self) -> bool {
78        matches!(
79            self,
80            Self::ILike | Self::NotILike | Self::RegexInsensitive | Self::NotRegexInsensitive
81        )
82    }
83
84    #[must_use]
85    pub const fn negated(self) -> bool {
86        matches!(
87            self,
88            Self::NotLike | Self::NotILike | Self::NotRegex | Self::NotRegexInsensitive
89        )
90    }
91}