1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::backend::Backend;
use crate::expression::{Expression, TypedExpressionType, ValidGrouping};
use crate::query_builder::*;
use crate::result::QueryResult;
use crate::sql_types;
macro_rules! numeric_operation {
($name:ident, $op:expr) => {
#[doc(hidden)]
#[derive(Debug, Copy, Clone, QueryId, ValidGrouping)]
pub struct $name<Lhs, Rhs> {
lhs: Lhs,
rhs: Rhs,
}
impl<Lhs, Rhs> $name<Lhs, Rhs> {
#[doc(hidden)]
pub fn new(left: Lhs, right: Rhs) -> Self {
$name {
lhs: left,
rhs: right,
}
}
}
impl<Lhs, Rhs> Expression for $name<Lhs, Rhs>
where
Lhs: Expression,
Lhs::SqlType: sql_types::ops::$name,
Rhs: Expression,
<Lhs::SqlType as sql_types::ops::$name>::Output: TypedExpressionType,
{
type SqlType = <Lhs::SqlType as sql_types::ops::$name>::Output;
}
impl<Lhs, Rhs, DB> QueryFragment<DB> for $name<Lhs, Rhs>
where
DB: Backend,
Lhs: QueryFragment<DB>,
Rhs: QueryFragment<DB>,
{
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()>
{
out.push_sql("(");
self.lhs.walk_ast(out.reborrow())?;
out.push_sql($op);
self.rhs.walk_ast(out.reborrow())?;
out.push_sql(")");
Ok(())
}
}
impl_selectable_expression!($name<Lhs, Rhs>);
generic_numeric_expr!($name, A, B);
};
}
numeric_operation!(Add, " + ");
numeric_operation!(Sub, " - ");
numeric_operation!(Mul, " * ");
numeric_operation!(Div, " / ");