1use std::marker::PhantomData;
2
3use crate::{
4 Expr, IntoExpr, Table,
5 value::{DynTypedExpr, MyTyp, NumTyp},
6};
7
8pub struct Update<S, Typ: MyTyp> {
10 inner: Box<dyn Fn(Expr<'static, S, Typ>) -> Expr<'static, S, Typ>>,
11}
12
13impl<S, Typ: MyTyp> Default for Update<S, Typ> {
14 fn default() -> Self {
15 Self {
16 inner: Box::new(|x| x),
17 }
18 }
19}
20
21impl<S: 'static, Typ: MyTyp> Update<S, Typ> {
22 pub fn set(val: impl IntoExpr<'static, S, Typ = Typ>) -> Self {
24 let val = val.into_expr();
25 Self {
26 inner: Box::new(move |_| val.clone()),
27 }
28 }
29
30 #[doc(hidden)]
31 pub fn apply(&self, val: impl IntoExpr<'static, S, Typ = Typ>) -> Expr<'static, S, Typ> {
32 (self.inner)(val.into_expr())
33 }
34}
35
36impl<S: 'static, Typ: NumTyp> Update<S, Typ> {
37 pub fn add(val: impl IntoExpr<'static, S, Typ = Typ>) -> Self {
39 let val = val.into_expr();
40 Self {
41 inner: Box::new(move |old| old.add(&val)),
42 }
43 }
44}
45
46pub trait TableInsert {
48 type T: Table;
49 fn into_insert(self) -> <Self::T as Table>::Insert;
50}
51
52pub struct Reader<S> {
53 pub(crate) builder: Vec<(&'static str, DynTypedExpr)>,
54 pub(crate) _p: PhantomData<S>,
55}
56
57impl<S> Default for Reader<S> {
58 fn default() -> Self {
59 Self {
60 builder: Default::default(),
61 _p: Default::default(),
62 }
63 }
64}
65
66impl<S> Reader<S> {
67 pub fn col(&mut self, name: &'static str, val: impl IntoExpr<'static, S>) {
68 self.col_erased(name, val.into_expr().inner.erase());
69 }
70
71 pub(crate) fn col_erased(&mut self, name: &'static str, val: DynTypedExpr) {
72 self.builder.push((name, val));
73 }
74}