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(not(feature = "std"))]
16use alloc::{string::String, vec::Vec};
17
18#[cfg(feature = "serde")]
19use serde::{Deserialize, Serialize};
20
21#[cfg(feature = "visitor")]
22use sqlparser_derive::{Visit, VisitMut};
23
24use super::display_separated;
25
26/// Unary operators
27#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
28#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
29#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
30pub enum UnaryOperator {
31    Plus,
32    Minus,
33    Not,
34    /// Bitwise Not, e.g. `~9` (PostgreSQL-specific)
35    PGBitwiseNot,
36    /// Square root, e.g. `|/9` (PostgreSQL-specific)
37    PGSquareRoot,
38    /// Cube root, e.g. `||/27` (PostgreSQL-specific)
39    PGCubeRoot,
40    /// Factorial, e.g. `9!` (PostgreSQL-specific)
41    PGPostfixFactorial,
42    /// Factorial, e.g. `!!9` (PostgreSQL-specific)
43    PGPrefixFactorial,
44    /// Absolute value, e.g. `@ -9` (PostgreSQL-specific)
45    PGAbs,
46}
47
48impl fmt::Display for UnaryOperator {
49    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50        f.write_str(match self {
51            UnaryOperator::Plus => "+",
52            UnaryOperator::Minus => "-",
53            UnaryOperator::Not => "NOT",
54            UnaryOperator::PGBitwiseNot => "~",
55            UnaryOperator::PGSquareRoot => "|/",
56            UnaryOperator::PGCubeRoot => "||/",
57            UnaryOperator::PGPostfixFactorial => "!",
58            UnaryOperator::PGPrefixFactorial => "!!",
59            UnaryOperator::PGAbs => "@",
60        })
61    }
62}
63
64/// Binary operators
65#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
66#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
67#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
68pub enum BinaryOperator {
69    Plus,
70    Minus,
71    Multiply,
72    Divide,
73    Modulo,
74    StringConcat,
75    Gt,
76    Lt,
77    GtEq,
78    LtEq,
79    Spaceship,
80    Eq,
81    NotEq,
82    And,
83    Or,
84    Xor,
85    BitwiseOr,
86    BitwiseAnd,
87    BitwiseXor,
88    PGBitwiseXor,
89    PGBitwiseShiftLeft,
90    PGBitwiseShiftRight,
91    PGExp,
92    PGRegexMatch,
93    PGRegexIMatch,
94    PGRegexNotMatch,
95    PGRegexNotIMatch,
96    /// PostgreSQL-specific custom operator.
97    ///
98    /// See [CREATE OPERATOR](https://www.postgresql.org/docs/current/sql-createoperator.html)
99    /// for more information.
100    PGCustomBinaryOperator(Vec<String>),
101}
102
103impl fmt::Display for BinaryOperator {
104    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105        match self {
106            BinaryOperator::Plus => f.write_str("+"),
107            BinaryOperator::Minus => f.write_str("-"),
108            BinaryOperator::Multiply => f.write_str("*"),
109            BinaryOperator::Divide => f.write_str("/"),
110            BinaryOperator::Modulo => f.write_str("%"),
111            BinaryOperator::StringConcat => f.write_str("||"),
112            BinaryOperator::Gt => f.write_str(">"),
113            BinaryOperator::Lt => f.write_str("<"),
114            BinaryOperator::GtEq => f.write_str(">="),
115            BinaryOperator::LtEq => f.write_str("<="),
116            BinaryOperator::Spaceship => f.write_str("<=>"),
117            BinaryOperator::Eq => f.write_str("="),
118            BinaryOperator::NotEq => f.write_str("<>"),
119            BinaryOperator::And => f.write_str("AND"),
120            BinaryOperator::Or => f.write_str("OR"),
121            BinaryOperator::Xor => f.write_str("XOR"),
122            BinaryOperator::BitwiseOr => f.write_str("|"),
123            BinaryOperator::BitwiseAnd => f.write_str("&"),
124            BinaryOperator::BitwiseXor => f.write_str("^"),
125            BinaryOperator::PGBitwiseXor => f.write_str("#"),
126            BinaryOperator::PGBitwiseShiftLeft => f.write_str("<<"),
127            BinaryOperator::PGBitwiseShiftRight => f.write_str(">>"),
128            BinaryOperator::PGExp => f.write_str("^"),
129            BinaryOperator::PGRegexMatch => f.write_str("~"),
130            BinaryOperator::PGRegexIMatch => f.write_str("~*"),
131            BinaryOperator::PGRegexNotMatch => f.write_str("!~"),
132            BinaryOperator::PGRegexNotIMatch => f.write_str("!~*"),
133            BinaryOperator::PGCustomBinaryOperator(idents) => {
134                write!(f, "OPERATOR({})", display_separated(idents, "."))
135            }
136        }
137    }
138}