1use crate::{ColumnTrait, EntityTrait, Iterable, QueryFilter, QueryOrder, QuerySelect, QueryTrait};
2use core::fmt::Debug;
3use core::marker::PhantomData;
4use sea_query::{Expr, IntoColumnRef, SelectStatement, SimpleExpr};
5
6#[derive(Clone, Debug)]
8pub struct Select<E>
9where
10 E: EntityTrait,
11{
12 pub(crate) query: SelectStatement,
13 pub(crate) entity: PhantomData<E>,
14}
15
16#[derive(Clone, Debug)]
18pub struct SelectTwo<E, F>
19where
20 E: EntityTrait,
21 F: EntityTrait,
22{
23 pub(crate) query: SelectStatement,
24 pub(crate) entity: PhantomData<(E, F)>,
25}
26
27#[derive(Clone, Debug)]
29pub struct SelectTwoMany<E, F>
30where
31 E: EntityTrait,
32 F: EntityTrait,
33{
34 pub(crate) query: SelectStatement,
35 pub(crate) entity: PhantomData<(E, F)>,
36}
37
38pub trait IntoSimpleExpr {
40 fn into_simple_expr(self) -> SimpleExpr;
42}
43
44macro_rules! impl_trait {
45 ( $trait: ident ) => {
46 impl<E> $trait for Select<E>
47 where
48 E: EntityTrait,
49 {
50 type QueryStatement = SelectStatement;
51
52 fn query(&mut self) -> &mut SelectStatement {
53 &mut self.query
54 }
55 }
56
57 impl<E, F> $trait for SelectTwo<E, F>
58 where
59 E: EntityTrait,
60 F: EntityTrait,
61 {
62 type QueryStatement = SelectStatement;
63
64 fn query(&mut self) -> &mut SelectStatement {
65 &mut self.query
66 }
67 }
68
69 impl<E, F> $trait for SelectTwoMany<E, F>
70 where
71 E: EntityTrait,
72 F: EntityTrait,
73 {
74 type QueryStatement = SelectStatement;
75
76 fn query(&mut self) -> &mut SelectStatement {
77 &mut self.query
78 }
79 }
80 };
81}
82
83impl_trait!(QuerySelect);
84impl_trait!(QueryFilter);
85impl_trait!(QueryOrder);
86
87impl<C> IntoSimpleExpr for C
88where
89 C: ColumnTrait,
90{
91 fn into_simple_expr(self) -> SimpleExpr {
92 SimpleExpr::Column(self.as_column_ref().into_column_ref())
93 }
94}
95
96impl IntoSimpleExpr for Expr {
97 fn into_simple_expr(self) -> SimpleExpr {
98 self.into()
99 }
100}
101
102impl IntoSimpleExpr for SimpleExpr {
103 fn into_simple_expr(self) -> SimpleExpr {
104 self
105 }
106}
107
108impl<E> Select<E>
109where
110 E: EntityTrait,
111{
112 pub(crate) fn new() -> Self {
113 Self {
114 query: SelectStatement::new(),
115 entity: PhantomData,
116 }
117 .prepare_select()
118 .prepare_from()
119 }
120
121 fn prepare_select(mut self) -> Self {
122 self.query.exprs(self.column_list());
123 self
124 }
125
126 fn column_list(&self) -> Vec<SimpleExpr> {
127 E::Column::iter()
128 .map(|col| col.select_as(col.into_expr()))
129 .collect()
130 }
131
132 fn prepare_from(mut self) -> Self {
133 self.query.from(E::default().table_ref());
134 self
135 }
136}
137
138impl<E> QueryTrait for Select<E>
139where
140 E: EntityTrait,
141{
142 type QueryStatement = SelectStatement;
143 fn query(&mut self) -> &mut SelectStatement {
144 &mut self.query
145 }
146 fn as_query(&self) -> &SelectStatement {
147 &self.query
148 }
149 fn into_query(self) -> SelectStatement {
150 self.query
151 }
152}
153
154macro_rules! select_two {
155 ( $selector: ident ) => {
156 impl<E, F> QueryTrait for $selector<E, F>
157 where
158 E: EntityTrait,
159 F: EntityTrait,
160 {
161 type QueryStatement = SelectStatement;
162 fn query(&mut self) -> &mut SelectStatement {
163 &mut self.query
164 }
165 fn as_query(&self) -> &SelectStatement {
166 &self.query
167 }
168 fn into_query(self) -> SelectStatement {
169 self.query
170 }
171 }
172 };
173}
174
175select_two!(SelectTwo);
176select_two!(SelectTwoMany);