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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use crate::backend::{Backend, DieselReserveSpecialization};
use crate::dsl::SqlTypeOf;
use crate::expression::{
    AppearsOnTable, Expression, QueryMetadata, Selectable, SelectableExpression,
    TypedExpressionType, ValidGrouping,
};
use crate::query_builder::*;
use crate::result::QueryResult;

#[derive(Debug)]
pub struct SelectBy<T: Selectable<DB>, DB: Backend> {
    selection: T::SelectExpression,
    p: std::marker::PhantomData<(T, DB)>,
}

impl<T, DB> Clone for SelectBy<T, DB>
where
    DB: Backend,
    T: Selectable<DB>,
{
    fn clone(&self) -> Self {
        Self {
            selection: T::construct_selection(),
            p: std::marker::PhantomData,
        }
    }
}

impl<T, DB> Copy for SelectBy<T, DB>
where
    T: Selectable<DB>,
    DB: Backend,
    T::SelectExpression: Copy,
{
}

impl<T, E, DB> QueryId for SelectBy<T, DB>
where
    DB: Backend,
    T: Selectable<DB, SelectExpression = E>,
    E: QueryId + Expression,
{
    type QueryId = E::QueryId;

    const HAS_STATIC_QUERY_ID: bool = E::HAS_STATIC_QUERY_ID;
}

impl<T, DB> SelectBy<T, DB>
where
    T: Selectable<DB>,
    DB: Backend,
{
    pub(crate) fn new() -> Self {
        Self {
            selection: T::construct_selection(),
            p: std::marker::PhantomData,
        }
    }
}

impl<T, E, DB> Expression for SelectBy<T, DB>
where
    DB: Backend,
    T: Selectable<DB, SelectExpression = E>,
    E: QueryId + Expression,
{
    type SqlType = SelectBy<T, DB>;
}

impl<T, DB> TypedExpressionType for SelectBy<T, DB>
where
    T: Selectable<DB>,
    DB: Backend,
{
}

impl<T, GB, E, DB> ValidGrouping<GB> for SelectBy<T, DB>
where
    DB: Backend,
    T: Selectable<DB, SelectExpression = E>,
    E: Expression + ValidGrouping<GB>,
{
    type IsAggregate = E::IsAggregate;
}

impl<T, DB> QueryMetadata<SelectBy<T, DB>> for DB
where
    DB: Backend,
    T: Selectable<DB>,
    DB: QueryMetadata<SqlTypeOf<T::SelectExpression>>,
{
    fn row_metadata(lookup: &mut Self::MetadataLookup, out: &mut Vec<Option<Self::TypeMetadata>>) {
        <DB as QueryMetadata<SqlTypeOf<<T as Selectable<DB>>::SelectExpression>>>::row_metadata(
            lookup, out,
        )
    }
}

impl<T, DB> QueryFragment<DB> for SelectBy<T, DB>
where
    T: Selectable<DB>,
    T::SelectExpression: QueryFragment<DB>,
    DB: Backend + DieselReserveSpecialization,
{
    fn walk_ast<'b>(&'b self, out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
        self.selection.walk_ast(out)
    }
}

impl<T, QS, DB> SelectableExpression<QS> for SelectBy<T, DB>
where
    DB: Backend,
    T: Selectable<DB>,
    T::SelectExpression: SelectableExpression<QS>,
    Self: AppearsOnTable<QS>,
{
}

impl<T, QS, DB> AppearsOnTable<QS> for SelectBy<T, DB>
where
    DB: Backend,
    T: Selectable<DB>,
    T::SelectExpression: AppearsOnTable<QS>,
    Self: Expression,
{
}