rusqlite/types/
value.rs

1use super::{Null, Type};
2
3/// Owning [dynamic type value](http://sqlite.org/datatype3.html). Value's type is typically
4/// dictated by SQLite (not by the caller).
5///
6/// See [`ValueRef`](crate::types::ValueRef) for a non-owning dynamic type
7/// value.
8#[derive(Clone, Debug, PartialEq)]
9pub enum Value {
10    /// The value is a `NULL` value.
11    Null,
12    /// The value is a signed integer.
13    Integer(i64),
14    /// The value is a floating point number.
15    Real(f64),
16    /// The value is a text string.
17    Text(String),
18    /// The value is a blob of data
19    Blob(Vec<u8>),
20}
21
22impl From<Null> for Value {
23    #[inline]
24    fn from(_: Null) -> Self {
25        Self::Null
26    }
27}
28
29impl From<bool> for Value {
30    #[inline]
31    fn from(i: bool) -> Self {
32        Self::Integer(i as i64)
33    }
34}
35
36impl From<isize> for Value {
37    #[inline]
38    fn from(i: isize) -> Self {
39        Self::Integer(i as i64)
40    }
41}
42
43#[cfg(feature = "i128_blob")]
44#[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))]
45impl From<i128> for Value {
46    #[inline]
47    fn from(i: i128) -> Self {
48        // We store these biased (e.g. with the most significant bit flipped)
49        // so that comparisons with negative numbers work properly.
50        Self::Blob(i128::to_be_bytes(i ^ (1_i128 << 127)).to_vec())
51    }
52}
53
54#[cfg(feature = "uuid")]
55#[cfg_attr(docsrs, doc(cfg(feature = "uuid")))]
56impl From<uuid::Uuid> for Value {
57    #[inline]
58    fn from(id: uuid::Uuid) -> Self {
59        Self::Blob(id.as_bytes().to_vec())
60    }
61}
62
63macro_rules! from_i64(
64    ($t:ty) => (
65        impl From<$t> for Value {
66            #[inline]
67            fn from(i: $t) -> Value {
68                Value::Integer(i64::from(i))
69            }
70        }
71    )
72);
73
74from_i64!(i8);
75from_i64!(i16);
76from_i64!(i32);
77from_i64!(u8);
78from_i64!(u16);
79from_i64!(u32);
80
81impl From<i64> for Value {
82    #[inline]
83    fn from(i: i64) -> Self {
84        Self::Integer(i)
85    }
86}
87
88impl From<f32> for Value {
89    #[inline]
90    fn from(f: f32) -> Self {
91        Self::Real(f.into())
92    }
93}
94
95impl From<f64> for Value {
96    #[inline]
97    fn from(f: f64) -> Self {
98        Self::Real(f)
99    }
100}
101
102impl From<String> for Value {
103    #[inline]
104    fn from(s: String) -> Self {
105        Self::Text(s)
106    }
107}
108
109impl From<Vec<u8>> for Value {
110    #[inline]
111    fn from(v: Vec<u8>) -> Self {
112        Self::Blob(v)
113    }
114}
115
116impl<T> From<Option<T>> for Value
117where
118    T: Into<Self>,
119{
120    #[inline]
121    fn from(v: Option<T>) -> Self {
122        match v {
123            Some(x) => x.into(),
124            None => Self::Null,
125        }
126    }
127}
128
129impl Value {
130    /// Returns SQLite fundamental datatype.
131    #[inline]
132    #[must_use]
133    pub fn data_type(&self) -> Type {
134        match *self {
135            Self::Null => Type::Null,
136            Self::Integer(_) => Type::Integer,
137            Self::Real(_) => Type::Real,
138            Self::Text(_) => Type::Text,
139            Self::Blob(_) => Type::Blob,
140        }
141    }
142}
143
144#[cfg(test)]
145mod test {
146    use super::Value;
147    use crate::types::Type;
148
149    #[test]
150    fn from() {
151        assert_eq!(Value::from(2f32), Value::Real(2f64));
152        assert_eq!(Value::from(3.), Value::Real(3.));
153        assert_eq!(Value::from(vec![0u8]), Value::Blob(vec![0u8]));
154    }
155
156    #[test]
157    fn data_type() {
158        assert_eq!(Value::Null.data_type(), Type::Null);
159        assert_eq!(Value::Integer(0).data_type(), Type::Integer);
160        assert_eq!(Value::Real(0.).data_type(), Type::Real);
161        assert_eq!(Value::Text(String::new()).data_type(), Type::Text);
162        assert_eq!(Value::Blob(vec![]).data_type(), Type::Blob);
163    }
164}