drizzle_core/expr/
primitives.rs

1//! Expr implementations for Rust primitive types.
2//!
3//! These implementations allow using Rust literals directly in type-safe
4//! SQL expressions.
5
6use crate::prelude::*;
7use crate::traits::SQLParam;
8
9use super::{Expr, NonNull, Null, Nullability, Scalar};
10use crate::types::{BigInt, Bool, Double, Float, Int, SmallInt, Text};
11
12// =============================================================================
13// Integer Types
14// =============================================================================
15
16impl<'a, V> Expr<'a, V> for i8
17where
18    V: SQLParam + 'a + From<i8>,
19    V: Into<Cow<'a, V>>,
20{
21    type SQLType = SmallInt;
22    type Nullable = NonNull;
23    type Aggregate = Scalar;
24}
25
26impl<'a, V> Expr<'a, V> for i16
27where
28    V: SQLParam + 'a + From<i16>,
29    V: Into<Cow<'a, V>>,
30{
31    type SQLType = SmallInt;
32    type Nullable = NonNull;
33    type Aggregate = Scalar;
34}
35
36impl<'a, V> Expr<'a, V> for i32
37where
38    V: SQLParam + 'a + From<i32>,
39    V: Into<Cow<'a, V>>,
40{
41    type SQLType = Int;
42    type Nullable = NonNull;
43    type Aggregate = Scalar;
44}
45
46impl<'a, V> Expr<'a, V> for i64
47where
48    V: SQLParam + 'a + From<i64>,
49    V: Into<Cow<'a, V>>,
50{
51    type SQLType = BigInt;
52    type Nullable = NonNull;
53    type Aggregate = Scalar;
54}
55
56impl<'a, V> Expr<'a, V> for isize
57where
58    V: SQLParam + 'a + From<isize>,
59    V: Into<Cow<'a, V>>,
60{
61    type SQLType = BigInt;
62    type Nullable = NonNull;
63    type Aggregate = Scalar;
64}
65
66// =============================================================================
67// Unsigned Integer Types
68// =============================================================================
69
70impl<'a, V> Expr<'a, V> for u8
71where
72    V: SQLParam + 'a + From<u8>,
73    V: Into<Cow<'a, V>>,
74{
75    type SQLType = SmallInt;
76    type Nullable = NonNull;
77    type Aggregate = Scalar;
78}
79
80impl<'a, V> Expr<'a, V> for u16
81where
82    V: SQLParam + 'a + From<u16>,
83    V: Into<Cow<'a, V>>,
84{
85    type SQLType = Int;
86    type Nullable = NonNull;
87    type Aggregate = Scalar;
88}
89
90impl<'a, V> Expr<'a, V> for u32
91where
92    V: SQLParam + 'a + From<u32>,
93    V: Into<Cow<'a, V>>,
94{
95    type SQLType = BigInt;
96    type Nullable = NonNull;
97    type Aggregate = Scalar;
98}
99
100impl<'a, V> Expr<'a, V> for u64
101where
102    V: SQLParam + 'a + From<u64>,
103    V: Into<Cow<'a, V>>,
104{
105    type SQLType = BigInt;
106    type Nullable = NonNull;
107    type Aggregate = Scalar;
108}
109
110impl<'a, V> Expr<'a, V> for usize
111where
112    V: SQLParam + 'a + From<usize>,
113    V: Into<Cow<'a, V>>,
114{
115    type SQLType = BigInt;
116    type Nullable = NonNull;
117    type Aggregate = Scalar;
118}
119
120// =============================================================================
121// Floating-Point Types
122// =============================================================================
123
124impl<'a, V> Expr<'a, V> for f32
125where
126    V: SQLParam + 'a + From<f32>,
127    V: Into<Cow<'a, V>>,
128{
129    type SQLType = Float;
130    type Nullable = NonNull;
131    type Aggregate = Scalar;
132}
133
134impl<'a, V> Expr<'a, V> for f64
135where
136    V: SQLParam + 'a + From<f64>,
137    V: Into<Cow<'a, V>>,
138{
139    type SQLType = Double;
140    type Nullable = NonNull;
141    type Aggregate = Scalar;
142}
143
144// =============================================================================
145// Boolean Type
146// =============================================================================
147
148impl<'a, V> Expr<'a, V> for bool
149where
150    V: SQLParam + 'a + From<bool>,
151    V: Into<Cow<'a, V>>,
152{
153    type SQLType = Bool;
154    type Nullable = NonNull;
155    type Aggregate = Scalar;
156}
157
158// =============================================================================
159// String Types
160// =============================================================================
161
162impl<'a, V> Expr<'a, V> for &'a str
163where
164    V: SQLParam + 'a + From<&'a str>,
165    V: Into<Cow<'a, V>>,
166{
167    type SQLType = Text;
168    type Nullable = NonNull;
169    type Aggregate = Scalar;
170}
171
172impl<'a, V> Expr<'a, V> for String
173where
174    V: SQLParam + 'a + From<String>,
175    V: Into<Cow<'a, V>>,
176{
177    type SQLType = Text;
178    type Nullable = NonNull;
179    type Aggregate = Scalar;
180}
181
182// =============================================================================
183// Option<T> - Makes Any Expression Nullable
184// =============================================================================
185
186impl<'a, V, T> Expr<'a, V> for Option<T>
187where
188    V: SQLParam + 'a,
189    T: Expr<'a, V>,
190    T::Nullable: Nullability,
191{
192    type SQLType = T::SQLType;
193    type Nullable = Null;
194    type Aggregate = T::Aggregate;
195}
196
197// =============================================================================
198// Reference Types - Delegate to Inner
199// =============================================================================
200
201impl<'a, V, T> Expr<'a, V> for &T
202where
203    V: SQLParam + 'a,
204    T: Expr<'a, V>,
205    T::Nullable: Nullability,
206{
207    type SQLType = T::SQLType;
208    type Nullable = T::Nullable;
209    type Aggregate = T::Aggregate;
210}
211
212// =============================================================================
213// UUID (Feature-Gated)
214// =============================================================================
215
216#[cfg(feature = "uuid")]
217impl<'a, V> Expr<'a, V> for uuid::Uuid
218where
219    V: SQLParam + 'a + From<uuid::Uuid>,
220    V: Into<Cow<'a, V>>,
221{
222    type SQLType = crate::types::Uuid;
223    type Nullable = NonNull;
224    type Aggregate = Scalar;
225}
226
227// =============================================================================
228// SQL Type - Backward Compatibility
229// Allows untyped columns (which return SQL) to work with typed functions.
230// =============================================================================
231
232impl<'a, V> Expr<'a, V> for crate::sql::SQL<'a, V>
233where
234    V: SQLParam + 'a,
235{
236    type SQLType = crate::types::Any;
237    type Nullable = Null;
238    type Aggregate = Scalar;
239}