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: Lhs, rhs: Rhs, } impl $name { // This function is used by `operator_allowed!` // which is internally used by `table!` // for "numeric" columns #[doc(hidden)] pub fn new(left: Lhs, right: Rhs) -> Self { $name { lhs: left, rhs: right, } } } impl Expression for $name where Lhs: Expression, Lhs::SqlType: sql_types::ops::$name, Rhs: Expression, ::Output: TypedExpressionType, { type SqlType = ::Output; } impl QueryFragment for $name where DB: Backend, Lhs: QueryFragment, Rhs: QueryFragment, { 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); generic_numeric_expr!($name, A, B); }; } numeric_operation!(Add, " + "); numeric_operation!(Sub, " - "); numeric_operation!(Mul, " * "); numeric_operation!(Div, " / ");