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
//! Contains the trait for mapping a [Row] into an object.
//!
//! Also contains the wrapper struct [SingleResult] for easy parsing of a single column result.
use crate::prelude::ColumnType;
use postgres::Row;
use std::ops::{Deref, DerefMut};
/// This trait parses the result of a single row in a query.
///
/// Required for [Entity::select_query] generic type.
pub trait ResultMapping {
/// Parses Self from a [Row].
fn from_row(row: Row) -> Self where Self: Sized;
}
impl ResultMapping for Row {
fn from_row(row: Row) -> Self
where
Self: Sized
{
row
}
}
/// Wrapper struct which holds a single element for result mapping from a select query.
///
/// Implements [ResultMapping], supports all types with [ColumnType].
pub struct SingleResult<T: ColumnType> {
inner: T,
}
impl<T: ColumnType> Deref for SingleResult<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T: ColumnType> DerefMut for SingleResult<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<T: ColumnType> ResultMapping for SingleResult<T> {
fn from_row(row: Row) -> Self
where
Self: Sized
{
SingleResult {
inner: row.get::<_, T>(0),
}
}
}
impl ResultMapping for () {
fn from_row(_row: Row) -> Self
where
Self: Sized
{
()
}
}