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}
24
25impl OpPrecedence for Operand<'_> {
26    fn precedence(&self, _writer: &dyn SqlWriter) -> i32 {
27        1_000_000
28    }
29}
30
31impl Expression for Operand<'_> {
32    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut DynQuery) {
33        writer.write_expression_operand(context, out, self)
34    }
35
36    fn accept_visitor(
37        &self,
38        matcher: &mut dyn ExpressionVisitor,
39        writer: &dyn SqlWriter,
40        context: &mut Context,
41        out: &mut DynQuery,
42    ) -> bool {
43        matcher.visit_operand(writer, context, out, self)
44    }
45}
46
47impl PartialEq for Operand<'_> {
48    fn eq(&self, other: &Self) -> bool {
49        match (self, other) {
50            (Self::LitBool(l), Self::LitBool(r)) => l == r,
51            (Self::LitFloat(l), Self::LitFloat(r)) => l == r,
52            (Self::LitIdent(l), Self::LitIdent(r)) => l == r,
53            (Self::LitField(l), Self::LitField(r)) => l == r,
54            (Self::LitInt(l), Self::LitInt(r)) => l == r,
55            (Self::LitStr(l), Self::LitStr(r)) => l == r,
56            (Self::LitArray(l), Self::LitArray(r)) => l == r,
57            (Self::LitTuple(l), Self::LitTuple(r)) => l == r,
58            (Self::Type(l), Self::Type(r)) => l.same_type(r),
59            (Self::Variable(l), Self::Variable(r)) => l == r,
60            (Self::Asterisk, Self::Asterisk) => true,
61            (Self::QuestionMark, Self::QuestionMark) => true,
62            _ => false,
63        }
64    }
65}