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
use std::{marker::PhantomData, rc::Rc};
use sea_query::{Alias, ExprTrait, IntoIden};
use crate::{
CustomJoin, Expr, IntoExpr, Table, TableRow,
alias::{JoinableTable, MyAlias, TmpTable},
ast::MySelect,
joinable::IntoJoinable,
private::Joinable,
value::{DbTyp, DynTypedExpr, EqTyp, MyTableRef},
};
/// [Rows] keeps track of all rows in the current query.
///
/// This is the base type for other query types like [crate::args::Aggregate] and [crate::args::Query].
/// It contains basic query functionality like joining tables and filters.
///
/// [Rows] mutability is only about which rows are included.
/// Adding new columns does not require mutating [Rows].
pub struct Rows<'inner, S> {
// we might store 'inner
pub(crate) phantom: PhantomData<fn(&'inner ()) -> &'inner ()>,
pub(crate) _p: PhantomData<S>,
pub(crate) ast: Rc<MySelect>,
}
impl<'inner, S> Rows<'inner, S> {
/// Join a table, this is like a super simple [Iterator::flat_map] but for queries.
///
/// After this operation [Rows] has rows for the combinations of each original row with each row of the table.
/// (Also called the "Carthesian product")
/// The expression that is returned refers to the joined table.
///
/// The parameter must be a table name from the schema like `v0::User`.
/// This table can be filtered by `#[index]`: `rows.join(v0::User.score(100))`.
///
/// See also [Self::filter_some] if you want to join a table that is filtered by `#[unique]`.
pub fn join<T: DbTyp>(
&mut self,
j: impl IntoJoinable<'inner, S, Typ = T>,
) -> Expr<'inner, S, T> {
let joinable = j.into_joinable();
let table_idx = self.ast.tables.len();
Rc::make_mut(&mut self.ast)
.tables
.push(joinable.table.clone());
for (name, val) in joinable.conds {
self.filter(Expr::adhoc(move |b| {
// it is fine to directly use the alias here because the filter is in the same scope as the join
sea_query::Expr::col((MyAlias::new(table_idx), name)).eq((val.func)(b))
}));
}
let table_idx = MyTableRef {
scope_rc: self.ast.scope_rc.clone(),
idx: table_idx,
table_name: joinable.table,
};
Expr::adhoc_promise(
move |b| {
sea_query::Expr::col((
b.get_table(table_idx.clone()),
Alias::new(table_idx.table_name.main_column()),
))
},
false, // the table is joined so this column is not null
)
}
#[doc(hidden)]
pub fn join_private<T: Table<Schema = S>>(&mut self) -> Expr<'inner, S, TableRow<T>> {
self.join(Joinable::table())
}
pub(crate) fn join_custom<T: CustomJoin<Schema = S>>(
&mut self,
t: T,
) -> Expr<'inner, S, TableRow<T>> {
self.join(Joinable::new(t.name()))
}
pub(crate) fn join_tmp<T: Table<Schema = S>>(
&mut self,
tmp: TmpTable,
) -> Expr<'inner, S, TableRow<T>> {
let tmp_string = tmp.into_iden();
self.join(Joinable::new(JoinableTable::Normal(tmp_string)))
}
/// Filter rows based on an expression.
pub fn filter(&mut self, prop: impl IntoExpr<'inner, S, Typ = bool>) {
let prop = DynTypedExpr::erase(prop);
Rc::make_mut(&mut self.ast).filters.push(prop);
}
/// Filter out rows where this expression is [None].
///
/// Returns a new expression with the unwrapped type.
pub fn filter_some<Typ: EqTyp>(
&mut self,
val: impl IntoExpr<'inner, S, Typ = Option<Typ>>,
) -> Expr<'inner, S, Typ> {
let val = val.into_expr();
Rc::make_mut(&mut self.ast)
.filters
.push(DynTypedExpr::erase(val.is_some()));
// we already removed all rows with null, so this is ok.
Expr::adhoc_promise(move |b| val.inner.build_expr(b), false)
}
}