Skip to main content

orbit_orm/
value.rs

1use chrono::{NaiveDate, NaiveDateTime};
2
3/// A value bound to a query. Orbit sends every value as a bind parameter, never as SQL text,
4/// except that PostgreSQL gets a `NULL` written into the SQL, since its parameters need a type.
5#[derive(Debug, Clone, PartialEq)]
6pub enum Value {
7    Null,
8    Bool(bool),
9    Int(i64),
10    UInt(u64),
11    Float(f64),
12    Text(String),
13    /// A `DATETIME` or `TIMESTAMP`, with its microseconds.
14    DateTime(NaiveDateTime),
15    /// A `DATE`.
16    Date(NaiveDate),
17    /// Raw bytes, for `BLOB` columns. Use `Text` for strings.
18    Bytes(Vec<u8>),
19}
20
21impl From<bool> for Value {
22    fn from(value: bool) -> Self {
23        Self::Bool(value)
24    }
25}
26
27impl From<i32> for Value {
28    fn from(value: i32) -> Self {
29        Self::Int(i64::from(value))
30    }
31}
32
33impl From<i64> for Value {
34    fn from(value: i64) -> Self {
35        Self::Int(value)
36    }
37}
38
39impl From<u32> for Value {
40    fn from(value: u32) -> Self {
41        Self::UInt(u64::from(value))
42    }
43}
44
45impl From<u64> for Value {
46    fn from(value: u64) -> Self {
47        Self::UInt(value)
48    }
49}
50
51impl From<f64> for Value {
52    fn from(value: f64) -> Self {
53        Self::Float(value)
54    }
55}
56
57impl From<&str> for Value {
58    fn from(value: &str) -> Self {
59        Self::Text(value.to_owned())
60    }
61}
62
63impl From<String> for Value {
64    fn from(value: String) -> Self {
65        Self::Text(value)
66    }
67}
68
69impl From<NaiveDateTime> for Value {
70    fn from(value: NaiveDateTime) -> Self {
71        Self::DateTime(value)
72    }
73}
74
75impl From<NaiveDate> for Value {
76    fn from(value: NaiveDate) -> Self {
77        Self::Date(value)
78    }
79}
80
81impl From<Vec<u8>> for Value {
82    fn from(value: Vec<u8>) -> Self {
83        Self::Bytes(value)
84    }
85}
86
87impl<T: Into<Value>> From<Option<T>> for Value {
88    fn from(value: Option<T>) -> Self {
89        value.map_or(Self::Null, Into::into)
90    }
91}