1use crate::expr::{Column, ColumnKey, ExprKind, Keyed, LabelKey, Labeled, SqlType};
4use crate::render::SelectItem;
5use std::marker::PhantomData;
6
7use crate::row::{Named, Row, RowCons, RowKey, RowNil};
8use crate::scope::{Find, Superset, Table, WrapNullable};
9
10mod private {
16 pub trait Sealed<Scope, Idx> {}
21}
22
23#[doc(hidden)]
28pub trait SelectableSealed {}
29
30impl<C, Scope, Idx> private::Sealed<Scope, Idx> for Column<C>
31where
32 C: crate::expr::ColumnKey,
33 Scope: Find<C::Table, Idx>,
34{
35}
36
37impl<K, Req, S: SqlType, Scope, Idx> private::Sealed<Scope, Idx> for Keyed<K, Req, S> where
38 Scope: Superset<Req, Idx>
39{
40}
41
42impl<K, Inner, Scope, Idx> private::Sealed<Scope, Idx> for Labeled<K, Inner> where
43 Inner: RowField<Scope, Idx>
44{
45}
46
47impl<T: AllColumns, Scope, Idx> private::Sealed<Scope, Idx> for All<T> where
48 T::Columns: ColumnList<Scope, Idx>
49{
50}
51
52pub trait SingleColumn {}
57
58#[diagnostic::on_unimplemented(
71 message = "`{Self}` can't be a field of this query's rows",
72 label = "a column, an aggregate, a window function, a `sql!` fragment, or a labelled one of those can be",
73 note = "an expression the builder inferred a type for — a comparison, an `is_null`, a `LIKE` — has to state what it decodes to with `.decodes_as::<..>()`, since that inference can contradict the join; a `sql!` fragment already states it"
74)]
75pub trait RowField<Scope, Idx>: RowKey + private::Sealed<Scope, Idx> {
76 type Value;
77 type Sql: SqlType;
82 fn item(&self) -> SelectItem;
83}
84
85impl<C: ColumnKey, Scope, Idx> RowField<Scope, Idx> for Column<C>
86where
87 Scope: Find<C::Table, Idx>,
88 C::Sql: WrapNullable<<Scope as Find<C::Table, Idx>>::Nullability>,
89 <C::Sql as WrapNullable<<Scope as Find<C::Table, Idx>>::Nullability>>::Output: SqlType,
90{
91 type Value = <<C::Sql as WrapNullable<
92 <Scope as Find<C::Table, Idx>>::Nullability,
93 >>::Output as SqlType>::Native;
94 type Sql = <C::Sql as WrapNullable<<Scope as Find<C::Table, Idx>>::Nullability>>::Output;
95 fn item(&self) -> SelectItem {
96 SelectItem::bare(ExprKind::Column {
97 table: <C::Table as Table>::NAME,
98 name: C::NAME,
99 })
100 }
101}
102
103impl<K, Req, S: SqlType, Scope, Idx> RowField<Scope, Idx> for Keyed<K, Req, S>
104where
105 Scope: Superset<Req, Idx>,
106{
107 type Value = S::Native;
108 type Sql = S;
109 fn item(&self) -> SelectItem {
110 SelectItem::bare(self.kind.clone())
111 }
112}
113
114impl<K: LabelKey, Inner: RowField<Scope, Idx>, Scope, Idx> RowField<Scope, Idx>
119 for Labeled<K, Inner>
120{
121 type Value = Inner::Value;
122 type Sql = Inner::Sql;
123 fn item(&self) -> SelectItem {
124 SelectItem::labeled(self.inner.item().kind, <K as Named>::NAME)
125 }
126}
127
128#[diagnostic::on_unimplemented(
132 message = "`{Self}` isn't a valid selection list here",
133 label = "a selection is a column, an aggregate, a window function, a `sql!` fragment, a labelled one of those, `<table>::All`, or a tuple of up to 32 of them",
134 note = "every element has to be in scope — `.from(..)`/`.join(..)` the tables it names — and an expression the builder inferred a type for has to state its decoded type with `.decodes_as::<..>()`"
135)]
136pub trait Selection<Scope, Idx>: private::Sealed<Scope, Idx> {
137 type Output;
138 fn items(&self) -> Vec<SelectItem>;
139}
140
141macro_rules! scalar_selection {
142 (impl[$($generics:tt)*] $ty:ty) => {
143 impl<$($generics)*, Scope, Idx> Selection<Scope, Idx> for $ty
144 where
145 $ty: RowField<Scope, Idx>,
146 {
147 type Output = <$ty as RowField<Scope, Idx>>::Value;
148 fn items(&self) -> Vec<SelectItem> {
149 vec![RowField::item(self)]
150 }
151 }
152 };
153}
154
155#[diagnostic::on_unimplemented(
160 message = "`{Self}` can't be part of a selection list",
161 label = "a column, an aggregate, a window function, a `sql!` fragment, a labelled one of those, or `<table>::All` can be",
162 note = "an expression the builder inferred a type for — a comparison, an `is_null`, a `LIKE` — has to state what it decodes to with `.decodes_as::<..>()`, since that inference can contradict the join"
163)]
164pub trait SelectionPart<Scope, Idx>: private::Sealed<Scope, Idx> {
165 type Fields<Tail>;
166 fn push_items(&self, out: &mut Vec<SelectItem>);
167}
168
169macro_rules! field_part {
170 (impl[$($generics:tt)*] $ty:ty) => {
171 impl<$($generics)*, Scope, Idx> SelectionPart<Scope, Idx> for $ty
172 where
173 $ty: RowField<Scope, Idx>,
174 {
175 type Fields<Tail> = RowCons<
176 <$ty as RowKey>::Key,
177 <$ty as RowField<Scope, Idx>>::Value,
178 Tail,
179 >;
180 fn push_items(&self, out: &mut Vec<SelectItem>) {
181 out.push(RowField::item(self));
182 }
183 }
184 };
185}
186macro_rules! selectable {
190 (impl[$($generics:tt)*] $ty:ty) => {
191 scalar_selection!(impl[$($generics)*] $ty);
192 field_part!(impl[$($generics)*] $ty);
193 };
194}
195selectable!(impl[C: ColumnKey] Column<C>);
196selectable!(impl[K, Req, S: SqlType] Keyed<K, Req, S>);
197selectable!(impl[K, Inner] Labeled<K, Inner>);
198
199pub struct All<T>(PhantomData<fn() -> T>);
205
206impl<T> All<T> {
207 pub const fn new() -> Self {
208 All(PhantomData)
209 }
210}
211
212impl<T> Clone for All<T> {
213 fn clone(&self) -> Self {
214 *self
215 }
216}
217impl<T> Copy for All<T> {}
218
219impl<T> Default for All<T> {
220 fn default() -> Self {
221 All::new()
222 }
223}
224
225pub trait AllColumns: SelectableSealed {
228 type Columns;
234}
235
236mod column_list {
244 pub trait Sealed {}
245 impl Sealed for crate::scope::Nil {}
246 impl<C: crate::expr::ColumnKey, Tail> Sealed for crate::scope::Cons<crate::expr::Column<C>, Tail> {}
247}
248
249pub trait ColumnList<Scope, Idx>: column_list::Sealed {
250 type Fields<Tail>;
251 fn push_items(out: &mut Vec<SelectItem>);
252}
253
254impl<Scope, Idx> ColumnList<Scope, Idx> for crate::scope::Nil {
255 type Fields<Tail> = Tail;
256 fn push_items(_out: &mut Vec<SelectItem>) {}
257}
258
259impl<C: ColumnKey, Tail, Scope, Idx> ColumnList<Scope, Idx> for crate::scope::Cons<Column<C>, Tail>
260where
261 Column<C>: RowField<Scope, Idx>,
262 Tail: ColumnList<Scope, Idx>,
263{
264 type Fields<T> = RowCons<C, <Column<C> as RowField<Scope, Idx>>::Value, Tail::Fields<T>>;
265 fn push_items(out: &mut Vec<SelectItem>) {
266 out.push(RowField::item(&Column::<C>::new()));
267 Tail::push_items(out);
268 }
269}
270
271impl<T: AllColumns, Scope, Idx> SelectionPart<Scope, Idx> for All<T>
272where
273 T::Columns: ColumnList<Scope, Idx>,
274{
275 type Fields<Tail> = <T::Columns as ColumnList<Scope, Idx>>::Fields<Tail>;
276 fn push_items(&self, out: &mut Vec<SelectItem>) {
277 <T::Columns as ColumnList<Scope, Idx>>::push_items(out);
278 }
279}
280
281impl<T: AllColumns, Scope, Idx> Selection<Scope, Idx> for All<T>
282where
283 T::Columns: ColumnList<Scope, Idx>,
284{
285 type Output = Row<<T::Columns as ColumnList<Scope, Idx>>::Fields<RowNil>>;
286 fn items(&self) -> Vec<SelectItem> {
287 let mut out = Vec::new();
288 <T::Columns as ColumnList<Scope, Idx>>::push_items(&mut out);
289 out
290 }
291}
292
293macro_rules! row_chain {
294 ($n:ident $i:ident) => {
295 <$n as SelectionPart<Scope, $i>>::Fields<RowNil>
296 };
297 ($n:ident $i:ident, $($rest:tt)*) => {
298 <$n as SelectionPart<Scope, $i>>::Fields<row_chain!($($rest)*)>
299 };
300}
301
302macro_rules! tuple_selection {
303 ($($n:ident $i:ident),+) => {
304 impl<Scope, $($n,)+ $($i,)+> private::Sealed<Scope, ($($i,)+)> for ($($n,)+)
305 where
306 $($n: SelectionPart<Scope, $i>,)+
307 {
308 }
309
310 #[allow(non_snake_case)]
311 impl<Scope, $($n,)+ $($i,)+> Selection<Scope, ($($i,)+)> for ($($n,)+)
312 where
313 $($n: SelectionPart<Scope, $i>,)+
314 {
315 type Output = Row<row_chain!($($n $i),+)>;
316 fn items(&self) -> Vec<SelectItem> {
317 let ($($n,)+) = self;
318 let mut out = Vec::new();
319 $(SelectionPart::push_items($n, &mut out);)+
320 out
321 }
322 }
323 };
324}
325tuple_selection!(A IA);
326tuple_selection!(A IA, B IB);
327tuple_selection!(A IA, B IB, C IC);
328tuple_selection!(A IA, B IB, C IC, D ID);
329tuple_selection!(A IA, B IB, C IC, D ID, E IE);
330tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF);
331tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG);
332tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH);
333tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II);
334tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ);
335tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK);
336tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL);
337tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM);
338tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN);
339tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO);
340tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP);
341tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ);
342tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR);
343tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS);
344tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS, T IT);
345tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS, T IT, U IU);
346tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS, T IT, U IU, V IV);
347tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS, T IT, U IU, V IV, W IW);
348tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS, T IT, U IU, V IV, W IW, X IX);
349tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS, T IT, U IU, V IV, W IW, X IX, Y IY);
350tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS, T IT, U IU, V IV, W IW, X IX, Y IY, Z IZ);
351tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS, T IT, U IU, V IV, W IW, X IX, Y IY, Z IZ, AA IAA);
352tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS, T IT, U IU, V IV, W IW, X IX, Y IY, Z IZ, AA IAA, BB IBB);
353tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS, T IT, U IU, V IV, W IW, X IX, Y IY, Z IZ, AA IAA, BB IBB, CC ICC);
354tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS, T IT, U IU, V IV, W IW, X IX, Y IY, Z IZ, AA IAA, BB IBB, CC ICC, DD IDD);
355tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS, T IT, U IU, V IV, W IW, X IX, Y IY, Z IZ, AA IAA, BB IBB, CC ICC, DD IDD, EE IEE);
356tuple_selection!(A IA, B IB, C IC, D ID, E IE, F IF, G IG, H IH, I II, J IJ, K IK, L IL, M IM, N IN, O IO, P IP, Q IQ, R IR, S IS, T IT, U IU, V IV, W IW, X IX, Y IY, Z IZ, AA IAA, BB IBB, CC ICC, DD IDD, EE IEE, FF IFF);