Skip to main content

tank_core/
value.rs

1use crate::{
2    AsValue, DynQuery, Error, Expression, GenericSqlWriter, Result, TableRef, interval::Interval,
3};
4use quote::{ToTokens, quote};
5use rust_decimal::Decimal;
6use serde_json::Value as JsonValue;
7use std::{
8    borrow::Cow,
9    collections::HashMap,
10    fmt::{self, Display},
11    hash::Hash,
12    mem::discriminant,
13};
14use time::{Date, OffsetDateTime, PrimitiveDateTime, Time};
15use uuid::Uuid;
16
17/// SQL value representation.
18///
19/// Variants correspond to database column types.
20#[derive(Default, Debug, Clone)]
21pub enum Value {
22    /// SQL NULL.
23    #[default]
24    Null,
25    /// Boolean value.
26    Boolean(Option<bool>),
27    /// 8-bit signed integer.
28    Int8(Option<i8>),
29    /// 16-bit signed integer.
30    Int16(Option<i16>),
31    /// 32-bit signed integer.
32    Int32(Option<i32>),
33    /// 64-bit signed integer.
34    Int64(Option<i64>),
35    /// 128-bit signed integer.
36    Int128(Option<i128>),
37    /// 8-bit unsigned integer.
38    UInt8(Option<u8>),
39    /// 16-bit unsigned integer.
40    UInt16(Option<u16>),
41    /// 32-bit unsigned integer.
42    UInt32(Option<u32>),
43    /// 64-bit unsigned integer.
44    UInt64(Option<u64>),
45    /// 128-bit unsigned integer.
46    UInt128(Option<u128>),
47    /// 32-bit floating point number.
48    Float32(Option<f32>),
49    /// 64-bit floating point number.
50    Float64(Option<f64>),
51    /// Decimal (value, precision, scale).
52    Decimal(Option<Decimal>, u8, u8),
53    /// Single character.
54    Char(Option<char>),
55    /// Variable-length character string.
56    Varchar(Option<Cow<'static, str>>),
57    /// Binary large object.
58    Blob(Option<Box<[u8]>>),
59    /// Date value (without time).
60    Date(Option<Date>),
61    /// Time value (without date).
62    Time(Option<Time>),
63    /// Timestamp (date and time).
64    Timestamp(Option<PrimitiveDateTime>),
65    /// Timestamp with time zone.
66    TimestampWithTimezone(Option<OffsetDateTime>),
67    /// Time interval.
68    Interval(Option<Interval>),
69    /// UUID (Universally Unique Identifier).
70    Uuid(Option<Uuid>),
71    /// Homogeneous array (values, inner type, length).
72    Array(Option<Box<[Value]>>, Box<Value>, u32),
73    /// Variable-length list (values, inner type).
74    List(Option<Vec<Value>>, Box<Value>),
75    /// Map (entries, key type, value type).
76    Map(Option<HashMap<Value, Value>>, Box<Value>, Box<Value>),
77    /// JSON value.
78    Json(Option<JsonValue>),
79    /// Named struct (fields, field types, type name).
80    Struct(Option<Vec<(String, Value)>>, Vec<(String, Value)>, TableRef),
81    /// Unknown type (usually used when no further information is available).
82    Unknown(Option<String>),
83}
84
85impl Value {
86    /// Checks if two values have the same type, ignoring their actual data.
87    pub fn same_type(&self, other: &Self) -> bool {
88        match (self, other) {
89            (Self::Decimal(.., l_width, l_scale), Self::Decimal(.., r_width, r_scale)) => {
90                (*l_width == 0 || *r_width == 0 || l_width == r_width)
91                    && (*l_scale == 0 || *r_scale == 0 || l_scale == r_scale)
92            }
93            (Self::Array(.., l_type, l_len), Self::Array(.., r_type, r_len)) => {
94                l_len == r_len && l_type.same_type(&r_type)
95            }
96            (Self::List(.., l), Self::List(.., r)) => l.same_type(r),
97            (Self::Map(.., l_key, l_value), Self::Map(.., r_key, r_value)) => {
98                l_key.same_type(r_key) && l_value.same_type(&r_value)
99            }
100            _ => discriminant(self) == discriminant(other),
101        }
102    }
103
104    /// Checks if the value is NULL.
105    pub fn is_null(&self) -> bool {
106        match self {
107            Value::Null
108            | Value::Boolean(None, ..)
109            | Value::Int8(None, ..)
110            | Value::Int16(None, ..)
111            | Value::Int32(None, ..)
112            | Value::Int64(None, ..)
113            | Value::Int128(None, ..)
114            | Value::UInt8(None, ..)
115            | Value::UInt16(None, ..)
116            | Value::UInt32(None, ..)
117            | Value::UInt64(None, ..)
118            | Value::UInt128(None, ..)
119            | Value::Float32(None, ..)
120            | Value::Float64(None, ..)
121            | Value::Decimal(None, ..)
122            | Value::Char(None, ..)
123            | Value::Varchar(None, ..)
124            | Value::Blob(None, ..)
125            | Value::Date(None, ..)
126            | Value::Time(None, ..)
127            | Value::Timestamp(None, ..)
128            | Value::TimestampWithTimezone(None, ..)
129            | Value::Interval(None, ..)
130            | Value::Uuid(None, ..)
131            | Value::Array(None, ..)
132            | Value::List(None, ..)
133            | Value::Map(None, ..)
134            | Value::Json(None, ..)
135            | Value::Json(Some(serde_json::Value::Null), ..)
136            | Value::Struct(None, ..)
137            | Value::Unknown(None, ..) => true,
138            _ => false,
139        }
140    }
141
142    /// Create a value retaining only the type information, with all data set to NULL.
143    pub fn as_null(&self) -> Value {
144        match self {
145            Value::Null => Value::Null,
146            Value::Boolean(..) => Value::Boolean(None),
147            Value::Int8(..) => Value::Int8(None),
148            Value::Int16(..) => Value::Int16(None),
149            Value::Int32(..) => Value::Int32(None),
150            Value::Int64(..) => Value::Int64(None),
151            Value::Int128(..) => Value::Int128(None),
152            Value::UInt8(..) => Value::UInt8(None),
153            Value::UInt16(..) => Value::UInt16(None),
154            Value::UInt32(..) => Value::UInt32(None),
155            Value::UInt64(..) => Value::UInt64(None),
156            Value::UInt128(..) => Value::UInt128(None),
157            Value::Float32(..) => Value::Float32(None),
158            Value::Float64(..) => Value::Float64(None),
159            Value::Decimal(.., w, s) => Value::Decimal(None, *w, *s),
160            Value::Char(..) => Value::Char(None),
161            Value::Varchar(..) => Value::Varchar(None),
162            Value::Blob(..) => Value::Blob(None),
163            Value::Date(..) => Value::Date(None),
164            Value::Time(..) => Value::Time(None),
165            Value::Timestamp(..) => Value::Timestamp(None),
166            Value::TimestampWithTimezone(..) => Value::TimestampWithTimezone(None),
167            Value::Interval(..) => Value::Interval(None),
168            Value::Uuid(..) => Value::Uuid(None),
169            Value::Array(.., t, len) => Value::Array(None, t.clone(), *len),
170            Value::List(.., t) => Value::List(None, t.clone()),
171            Value::Map(.., k, v) => Value::Map(None, k.clone(), v.clone()),
172            Value::Json(..) => Value::Json(None),
173            Value::Struct(.., ty, name) => Value::Struct(None, ty.clone(), name.clone()),
174            Value::Unknown(..) => Value::Unknown(None),
175        }
176    }
177
178    /// Attempts to convert the value to the type of another value, if they are compatible.
179    pub fn try_as(self, target_type: &Value) -> Result<Value> {
180        if self.same_type(target_type) {
181            return Ok(self);
182        }
183        match target_type {
184            Value::Boolean(..) => bool::try_from_value(self).map(AsValue::as_value),
185            Value::Int8(..) => i8::try_from_value(self).map(AsValue::as_value),
186            Value::Int16(..) => i16::try_from_value(self).map(AsValue::as_value),
187            Value::Int32(..) => i32::try_from_value(self).map(AsValue::as_value),
188            Value::Int64(..) => i64::try_from_value(self).map(AsValue::as_value),
189            Value::Int128(..) => i128::try_from_value(self).map(AsValue::as_value),
190            Value::UInt8(..) => u8::try_from_value(self).map(AsValue::as_value),
191            Value::UInt16(..) => u16::try_from_value(self).map(AsValue::as_value),
192            Value::UInt32(..) => u32::try_from_value(self).map(AsValue::as_value),
193            Value::UInt64(..) => u64::try_from_value(self).map(AsValue::as_value),
194            Value::UInt128(..) => u128::try_from_value(self).map(AsValue::as_value),
195            Value::Float32(..) => f32::try_from_value(self).map(AsValue::as_value),
196            Value::Float64(..) => f64::try_from_value(self).map(AsValue::as_value),
197            Value::Decimal(..) => Decimal::try_from_value(self).map(AsValue::as_value),
198            Value::Char(..) => char::try_from_value(self).map(AsValue::as_value),
199            Value::Varchar(..) => String::try_from_value(self).map(AsValue::as_value),
200            Value::Blob(..) => Box::<[u8]>::try_from_value(self).map(AsValue::as_value),
201            Value::Date(..) => Date::try_from_value(self).map(AsValue::as_value),
202            Value::Time(..) => Time::try_from_value(self).map(AsValue::as_value),
203            Value::Timestamp(..) => PrimitiveDateTime::try_from_value(self).map(AsValue::as_value),
204            Value::TimestampWithTimezone(..) => {
205                OffsetDateTime::try_from_value(self).map(AsValue::as_value)
206            }
207            Value::Interval(..) => Interval::try_from_value(self).map(AsValue::as_value),
208            Value::Uuid(..) => Uuid::try_from_value(self).map(AsValue::as_value),
209            // Value::Array(.., ty, len) => {
210            //     Box::<[Value]>::try_from_value(self).map(AsValue::as_value)
211            // }
212            // Value::List(..) => Box::<[Value]>::try_from_value(self).map(AsValue::as_value),
213            // Value::Map(..) => Date::try_from_value(self).map(AsValue::as_value),
214            _ => {
215                return Err(Error::msg(format!(
216                    "Cannot convert value {:?} to {:?}",
217                    self, target_type
218                )));
219            }
220        }
221    }
222
223    /// Checks if the value is a scalar type (not an array, struct, etc.).
224    pub fn is_scalar(&self) -> bool {
225        match self {
226            Value::Boolean(..)
227            | Value::Int8(..)
228            | Value::Int16(..)
229            | Value::Int32(..)
230            | Value::Int64(..)
231            | Value::Int128(..)
232            | Value::UInt8(..)
233            | Value::UInt16(..)
234            | Value::UInt32(..)
235            | Value::UInt64(..)
236            | Value::UInt128(..)
237            | Value::Float32(..)
238            | Value::Float64(..)
239            | Value::Decimal(..)
240            | Value::Char(..)
241            | Value::Varchar(..)
242            | Value::Blob(..)
243            | Value::Date(..)
244            | Value::Time(..)
245            | Value::Timestamp(..)
246            | Value::TimestampWithTimezone(..)
247            | Value::Interval(..)
248            | Value::Uuid(..)
249            | Value::Unknown(..) => true,
250            _ => false,
251        }
252    }
253}
254
255impl PartialEq for Value {
256    fn eq(&self, other: &Self) -> bool {
257        match (self, other) {
258            (Self::Boolean(l), Self::Boolean(r)) => l == r,
259            (Self::Int8(l), Self::Int8(r)) => l == r,
260            (Self::Int16(l), Self::Int16(r)) => l == r,
261            (Self::Int32(l), Self::Int32(r)) => l == r,
262            (Self::Int64(l), Self::Int64(r)) => l == r,
263            (Self::Int128(l), Self::Int128(r)) => l == r,
264            (Self::UInt8(l), Self::UInt8(r)) => l == r,
265            (Self::UInt16(l), Self::UInt16(r)) => l == r,
266            (Self::UInt32(l), Self::UInt32(r)) => l == r,
267            (Self::UInt64(l), Self::UInt64(r)) => l == r,
268            (Self::UInt128(l), Self::UInt128(r)) => l == r,
269            (Self::Float32(l), Self::Float32(r)) => {
270                l == r
271                    || l.and_then(|l| r.and_then(|r| Some(l.is_nan() && r.is_nan())))
272                        .unwrap_or_default()
273            }
274            (Self::Float64(l), Self::Float64(r)) => {
275                l == r
276                    || l.and_then(|l| r.and_then(|r| Some(l.is_nan() && r.is_nan())))
277                        .unwrap_or_default()
278            }
279            (Self::Decimal(l, l_width, l_scale), Self::Decimal(r, r_width, r_scale)) => {
280                l == r && l_width == r_width && l_scale == r_scale
281            }
282            (Self::Char(l), Self::Char(r)) => l == r,
283            (Self::Varchar(l), Self::Varchar(r)) => l == r,
284            (Self::Blob(l), Self::Blob(r)) => l == r,
285            (Self::Date(l), Self::Date(r)) => l == r,
286            (Self::Time(l), Self::Time(r)) => l == r,
287            (Self::Timestamp(l), Self::Timestamp(r)) => l == r,
288            (Self::TimestampWithTimezone(l), Self::TimestampWithTimezone(r)) => l == r,
289            (Self::Interval(l), Self::Interval(r)) => l == r,
290            (Self::Uuid(l), Self::Uuid(r)) => l == r,
291            (Self::Array(l, ..), Self::Array(r, ..)) => l == r && self.same_type(other),
292            (Self::List(l, ..), Self::List(r, ..)) => l == r && self.same_type(other),
293            (Self::Map(None, ..), Self::Map(None, ..)) => self.same_type(other),
294            (Self::Map(Some(l), ..), Self::Map(Some(r), ..)) => {
295                l.is_empty() == r.is_empty() && self.same_type(other)
296            }
297            (Self::Map(..), Self::Map(..)) => self.same_type(other),
298            (Self::Json(l), Self::Json(r)) => l == r,
299            (Self::Struct(l, l_ty, l_name), Self::Struct(r, r_ty, r_name)) => {
300                l_name == r_name && l == r && l_ty == r_ty
301            }
302            (Self::Unknown(..), Self::Unknown(..)) => false,
303            _ => false,
304        }
305    }
306}
307
308impl Eq for Value {}
309
310impl Hash for Value {
311    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
312        use Value::*;
313        discriminant(self).hash(state);
314        match self {
315            Null => {}
316            Boolean(v) => v.hash(state),
317            Int8(v) => v.hash(state),
318            Int16(v) => v.hash(state),
319            Int32(v) => v.hash(state),
320            Int64(v) => v.hash(state),
321            Int128(v) => v.hash(state),
322
323            UInt8(v) => v.hash(state),
324            UInt16(v) => v.hash(state),
325            UInt32(v) => v.hash(state),
326            UInt64(v) => v.hash(state),
327            UInt128(v) => v.hash(state),
328            Float32(Some(v)) => {
329                v.to_bits().hash(state);
330            }
331            Float32(None) => None::<u32>.hash(state),
332            Float64(Some(v)) => {
333                v.to_bits().hash(state);
334            }
335            Float64(None) => None::<u64>.hash(state),
336            Decimal(v, width, scale) => {
337                v.hash(state);
338                width.hash(state);
339                scale.hash(state);
340            }
341            Char(v) => v.hash(state),
342            Varchar(v) => v.hash(state),
343            Blob(v) => v.hash(state),
344            Date(v) => v.hash(state),
345            Time(v) => v.hash(state),
346            Timestamp(v) => v.hash(state),
347            TimestampWithTimezone(v) => v.hash(state),
348            Interval(v) => v.hash(state),
349            Uuid(v) => v.hash(state),
350            Array(v, typ, len) => {
351                v.hash(state);
352                typ.hash(state);
353                len.hash(state);
354            }
355            List(v, typ) => {
356                v.hash(state);
357                typ.hash(state);
358            }
359            Map(v, key, val) => {
360                match v {
361                    Some(map) => {
362                        for (key, val) in map {
363                            key.hash(state);
364                            val.hash(state);
365                        }
366                    }
367                    None => {}
368                }
369                key.hash(state);
370                val.hash(state);
371            }
372            Json(v) => v.hash(state),
373            Struct(v, t, name) => {
374                match v {
375                    Some(v) => v.hash(state),
376                    None => {}
377                }
378                t.hash(state);
379                name.hash(state);
380            }
381            Unknown(v) => v.hash(state),
382        }
383    }
384}
385
386impl Display for Value {
387    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
388        let mut out = DynQuery::default();
389        self.write_query(&GenericSqlWriter::new(), &mut Default::default(), &mut out);
390        let _ = f.write_str(out.buffer());
391        Ok(())
392    }
393}
394
395/// Internally decoded type info for macros.
396#[derive(Default)]
397pub struct TypeDecoded {
398    /// Representative value establishing variant & metadata.
399    pub value: Value,
400    /// Nullability indicator.
401    pub nullable: bool,
402}
403
404impl ToTokens for Value {
405    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
406        let ts = match self {
407            Value::Null => quote!(::tank::Value::Null),
408            Value::Boolean(..) => quote!(::tank::Value::Boolean(None)),
409            Value::Int8(..) => quote!(::tank::Value::Int8(None)),
410            Value::Int16(..) => quote!(::tank::Value::Int16(None)),
411            Value::Int32(..) => quote!(::tank::Value::Int32(None)),
412            Value::Int64(..) => quote!(::tank::Value::Int64(None)),
413            Value::Int128(..) => quote!(::tank::Value::Int128(None)),
414            Value::UInt8(..) => quote!(::tank::Value::UInt8(None)),
415            Value::UInt16(..) => quote!(::tank::Value::UInt16(None)),
416            Value::UInt32(..) => quote!(::tank::Value::UInt32(None)),
417            Value::UInt64(..) => quote!(::tank::Value::UInt64(None)),
418            Value::UInt128(..) => quote!(::tank::Value::UInt128(None)),
419            Value::Float32(..) => quote!(::tank::Value::Float32(None)),
420            Value::Float64(..) => quote!(::tank::Value::Float64(None)),
421            Value::Decimal(.., width, scale) => {
422                quote!(::tank::Value::Decimal(None, #width, #scale))
423            }
424            Value::Char(..) => quote!(tank::Value::Char(None)),
425            Value::Varchar(..) => quote!(::tank::Value::Varchar(None)),
426            Value::Blob(..) => quote!(::tank::Value::Blob(None)),
427            Value::Date(..) => quote!(::tank::Value::Date(None)),
428            Value::Time(..) => quote!(::tank::Value::Time(None)),
429            Value::Timestamp(..) => quote!(::tank::Value::Timestamp(None)),
430            Value::TimestampWithTimezone(..) => quote!(::tank::Value::TimestampWithTimezone(None)),
431            Value::Interval(..) => quote!(::tank::Value::Interval(None)),
432            Value::Uuid(..) => quote!(::tank::Value::Uuid(None)),
433            Value::Array(.., inner, size) => {
434                quote!(::tank::Value::Array(None, Box::new(#inner), #size))
435            }
436            Value::List(.., inner) => {
437                let inner = inner.as_ref().to_token_stream();
438                quote!(::tank::Value::List(None, Box::new(#inner)))
439            }
440            Value::Map(.., key, value) => {
441                let key = key.as_ref().to_token_stream();
442                let value = value.as_ref().to_token_stream();
443                quote!(::tank::Value::Map(None, Box::new(#key), Box::new(#value)))
444            }
445            Value::Json(..) => quote!(::tank::Value::Json(None)),
446            Value::Struct(.., ty, name) => {
447                let values = ty.into_iter().map(|(k, v)| quote!((#k.into(), #v)));
448                quote!(::tank::Value::Struct(None, vec!(#(#values),*), #name))
449            }
450            Value::Unknown(..) => quote!(::tank::Value::Unknown(None)),
451        };
452        tokens.extend(ts);
453    }
454}