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