nanosql 0.10.0

Tiny, strongly-typed data mapper for SQLite
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
//! Deserializing SQL rows into strongly-typed values.

use core::str;
use core::num::{
    NonZeroI8,
    NonZeroU8,
    NonZeroI16,
    NonZeroU16,
    NonZeroI32,
    NonZeroU32,
    NonZeroI64,
    NonZeroU64,
    NonZeroIsize,
    NonZeroUsize,
};
use core::hash::Hash;
use core::ops::{Deref, DerefMut};
use std::borrow::{Borrow, BorrowMut, Cow};
use std::rc::Rc;
use std::sync::Arc;
use std::collections::{VecDeque, BinaryHeap, HashSet, HashMap, BTreeSet, BTreeMap};
use rusqlite::{Statement, Row, Rows, types::{Value, ValueRef, ToSqlOutput, FromSql}};
#[cfg(feature = "not-nan")]
use ordered_float::NotNan;
#[cfg(feature = "chrono")]
use chrono::{DateTime, Utc, FixedOffset, Local};
#[cfg(feature = "uuid")]
use uuid::Uuid;
#[cfg(feature = "json")]
use serde_json::Value as JsonValue;
use crate::error::{Error, Result, RowCount};


/// This trait describes types that deserialize from a relation (set of rows),
/// as returned by a compiled statement.
pub trait ResultSet: Sized {
    /// Convert a dynamically-typed relation to a statically-typed collection.
    fn from_rows(rows: Rows<'_>) -> Result<Self>;
}

impl ResultSet for () {
    fn from_rows(mut rows: Rows<'_>) -> Result<Self> {
        if rows.next()?.is_none() {
            Ok(())
        } else {
            Err(Error::RowCountMismatch {
                expected: RowCount::exactly(0),
                actual: RowCount::at_least(1),
            })
        }
    }
}

/// A convenience wrapper for expecting exactly one record to be returned from a query.
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct Single<T>(pub T);

impl<T> Single<T> {
    /// Extracts the wrapped value.
    pub fn into_inner(self) -> T {
        let Single(value) = self;
        value
    }
}

impl<T> AsRef<T> for Single<T> {
    fn as_ref(&self) -> &T {
        &self.0
    }
}

impl<T> AsMut<T> for Single<T> {
    fn as_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

impl<T> From<T> for Single<T> {
    fn from(value: T) -> Self {
        Single(value)
    }
}

impl<T> Deref for Single<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> DerefMut for Single<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T> Borrow<T> for Single<T> {
    fn borrow(&self) -> &T {
        &self.0
    }
}

impl<T> BorrowMut<T> for Single<T> {
    fn borrow_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

impl<T: ResultRecord> ResultSet for Single<T> {
    fn from_rows(mut rows: Rows<'_>) -> Result<Self> {
        let row = rows.next()?.ok_or(Error::RowCountMismatch {
            expected: RowCount::exactly(1),
            actual: RowCount::exactly(0),
        })?;
        let value = T::from_row(row)?;

        if rows.next()?.is_none() {
            Ok(Single(value))
        } else {
            Err(Error::RowCountMismatch {
                expected: RowCount::exactly(1),
                actual: RowCount::at_least(2),
            })
        }
    }
}

impl<T: ResultRecord> ResultSet for Option<T> {
    fn from_rows(mut rows: Rows<'_>) -> Result<Self> {
        let Some(row) = rows.next()? else {
            return Ok(None);
        };

        // special case for primitives: allow a single NULL row/column to be interpreted as `None`
        let value = if row.as_ref().column_count() == 1 && row.get_ref(0)? == ValueRef::Null {
            None
        } else {
            Some(T::from_row(row)?)
        };

        if rows.next()?.is_none() {
            Ok(value)
        } else {
            Err(Error::RowCountMismatch {
                expected: RowCount::at_most(1),
                actual: RowCount::at_least(2),
            })
        }
    }
}

impl<const N: usize, T: ResultRecord> ResultSet for [T; N] {
    fn from_rows(mut rows: Rows<'_>) -> Result<Self> {
        // Instead of relying on `Vec::try_into::<[T; N]>()`,
        // we spare a dynamic allocation by building the results
        // in-place, on a stack-allocated array.
        let mut tmp = [(); N].map(|_| None);

        for (i, item) in tmp.iter_mut().enumerate() {
            let row = rows.next()?.ok_or(Error::RowCountMismatch {
                expected: RowCount::exactly(N),
                actual: RowCount::exactly(i),
            })?;

            *item = T::from_row(row)?.into();
        }

        if rows.next()?.is_none() {
            Ok(tmp.map(Option::unwrap))
        } else {
            Err(Error::RowCountMismatch {
                expected: RowCount::exactly(N),
                actual: RowCount::at_least(N + 1),
            })
        }
    }
}

impl<T: ResultRecord> ResultSet for Vec<T> {
    fn from_rows(mut rows: Rows<'_>) -> Result<Self> {
        let mut result_set = Vec::new();

        while let Some(row) = rows.next()? {
            result_set.push(T::from_row(row)?);
        }

        Ok(result_set)
    }
}

impl<T: ResultRecord> ResultSet for Box<[T]> {
    fn from_rows(rows: Rows<'_>) -> Result<Self> {
        Vec::from_rows(rows).map(Self::from)
    }
}

impl<T: ResultRecord> ResultSet for Rc<[T]> {
    fn from_rows(rows: Rows<'_>) -> Result<Self> {
        Vec::from_rows(rows).map(Self::from)
    }
}

impl<T: ResultRecord> ResultSet for Arc<[T]> {
    fn from_rows(rows: Rows<'_>) -> Result<Self> {
        Vec::from_rows(rows).map(Self::from)
    }
}

impl<T: ResultRecord> ResultSet for VecDeque<T> {
    fn from_rows(rows: Rows<'_>) -> Result<Self> {
        Vec::from_rows(rows).map(Self::from)
    }
}

impl<T> ResultSet for BinaryHeap<T>
where
    T: Ord + ResultRecord
{
    fn from_rows(rows: Rows<'_>) -> Result<Self> {
        Vec::from_rows(rows).map(Self::from)
    }
}

impl<T> ResultSet for HashSet<T>
where
    T: Eq + Hash + ResultRecord
{
    fn from_rows(mut rows: Rows<'_>) -> Result<Self> {
        let mut result_set = HashSet::new();

        while let Some(row) = rows.next()? {
            result_set.insert(T::from_row(row)?);
        }

        Ok(result_set)
    }
}

impl<T> ResultSet for BTreeSet<T>
where
    T: Ord + ResultRecord
{
    fn from_rows(mut rows: Rows<'_>) -> Result<Self> {
        let mut result_set = BTreeSet::new();

        while let Some(row) = rows.next()? {
            result_set.insert(T::from_row(row)?);
        }

        Ok(result_set)
    }
}

/// The `ResultSet` impl for mapping types can be used when the returned rows
/// have exactly 2 columns: the first column will be used as the key, and the
/// second column will be used as the value. Thus, the map will have as many
/// entries as there are rows in the result set (assuming all keys are unique).
///
/// Note that this is different from the [`ResultRecord`] impl on mappings,
/// which maps column _names_ to column values and works for arbitrarily many
/// columns (but only represents a single row). See the documentation of that
/// trait for a more detailed explanation.
///
/// ```
/// # use std::collections::HashMap;
/// # use nanosql::{define_query, Result, Connection, ConnectionExt, ResultSet};
/// define_query! {
///     KeysAndValues<'p>: () => HashMap<String, i64> {
///         "VALUES ('answer', 42), ('inv_alpha', 137)"
///     }
/// }
///
/// # fn main() -> Result<()> {
/// let conn = Connection::connect_in_memory()?;
/// let mut query = conn.compile(KeysAndValues)?;
/// let map = query.invoke(())?;
///
/// assert_eq!(
///     map,
///     HashMap::from([
///         ("inv_alpha".into(), 137), // order intentionally reversed
///         ("answer".into(), 42),
///     ])
/// );
/// # Ok(())
/// # }
/// ```
impl<K, V> ResultSet for HashMap<K, V>
where
    K: Eq + Hash + FromSql,
    V: FromSql,
{
    fn from_rows(mut rows: Rows<'_>) -> Result<Self> {
        let mut result_set = HashMap::new();

        while let Some(row) = rows.next()? {
            let (key, value) = <(K, V)>::from_row(row)?;
            result_set.insert(key, value);
        }

        Ok(result_set)
    }
}

/// The `ResultSet` impl for mapping types can be used when the returned rows
/// have exactly 2 columns: the first column will be used as the key, and the
/// second column will be used as the value. Thus, the map will have as many
/// entries as there are rows in the result set (assuming all keys are unique).
///
/// Note that this is different from the [`ResultRecord`] impl on mappings,
/// which maps column _names_ to column values and works for arbitrarily many
/// columns (but only represents a single row). See the documentation of that
/// trait for a more detailed explanation.
///
/// ```
/// # use std::collections::BTreeMap;
/// # use std::rc::Rc;
/// # use nanosql::{define_query, Result, Connection, ConnectionExt, ResultSet};
/// define_query! {
///     KeysAndValues<'p>: () => BTreeMap<Rc<str>, Option<f32>> {
///         "VALUES ('days_per_year', 365.2425), ('missing', NULL)"
///     }
/// }
///
/// # fn main() -> Result<()> {
/// let conn = Connection::connect_in_memory()?;
/// let mut query = conn.compile(KeysAndValues)?;
/// let map = query.invoke(())?;
///
/// assert_eq!(
///     map,
///     BTreeMap::from([
///         ("missing".into(), None), // order intentionally reversed
///         ("days_per_year".into(), Some(365.2425)),
///     ])
/// );
/// # Ok(())
/// # }
/// ```
impl<K, V> ResultSet for BTreeMap<K, V>
where
    K: Ord + FromSql,
    V: FromSql,
{
    fn from_rows(mut rows: Rows<'_>) -> Result<Self> {
        let mut result_set = BTreeMap::new();

        while let Some(row) = rows.next()? {
            let (key, value) = <(K, V)>::from_row(row)?;
            result_set.insert(key, value);
        }

        Ok(result_set)
    }
}

/// This trait describes types that deserialize from a single row (tuple).
///
/// When derived on a `struct`, the `rename_all` (type-level) and `rename`
/// and `ignore` (field-level) attributes work identically to those of
/// [`Table`](crate::table::Table); see its documentation for more details.
pub trait ResultRecord: Sized {
    /// Convert a dynamically-typed relational tuple to a statically-typed record.
    fn from_row(row: &Row<'_>) -> Result<Self>;
}

/// Private helper for ensuring that exactly 1 column is found when
/// building a strongly-typed tuple from a dynamically-typed `Row`.
fn primitive_from_sql<T: FromSql>(row: &Row<'_>) -> Result<T> {
    let expected = 1;
    let actual = row.as_ref().column_count();

    if actual == expected {
        Ok(row.get(0)?)
    } else {
        Err(Error::ColumnCountMismatch { expected, actual })
    }
}

macro_rules! impl_result_record_for_primitive {
    ($($ty:ty,)*) => {$(
        impl ResultRecord for $ty {
            fn from_row(row: &Row<'_>) -> Result<Self> {
                primitive_from_sql(row)
            }
        }
    )*}
}

impl_result_record_for_primitive!{
    bool,
    i8,
    u8,
    i16,
    u16,
    i32,
    u32,
    i64,
    u64,
    isize,
    usize,
    NonZeroI8,
    NonZeroU8,
    NonZeroI16,
    NonZeroU16,
    NonZeroI32,
    NonZeroU32,
    NonZeroI64,
    NonZeroU64,
    NonZeroIsize,
    NonZeroUsize,
    f32,
    f64,
    Box<str>,
    Rc<str>,
    Arc<str>,
    String,
    Vec<u8>,
    Value,
}

#[cfg(feature = "chrono")]
impl_result_record_for_primitive! {
    DateTime<Utc>,
    DateTime<FixedOffset>,
    DateTime<Local>,
}

#[cfg(feature = "uuid")]
impl_result_record_for_primitive!{
    Uuid,
}

#[cfg(feature = "json")]
impl_result_record_for_primitive!{
    JsonValue,
}

macro_rules! impl_result_record_for_tuple {
    () => {
        impl ResultRecord for () {
            fn from_row(row: &Row<'_>) -> Result<Self> {
                let expected = 0;
                let actual = row.as_ref().column_count();

                if actual == expected {
                    Ok(())
                } else {
                    Err(Error::ColumnCountMismatch { expected, actual })
                }
            }
        }
    };
    ($head_id:ident => $head_ty:ident; $($rest_id:ident => $rest_ty:ident;)*) => {
        impl<$head_ty, $($rest_ty,)*> ResultRecord for ($head_ty, $($rest_ty,)*)
        where
            $head_ty: FromSql,
            $($rest_ty: FromSql,)*
        {
            fn from_row(row: &Row<'_>) -> Result<Self> {
                let mut index = 0;

                let $head_id = row.get(index)?;
                index += 1;

                $(
                    let $rest_id = row.get(index)?;
                    index += 1;
                )*

                let expected = index;
                let actual = row.as_ref().column_count();

                if actual == expected {
                    Ok(($head_id, $($rest_id,)*))
                } else {
                    Err(Error::ColumnCountMismatch { expected, actual })
                }
            }
        }
        impl_result_record_for_tuple!($($rest_id => $rest_ty;)*);
    };
}

impl_result_record_for_tuple!{
    a => A;
    b => B;
    c => C;
    d => D;
    e => E;
    f => F;
    g => G;
    h => H;
    i => I;
    j => J;
    k => K;
    l => L;
    m => M;
    n => N;
    o => O;
    p => P;
    q => Q;
    r => R;
    s => S;
    t => T;
    u => U;
    v => V;
    w => W;
    x => X;
    y => Y;
    z => Z;
}

impl<T> ResultRecord for Cow<'_, T>
where
    T: ?Sized + ToOwned,
    T::Owned: ResultRecord,
{
    fn from_row(row: &Row<'_>) -> Result<Self> {
        ResultRecord::from_row(row).map(Cow::Owned)
    }
}

impl<T: FromSql> ResultRecord for Option<T> {
    fn from_row(row: &Row<'_>) -> Result<Self> {
        primitive_from_sql(row)
    }
}

impl<const N: usize> ResultRecord for [u8; N] {
    fn from_row(row: &Row<'_>) -> Result<Self> {
        primitive_from_sql(row)
    }
}

impl ResultRecord for Box<[u8]> {
    fn from_row(row: &Row<'_>) -> Result<Self> {
        primitive_from_sql(row).map(Vec::into_boxed_slice)
    }
}

impl ResultRecord for Rc<[u8]> {
    fn from_row(row: &Row<'_>) -> Result<Self> {
        primitive_from_sql(row)
    }
}

impl ResultRecord for Arc<[u8]> {
    fn from_row(row: &Row<'_>) -> Result<Self> {
        primitive_from_sql(row)
    }
}

impl ResultRecord for ToSqlOutput<'_> {
    fn from_row(row: &Row<'_>) -> Result<Self> {
        primitive_from_sql(row).map(ToSqlOutput::Owned)
    }
}

/// Convenience helper for ignoring the return value of a query.
/// This type successfully deserializes from any number of columns.
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash, Debug)]
pub struct IgnoredAny;

impl ResultRecord for IgnoredAny {
    fn from_row(_: &Row<'_>) -> Result<Self> {
        Ok(IgnoredAny)
    }
}

#[cfg(feature = "not-nan")]
impl ResultRecord for NotNan<f32> {
    fn from_row(row: &Row<'_>) -> Result<Self> {
        let x: f32 = primitive_from_sql(row)?;
        let value = NotNan::try_from(x)?;
        Ok(value)
    }
}

#[cfg(feature = "not-nan")]
impl ResultRecord for NotNan<f64> {
    fn from_row(row: &Row<'_>) -> Result<Self> {
        let x: f64 = primitive_from_sql(row)?;
        let value = NotNan::try_from(x)?;
        Ok(value)
    }
}

/// The `ResultRecord` impl for maps represents an arbitrary row as column
/// names mapped to the corresponding column values. It works for however
/// many columns; the `.len()` of the map will be the number of columns in
/// the returned rows.
///
/// Note that this is not the same as the [`ResultSet`] impl for maps, which
/// maps keys in the first column to values in the second column, and thus
/// only works for queries returning exactly two-columns. See its documentation
/// for a more detailed explanation.
///
/// ```
/// # use std::collections::HashMap;
/// # use nanosql::{define_query, Connection, ConnectionExt, Result};
/// use nanosql::Value;
///
/// define_query! {
///     OneRowManyColumns<'p>: () => Vec<HashMap<String, Value>> {
///         r#"
///         WITH t("qux", "baz", "mem") AS (
///             VALUES ('some string', 999, NULL)
///         )
///         SELECT
///             "qux" AS "qux",
///             "baz" AS "baz",
///             "mem" AS "mem"
///         FROM t;
///         "#
///     }
/// }
/// # fn main() -> Result<()> {
/// let conn = Connection::connect_in_memory()?;
/// let mut query = conn.compile(OneRowManyColumns)?;
///
/// assert_eq!(
///     query.invoke(())?,
///     [
///         HashMap::from([
///             ("baz".into(), Value::Integer(999)),
///             ("qux".into(), Value::Text("some string".into())),
///             ("mem".into(), Value::Null),
///         ])
///     ]
/// );
/// # Ok(())
/// # }
/// ```
impl<K, V> ResultRecord for HashMap<K, V>
where
    K: Eq + Hash + for<'a> From<&'a str>,
    V: FromSql,
{
    fn from_row(row: &Row<'_>) -> Result<Self> {
        let stmt: &Statement<'_> = row.as_ref();
        let mut map = HashMap::new();

        for i in 0..stmt.column_count() {
            let name = stmt.column_name(i)?;
            let key = name.into();
            let value = row.get(i)?;

            map.insert(key, value);
        }

        Ok(map)
    }
}

/// The `ResultRecord` impl for maps represents an arbitrary row as column
/// names mapped to the corresponding column values. It works for however
/// many columns; the `.len()` of the map will be the number of columns in
/// the returned rows.
///
/// Note that this is not the same as the [`ResultSet`] impl for maps, which
/// maps keys in the first column to values in the second column, and thus
/// only works for queries returning exactly two-columns. See its documentation
/// for a more detailed explanation.
///
/// ```
/// # use std::collections::BTreeMap;
/// # use nanosql::{define_query, Connection, ConnectionExt, Result};
/// use nanosql::Value;
///
/// define_query! {
///     OneRowManyColumns<'p>: () => [BTreeMap<Box<str>, Value>; 1] {
///         r#"
///         WITH t("qux", "baz", "mem") AS (
///             VALUES ('some string', 987, NULL)
///         )
///         SELECT
///             "qux" AS "qux",
///             "baz" AS "baz",
///             "mem" AS "mem"
///         FROM t;
///         "#
///     }
/// }
/// # fn main() -> Result<()> {
/// let conn = Connection::connect_in_memory()?;
/// let mut query = conn.compile(OneRowManyColumns)?;
///
/// assert_eq!(
///     query.invoke(())?,
///     [
///         BTreeMap::from([
///             ("baz".into(), Value::Integer(987)),
///             ("qux".into(), Value::Text("some string".into())),
///             ("mem".into(), Value::Null),
///         ])
///     ]
/// );
/// # Ok(())
/// # }
/// ```
impl<K, V> ResultRecord for BTreeMap<K, V>
where
    K: Ord + for<'a> From<&'a str>,
    V: FromSql,
{
    fn from_row(row: &Row<'_>) -> Result<Self> {
        let stmt: &Statement<'_> = row.as_ref();
        let mut map = BTreeMap::new();

        for i in 0..stmt.column_count() {
            let name = stmt.column_name(i)?;
            let key = name.into();
            let value = row.get(i)?;

            map.insert(key, value);
        }

        Ok(map)
    }
}