tank_core/expression/
expression.rs

1use crate::{
2    OpPrecedence, Value,
3    writer::{Context, SqlWriter},
4};
5use std::fmt::Debug;
6
7/// A renderable SQL expression node.
8pub trait Expression: OpPrecedence + Send + Sync + Debug {
9    /// Serialize the expression into the output string using the sql writer.
10    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut String);
11    /// Whether this expression carries ordering information.
12    fn is_ordered(&self) -> bool {
13        false
14    }
15}
16
17impl<T: Expression> Expression for &T {
18    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut String) {
19        (*self).write_query(writer, context, out);
20    }
21    fn is_ordered(&self) -> bool {
22        (*self).is_ordered()
23    }
24}
25
26impl Expression for &dyn Expression {
27    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut String) {
28        (*self).write_query(writer, context, out);
29    }
30    fn is_ordered(&self) -> bool {
31        (*self).is_ordered()
32    }
33}
34
35impl Expression for () {
36    fn write_query(&self, _writer: &dyn SqlWriter, _context: &mut Context, _out: &mut String) {}
37}
38
39impl Expression for bool {
40    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut String) {
41        writer.write_value_bool(context, out, *self);
42    }
43}
44
45impl<'a, T: Expression> From<&'a T> for &'a dyn Expression {
46    fn from(value: &'a T) -> Self {
47        value as &'a dyn Expression
48    }
49}
50
51impl Expression for Value {
52    fn write_query(&self, writer: &dyn SqlWriter, context: &mut Context, out: &mut String) {
53        writer.write_value(context, out, self);
54    }
55}