Debian Codegraph @

src/expression/coerce.rs

raw
1 use crate::expression::*;
2 use crate::query_builder::*;
3 use crate::result::QueryResult;
4 use crate::sql_types::DieselNumericOps;
5 use std::marker::PhantomData;
6
7 #[derive(Debug, Copy, Clone, QueryId, DieselNumericOps)]
8 #[doc(hidden)]
9 /// Coerces an expression to be another type. No checks are performed to ensure
10 /// that the new type is valid in all positions that the previous type was.
11 /// This does not perform an actual cast, it just lies to our type system.
12 ///
13 /// This is used for a few expressions where we know that the types are actually
14 /// always interchangeable. (Examples of this include `Timestamp` vs
15 /// `Timestamptz`, `VarChar` vs `Text`, and `Json` vs `Jsonb`).
16 ///
17 /// This struct should not be considered a general solution to equivalent types.
18 /// It is a short term workaround for expressions which are known to be commonly
19 /// used.
20 pub struct Coerce<T, ST> {
21 expr: T,
22 _marker: PhantomData<ST>,
23 }
24
25 impl<T, ST> Coerce<T, ST> {
26 pub fn new(expr: T) -> Self {
27 Coerce {
28 expr: expr,
29 _marker: PhantomData,
30 }
31 }
32 }
33
34 impl<T, ST> Expression for Coerce<T, ST>
35 where
36 T: Expression,
37 ST: SqlType + TypedExpressionType,
38 {
39 type SqlType = ST;
40 }
41
42 impl<T, ST, QS> SelectableExpression<QS> for Coerce<T, ST>
43 where
44 T: SelectableExpression<QS>,
45 Self: Expression,
46 {
47 }
48
49 impl<T, ST, QS> AppearsOnTable<QS> for Coerce<T, ST>
50 where
51 T: AppearsOnTable<QS>,
52 Self: Expression,
53 {
54 }
55
56 impl<T, ST, DB> QueryFragment<DB> for Coerce<T, ST>
57 where
58 T: QueryFragment<DB>,
59 DB: Backend,
60 {
61 fn walk_ast<'b>(&'b self, pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
62 self.expr.walk_ast(pass)
63 }
64 }
65
66 impl<T, ST, GB> ValidGrouping<GB> for Coerce<T, ST>
67 where
68 T: ValidGrouping<GB>,
69 {
70 type IsAggregate = T::IsAggregate;
71 }