1use std::{marker::PhantomData, rc::Rc};
2
3use sea_query::Iden;
4
5use crate::{
6 Expr, Table,
7 alias::TmpTable,
8 ast::MySelect,
9 db::Join,
10 value::{IntoExpr, MyTableRef, Typed},
11};
12
13pub struct Rows<'inner, S> {
21 pub(crate) phantom: PhantomData<fn(&'inner ()) -> &'inner ()>,
23 pub(crate) _p: PhantomData<S>,
24 pub(crate) ast: Rc<MySelect>,
25}
26
27impl<'inner, S> Rows<'inner, S> {
28 pub fn join<T: Table<Schema = S>>(&mut self, _: T) -> Expr<'inner, S, T> {
33 self.join_string(T::NAME.to_owned())
34 }
35
36 pub(crate) fn join_custom<T: Table<Schema = S>>(&mut self, t: T) -> Expr<'inner, S, T> {
37 self.join_string(t.name())
38 }
39
40 pub(crate) fn join_tmp<T: Table<Schema = S>>(&mut self, tmp: TmpTable) -> Expr<'inner, S, T> {
41 let mut tmp_string = String::new();
42 tmp.unquoted(&mut tmp_string);
43 self.join_string(tmp_string)
44 }
45
46 fn join_string<T: Table<Schema = S>>(&mut self, name: String) -> Expr<'inner, S, T> {
47 let table_idx = self.ast.tables.len();
48 Rc::make_mut(&mut self.ast).tables.push(name);
49 Expr::new(Join::new(MyTableRef {
50 scope_rc: self.ast.scope_rc.clone(),
51 idx: table_idx,
52 }))
53 }
54
55 pub fn filter(&mut self, prop: impl IntoExpr<'inner, S, Typ = bool>) {
62 let prop = prop.into_expr();
63 Rc::make_mut(&mut self.ast).filters.push(prop.inner);
64 }
65
66 pub fn filter_some<Typ: 'static>(
70 &mut self,
71 val: impl IntoExpr<'inner, S, Typ = Option<Typ>>,
72 ) -> Expr<'inner, S, Typ> {
73 let val = val.into_expr();
74 Rc::make_mut(&mut self.ast)
75 .filters
76 .push(val.is_some().inner);
77
78 Expr::adhoc(move |b| val.inner.build_expr(b))
79 }
80}