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#[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 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#[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}