diesel/expression/functions/
date_and_time.rs1use crate::backend::Backend;
2use crate::expression::coerce::Coerce;
3use crate::expression::functions::define_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);
30define_sql_function! {
31 fn date(expr: Timestamp) -> Date;
51}
52
53impl AsExpression<Nullable<Timestamp>> for now {
54 type Expression = Coerce<now, Nullable<Timestamp>>;
55
56 fn as_expression(self) -> Self::Expression {
57 Coerce::new(self)
58 }
59}
60
61#[cfg(feature = "postgres_backend")]
62impl AsExpression<Timestamptz> for now {
63 type Expression = Coerce<now, Timestamptz>;
64
65 fn as_expression(self) -> Self::Expression {
66 Coerce::new(self)
67 }
68}
69
70#[cfg(feature = "postgres_backend")]
71impl AsExpression<Nullable<Timestamptz>> for now {
72 type Expression = Coerce<now, Nullable<Timestamptz>>;
73
74 fn as_expression(self) -> Self::Expression {
75 Coerce::new(self)
76 }
77}
78
79#[cfg(feature = "sqlite")]
80impl AsExpression<TimestamptzSqlite> for now {
81 type Expression = Coerce<now, TimestamptzSqlite>;
82
83 fn as_expression(self) -> Self::Expression {
84 Coerce::new(self)
85 }
86}
87
88#[cfg(feature = "sqlite")]
89impl AsExpression<Nullable<TimestamptzSqlite>> for now {
90 type Expression = Coerce<now, Nullable<TimestamptzSqlite>>;
91
92 fn as_expression(self) -> Self::Expression {
93 Coerce::new(self)
94 }
95}
96
97#[allow(non_camel_case_types)]
99#[derive(Debug, Copy, Clone, QueryId, ValidGrouping)]
100pub struct today;
101
102impl Expression for today {
103 type SqlType = Date;
104}
105
106impl<DB: Backend> QueryFragment<DB> for today {
107 fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
108 out.push_sql("CURRENT_DATE");
109 Ok(())
110 }
111}
112
113impl_selectable_expression!(today);
114
115operator_allowed!(today, Add, add);
116operator_allowed!(today, Sub, sub);
117
118impl AsExpression<Nullable<Date>> for today {
119 type Expression = Coerce<today, Nullable<Date>>;
120
121 fn as_expression(self) -> Self::Expression {
122 Coerce::new(self)
123 }
124}