zino_orm/
value.rs

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
use super::{Entity, QueryBuilder, Schema};
use http::Uri;
use std::{borrow::Cow, net::IpAddr, path::Path};
use url::Url;
use zino_core::{
    datetime::{Date, DateTime, Time},
    extension::JsonObjectExt,
    Decimal, JsonValue, Map, Uuid,
};

/// A generic interface for converting into SQL values.
pub trait IntoSqlValue {
    /// Converts `self` to a SQL value.
    fn into_sql_value(self) -> JsonValue;
}

macro_rules! impl_into_sql_value {
    ($($Ty: ty),+ $(,)?) => {
        $(
            impl IntoSqlValue for $Ty {
                #[inline]
                fn into_sql_value(self) -> JsonValue {
                    self.into()
                }
            }
        )+
    }
}

impl_into_sql_value!(
    (),
    bool,
    u8,
    u16,
    u32,
    u64,
    usize,
    i8,
    i16,
    i32,
    i64,
    isize,
    f32,
    f64,
    &str,
    String,
    JsonValue,
    Map,
    Date,
    Time,
);

impl IntoSqlValue for DateTime {
    #[inline]
    fn into_sql_value(self) -> JsonValue {
        if cfg!(feature = "orm-postgres") {
            self.to_string().into()
        } else {
            self.to_utc_timestamp().into()
        }
    }
}

impl IntoSqlValue for Decimal {
    #[inline]
    fn into_sql_value(self) -> JsonValue {
        self.to_string().into()
    }
}

impl IntoSqlValue for IpAddr {
    #[inline]
    fn into_sql_value(self) -> JsonValue {
        self.to_string().into()
    }
}

impl IntoSqlValue for Uuid {
    #[inline]
    fn into_sql_value(self) -> JsonValue {
        self.to_string().into()
    }
}

impl IntoSqlValue for &Path {
    #[inline]
    fn into_sql_value(self) -> JsonValue {
        self.to_str().into()
    }
}

impl IntoSqlValue for &Uri {
    #[inline]
    fn into_sql_value(self) -> JsonValue {
        self.to_string().into()
    }
}

impl IntoSqlValue for &Url {
    #[inline]
    fn into_sql_value(self) -> JsonValue {
        self.as_str().into()
    }
}

impl IntoSqlValue for Cow<'_, str> {
    #[inline]
    fn into_sql_value(self) -> JsonValue {
        self.into()
    }
}

impl<T: IntoSqlValue> IntoSqlValue for Option<T> {
    #[inline]
    fn into_sql_value(self) -> JsonValue {
        self.map(|v| v.into_sql_value()).into()
    }
}

impl<T: IntoSqlValue> IntoSqlValue for Vec<T> {
    #[inline]
    fn into_sql_value(self) -> JsonValue {
        JsonValue::Array(self.into_iter().map(|v| v.into_sql_value()).collect())
    }
}

impl<T: IntoSqlValue, const N: usize> IntoSqlValue for [T; N] {
    #[inline]
    fn into_sql_value(self) -> JsonValue {
        JsonValue::Array(self.into_iter().map(|v| v.into_sql_value()).collect())
    }
}

impl<T: Clone + IntoSqlValue> IntoSqlValue for &[T] {
    #[inline]
    fn into_sql_value(self) -> JsonValue {
        JsonValue::Array(self.iter().map(|v| v.clone().into_sql_value()).collect())
    }
}

impl<E: Entity + Schema> IntoSqlValue for QueryBuilder<E> {
    #[inline]
    fn into_sql_value(self) -> JsonValue {
        Map::from_entry("$subquery", self.build_subquery()).into()
    }
}