1pub mod aggregate;
2mod db_typ;
3pub mod from_expr;
4pub mod into_expr;
5#[cfg(feature = "jiff-02")]
6mod jiff_operations;
7mod operations;
8pub mod optional;
9
10use std::{cell::OnceCell, fmt::Debug, marker::PhantomData, ops::Deref, rc::Rc};
11
12use crate::{
13 IntoExpr, IntoSelect, Select, Table,
14 db::TableRow,
15 lower::{self, JoinableTable},
16 mutable::Mutable,
17 private::IntoJoinable,
18};
19pub use db_typ::{DbTyp, StorableTyp};
20
21pub trait NumTyp: OrdTyp + Clone + Copy {
22 const ZERO: &str;
23}
24
25impl NumTyp for i64 {
26 const ZERO: &str = "0";
27}
28impl NumTyp for f64 {
29 const ZERO: &str = "0.0";
30}
31
32pub trait OrdTyp: EqTyp {}
33impl OrdTyp for String {}
34impl OrdTyp for Vec<u8> {}
35impl OrdTyp for i64 {}
36impl OrdTyp for f64 {}
37impl OrdTyp for bool {}
38#[cfg(feature = "jiff-02")]
39impl OrdTyp for jiff::Timestamp {}
40#[cfg(feature = "jiff-02")]
41impl OrdTyp for jiff::civil::Date {}
42
43pub trait BuffTyp: DbTyp {}
44impl BuffTyp for String {}
45impl BuffTyp for Vec<u8> {}
46
47#[diagnostic::on_unimplemented(
48 message = "Columns with type `{Self}` can not be checked for equality",
49 note = "`EqTyp` is also implemented for all table types"
50)]
51pub trait EqTyp: DbTyp {}
52
53impl EqTyp for String {}
54impl EqTyp for Vec<u8> {}
55impl EqTyp for i64 {}
56impl EqTyp for f64 {}
57impl EqTyp for bool {}
58#[cfg(feature = "jiff-02")]
59impl EqTyp for jiff::Timestamp {}
60#[cfg(feature = "jiff-02")]
61impl EqTyp for jiff::civil::Date {}
62#[diagnostic::do_not_recommend]
63impl<T: Table> EqTyp for TableRow<T> {}
64
65pub trait OptTable: DbTyp {
66 type Schema;
67 type Select;
68 type Mutable<'t>;
69 fn select_opt_mutable(
70 val: Expr<'_, Self::Schema, Self>,
71 ) -> Select<'_, Self::Schema, Self::Select>;
72
73 fn into_mutable<'t>(val: Self::Select) -> Self::Mutable<'t>;
74}
75
76impl<T: Table> OptTable for TableRow<T> {
77 type Schema = T::Schema;
78 type Select = (T::Select, TableRow<T>);
79 type Mutable<'t> = Mutable<'t, T>;
80 fn select_opt_mutable(
81 val: Expr<'_, Self::Schema, Self>,
82 ) -> Select<'_, Self::Schema, Self::Select> {
83 (T::into_select(val.clone()), val).into_select()
84 }
85
86 fn into_mutable<'t>((inner, row_id): Self::Select) -> Self::Mutable<'t> {
87 Mutable::new(T::select_mutable(inner), row_id)
88 }
89}
90
91impl<T: Table> OptTable for Option<TableRow<T>> {
92 type Schema = T::Schema;
93 type Select = Option<(T::Select, TableRow<T>)>;
94 type Mutable<'t> = Option<Mutable<'t, T>>;
95 fn select_opt_mutable(
96 val: Expr<'_, Self::Schema, Self>,
97 ) -> Select<'_, Self::Schema, Self::Select> {
98 crate::optional(|row| {
99 let val = row.and(val);
100 row.then_select((T::into_select(val.clone()), val))
101 })
102 }
103
104 fn into_mutable<'t>(val: Self::Select) -> Self::Mutable<'t> {
105 val.map(TableRow::<T>::into_mutable)
106 }
107}
108
109pub struct Expr<'column, S, T: DbTyp> {
117 pub(crate) _local: PhantomData<*const ()>,
118 pub(crate) inner: Rc<lower::Expr>,
119 pub(crate) _p: PhantomData<&'column ()>,
120 pub(crate) _p2: PhantomData<S>,
121 pub(crate) ext: OnceCell<Box<T::Ext<'static>>>,
122 pub(crate) not_null_key: bool,
124}
125
126#[cfg_attr(false, mutants::skip)]
127impl<S, T: DbTyp> Debug for Expr<'_, S, T> {
128 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
129 write!(f, "Expr of type {}", std::any::type_name::<T>())
130 }
131}
132
133impl<'column, S, T: DbTyp> Expr<'column, S, T> {
134 #[doc(hidden)]
136 pub fn _migrate<OldS>(prev: impl IntoExpr<'column, OldS>) -> Self {
137 let prev = prev.into_expr().inner;
138 Self::new(prev)
139 }
140}
141
142pub fn adhoc_expr<S, T: DbTyp>(f: lower::Expr) -> Expr<'static, S, T> {
143 Expr::adhoc(f)
144}
145
146pub fn new_column<'x, S, C: DbTyp, T: Table>(
147 table: impl IntoExpr<'x, S, Typ = TableRow<T>>,
148 name: &'static str,
149) -> Expr<'x, S, C> {
150 let table = table.into_expr();
151 Expr::new_inner(
152 table.inner.col(
153 JoinableTable::Table(T::NAME),
154 name,
155 T::ID,
156 table.not_null_key,
157 ),
158 table.not_null_key && !C::NULLABLE,
159 )
160}
161
162pub fn unique_from_joinable<'inner, T: Table>(
163 j: impl IntoJoinable<'inner, T::Schema, Typ = TableRow<T>>,
164) -> Expr<'inner, T::Schema, Option<TableRow<T>>> {
165 let joinable = j.into_joinable();
166 let unique = Rc::new(lower::Unique {
167 table: joinable.table,
168 conds: joinable.conds,
169 guaranteed: false,
170 });
171 Expr::adhoc(lower::Expr::RowIndex(lower::RowLike::Unique(unique), T::ID))
172}
173
174impl<S, T: DbTyp> Expr<'_, S, T> {
175 pub(crate) fn adhoc(e: lower::Expr) -> Self {
176 Self::new(Rc::new(e))
177 }
178
179 pub(crate) fn new(val: Rc<lower::Expr>) -> Self {
180 Self::new_inner(val, false)
181 }
182
183 pub(crate) fn new_inner(val: Rc<lower::Expr>, not_null_key: bool) -> Self {
184 Self {
185 _local: PhantomData,
186 inner: val,
187 _p: PhantomData,
188 _p2: PhantomData,
189 ext: OnceCell::new(),
190 not_null_key,
191 }
192 }
193}
194
195impl<S, T: DbTyp> Clone for Expr<'_, S, T> {
196 fn clone(&self) -> Self {
197 Self {
198 _local: PhantomData,
199 inner: self.inner.clone(),
200 _p: self._p,
201 _p2: self._p2,
202 ext: OnceCell::new(),
203 not_null_key: self.not_null_key,
204 }
205 }
206}
207
208impl<'t, T: Table> Deref for Expr<'t, T::Schema, TableRow<T>> {
209 type Target = T::Ext2<'t>;
210
211 fn deref(&self) -> &Self::Target {
212 T::covariant_ext(self.ext.get_or_init(|| {
213 let expr = Expr {
214 _local: PhantomData,
215 inner: self.inner.clone(),
216 _p: PhantomData::<&'static ()>,
217 _p2: PhantomData,
218 ext: OnceCell::new(),
219 not_null_key: self.not_null_key,
220 };
221 Box::new(T::build_ext2(&expr))
222 }))
223 }
224}