| 1 |
use crate::backend::Backend; |
| 2 |
use crate::expression::coerce::Coerce; |
| 3 |
use crate::expression::functions::declare_sql_function; |
| 4 |
use crate::expression::{AsExpression, Expression, ValidGrouping}; |
| 5 |
use crate::query_builder::*; |
| 6 |
use crate::result::QueryResult; |
| 7 |
use 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)] |
| 13 |
pub struct now; |
| 14 |
|
| 15 |
impl Expression for now { |
| 16 |
type SqlType = Timestamp; |
| 17 |
} |
| 18 |
|
| 19 |
impl<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 |
|
| 26 |
impl_selectable_expression!(now); |
| 27 |
|
| 28 |
operator_allowed!(now, Add, add); |
| 29 |
operator_allowed!(now, Sub, sub); |
| 30 |
|
| 31 |
#[declare_sql_function] |
| 32 |
extern "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 |
|
| 55 |
impl 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")] |
| 64 |
impl 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")] |
| 73 |
impl 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")] |
| 82 |
impl 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")] |
| 91 |
impl 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)] |
| 102 |
pub struct today; |
| 103 |
|
| 104 |
impl Expression for today { |
| 105 |
type SqlType = Date; |
| 106 |
} |
| 107 |
|
| 108 |
impl<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 |
|
| 115 |
impl_selectable_expression!(today); |
| 116 |
|
| 117 |
operator_allowed!(today, Add, add); |
| 118 |
operator_allowed!(today, Sub, sub); |
| 119 |
|
| 120 |
impl 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 |
} |