diesel/expression/functions/
date_and_time.rs

1use backend::Backend;
2use expression::coerce::Coerce;
3use expression::{AsExpression, Expression, NonAggregate};
4use query_builder::*;
5use result::QueryResult;
6use sql_types::*;
7
8/// Represents the SQL `CURRENT_TIMESTAMP` constant. This is equivalent to the
9/// `NOW()` function on backends that support it.
10#[allow(non_camel_case_types)]
11#[derive(Debug, Copy, Clone, QueryId)]
12pub struct now;
13
14impl Expression for now {
15    type SqlType = Timestamp;
16}
17
18impl NonAggregate for now {}
19
20impl<DB: Backend> QueryFragment<DB> for now {
21    fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> {
22        out.push_sql("CURRENT_TIMESTAMP");
23        Ok(())
24    }
25}
26
27impl_selectable_expression!(now);
28
29operator_allowed!(now, Add, add);
30operator_allowed!(now, Sub, sub);
31sql_function! {
32    /// Represents the SQL `DATE` function. The argument should be a Timestamp
33    /// expression, and the return value will be an expression of type Date.
34
35    /// # Examples
36
37    /// ```ignore
38    /// # #[macro_use] extern crate diesel;
39    /// # extern crate chrono;
40    /// # include!("../../doctest_setup.rs");
41    /// # use diesel::dsl::*;
42    /// #
43    /// # fn main() {
44    /// #     let connection = establish_connection();
45    /// let today: chrono::NaiveDate = diesel::select(date(now)).first(&connection).unwrap();
46    /// # }
47    /// ```
48    fn date(expr: Timestamp) -> Date;
49}
50
51impl AsExpression<Nullable<Timestamp>> for now {
52    type Expression = Coerce<now, Nullable<Timestamp>>;
53
54    fn as_expression(self) -> Self::Expression {
55        Coerce::new(self)
56    }
57}
58
59#[cfg(feature = "postgres")]
60impl AsExpression<Timestamptz> for now {
61    type Expression = Coerce<now, Timestamptz>;
62
63    fn as_expression(self) -> Self::Expression {
64        Coerce::new(self)
65    }
66}
67
68#[cfg(feature = "postgres")]
69impl AsExpression<Nullable<Timestamptz>> for now {
70    type Expression = Coerce<now, Nullable<Timestamptz>>;
71
72    fn as_expression(self) -> Self::Expression {
73        Coerce::new(self)
74    }
75}