bottle-orm 0.5.9

A lightweight and simple ORM for Rust built on top of sqlx
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use sqlx::{any::AnyRow, Error, Row};
use std::collections::HashMap;
use serde::{Serialize, Deserialize};

// ============================================================================
// AnyInfo Structure
// ============================================================================

/// Contains metadata about a database column.
///
/// This struct is used to describe the schema of a model or query result,
/// providing the necessary information for the query builder to construct
/// valid SQL statements.
#[derive(Debug, Clone)]
pub struct AnyInfo {
    /// The name of the column in the database.
    pub column: &'static str,

    /// The SQL type of the column (e.g., "INTEGER", "TEXT", "UUID").
    pub sql_type: &'static str,

    /// The name of the table this column belongs to (empty for un-associated columns).
    pub table: &'static str,
}

/// A generic placeholder struct that implements AnyImpl.
/// Used in closures where a concrete model type is required but any model should be accepted.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnyImplStruct {}

impl AnyImpl for AnyImplStruct {
    fn columns() -> Vec<AnyInfo> { Vec::new() }
    fn to_map(&self) -> HashMap<String, Option<String>> { HashMap::new() }
}

impl FromAnyRow for AnyImplStruct {
    fn from_any_row(_row: &AnyRow) -> Result<Self, Error> { Ok(AnyImplStruct {}) }
    fn from_any_row_at(_row: &AnyRow, _index: &mut usize) -> Result<Self, Error> { Ok(AnyImplStruct {}) }
}

impl crate::model::Model for AnyImplStruct {
    fn table_name() -> &'static str { "" }
    fn columns() -> Vec<crate::model::ColumnInfo> { Vec::new() }
    fn column_names() -> Vec<String> { Vec::new() }
    fn active_columns() -> Vec<&'static str> { Vec::new() }
    fn relations() -> Vec<crate::model::RelationInfo> { Vec::new() }
    fn load_relations<'a>(
        _relation_name: &'a str,
        _models: &'a mut [Self],
        _tx: &'a dyn crate::database::Connection,
        _query_modifier: Option<std::sync::Arc<dyn std::any::Any + Send + Sync>>,
    ) -> futures::future::BoxFuture<'a, Result<(), sqlx::Error>> {
        Box::pin(async move { Ok(()) })
    }
    fn to_map(&self) -> HashMap<String, Option<String>> { HashMap::new() }
}

/// A trait for types that can be mapped from an `AnyRow` and provide column metadata.
///
/// This trait is the backbone of the ORM's reflection capabilities. It allows the
/// system to know which columns correspond to which fields in a Rust struct.
///
/// This trait is typically implemented automatically via the `FromAnyRow` derive macro,
/// but can be implemented manually for custom scenarios.
pub trait AnyImpl {
    /// Returns a vector of `AnyInfo` describing the columns associated with this type.
    fn columns() -> Vec<AnyInfo>;

    /// Converts this instance to a HashMap for dynamic query building.
    fn to_map(&self) -> HashMap<String, Option<String>>;
}

/// A trait for types that can be mapped from an `AnyRow`.
pub trait FromAnyRow: Sized {
    /// Constructs the type from the whole row.
    fn from_any_row(row: &AnyRow) -> Result<Self, Error>;

    /// Constructs the type from the row starting at the given index,
    /// incrementing the index for each column consumed.
    fn from_any_row_at(row: &AnyRow, index: &mut usize) -> Result<Self, Error>;
}

// ============================================================================
// Primitive Implementations
// ============================================================================

macro_rules! impl_supported_primitive {
    ($($t:ty),*) => {
        $(
            impl AnyImpl for $t {
                fn columns() -> Vec<AnyInfo> { Vec::new() }
                fn to_map(&self) -> HashMap<String, Option<String>> { HashMap::new() }
            }

            impl FromAnyRow for $t {
                fn from_any_row(row: &AnyRow) -> Result<Self, Error> {
                    row.try_get(0).map_err(|e| Error::Decode(Box::new(e)))
                }

                fn from_any_row_at(row: &AnyRow, index: &mut usize) -> Result<Self, Error> {
                    if *index >= row.len() {
                        return Err(Error::ColumnIndexOutOfBounds { index: *index, len: row.len() });
                    }
                    let res = row.try_get(*index);
                    *index += 1;
                    res.map_err(|e| Error::Decode(Box::new(e)))
                }
            }
        )*
    };
}

// Primitives directly supported by sqlx::Any (Decode implemented)
impl_supported_primitive!(bool, i16, i32, i64, f32, f64, String);

macro_rules! impl_cast_primitive {
    ($($t:ty),*) => {
        $(
            impl AnyImpl for $t {
                fn columns() -> Vec<AnyInfo> { Vec::new() }
                fn to_map(&self) -> HashMap<String, Option<String>> { HashMap::new() }
            }

            impl FromAnyRow for $t {
                fn from_any_row(row: &AnyRow) -> Result<Self, Error> {
                    let val: i64 = row.try_get(0).map_err(|e| Error::Decode(Box::new(e)))?;
                    <$t>::try_from(val).map_err(|e| Error::Decode(Box::new(e)))
                }

                fn from_any_row_at(row: &AnyRow, index: &mut usize) -> Result<Self, Error> {
                    if *index >= row.len() {
                        return Err(Error::ColumnIndexOutOfBounds { index: *index, len: row.len() });
                    }
                    let res = row.try_get::<i64, _>(*index);
                    *index += 1;
                    let val = res.map_err(|e| Error::Decode(Box::new(e)))?;
                    <$t>::try_from(val).map_err(|e| Error::Decode(Box::new(e)))
                }
            }
        )*
    };
}

// Primitives that might need casting from i64
impl_cast_primitive!(i8, isize, u8, u16, u32, u64, usize);

// ============================================================================
// Array and JSON Implementations
// ============================================================================

impl<T> AnyImpl for Vec<T>
where
    T: AnyImpl + Serialize + for<'de> Deserialize<'de>,
{
    fn columns() -> Vec<AnyInfo> {
        Vec::new()
    }
    fn to_map(&self) -> HashMap<String, Option<String>> {
        let mut map = HashMap::new();
        if let Ok(json) = serde_json::to_string(self) {
            map.insert("".to_string(), Some(json));
        }
        map
    }
}

impl<T> FromAnyRow for Vec<T>
where
    T: Serialize + for<'de> Deserialize<'de> + Send,
{
    fn from_any_row(row: &AnyRow) -> Result<Self, Error> {
        let mut index = 0;
        Self::from_any_row_at(row, &mut index)
    }

    fn from_any_row_at(row: &AnyRow, index: &mut usize) -> Result<Self, Error> {
        if *index >= row.len() {
            return Err(Error::ColumnIndexOutOfBounds { index: *index, len: row.len() });
        }
        let res = row.try_get::<String, _>(*index);
        *index += 1;
        let s = res.map_err(|e| Error::Decode(Box::new(e)))?;
        serde_json::from_str(&s).map_err(|e| Error::Decode(Box::new(e)))
    }
}

impl AnyImpl for serde_json::Value {
    fn columns() -> Vec<AnyInfo> {
        Vec::new()
    }
    fn to_map(&self) -> HashMap<String, Option<String>> {
        let mut map = HashMap::new();
        map.insert("".to_string(), Some(self.to_string()));
        map
    }
}

impl FromAnyRow for serde_json::Value {
    fn from_any_row(row: &AnyRow) -> Result<Self, Error> {
        let mut index = 0;
        Self::from_any_row_at(row, &mut index)
    }

    fn from_any_row_at(row: &AnyRow, index: &mut usize) -> Result<Self, Error> {
        if *index >= row.len() {
            return Err(Error::ColumnIndexOutOfBounds { index: *index, len: row.len() });
        }
        let res = row.try_get::<String, _>(*index);
        *index += 1;
        let s = res.map_err(|e| Error::Decode(Box::new(e)))?;
        serde_json::from_str(&s).map_err(|e| Error::Decode(Box::new(e)))
    }
}

// ============================================================================
// External Type Implementations
// ============================================================================

impl AnyImpl for uuid::Uuid {
    fn columns() -> Vec<AnyInfo> {
        Vec::new()
    }
    fn to_map(&self) -> HashMap<String, Option<String>> {
        HashMap::new()
    }
}

impl FromAnyRow for uuid::Uuid {
    fn from_any_row(row: &AnyRow) -> Result<Self, Error> {
        let mut index = 0;
        Self::from_any_row_at(row, &mut index)
    }

    fn from_any_row_at(row: &AnyRow, index: &mut usize) -> Result<Self, Error> {
        if *index >= row.len() {
            return Err(Error::ColumnIndexOutOfBounds { index: *index, len: row.len() });
        }
        let res = row.try_get::<String, _>(*index);
        *index += 1;
        let s = res.map_err(|e| Error::Decode(Box::new(e)))?;
        s.parse().map_err(|e| Error::Decode(Box::new(e)))
    }
}

impl AnyImpl for chrono::NaiveDateTime {
    fn columns() -> Vec<AnyInfo> {
        Vec::new()
    }
    fn to_map(&self) -> HashMap<String, Option<String>> {
        HashMap::new()
    }
}

impl FromAnyRow for chrono::NaiveDateTime {
    fn from_any_row(row: &AnyRow) -> Result<Self, Error> {
        let mut index = 0;
        Self::from_any_row_at(row, &mut index)
    }

    fn from_any_row_at(row: &AnyRow, index: &mut usize) -> Result<Self, Error> {
        if *index >= row.len() {
            return Err(Error::ColumnIndexOutOfBounds { index: *index, len: row.len() });
        }
        let res = row.try_get::<String, _>(*index);
        match res {
            Ok(s) => {
                *index += 1;
                crate::temporal::parse_naive_datetime(&s).map_err(|e| Error::Decode(Box::new(e)))
            }
            Err(e) => {
                // Try numeric fallback (some drivers might return i64 for timestamps)
                if let Ok(i) = row.try_get::<i64, _>(*index) {
                    *index += 1;
                    return Ok(chrono::DateTime::from_timestamp(i, 0).map(|dt| dt.naive_utc()).unwrap_or_default());
                }
                // If both fail, we should still increment if it's likely a column was there but we couldn't decode it
                // Actually, for temporal it's tricky, but if it's NULL, both try_get will fail.
                // Let's check for NULL explicitly.
                if let Ok(None) = row.try_get::<Option<String>, _>(*index) {
                     *index += 1;
                     return Err(Error::Decode(Box::new(e))); // Option will catch this
                }

                Err(Error::Decode(Box::new(e)))
            }
        }
    }
}

impl AnyImpl for chrono::NaiveDate {
    fn columns() -> Vec<AnyInfo> {
        Vec::new()
    }
    fn to_map(&self) -> HashMap<String, Option<String>> {
        HashMap::new()
    }
}

impl FromAnyRow for chrono::NaiveDate {
    fn from_any_row(row: &AnyRow) -> Result<Self, Error> {
        let mut index = 0;
        Self::from_any_row_at(row, &mut index)
    }

    fn from_any_row_at(row: &AnyRow, index: &mut usize) -> Result<Self, Error> {
        if *index >= row.len() {
            return Err(Error::ColumnIndexOutOfBounds { index: *index, len: row.len() });
        }
        let res = row.try_get::<String, _>(*index);
        *index += 1;
        let s = res.map_err(|e| Error::Decode(Box::new(e)))?;
        crate::temporal::parse_naive_date(&s).map_err(|e| Error::Decode(Box::new(e)))
    }
}

impl AnyImpl for chrono::NaiveTime {
    fn columns() -> Vec<AnyInfo> {
        Vec::new()
    }
    fn to_map(&self) -> HashMap<String, Option<String>> {
        HashMap::new()
    }
}

impl FromAnyRow for chrono::NaiveTime {
    fn from_any_row(row: &AnyRow) -> Result<Self, Error> {
        let mut index = 0;
        Self::from_any_row_at(row, &mut index)
    }

    fn from_any_row_at(row: &AnyRow, index: &mut usize) -> Result<Self, Error> {
        if *index >= row.len() {
            return Err(Error::ColumnIndexOutOfBounds { index: *index, len: row.len() });
        }
        let res = row.try_get::<String, _>(*index);
        *index += 1;
        let s = res.map_err(|e| Error::Decode(Box::new(e)))?;
        crate::temporal::parse_naive_time(&s).map_err(|e| Error::Decode(Box::new(e)))
    }
}

impl AnyImpl for chrono::DateTime<chrono::Utc> {
    fn columns() -> Vec<AnyInfo> {
        Vec::new()
    }
    fn to_map(&self) -> HashMap<String, Option<String>> {
        HashMap::new()
    }
}

impl FromAnyRow for chrono::DateTime<chrono::Utc> {
    fn from_any_row(row: &AnyRow) -> Result<Self, Error> {
        let mut index = 0;
        Self::from_any_row_at(row, &mut index)
    }

    fn from_any_row_at(row: &AnyRow, index: &mut usize) -> Result<Self, Error> {
        if *index >= row.len() {
            return Err(Error::ColumnIndexOutOfBounds { index: *index, len: row.len() });
        }
        let res = row.try_get::<String, _>(*index);
        match res {
            Ok(s) => {
                *index += 1;
                crate::temporal::parse_datetime_utc(&s).map_err(|e| Error::Decode(Box::new(e)))
            }
            Err(e) => {
                // Try numeric fallback
                if let Ok(i) = row.try_get::<i64, _>(*index) {
                    *index += 1;
                    return Ok(chrono::DateTime::from_timestamp(i, 0).unwrap_or_else(|| chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(chrono::NaiveDateTime::default(), chrono::Utc)));
                }
                
                if let Ok(None) = row.try_get::<Option<String>, _>(*index) {
                    *index += 1;
                    return Err(Error::Decode(Box::new(e)));
                }

                Err(Error::Decode(Box::new(e)))
            }
        }
    }
}

// ============================================================================
// Option Implementation
// ============================================================================

impl<T: AnyImpl> AnyImpl for Option<T> {
    fn columns() -> Vec<AnyInfo> {
        T::columns()
    }
    fn to_map(&self) -> HashMap<String, Option<String>> {
        match self {
            Some(v) => v.to_map(),
            None => HashMap::new(),
        }
    }
}

impl<T: FromAnyRow> FromAnyRow for Option<T> {
    fn from_any_row(row: &AnyRow) -> Result<Self, Error> {
        match T::from_any_row(row) {
            Ok(v) => Ok(Some(v)),
            Err(_) => Ok(None),
        }
    }

    fn from_any_row_at(row: &AnyRow, index: &mut usize) -> Result<Self, Error> {
        match T::from_any_row_at(row, index) {
            Ok(v) => Ok(Some(v)),
            Err(_) => Ok(None),
        }
    }
}

// ============================================================================
// Tuple Implementations
// ============================================================================

macro_rules! impl_any_tuple {
    ($($T:ident),+) => {
        impl<$($T: AnyImpl),+> AnyImpl for ($($T,)+) {
            fn columns() -> Vec<AnyInfo> {
                let mut cols = Vec::new();
                $(
                    cols.extend($T::columns());
                )+
                cols
            }

            fn to_map(&self) -> HashMap<String, Option<String>> {
                let mut map = HashMap::new();
                #[allow(non_snake_case)]
                let ($($T,)+) = self;
                $(
                    map.extend($T.to_map());
                )+
                map
            }
        }

        impl<$($T: FromAnyRow),+> FromAnyRow for ($($T,)+) {
            fn from_any_row(row: &AnyRow) -> Result<Self, Error> {
                let mut index = 0;
                Ok((
                    $(
                        $T::from_any_row_at(row, &mut index)?,
                    )+
                ))
            }

            fn from_any_row_at(row: &AnyRow, index: &mut usize) -> Result<Self, Error> {
                Ok((
                    $(
                        $T::from_any_row_at(row, index)?,
                    )+
                ))
            }
        }
    };
}

impl_any_tuple!(T1);
impl_any_tuple!(T1, T2);
impl_any_tuple!(T1, T2, T3);
impl_any_tuple!(T1, T2, T3, T4);
impl_any_tuple!(T1, T2, T3, T4, T5);
impl_any_tuple!(T1, T2, T3, T4, T5, T6);
impl_any_tuple!(T1, T2, T3, T4, T5, T6, T7);
impl_any_tuple!(T1, T2, T3, T4, T5, T6, T7, T8);