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
62
63
64
65
66
67
68
69
70
71
72
73
use std::marker::PhantomData;

use crate::backend::Backend;
use crate::expression::*;
use crate::query_builder::*;
use crate::result::QueryResult;
use crate::sql_types::{DieselNumericOps, SqlType};

#[derive(Debug, Copy, Clone, QueryId, DieselNumericOps)]
#[doc(hidden)]
/// Coerces an expression to be another type. No checks are performed to ensure
/// that the new type is valid in all positions that the previous type was.
/// This does not perform an actual cast, it just lies to our type system.
///
/// This is used for a few expressions where we know that the types are actually
/// always interchangeable. (Examples of this include `Timestamp` vs
/// `Timestamptz`, `VarChar` vs `Text`, and `Json` vs `Jsonb`).
///
/// This struct should not be considered a general solution to equivalent types.
/// It is a short term workaround for expressions which are known to be commonly
/// used.
pub struct Coerce<T, ST> {
    expr: T,
    _marker: PhantomData<ST>,
}

impl<T, ST> Coerce<T, ST> {
    pub fn new(expr: T) -> Self {
        Coerce {
            expr: expr,
            _marker: PhantomData,
        }
    }
}

impl<T, ST> Expression for Coerce<T, ST>
where
    T: Expression,
    ST: SqlType + TypedExpressionType,
{
    type SqlType = ST;
}

impl<T, ST, QS> SelectableExpression<QS> for Coerce<T, ST>
where
    T: SelectableExpression<QS>,
    Self: Expression,
{
}

impl<T, ST, QS> AppearsOnTable<QS> for Coerce<T, ST>
where
    T: AppearsOnTable<QS>,
    Self: Expression,
{
}

impl<T, ST, DB> QueryFragment<DB> for Coerce<T, ST>
where
    T: QueryFragment<DB>,
    DB: Backend,
{
    fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        self.expr.walk_ast(pass)
    }
}

impl<T, ST, GB> ValidGrouping<GB> for Coerce<T, ST>
where
    T: ValidGrouping<GB>,
{
    type IsAggregate = T::IsAggregate;
}