Skip to main content

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