1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use backend::Backend;
use expression::{Expression, SelectableExpression, NonAggregate};
use query_builder::*;
use result::QueryResult;
use types::*;

/// Represents the SQL `CURRENT_TIMESTAMP` constant. This is equivalent to the
/// `NOW()` function on backends that support it.
#[allow(non_camel_case_types)]
#[derive(Debug, Copy, Clone)]
pub struct now;

impl Expression for now {
    type SqlType = Timestamp;
}

impl<QS> SelectableExpression<QS> for now {
    type SqlTypeForSelect = Timestamp;
}

impl NonAggregate for now {
}

impl<DB: Backend> QueryFragment<DB> for now {
    fn to_sql(&self, out: &mut DB::QueryBuilder) -> BuildQueryResult {
        out.push_sql("CURRENT_TIMESTAMP");
        Ok(())
    }

    fn collect_binds(&self, _out: &mut DB::BindCollector) -> QueryResult<()> {
        Ok(())
    }

    fn is_safe_to_cache_prepared(&self) -> bool {
        true
    }
}

impl_query_id!(now);

operator_allowed!(now, Add, add);
operator_allowed!(now, Sub, sub);
sql_function!(date, date_t, (x: Timestamp) -> Date,
"Represents the SQL `DATE` function. The argument should be a Timestamp
expression, and the return value will be an expression of type Date");

#[cfg(feature="postgres")]
use expression::AsExpression;
#[cfg(feature="postgres")]
use expression::coerce::Coerce;
#[cfg(feature="postgres")]
use types::Timestamptz;

#[cfg(feature="postgres")]
impl AsExpression<Timestamptz> for now {
    type Expression = Coerce<now, Timestamptz>;

    fn as_expression(self) -> Self::Expression {
        Coerce::new(self)
    }
}