| 1 |
use crate::expression::{Expression, ValidGrouping}; |
| 2 |
use crate::pg::Pg; |
| 3 |
use crate::query_builder::*; |
| 4 |
use crate::result::QueryResult; |
| 5 |
use crate::sql_types::{is_nullable, Date, Nullable, SqlType, Timestamp, Timestamptz, VarChar}; |
| 6 |
|
| 7 |
/// Marker trait for types which are valid in `AT TIME ZONE` expressions |
| 8 |
pub trait DateTimeLike {} |
| 9 |
impl DateTimeLike for Date {} |
| 10 |
impl DateTimeLike for Timestamp {} |
| 11 |
impl DateTimeLike for Timestamptz {} |
| 12 |
impl<T> DateTimeLike for Nullable<T> where T: SqlType<IsNull = is_nullable::NotNull> + DateTimeLike {} |
| 13 |
#[derive(Debug, Copy, Clone, QueryId, ValidGrouping)] |
| 14 |
pub struct AtTimeZone<Ts, Tz> { |
| 15 |
timestamp: Ts, |
| 16 |
timezone: Tz, |
| 17 |
} |
| 18 |
|
| 19 |
impl<Ts, Tz> AtTimeZone<Ts, Tz> { |
| 20 |
pub fn new(timestamp: Ts, timezone: Tz) -> Self { |
| 21 |
AtTimeZone { |
| 22 |
timestamp: timestamp, |
| 23 |
timezone: timezone, |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
impl<Ts, Tz> Expression for AtTimeZone<Ts, Tz> |
| 29 |
where |
| 30 |
Ts: Expression, |
| 31 |
Ts::SqlType: DateTimeLike, |
| 32 |
Tz: Expression<SqlType = VarChar>, |
| 33 |
{ |
| 34 |
type SqlType = Timestamp; |
| 35 |
} |
| 36 |
|
| 37 |
impl<Ts, Tz> QueryFragment<Pg> for AtTimeZone<Ts, Tz> |
| 38 |
where |
| 39 |
Ts: QueryFragment<Pg>, |
| 40 |
Tz: QueryFragment<Pg>, |
| 41 |
{ |
| 42 |
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> { |
| 43 |
self.timestamp.walk_ast(out.reborrow())?; |
| 44 |
out.push_sql(" AT TIME ZONE "); |
| 45 |
self.timezone.walk_ast(out.reborrow())?; |
| 46 |
Ok(()) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl_selectable_expression!(AtTimeZone<Ts, Tz>); |