tank_core/expression/
expression.rs

1use crate::{
2    DynQuery, OpPrecedence, Value,
3    writer::{Context, SqlWriter},
4};
5use std::fmt::Debug;
6
7/// Renderable SQL expression.
8pub trait Expression: OpPrecedence + Send + Sync + Debug {
9    /// Serialize the expression into `out` using `writer`.
10    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut DynQuery);
11    /// True if it encodes ordering.
12    fn is_ordered(&self) -> bool {
13        false
14    }
15    /// True if it is an expression that simply evaluates to true
16    fn is_true(&self) -> bool {
17        false
18    }
19}
20
21impl<T: Expression> Expression for &T {
22    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut DynQuery) {
23        (*self).write_query(writer, context, out);
24    }
25    fn is_ordered(&self) -> bool {
26        (*self).is_ordered()
27    }
28    fn is_true(&self) -> bool {
29        (*self).is_true()
30    }
31}
32
33impl Expression for &dyn Expression {
34    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut DynQuery) {
35        (*self).write_query(writer, context, out);
36    }
37    fn is_ordered(&self) -> bool {
38        (*self).is_ordered()
39    }
40    fn is_true(&self) -> bool {
41        (*self).is_true()
42    }
43}
44
45impl Expression for () {
46    fn write_query(&self, _writer: &dyn SqlWriter, _context: &mut Context, _out: &mut DynQuery) {}
47}
48
49impl Expression for bool {
50    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut DynQuery) {
51        writer.write_value_bool(context, out, *self);
52    }
53    fn is_true(&self) -> bool {
54        *self
55    }
56}
57
58impl Expression for &'static str {
59    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut DynQuery) {
60        writer.write_value_string(context, out, self);
61    }
62}
63
64impl<'a, T: Expression> From<&'a T> for &'a dyn Expression {
65    fn from(value: &'a T) -> Self {
66        value as &'a dyn Expression
67    }
68}
69
70impl Expression for Value {
71    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut DynQuery) {
72        writer.write_value(context, out, self);
73    }
74}