Skip to main content

tank_core/
value.rs

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