diesel/expression/functions/
date_and_time.rs

1use crate::backend::Backend;
2use crate::expression::coerce::Coerce;
3use crate::expression::functions::declare_sql_function;
4use crate::expression::{AsExpression, Expression, ValidGrouping};
5use crate::query_builder::*;
6use crate::result::QueryResult;
7use crate::sql_types::*;
8
9/// Represents the SQL `CURRENT_TIMESTAMP` constant. This is equivalent to the
10/// `NOW()` function on backends that support it.
11#[allow(non_camel_case_types)]
12#[derive(Debug, Copy, Clone, QueryId, ValidGrouping)]
13pub struct now;
14
15impl Expression for now {
16    type SqlType = Timestamp;
17}
18
19impl<DB: Backend> QueryFragment<DB> for now {
20    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
21        out.push_sql("CURRENT_TIMESTAMP");
22        Ok(())
23    }
24}
25
26impl_selectable_expression!(now);
27
28operator_allowed!(now, Add, add);
29operator_allowed!(now, Sub, sub);
30
31#[declare_sql_function]
32extern "SQL" {
33    /// Represents the SQL `DATE` function. The argument should be a Timestamp
34    /// expression, and the return value will be an expression of type Date.
35    ///
36    /// # Examples
37    ///
38    /// ```
39    /// # include!("../../doctest_setup.rs");
40    /// # use diesel::dsl::{now, date};
41    /// # use diesel::deserialize::Queryable;
42    /// #
43    /// # fn test<R: Queryable<diesel::sql_types::Date, DB> + 'static>() -> QueryResult<R> {
44    /// #     let connection = &mut establish_connection();
45    /// let today = diesel::select(date(now)).first(connection)?;
46    /// #     Ok(today)
47    /// # }
48    /// # fn main() {
49    /// #
50    /// # }
51    /// ```
52    fn date(expr: Timestamp) -> Date;
53}
54
55impl AsExpression<Nullable<Timestamp>> for now {
56    type Expression = Coerce<now, Nullable<Timestamp>>;
57
58    fn as_expression(self) -> Self::Expression {
59        Coerce::new(self)
60    }
61}
62
63#[cfg(feature = "postgres_backend")]
64impl AsExpression<Timestamptz> for now {
65    type Expression = Coerce<now, Timestamptz>;
66
67    fn as_expression(self) -> Self::Expression {
68        Coerce::new(self)
69    }
70}
71
72#[cfg(feature = "postgres_backend")]
73impl AsExpression<Nullable<Timestamptz>> for now {
74    type Expression = Coerce<now, Nullable<Timestamptz>>;
75
76    fn as_expression(self) -> Self::Expression {
77        Coerce::new(self)
78    }
79}
80
81#[cfg(feature = "sqlite")]
82impl AsExpression<TimestamptzSqlite> for now {
83    type Expression = Coerce<now, TimestamptzSqlite>;
84
85    fn as_expression(self) -> Self::Expression {
86        Coerce::new(self)
87    }
88}
89
90#[cfg(feature = "sqlite")]
91impl AsExpression<Nullable<TimestamptzSqlite>> for now {
92    type Expression = Coerce<now, Nullable<TimestamptzSqlite>>;
93
94    fn as_expression(self) -> Self::Expression {
95        Coerce::new(self)
96    }
97}
98
99/// Represents the SQL `CURRENT_DATE` constant.
100#[allow(non_camel_case_types)]
101#[derive(Debug, Copy, Clone, QueryId, ValidGrouping)]
102pub struct today;
103
104impl Expression for today {
105    type SqlType = Date;
106}
107
108impl<DB: Backend> QueryFragment<DB> for today {
109    fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
110        out.push_sql("CURRENT_DATE");
111        Ok(())
112    }
113}
114
115impl_selectable_expression!(today);
116
117operator_allowed!(today, Add, add);
118operator_allowed!(today, Sub, sub);
119
120impl AsExpression<Nullable<Date>> for today {
121    type Expression = Coerce<today, Nullable<Date>>;
122
123    fn as_expression(self) -> Self::Expression {
124        Coerce::new(self)
125    }
126}