rust_query/
rows.rs

1use std::{marker::PhantomData, rc::Rc};
2
3use sea_query::{ExprTrait, IntoIden};
4
5use crate::{
6    CustomJoin, Expr, Table,
7    alias::{Field, JoinableTable, TmpTable},
8    ast::MySelect,
9    db::Join,
10    joinable::Joinable,
11    value::{DynTypedExpr, IntoExpr, MyTableRef, MyTyp},
12};
13
14/// [Rows] keeps track of all rows in the current query.
15///
16/// This is the base type for other query types like [crate::args::Aggregate] and [crate::args::Query].
17/// It contains basic query functionality like joining tables and filters.
18///
19/// [Rows] mutability is only about which rows are included.
20/// Adding new columns does not require mutating [Rows].
21pub struct Rows<'inner, S> {
22    // we might store 'inner
23    pub(crate) phantom: PhantomData<fn(&'inner ()) -> &'inner ()>,
24    pub(crate) _p: PhantomData<S>,
25    pub(crate) ast: Rc<MySelect>,
26}
27
28impl<'inner, S> Rows<'inner, S> {
29    /// Join a table, this is like a super simple [Iterator::flat_map] but for queries.
30    ///
31    /// After this operation [Rows] has rows for the combinations of each original row with each row of the table.
32    /// (Also called the "Carthesian product")
33    /// The expression that is returned refers to the joined table.
34    ///
35    /// The parameter must be a table name from the schema like `v0::User`.
36    /// This table can be filtered by `#[index]`: `rows.join(v0::User.score(100))`.
37    ///
38    /// See also [Self::filter_some] if you want to join a table that is filtered by `#[unique]`.
39    pub fn join<T: Table<Schema = S>>(
40        &mut self,
41        j: impl Joinable<'inner, Typ = T>,
42    ) -> Expr<'inner, S, T> {
43        let out = self.join_private::<T>();
44        for (name, val) in j.conds() {
45            let out = out.inner.clone();
46            self.filter(Expr::adhoc(move |b| {
47                sea_query::Expr::col((out.build_table(b), Field::Str(name))).eq((val.func)(b))
48            }));
49        }
50        out
51    }
52
53    #[doc(hidden)]
54    pub fn join_private<T: Table<Schema = S>>(&mut self) -> Expr<'inner, S, T> {
55        self.join_inner(JoinableTable::Normal(T::NAME.into()))
56    }
57
58    pub(crate) fn join_custom<T: CustomJoin<Schema = S>>(&mut self, t: T) -> Expr<'inner, S, T> {
59        self.join_inner(t.name())
60    }
61
62    pub(crate) fn join_tmp<T: Table<Schema = S>>(&mut self, tmp: TmpTable) -> Expr<'inner, S, T> {
63        let tmp_string = tmp.into_iden();
64        self.join_inner(JoinableTable::Normal(tmp_string))
65    }
66
67    fn join_inner<T: Table<Schema = S>>(&mut self, name: JoinableTable) -> Expr<'inner, S, T> {
68        let table_idx = self.ast.tables.len();
69        Rc::make_mut(&mut self.ast).tables.push(name);
70        Expr::new(Join::new(MyTableRef {
71            scope_rc: self.ast.scope_rc.clone(),
72            idx: table_idx,
73        }))
74    }
75
76    // Join a vector of values.
77    // pub fn vec<V: IntoExpr<'inner>>(&mut self, vec: Vec<V>) -> Join<'inner, V::Typ> {
78    //     todo!()
79    // }
80
81    /// Filter rows based on an expression.
82    pub fn filter(&mut self, prop: impl IntoExpr<'inner, S, Typ = bool>) {
83        let prop = DynTypedExpr::erase(prop);
84        Rc::make_mut(&mut self.ast).filters.push(prop);
85    }
86
87    /// Filter out rows where this expression is [None].
88    ///
89    /// Returns a new expression with the unwrapped type.
90    pub fn filter_some<Typ: MyTyp>(
91        &mut self,
92        val: impl IntoExpr<'inner, S, Typ = Option<Typ>>,
93    ) -> Expr<'inner, S, Typ> {
94        let val = val.into_expr();
95        Rc::make_mut(&mut self.ast)
96            .filters
97            .push(DynTypedExpr::erase(val.is_some()));
98
99        // we already removed all rows with null, so this is ok.
100        Expr::adhoc_promise(move |b| val.inner.build_expr(b), false)
101    }
102}