Skip to main content

tank_core/expression/
operand.rs

1use crate::{
2    DynQuery, Expression, ExpressionVisitor, OpPrecedence, Value,
3    writer::{Context, SqlWriter},
4};
5
6#[derive(Debug)]
7pub enum Operand<'a> {
8    Null,
9    LitBool(bool),
10    LitInt(i128),
11    LitFloat(f64),
12    LitStr(&'a str),
13    LitIdent(&'a str),
14    LitField(&'a [&'a str]),
15    LitArray(&'a [Operand<'a>]),
16    LitTuple(&'a [Operand<'a>]),
17    Type(Value),
18    Variable(Value),
19    Value(&'a Value),
20    Call(&'static str, &'a [&'a dyn Expression]),
21    Asterisk,
22    QuestionMark,
23    CurrentTimestampMs,
24}
25
26impl OpPrecedence for Operand<'_> {
27    fn precedence(&self, _writer: &dyn SqlWriter) -> i32 {
28        1_000_000
29    }
30}
31
32impl Expression for Operand<'_> {
33    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut DynQuery) {
34        writer.write_expression_operand(context, out, self)
35    }
36
37    fn accept_visitor(
38        &self,
39        matcher: &mut dyn ExpressionVisitor,
40        writer: &dyn SqlWriter,
41        context: &mut Context,
42        out: &mut DynQuery,
43    ) -> bool {
44        matcher.visit_operand(writer, context, out, self)
45    }
46}
47
48impl PartialEq for Operand<'_> {
49    fn eq(&self, other: &Self) -> bool {
50        match (self, other) {
51            (Self::LitBool(l), Self::LitBool(r)) => l == r,
52            (Self::LitFloat(l), Self::LitFloat(r)) => l == r,
53            (Self::LitIdent(l), Self::LitIdent(r)) => l == r,
54            (Self::LitField(l), Self::LitField(r)) => l == r,
55            (Self::LitInt(l), Self::LitInt(r)) => l == r,
56            (Self::LitStr(l), Self::LitStr(r)) => l == r,
57            (Self::LitArray(l), Self::LitArray(r)) => l == r,
58            (Self::LitTuple(l), Self::LitTuple(r)) => l == r,
59            (Self::Type(l), Self::Type(r)) => l.same_type(r),
60            (Self::Variable(l), Self::Variable(r)) => l == r,
61            (Self::Asterisk, Self::Asterisk) => true,
62            (Self::QuestionMark, Self::QuestionMark) => true,
63            (Self::CurrentTimestampMs, Self::CurrentTimestampMs) => true,
64            _ => false,
65        }
66    }
67}