rust_query/value/
into_expr.rs1use std::rc::Rc;
2
3use crate::{
4 Expr, Lazy, Table, TableRow,
5 lower::{self, ord_rc::OrdRc},
6 value::{DbTyp, EqTyp},
7};
8
9pub trait IntoExpr<'column, S> {
25 type Typ: DbTyp;
27
28 fn into_expr(self) -> Expr<'column, S, Self::Typ>;
30}
31
32impl<'column, S, T: IntoExpr<'column, S, Typ = X>, X: EqTyp> IntoExpr<'column, S> for Option<T> {
33 type Typ = Option<X>;
34 fn into_expr(self) -> Expr<'column, S, Self::Typ> {
35 let this = self.map(|x| x.into_expr().inner);
36
37 Expr::new(this.unwrap_or_else(|| Rc::new(lower::Expr::Constant("NULL"))))
39 }
40}
41
42impl<'column, S> IntoExpr<'column, S> for String {
43 type Typ = String;
44 fn into_expr(self) -> Expr<'column, S, Self::Typ> {
45 Expr::adhoc(lower::Expr::Parameter(OrdRc::new(self)))
46 }
47}
48
49impl<'column, S> IntoExpr<'column, S> for &str {
50 type Typ = String;
51 fn into_expr(self) -> Expr<'column, S, Self::Typ> {
52 self.to_owned().into_expr()
53 }
54}
55
56impl<'column, S> IntoExpr<'column, S> for Vec<u8> {
57 type Typ = Vec<u8>;
58 fn into_expr(self) -> Expr<'column, S, Self::Typ> {
59 Expr::adhoc(lower::Expr::Parameter(OrdRc::new(self)))
60 }
61}
62
63impl<'column, S> IntoExpr<'column, S> for &[u8] {
64 type Typ = Vec<u8>;
65 fn into_expr(self) -> Expr<'column, S, Self::Typ> {
66 self.to_owned().into_expr()
67 }
68}
69
70impl<'column, S> IntoExpr<'column, S> for bool {
71 type Typ = bool;
72 fn into_expr(self) -> Expr<'column, S, Self::Typ> {
73 Expr::adhoc(lower::Expr::Parameter(OrdRc::new(self)))
74 }
75}
76
77impl<'column, S> IntoExpr<'column, S> for i64 {
78 type Typ = i64;
79 fn into_expr(self) -> Expr<'column, S, Self::Typ> {
80 Expr::adhoc(lower::Expr::Parameter(OrdRc::new(self)))
81 }
82}
83impl<'column, S> IntoExpr<'column, S> for f64 {
84 type Typ = f64;
85 fn into_expr(self) -> Expr<'column, S, Self::Typ> {
86 Expr::adhoc(lower::Expr::Parameter(OrdRc::new(self)))
87 }
88}
89
90#[cfg(feature = "jiff-02")]
91impl<'column, S> IntoExpr<'column, S> for jiff::Timestamp {
97 type Typ = Self;
98 fn into_expr(self) -> Expr<'column, S, Self::Typ> {
99 Expr::adhoc(lower::Expr::Parameter(OrdRc::new(self.out_to_value())))
100 }
101}
102
103#[cfg(feature = "jiff-02")]
104impl<'column, S> IntoExpr<'column, S> for jiff::civil::Date {
108 type Typ = Self;
109
110 fn into_expr(self) -> Expr<'column, S, Self::Typ> {
111 Expr::adhoc(lower::Expr::Parameter(OrdRc::new(self.out_to_value())))
112 }
113}
114
115impl<'column, S, T> IntoExpr<'column, S> for &T
116where
117 T: IntoExpr<'column, S> + Clone,
118{
119 type Typ = T::Typ;
120 fn into_expr(self) -> Expr<'column, S, Self::Typ> {
121 T::into_expr(self.clone())
122 }
123}
124
125impl<'column, T: Table> IntoExpr<'column, T::Schema> for TableRow<T> {
126 type Typ = Self;
127 fn into_expr(self) -> Expr<'static, T::Schema, Self::Typ> {
128 let idx = self.inner.idx;
129
130 Expr::adhoc(lower::Expr::Parameter(OrdRc::new(idx)))
131 }
132}
133
134impl<'column, T: Table> IntoExpr<'column, T::Schema> for Lazy<'_, T> {
135 type Typ = TableRow<T>;
136
137 fn into_expr(self) -> crate::Expr<'column, T::Schema, Self::Typ> {
138 self.id.into_expr()
139 }
140}
141
142impl<'column, S, T: DbTyp> IntoExpr<'column, S> for Expr<'column, S, T> {
143 type Typ = T;
144 fn into_expr(self) -> Expr<'column, S, Self::Typ> {
145 self
146 }
147}