wing_sqlparser/ast/
operator.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13use core::fmt;
14
15#[cfg(feature = "serde")]
16use serde::{Deserialize, Serialize};
17
18/// Unary operators
19#[derive(Debug, Clone, PartialEq, Eq, Hash)]
20#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
21pub enum UnaryOperator {
22    Plus,
23    Minus,
24    Not,
25    /// Bitwise Not, e.g. `~9` (PostgreSQL-specific)
26    PGBitwiseNot,
27    /// Square root, e.g. `|/9` (PostgreSQL-specific)
28    PGSquareRoot,
29    /// Cube root, e.g. `||/27` (PostgreSQL-specific)
30    PGCubeRoot,
31    /// Factorial, e.g. `9!` (PostgreSQL-specific)
32    PGPostfixFactorial,
33    /// Factorial, e.g. `!!9` (PostgreSQL-specific)
34    PGPrefixFactorial,
35    /// Absolute value, e.g. `@ -9` (PostgreSQL-specific)
36    PGAbs,
37}
38
39impl fmt::Display for UnaryOperator {
40    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41        f.write_str(match self {
42            UnaryOperator::Plus => "+",
43            UnaryOperator::Minus => "-",
44            UnaryOperator::Not => "NOT",
45            UnaryOperator::PGBitwiseNot => "~",
46            UnaryOperator::PGSquareRoot => "|/",
47            UnaryOperator::PGCubeRoot => "||/",
48            UnaryOperator::PGPostfixFactorial => "!",
49            UnaryOperator::PGPrefixFactorial => "!!",
50            UnaryOperator::PGAbs => "@",
51        })
52    }
53}
54
55/// Binary operators
56#[derive(Debug, Clone, PartialEq, Eq, Hash)]
57#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
58pub enum BinaryOperator {
59    Plus,
60    Minus,
61    Multiply,
62    Divide,
63    Modulo,
64    StringConcat,
65    Gt,
66    Lt,
67    GtEq,
68    LtEq,
69    Spaceship,
70    Eq,
71    NotEq,
72    And,
73    Or,
74    Xor,
75    Like,
76    NotLike,
77    ILike,
78    NotILike,
79    BitwiseOr,
80    BitwiseAnd,
81    BitwiseXor,
82    PGBitwiseXor,
83    PGBitwiseShiftLeft,
84    PGBitwiseShiftRight,
85    PGRegexMatch,
86    PGRegexIMatch,
87    PGRegexNotMatch,
88    PGRegexNotIMatch,
89}
90
91impl fmt::Display for BinaryOperator {
92    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93        f.write_str(match self {
94            BinaryOperator::Plus => "+",
95            BinaryOperator::Minus => "-",
96            BinaryOperator::Multiply => "*",
97            BinaryOperator::Divide => "/",
98            BinaryOperator::Modulo => "%",
99            BinaryOperator::StringConcat => "||",
100            BinaryOperator::Gt => ">",
101            BinaryOperator::Lt => "<",
102            BinaryOperator::GtEq => ">=",
103            BinaryOperator::LtEq => "<=",
104            BinaryOperator::Spaceship => "<=>",
105            BinaryOperator::Eq => "=",
106            BinaryOperator::NotEq => "<>",
107            BinaryOperator::And => "AND",
108            BinaryOperator::Or => "OR",
109            BinaryOperator::Xor => "XOR",
110            BinaryOperator::Like => "LIKE",
111            BinaryOperator::NotLike => "NOT LIKE",
112            BinaryOperator::ILike => "ILIKE",
113            BinaryOperator::NotILike => "NOT ILIKE",
114            BinaryOperator::BitwiseOr => "|",
115            BinaryOperator::BitwiseAnd => "&",
116            BinaryOperator::BitwiseXor => "^",
117            BinaryOperator::PGBitwiseXor => "#",
118            BinaryOperator::PGBitwiseShiftLeft => "<<",
119            BinaryOperator::PGBitwiseShiftRight => ">>",
120            BinaryOperator::PGRegexMatch => "~",
121            BinaryOperator::PGRegexIMatch => "~*",
122            BinaryOperator::PGRegexNotMatch => "!~",
123            BinaryOperator::PGRegexNotIMatch => "!~*",
124        })
125    }
126}