taos_query/common/
value.rs

1use std::{borrow::Cow, fmt::Display, str::Utf8Error};
2
3use super::{Timestamp, Ty};
4use bytes::Bytes;
5use rust_decimal::prelude::*;
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone)]
9pub enum BorrowedValue<'b> {
10    Null(Ty),    // 0
11    Bool(bool),  // 1
12    TinyInt(i8), // 2
13    SmallInt(i16),
14    Int(i32),
15    BigInt(i64),
16    Float(f32),
17    Double(f64),
18    VarChar(&'b str),
19    Timestamp(Timestamp),
20    NChar(Cow<'b, str>),
21    UTinyInt(u8),
22    USmallInt(u16),
23    UInt(u32),
24    UBigInt(u64), // 14
25    Json(Cow<'b, [u8]>),
26    VarBinary(Cow<'b, [u8]>),
27    Decimal(Decimal),
28    Blob(&'b [u8]),
29    MediumBlob(&'b [u8]),
30    Geometry(Cow<'b, [u8]>),
31}
32
33macro_rules! borrowed_value_to_native {
34    ($v:expr) => {
35        match $v {
36            BorrowedValue::Null(_) => None,
37            BorrowedValue::Bool(v) => Some(if *v { 1 } else { 0 }),
38            BorrowedValue::TinyInt(v) => Some(*v as _),
39            BorrowedValue::SmallInt(v) => Some(*v as _),
40            BorrowedValue::Int(v) => Some(*v as _),
41            BorrowedValue::BigInt(v) => Some(*v as _),
42            BorrowedValue::Float(v) => Some(*v as _),
43            BorrowedValue::Double(v) => Some(*v as _),
44            BorrowedValue::VarChar(s) => s.parse().map(Some).unwrap_or(None),
45            BorrowedValue::Timestamp(v) => Some(v.as_raw_i64() as _),
46            BorrowedValue::NChar(s) => s.parse().map(Some).unwrap_or(None),
47            BorrowedValue::UTinyInt(v) => Some(*v as _),
48            BorrowedValue::USmallInt(v) => Some(*v as _),
49            BorrowedValue::UInt(v) => Some(*v as _),
50            BorrowedValue::UBigInt(v) => Some(*v as _),
51            BorrowedValue::Json(v) => serde_json::from_slice(&v).ok(),
52            BorrowedValue::VarBinary(_v) => todo!(),
53            BorrowedValue::Decimal(_) => todo!(),
54            BorrowedValue::Blob(_) => todo!(),
55            BorrowedValue::MediumBlob(_) => todo!(),
56            BorrowedValue::Geometry(_) => todo!(),
57        }
58    };
59}
60
61macro_rules! borrowed_value_to_float {
62    ($v:expr) => {
63        match $v {
64            BorrowedValue::Null(_) => None,
65            BorrowedValue::Bool(v) => Some(if *v { 1. } else { 0. }),
66            BorrowedValue::TinyInt(v) => Some(*v as _),
67            BorrowedValue::SmallInt(v) => Some(*v as _),
68            BorrowedValue::Int(v) => Some(*v as _),
69            BorrowedValue::BigInt(v) => Some(*v as _),
70            BorrowedValue::Float(v) => Some(*v as _),
71            BorrowedValue::Double(v) => Some(*v as _),
72            BorrowedValue::VarChar(s) => s.parse().map(Some).unwrap_or(None),
73            BorrowedValue::Timestamp(v) => Some(v.as_raw_i64() as _),
74            BorrowedValue::NChar(s) => s.parse().map(Some).unwrap_or(None),
75            BorrowedValue::UTinyInt(v) => Some(*v as _),
76            BorrowedValue::USmallInt(v) => Some(*v as _),
77            BorrowedValue::UInt(v) => Some(*v as _),
78            BorrowedValue::UBigInt(v) => Some(*v as _),
79            BorrowedValue::Json(v) => serde_json::from_slice(&v).ok(),
80            BorrowedValue::VarBinary(_) => todo!(),
81            BorrowedValue::Decimal(_) => todo!(),
82            BorrowedValue::Blob(_) => todo!(),
83            BorrowedValue::MediumBlob(_) => todo!(),
84            BorrowedValue::Geometry(_) => todo!(),
85        }
86    };
87}
88
89impl<'b> BorrowedValue<'b> {
90    /// The data type of this value.
91    pub const fn ty(&self) -> Ty {
92        use BorrowedValue::*;
93        match self {
94            Null(ty) => *ty,
95            Bool(_) => Ty::Bool,
96            TinyInt(_) => Ty::TinyInt,
97            SmallInt(_) => Ty::SmallInt,
98            Int(_) => Ty::Int,
99            BigInt(_) => Ty::BigInt,
100            UTinyInt(_) => Ty::UTinyInt,
101            USmallInt(_) => Ty::USmallInt,
102            UInt(_) => Ty::UInt,
103            UBigInt(_) => Ty::UBigInt,
104            Float(_) => Ty::Float,
105            Double(_) => Ty::Double,
106            VarChar(_) => Ty::VarChar,
107            Timestamp(_) => Ty::Timestamp,
108            Json(_) => Ty::Json,
109            NChar(_) => Ty::NChar,
110            VarBinary(_) => Ty::VarBinary,
111            Decimal(_) => Ty::Decimal,
112            Blob(_) => Ty::Blob,
113            MediumBlob(_) => Ty::MediumBlob,
114            Geometry(_) => Ty::Geometry,
115        }
116    }
117
118    pub fn to_sql_value(&self) -> String {
119        use BorrowedValue::*;
120        match self {
121            Null(_) => "NULL".to_string(),
122            Bool(v) => format!("{v}"),
123            TinyInt(v) => format!("{v}"),
124            SmallInt(v) => format!("{v}"),
125            Int(v) => format!("{v}"),
126            BigInt(v) => format!("{v}"),
127            Float(v) => format!("{v}"),
128            Double(v) => format!("{v}"),
129            VarChar(v) => format!("\"{}\"", v.escape_debug()),
130            Timestamp(v) => format!("{}", v.as_raw_i64()),
131            NChar(v) => format!("\"{}\"", v.escape_debug()),
132            UTinyInt(v) => format!("{v}"),
133            USmallInt(v) => format!("{v}"),
134            UInt(v) => format!("{v}"),
135            UBigInt(v) => format!("{v}"),
136            Json(v) => format!("\"{}\"", unsafe { std::str::from_utf8_unchecked(v) }),
137            VarBinary(_) => todo!(),
138            Decimal(_) => todo!(),
139            Blob(_) => todo!(),
140            MediumBlob(_) => todo!(),
141            Geometry(_) => todo!(),
142        }
143    }
144
145    /// Check if the value is null.
146    pub const fn is_null(&self) -> bool {
147        matches!(self, BorrowedValue::Null(_))
148    }
149    /// Only VarChar, NChar, Json could be treated as [&str].
150    fn strict_as_str(&self) -> &str {
151        use BorrowedValue::*;
152        match self {
153            VarChar(v) => v,
154            NChar(v) => v,
155            Null(_) => panic!("expect str but value is null"),
156            Timestamp(_) => panic!("expect str but value is timestamp"),
157            _ => panic!("expect str but only varchar/binary/nchar is supported"),
158        }
159    }
160    pub fn to_string(&self) -> Result<String, Utf8Error> {
161        use BorrowedValue::*;
162        match self {
163            Null(_) => Ok(String::new()),
164            Bool(v) => Ok(format!("{v}")),
165            VarChar(v) => Ok(v.to_string()),
166            Json(v) => Ok(unsafe { std::str::from_utf8_unchecked(v) }.to_string()),
167            NChar(v) => Ok(v.to_string()),
168            TinyInt(v) => Ok(format!("{v}")),
169            SmallInt(v) => Ok(format!("{v}")),
170            Int(v) => Ok(format!("{v}")),
171            BigInt(v) => Ok(format!("{v}")),
172            UTinyInt(v) => Ok(format!("{v}")),
173            USmallInt(v) => Ok(format!("{v}")),
174            UInt(v) => Ok(format!("{v}")),
175            UBigInt(v) => Ok(format!("{v}")),
176            Float(v) => Ok(format!("{v}")),
177            Double(v) => Ok(format!("{v}")),
178            Timestamp(v) => Ok(v.to_datetime_with_tz().to_rfc3339()),
179            _ => unreachable!("un supported type to string"),
180        }
181    }
182
183    pub fn to_value(&self) -> Value {
184        use BorrowedValue::*;
185        match self {
186            Null(ty) => Value::Null(*ty),
187            Bool(v) => Value::Bool(*v),
188            TinyInt(v) => Value::TinyInt(*v),
189            SmallInt(v) => Value::SmallInt(*v),
190            Int(v) => Value::Int(*v),
191            BigInt(v) => Value::BigInt(*v),
192            UTinyInt(v) => Value::UTinyInt(*v),
193            USmallInt(v) => Value::USmallInt(*v),
194            UInt(v) => Value::UInt(*v),
195            UBigInt(v) => Value::UBigInt(*v),
196            Float(v) => Value::Float(*v),
197            Double(v) => Value::Double(*v),
198            VarChar(v) => Value::VarChar(v.to_string()),
199            Timestamp(v) => Value::Timestamp(*v),
200            Json(v) => {
201                Value::Json(serde_json::from_slice(v).expect("json should always be deserialized"))
202            }
203            NChar(str) => Value::NChar(str.to_string()),
204            VarBinary(v) => Value::VarBinary(Bytes::copy_from_slice(v.as_ref())),
205            Decimal(_) => todo!(),
206            Blob(_) => todo!(),
207            MediumBlob(_) => todo!(),
208            Geometry(v) => Value::Geometry(Bytes::copy_from_slice(v.as_ref())),
209        }
210    }
211
212    pub fn to_json_value(&self) -> serde_json::Value {
213        use BorrowedValue::*;
214        match self {
215            Null(_) => serde_json::Value::Null,
216            Bool(v) => serde_json::Value::Bool(*v),
217            TinyInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
218            SmallInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
219            Int(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
220            BigInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
221            UTinyInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
222            USmallInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
223            UInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
224            UBigInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
225            Float(v) => serde_json::Value::Number(serde_json::Number::from_f64(*v as f64).unwrap()),
226            Double(v) => serde_json::Value::Number(serde_json::Number::from_f64(*v).unwrap()),
227            VarChar(v) => serde_json::Value::String(v.to_string()),
228            Timestamp(v) => serde_json::Value::Number(serde_json::Number::from(v.as_raw_i64())),
229            Json(v) => serde_json::from_slice(v).expect("json should always be deserialized"),
230            NChar(str) => serde_json::Value::String(str.to_string()),
231            VarBinary(_) => todo!(),
232            Decimal(_) => todo!(),
233            Blob(_) => todo!(),
234            MediumBlob(_) => todo!(),
235            Geometry(_) => todo!(),
236        }
237    }
238
239    #[inline]
240    pub fn into_value(self) -> Value {
241        use BorrowedValue::*;
242        match self {
243            Null(ty) => Value::Null(ty),
244            Bool(v) => Value::Bool(v),
245            TinyInt(v) => Value::TinyInt(v),
246            SmallInt(v) => Value::SmallInt(v),
247            Int(v) => Value::Int(v),
248            BigInt(v) => Value::BigInt(v),
249            UTinyInt(v) => Value::UTinyInt(v),
250            USmallInt(v) => Value::USmallInt(v),
251            UInt(v) => Value::UInt(v),
252            UBigInt(v) => Value::UBigInt(v),
253            Float(v) => Value::Float(v),
254            Double(v) => Value::Double(v),
255            VarChar(v) => Value::VarChar(v.to_string()),
256            Timestamp(v) => Value::Timestamp(v),
257            Json(v) => {
258                Value::Json(serde_json::from_slice(&v).expect("json should always be deserialized"))
259            }
260            NChar(str) => Value::NChar(str.to_string()),
261            VarBinary(v) => Value::VarBinary(Bytes::from(v.into_owned())),
262            Decimal(_) => todo!(),
263            Blob(_) => todo!(),
264            MediumBlob(_) => todo!(),
265            Geometry(v) => Value::Geometry(Bytes::from(v.into_owned())),
266        }
267    }
268
269    pub(crate) fn to_bool(&self) -> Option<bool> {
270        match self {
271            BorrowedValue::Null(_) => None,
272            BorrowedValue::Bool(v) => Some(*v),
273            BorrowedValue::TinyInt(v) => Some(*v > 0),
274            BorrowedValue::SmallInt(v) => Some(*v > 0),
275            BorrowedValue::Int(v) => Some(*v > 0),
276            BorrowedValue::BigInt(v) => Some(*v > 0),
277            BorrowedValue::Float(v) => Some(*v > 0.),
278            BorrowedValue::Double(v) => Some(*v > 0.),
279            BorrowedValue::VarChar(s) => match *s {
280                "" => None,
281                "false" | "f" | "F" | "FALSE" | "False" => Some(false),
282                "true" | "t" | "T" | "TRUE" | "True" => Some(true),
283                _ => Some(true),
284            },
285            BorrowedValue::Timestamp(_) => Some(true),
286            BorrowedValue::NChar(s) => match s.as_ref() {
287                "" => None,
288                "false" | "f" | "F" | "FALSE" | "False" => Some(false),
289                "true" | "t" | "T" | "TRUE" | "True" => Some(true),
290                _ => Some(true),
291            },
292            BorrowedValue::UTinyInt(v) => Some(*v != 0),
293            BorrowedValue::USmallInt(v) => Some(*v != 0),
294            BorrowedValue::UInt(v) => Some(*v != 0),
295            BorrowedValue::UBigInt(v) => Some(*v != 0),
296            BorrowedValue::Json(_) => Some(true),
297            BorrowedValue::VarBinary(_) => todo!(),
298            BorrowedValue::Decimal(_) => todo!(),
299            BorrowedValue::Blob(_) => todo!(),
300            BorrowedValue::MediumBlob(_) => todo!(),
301            BorrowedValue::Geometry(_) => todo!(),
302        }
303    }
304
305    pub(crate) fn to_i8(&self) -> Option<i8> {
306        borrowed_value_to_native!(self)
307    }
308    pub(crate) fn to_i16(&self) -> Option<i16> {
309        borrowed_value_to_native!(self)
310    }
311    pub(crate) fn to_i32(&self) -> Option<i32> {
312        borrowed_value_to_native!(self)
313    }
314
315    pub(crate) fn to_i64(&self) -> Option<i64> {
316        borrowed_value_to_native!(self)
317    }
318    pub(crate) fn to_u8(&self) -> Option<u8> {
319        borrowed_value_to_native!(self)
320    }
321    pub(crate) fn to_u16(&self) -> Option<u16> {
322        borrowed_value_to_native!(self)
323    }
324
325    pub(crate) fn to_u32(&self) -> Option<u32> {
326        borrowed_value_to_native!(self)
327    }
328
329    pub(crate) fn to_u64(&self) -> Option<u64> {
330        borrowed_value_to_native!(self)
331    }
332
333    pub(crate) fn to_f32(&self) -> Option<f32> {
334        borrowed_value_to_float!(self)
335    }
336    pub(crate) fn to_f64(&self) -> Option<f64> {
337        borrowed_value_to_float!(self)
338    }
339    pub(crate) fn to_str(&self) -> Option<Cow<str>> {
340        match self {
341            BorrowedValue::Null(_) => None,
342            BorrowedValue::Bool(v) => Some(v.to_string().into()),
343            BorrowedValue::TinyInt(v) => Some(v.to_string().into()),
344            BorrowedValue::SmallInt(v) => Some(v.to_string().into()),
345            BorrowedValue::Int(v) => Some(v.to_string().into()),
346            BorrowedValue::BigInt(v) => Some(v.to_string().into()),
347            BorrowedValue::Float(v) => Some(v.to_string().into()),
348            BorrowedValue::Double(v) => Some(v.to_string().into()),
349            BorrowedValue::VarChar(s) => Some((*s).into()),
350            BorrowedValue::Timestamp(v) => Some(v.to_datetime_with_tz().to_string().into()),
351            BorrowedValue::NChar(s) => Some(s.as_ref().into()),
352            BorrowedValue::UTinyInt(v) => Some(v.to_string().into()),
353            BorrowedValue::USmallInt(v) => Some(v.to_string().into()),
354            BorrowedValue::UInt(v) => Some(v.to_string().into()),
355            BorrowedValue::UBigInt(v) => Some(v.to_string().into()),
356            BorrowedValue::Json(v) => Some(unsafe { std::str::from_utf8_unchecked(v) }.into()),
357            BorrowedValue::VarBinary(_) => todo!(),
358            BorrowedValue::Decimal(_) => todo!(),
359            BorrowedValue::Blob(_) => todo!(),
360            BorrowedValue::MediumBlob(_) => todo!(),
361            BorrowedValue::Geometry(_) => todo!(),
362        }
363    }
364    #[allow(dead_code)]
365    pub(crate) fn to_bytes(&self) -> Option<Bytes> {
366        match self {
367            BorrowedValue::VarBinary(v) => Some(Bytes::from(v.to_vec())),
368            BorrowedValue::Geometry(v) => Some(Bytes::from(v.to_vec())),
369            _ => None,
370        }
371    }
372
373    pub(crate) fn to_timestamp(&self) -> Option<Timestamp> {
374        match self {
375            BorrowedValue::Null(_) => None,
376            BorrowedValue::Timestamp(v) => Some(*v),
377            _ => panic!("Unsupported conversion from {} to timestamp", self.ty()),
378        }
379    }
380}
381
382impl<'b> Display for BorrowedValue<'b> {
383    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
384        use BorrowedValue::*;
385        match self {
386            Null(_) => f.write_str("NULL"),
387            Bool(v) => f.write_fmt(format_args!("{v}")),
388            TinyInt(v) => f.write_fmt(format_args!("{v}")),
389            SmallInt(v) => f.write_fmt(format_args!("{v}")),
390            Int(v) => f.write_fmt(format_args!("{v}")),
391            BigInt(v) => f.write_fmt(format_args!("{v}")),
392            Float(v) => f.write_fmt(format_args!("{v}")),
393            Double(v) => f.write_fmt(format_args!("{v}")),
394            VarChar(v) => f.write_fmt(format_args!("{v}")),
395            Timestamp(v) => f.write_fmt(format_args!("{v}")),
396            NChar(v) => f.write_fmt(format_args!("{v}")),
397            UTinyInt(v) => f.write_fmt(format_args!("{v}")),
398            USmallInt(v) => f.write_fmt(format_args!("{v}")),
399            UInt(v) => f.write_fmt(format_args!("{v}")),
400            UBigInt(v) => f.write_fmt(format_args!("{v}")),
401            Json(v) => f.write_fmt(format_args!("{}", v.as_ref().escape_ascii())),
402            VarBinary(v) => f.write_fmt(format_args!("{:?}", v.to_vec())),
403            Decimal(_) => todo!(),
404            Blob(_) => todo!(),
405            MediumBlob(_) => todo!(),
406            Geometry(v) => f.write_fmt(format_args!("{:?}", v.to_vec())),
407        }
408    }
409}
410
411unsafe impl<'b> Send for BorrowedValue<'b> {}
412
413// #[derive(Debug, Clone)]
414#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
415pub enum Value {
416    Null(Ty),    // 0
417    Bool(bool),  // 1
418    TinyInt(i8), // 2
419    SmallInt(i16),
420    Int(i32),
421    BigInt(i64),
422    Float(f32),
423    Double(f64),
424    VarChar(String),
425    Timestamp(Timestamp),
426    NChar(String),
427    UTinyInt(u8),
428    USmallInt(u16),
429    UInt(u32),
430    UBigInt(u64), // 14
431    Json(serde_json::Value),
432    VarBinary(Bytes),
433    Decimal(Decimal),
434    Blob(Vec<u8>),
435    MediumBlob(Vec<u8>),
436    Geometry(Bytes),
437}
438
439impl Display for Value {
440    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
441        use Value::*;
442        match self {
443            Null(_) => f.write_str("NULL"),
444            Bool(v) => f.write_fmt(format_args!("{v}")),
445            TinyInt(v) => f.write_fmt(format_args!("{v}")),
446            SmallInt(v) => f.write_fmt(format_args!("{v}")),
447            Int(v) => f.write_fmt(format_args!("{v}")),
448            BigInt(v) => f.write_fmt(format_args!("{v}")),
449            Float(v) => f.write_fmt(format_args!("{v}")),
450            Double(v) => f.write_fmt(format_args!("{v}")),
451            VarChar(v) => f.write_fmt(format_args!("{v}")),
452            Timestamp(v) => f.write_fmt(format_args!("{v}")),
453            NChar(v) => f.write_fmt(format_args!("{v}")),
454            UTinyInt(v) => f.write_fmt(format_args!("{v}")),
455            USmallInt(v) => f.write_fmt(format_args!("{v}")),
456            UInt(v) => f.write_fmt(format_args!("{v}")),
457            UBigInt(v) => f.write_fmt(format_args!("{v}")),
458            Json(v) => f.write_fmt(format_args!("{v}")),
459            VarBinary(v) => f.write_fmt(format_args!("{:?}", v.to_vec())),
460            Decimal(_) => todo!(),
461            Blob(_) => todo!(),
462            MediumBlob(_) => todo!(),
463            Geometry(v) => f.write_fmt(format_args!("{:?}", v.to_vec())),
464        }
465    }
466}
467
468impl Value {
469    /// The data type of this value.
470    pub const fn ty(&self) -> Ty {
471        use Value::*;
472        match self {
473            Null(ty) => *ty,
474            Bool(_) => Ty::Bool,
475            TinyInt(_) => Ty::TinyInt,
476            SmallInt(_) => Ty::SmallInt,
477            Int(_) => Ty::Int,
478            BigInt(_) => Ty::BigInt,
479            UTinyInt(_) => Ty::UTinyInt,
480            USmallInt(_) => Ty::USmallInt,
481            UInt(_) => Ty::UInt,
482            UBigInt(_) => Ty::UBigInt,
483            Float(_) => Ty::Float,
484            Double(_) => Ty::Double,
485            VarChar(_) => Ty::VarChar,
486            Timestamp(_) => Ty::Timestamp,
487            Json(_) => Ty::Json,
488            NChar(_) => Ty::NChar,
489            VarBinary(_) => Ty::VarBinary,
490            Decimal(_) => Ty::Decimal,
491            Blob(_) => Ty::Blob,
492            MediumBlob(_) => Ty::MediumBlob,
493            Geometry(_) => Ty::Geometry,
494        }
495    }
496
497    pub fn to_borrowed_value(&self) -> BorrowedValue {
498        use Value::*;
499        match self {
500            Null(ty) => BorrowedValue::Null(*ty),
501            Bool(v) => BorrowedValue::Bool(*v),
502            TinyInt(v) => BorrowedValue::TinyInt(*v),
503            SmallInt(v) => BorrowedValue::SmallInt(*v),
504            Int(v) => BorrowedValue::Int(*v),
505            BigInt(v) => BorrowedValue::BigInt(*v),
506            UTinyInt(v) => BorrowedValue::UTinyInt(*v),
507            USmallInt(v) => BorrowedValue::USmallInt(*v),
508            UInt(v) => BorrowedValue::UInt(*v),
509            UBigInt(v) => BorrowedValue::UBigInt(*v),
510            Float(v) => BorrowedValue::Float(*v),
511            Double(v) => BorrowedValue::Double(*v),
512            VarChar(v) => BorrowedValue::VarChar(v),
513            Timestamp(v) => BorrowedValue::Timestamp(*v),
514            Json(j) => BorrowedValue::Json(j.to_string().into_bytes().into()),
515            NChar(v) => BorrowedValue::NChar(v.as_str().into()),
516            VarBinary(v) => BorrowedValue::VarBinary(Cow::Borrowed(v.as_ref())),
517            Decimal(v) => BorrowedValue::Decimal(*v),
518            Blob(v) => BorrowedValue::Blob(v),
519            MediumBlob(v) => BorrowedValue::MediumBlob(v),
520            Geometry(v) => BorrowedValue::Geometry(Cow::Borrowed(v.as_ref())),
521        }
522    }
523
524    /// Check if the value is null.
525    pub const fn is_null(&self) -> bool {
526        matches!(self, Value::Null(_))
527    }
528    /// Only VarChar, NChar, Json could be treated as [&str].
529    pub fn strict_as_str(&self) -> &str {
530        use Value::*;
531        match self {
532            VarChar(v) => v.as_str(),
533            NChar(v) => v.as_str(),
534            Json(v) => v.as_str().expect("invalid str type"),
535            Null(_) => "Null",
536            Timestamp(_) => panic!("expect str but value is timestamp"),
537            _ => panic!("expect str but only varchar/binary/json/nchar is supported"),
538        }
539    }
540
541    pub fn to_sql_value(&self) -> String {
542        use Value::*;
543        match self {
544            Null(_) => "NULL".to_string(),
545            Bool(v) => format!("{v}"),
546            TinyInt(v) => format!("{v}"),
547            SmallInt(v) => format!("{v}"),
548            Int(v) => format!("{v}"),
549            BigInt(v) => format!("{v}"),
550            Float(v) => format!("{v}"),
551            Double(v) => format!("{v}"),
552            VarChar(v) => format!("\"{}\"", v.escape_debug()),
553            Timestamp(v) => format!("{}", v.as_raw_i64()),
554            NChar(v) => format!("\"{}\"", v.escape_debug()),
555            UTinyInt(v) => format!("{v}"),
556            USmallInt(v) => format!("{v}"),
557            UInt(v) => format!("{v}"),
558            UBigInt(v) => format!("{v}"),
559            Json(v) => format!("\"{}\"", v),
560            VarBinary(_) => todo!(),
561            Decimal(_) => todo!(),
562            Blob(_) => todo!(),
563            MediumBlob(_) => todo!(),
564            Geometry(_) => todo!(),
565        }
566    }
567
568    pub fn to_string(&self) -> Result<String, Utf8Error> {
569        use Value::*;
570        match self {
571            Null(_) => Ok(String::new()),
572            Bool(v) => Ok(format!("{v}")),
573            VarChar(v) => Ok(v.to_string()),
574            Json(v) => Ok(v.to_string()),
575            NChar(v) => Ok(v.to_string()),
576            TinyInt(v) => Ok(format!("{v}")),
577            SmallInt(v) => Ok(format!("{v}")),
578            Int(v) => Ok(format!("{v}")),
579            BigInt(v) => Ok(format!("{v}")),
580            UTinyInt(v) => Ok(format!("{v}")),
581            USmallInt(v) => Ok(format!("{v}")),
582            UInt(v) => Ok(format!("{v}")),
583            UBigInt(v) => Ok(format!("{v}")),
584            Float(v) => Ok(format!("{v}")),
585            Double(v) => Ok(format!("{v}")),
586            Timestamp(v) => Ok(v.to_datetime_with_tz().to_rfc3339()),
587            _ => unreachable!("unsupported type to string"),
588        }
589    }
590
591    #[warn(unreachable_patterns)]
592    pub fn to_json_value(&self) -> serde_json::Value {
593        use Value::*;
594        match self {
595            Null(_) => serde_json::Value::Null,
596            Bool(v) => serde_json::Value::Bool(*v),
597            TinyInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
598            SmallInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
599            Int(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
600            BigInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
601            UTinyInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
602            USmallInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
603            UInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
604            UBigInt(v) => serde_json::Value::Number(serde_json::Number::from(*v)),
605            Float(v) => serde_json::Value::Number(serde_json::Number::from_f64(*v as f64).unwrap()),
606            Double(v) => serde_json::Value::Number(serde_json::Number::from_f64(*v).unwrap()),
607            VarChar(v) => serde_json::Value::String(v.to_string()),
608            Timestamp(v) => serde_json::Value::Number(serde_json::Number::from(v.as_raw_i64())),
609            Json(v) => v.clone(),
610            NChar(str) => serde_json::Value::String(str.to_string()),
611            Decimal(v) => serde_json::Value::String(format!("{:?}", v)),
612            Blob(v) => serde_json::Value::String(format!("{:?}", v)),
613            MediumBlob(v) => serde_json::Value::String(format!("{:?}", v)),
614            VarBinary(v) => serde_json::Value::String(format!("{:?}", v.to_vec())),
615            Geometry(v) => serde_json::Value::String(format!("{:?}", v.to_vec())),
616        }
617    }
618}
619
620impl<'b> PartialEq<&Value> for BorrowedValue<'b> {
621    fn eq(&self, other: &&Value) -> bool {
622        self == *other
623    }
624}
625
626impl<'b> PartialEq<Value> for BorrowedValue<'b> {
627    fn eq(&self, other: &Value) -> bool {
628        match (self, other) {
629            (Self::Null(l0), Value::Null(r0)) => l0 == r0,
630            (Self::Bool(l0), Value::Bool(r0)) => l0 == r0,
631            (Self::TinyInt(l0), Value::TinyInt(r0)) => l0 == r0,
632            (Self::SmallInt(l0), Value::SmallInt(r0)) => l0 == r0,
633            (Self::Int(l0), Value::Int(r0)) => l0 == r0,
634            (Self::BigInt(l0), Value::BigInt(r0)) => l0 == r0,
635            (Self::Float(l0), Value::Float(r0)) => l0 == r0,
636            (Self::Double(l0), Value::Double(r0)) => l0 == r0,
637            (Self::VarChar(l0), Value::VarChar(r0)) => l0 == r0,
638            (Self::Timestamp(l0), Value::Timestamp(r0)) => l0 == r0,
639            (Self::NChar(l0), Value::NChar(r0)) => l0 == r0,
640            (Self::UTinyInt(l0), Value::UTinyInt(r0)) => l0 == r0,
641            (Self::USmallInt(l0), Value::USmallInt(r0)) => l0 == r0,
642            (Self::UInt(l0), Value::UInt(r0)) => l0 == r0,
643            (Self::UBigInt(l0), Value::UBigInt(r0)) => l0 == r0,
644            (Self::Json(l0), Value::Json(r0)) => l0.as_ref() == serde_json::to_vec(r0).unwrap(),
645            (Self::VarBinary(l0), Value::VarBinary(r0)) => l0.as_ref() == r0.as_ref(),
646            (Self::Decimal(l0), Value::Decimal(r0)) => l0 == r0,
647            (Self::Blob(l0), Value::Blob(r0)) => l0 == r0,
648            (Self::MediumBlob(l0), Value::MediumBlob(r0)) => l0 == r0,
649            (Self::Geometry(l0), Value::Geometry(r0)) => l0.as_ref() == r0.as_ref(),
650            _ => false,
651        }
652    }
653}
654
655impl<'b> PartialEq<BorrowedValue<'b>> for Value {
656    fn eq(&self, other: &BorrowedValue<'b>) -> bool {
657        match (other, self) {
658            (BorrowedValue::Null(l0), Value::Null(r0)) => l0 == r0,
659            (BorrowedValue::Bool(l0), Value::Bool(r0)) => l0 == r0,
660            (BorrowedValue::TinyInt(l0), Value::TinyInt(r0)) => l0 == r0,
661            (BorrowedValue::SmallInt(l0), Value::SmallInt(r0)) => l0 == r0,
662            (BorrowedValue::Int(l0), Value::Int(r0)) => l0 == r0,
663            (BorrowedValue::BigInt(l0), Value::BigInt(r0)) => l0 == r0,
664            (BorrowedValue::Float(l0), Value::Float(r0)) => l0 == r0,
665            (BorrowedValue::Double(l0), Value::Double(r0)) => l0 == r0,
666            (BorrowedValue::VarChar(l0), Value::VarChar(r0)) => l0 == r0,
667            (BorrowedValue::Timestamp(l0), Value::Timestamp(r0)) => l0 == r0,
668            (BorrowedValue::NChar(l0), Value::NChar(r0)) => l0 == r0,
669            (BorrowedValue::UTinyInt(l0), Value::UTinyInt(r0)) => l0 == r0,
670            (BorrowedValue::USmallInt(l0), Value::USmallInt(r0)) => l0 == r0,
671            (BorrowedValue::UInt(l0), Value::UInt(r0)) => l0 == r0,
672            (BorrowedValue::UBigInt(l0), Value::UBigInt(r0)) => l0 == r0,
673            (BorrowedValue::Json(l0), Value::Json(r0)) => {
674                l0.as_ref() == serde_json::to_vec(r0).unwrap()
675            }
676            (BorrowedValue::VarBinary(l0), Value::VarBinary(r0)) => l0.as_ref() == r0.as_ref(),
677            (BorrowedValue::Decimal(l0), Value::Decimal(r0)) => l0 == r0,
678            (BorrowedValue::Blob(l0), Value::Blob(r0)) => l0 == r0,
679            (BorrowedValue::MediumBlob(l0), Value::MediumBlob(r0)) => l0 == r0,
680            (BorrowedValue::Geometry(l0), Value::Geometry(r0)) => l0.as_ref() == r0.as_ref(),
681            _ => false,
682        }
683    }
684}
685
686macro_rules! _impl_primitive_from {
687    ($f:ident, $t:ident) => {
688        impl From<$f> for Value {
689            fn from(value: $f) -> Self {
690                Value::$t(value)
691            }
692        }
693        impl From<Option<$f>> for Value {
694            fn from(value: Option<$f>) -> Self {
695                match value {
696                    Some(value) => Value::$t(value),
697                    None => Value::Null(Ty::$t),
698                }
699            }
700        }
701    };
702}
703
704_impl_primitive_from!(bool, Bool);
705_impl_primitive_from!(i8, TinyInt);
706_impl_primitive_from!(i16, SmallInt);
707_impl_primitive_from!(i32, Int);
708_impl_primitive_from!(i64, BigInt);
709_impl_primitive_from!(u8, UTinyInt);
710_impl_primitive_from!(u16, USmallInt);
711_impl_primitive_from!(u32, UInt);
712_impl_primitive_from!(u64, UBigInt);
713_impl_primitive_from!(f32, Float);
714_impl_primitive_from!(f64, Double);
715_impl_primitive_from!(Timestamp, Timestamp);
716mod de;
717
718#[cfg(test)]
719mod tests {
720    use crate::common::Precision;
721
722    use super::*;
723
724    #[test]
725    fn test_borrowed_value_to_native() {
726        let tiny_int_value = BorrowedValue::TinyInt(42);
727        assert_eq!(tiny_int_value.to_i8(), Some(42));
728        assert_eq!(tiny_int_value.to_i16(), Some(42));
729        assert_eq!(tiny_int_value.to_i32(), Some(42));
730        assert_eq!(tiny_int_value.to_i64(), Some(42));
731        assert_eq!(tiny_int_value.to_u8(), Some(42));
732        assert_eq!(tiny_int_value.to_u16(), Some(42));
733        assert_eq!(tiny_int_value.to_u32(), Some(42));
734        assert_eq!(tiny_int_value.to_u64(), Some(42));
735
736        let float_value = BorrowedValue::Float(3.14);
737        println!("float_value: {:?}", float_value.to_f32());
738        println!("float_value: {:?}", float_value.to_f64());
739    }
740
741    #[test]
742    fn test_null_value() {
743        let null_value = Value::Null(Ty::Int);
744        assert_eq!(null_value.ty(), Ty::Int);
745        assert_eq!(null_value.to_sql_value(), "NULL".to_string());
746        assert_eq!(null_value.to_string(), Ok("".to_string()));
747        assert_eq!(null_value.to_json_value(), serde_json::Value::Null);
748        assert_eq!(format!("{}", null_value), "NULL");
749        let null_value_borrowed = null_value.to_borrowed_value();
750        assert_eq!(null_value_borrowed.ty(), Ty::Int);
751        assert_eq!(null_value_borrowed.to_sql_value(), "NULL".to_string());
752        assert_eq!(null_value_borrowed.to_string(), Ok("".to_string()));
753        assert_eq!(null_value_borrowed.to_json_value(), serde_json::Value::Null);
754        assert_eq!(format!("{}", null_value_borrowed), "NULL");
755        println!("{:?}", null_value_borrowed.to_str());
756        assert_eq!(null_value_borrowed.to_bool(), None);
757        assert_eq!(null_value_borrowed.to_value(), null_value);
758        assert_eq!(null_value_borrowed.clone().into_value(), null_value);
759        assert_eq!(null_value_borrowed, null_value);
760        assert_eq!(null_value, null_value_borrowed);
761        assert_eq!(null_value_borrowed, &null_value);
762    }
763
764    #[test]
765    fn test_bool_value() {
766        let bool_value = Value::Bool(true);
767        assert_eq!(bool_value.ty(), Ty::Bool);
768        assert_eq!(bool_value.to_sql_value(), "true".to_string());
769        assert_eq!(bool_value.to_string(), Ok("true".to_string()));
770        assert_eq!(bool_value.to_json_value(), serde_json::Value::Bool(true));
771        assert_eq!(format!("{}", bool_value), "true");
772        let bool_value_borrowed = bool_value.to_borrowed_value();
773        assert_eq!(bool_value_borrowed.ty(), Ty::Bool);
774        assert_eq!(bool_value_borrowed.to_sql_value(), "true".to_string());
775        assert_eq!(bool_value_borrowed.to_string(), Ok("true".to_string()));
776        assert_eq!(
777            bool_value_borrowed.to_json_value(),
778            serde_json::Value::Bool(true)
779        );
780        assert_eq!(format!("{}", bool_value_borrowed), "true");
781        println!("{:?}", bool_value_borrowed.to_str());
782        assert_eq!(bool_value_borrowed.to_bool(), Some(true));
783        assert_eq!(bool_value_borrowed.to_value(), bool_value);
784        assert_eq!(bool_value_borrowed.clone().into_value(), bool_value);
785        assert_eq!(bool_value_borrowed, bool_value);
786        assert_eq!(bool_value, bool_value_borrowed);
787        assert_eq!(bool_value_borrowed, &bool_value);
788    }
789
790    #[test]
791    fn test_tiny_int_value() {
792        let tiny_int_value = Value::TinyInt(42);
793        assert_eq!(tiny_int_value.ty(), Ty::TinyInt);
794        assert_eq!(tiny_int_value.to_sql_value(), "42".to_string());
795        assert_eq!(tiny_int_value.to_string(), Ok("42".to_string()));
796        assert_eq!(
797            tiny_int_value.to_json_value(),
798            serde_json::Value::Number(42.into())
799        );
800        assert_eq!(format!("{}", tiny_int_value), "42");
801        let tiny_int_value_borrowed = tiny_int_value.to_borrowed_value();
802        assert_eq!(tiny_int_value_borrowed.ty(), Ty::TinyInt);
803        assert_eq!(tiny_int_value_borrowed.to_sql_value(), "42".to_string());
804        assert_eq!(tiny_int_value_borrowed.to_string(), Ok("42".to_string()));
805        assert_eq!(
806            tiny_int_value_borrowed.to_json_value(),
807            serde_json::Value::Number(42.into())
808        );
809        assert_eq!(format!("{}", tiny_int_value_borrowed), "42");
810        println!("{:?}", tiny_int_value_borrowed.to_str());
811        assert_eq!(tiny_int_value_borrowed.to_bool(), Some(true));
812        assert_eq!(tiny_int_value_borrowed.to_value(), tiny_int_value);
813        assert_eq!(tiny_int_value_borrowed.clone().into_value(), tiny_int_value);
814        assert_eq!(tiny_int_value_borrowed, tiny_int_value);
815        assert_eq!(tiny_int_value, tiny_int_value_borrowed);
816        assert_eq!(tiny_int_value_borrowed, &tiny_int_value);
817    }
818
819    #[test]
820    fn test_small_int_value() {
821        let small_int_value = Value::SmallInt(1000);
822        assert_eq!(small_int_value.ty(), Ty::SmallInt);
823        assert_eq!(small_int_value.to_sql_value(), "1000".to_string());
824        assert_eq!(small_int_value.to_string(), Ok("1000".to_string()));
825        assert_eq!(
826            small_int_value.to_json_value(),
827            serde_json::Value::Number(1000.into())
828        );
829        assert_eq!(format!("{}", small_int_value), "1000");
830        let small_int_value_borrowed = small_int_value.to_borrowed_value();
831        assert_eq!(small_int_value_borrowed.ty(), Ty::SmallInt);
832        assert_eq!(small_int_value_borrowed.to_sql_value(), "1000".to_string());
833        assert_eq!(small_int_value_borrowed.to_string(), Ok("1000".to_string()));
834        assert_eq!(
835            small_int_value_borrowed.to_json_value(),
836            serde_json::Value::Number(1000.into())
837        );
838        assert_eq!(format!("{}", small_int_value_borrowed), "1000");
839        println!("{:?}", small_int_value_borrowed.to_str());
840        assert_eq!(small_int_value_borrowed.to_bool(), Some(true));
841        assert_eq!(small_int_value_borrowed.to_value(), small_int_value);
842        assert_eq!(
843            small_int_value_borrowed.clone().into_value(),
844            small_int_value
845        );
846        assert_eq!(small_int_value_borrowed, small_int_value);
847        assert_eq!(small_int_value, small_int_value_borrowed);
848        assert_eq!(small_int_value_borrowed, &small_int_value);
849    }
850
851    #[test]
852    fn test_int_value() {
853        let int_value = Value::Int(-500);
854        assert_eq!(int_value.ty(), Ty::Int);
855        assert_eq!(int_value.to_sql_value(), "-500".to_string());
856        assert_eq!(int_value.to_string(), Ok("-500".to_string()));
857        assert_eq!(
858            int_value.to_json_value(),
859            serde_json::Value::Number((-500).into())
860        );
861        assert_eq!(format!("{}", int_value), "-500");
862        let int_value_borrowed = int_value.to_borrowed_value();
863        assert_eq!(int_value_borrowed.ty(), Ty::Int);
864        assert_eq!(int_value_borrowed.to_sql_value(), "-500".to_string());
865        assert_eq!(int_value_borrowed.to_string(), Ok("-500".to_string()));
866        assert_eq!(
867            int_value_borrowed.to_json_value(),
868            serde_json::Value::Number((-500).into())
869        );
870        assert_eq!(format!("{}", int_value_borrowed), "-500");
871        println!("{:?}", int_value_borrowed.to_str());
872        assert_eq!(int_value_borrowed.to_bool(), Some(false));
873        assert_eq!(int_value_borrowed.to_value(), int_value);
874        assert_eq!(int_value_borrowed.clone().into_value(), int_value);
875        assert_eq!(int_value_borrowed, int_value);
876        assert_eq!(int_value, int_value_borrowed);
877        assert_eq!(int_value_borrowed, &int_value);
878    }
879
880    #[test]
881    fn test_big_int_value() {
882        let big_int_value = Value::BigInt(1234567890);
883        assert_eq!(big_int_value.ty(), Ty::BigInt);
884        assert_eq!(big_int_value.to_sql_value(), "1234567890".to_string());
885        assert_eq!(big_int_value.to_string(), Ok("1234567890".to_string()));
886        assert_eq!(
887            big_int_value.to_json_value(),
888            serde_json::Value::Number(1234567890.into())
889        );
890        assert_eq!(format!("{}", big_int_value), "1234567890");
891        let big_int_value_borrowed = big_int_value.to_borrowed_value();
892        assert_eq!(big_int_value_borrowed.ty(), Ty::BigInt);
893        assert_eq!(
894            big_int_value_borrowed.to_sql_value(),
895            "1234567890".to_string()
896        );
897        assert_eq!(
898            big_int_value_borrowed.to_string(),
899            Ok("1234567890".to_string())
900        );
901        assert_eq!(
902            big_int_value_borrowed.to_json_value(),
903            serde_json::Value::Number(1234567890.into())
904        );
905        assert_eq!(format!("{}", big_int_value_borrowed), "1234567890");
906        println!("{:?}", big_int_value_borrowed.to_str());
907        assert_eq!(big_int_value_borrowed.to_bool(), Some(true));
908        assert_eq!(big_int_value_borrowed.to_value(), big_int_value);
909        assert_eq!(big_int_value_borrowed.clone().into_value(), big_int_value);
910        assert_eq!(big_int_value_borrowed, big_int_value);
911        assert_eq!(big_int_value, big_int_value_borrowed);
912        assert_eq!(big_int_value_borrowed, &big_int_value);
913    }
914
915    #[test]
916    fn test_utiny_int_value() {
917        let utiny_int_value = Value::UTinyInt(42);
918        assert_eq!(utiny_int_value.ty(), Ty::UTinyInt);
919        assert_eq!(utiny_int_value.to_sql_value(), "42".to_string());
920        assert_eq!(utiny_int_value.to_string(), Ok("42".to_string()));
921        assert_eq!(
922            utiny_int_value.to_json_value(),
923            serde_json::Value::Number(42.into())
924        );
925        assert_eq!(format!("{}", utiny_int_value), "42");
926        let utiny_int_value_borrowed = utiny_int_value.to_borrowed_value();
927        assert_eq!(utiny_int_value_borrowed.ty(), Ty::UTinyInt);
928        assert_eq!(utiny_int_value_borrowed.to_sql_value(), "42".to_string());
929        assert_eq!(utiny_int_value_borrowed.to_string(), Ok("42".to_string()));
930        assert_eq!(
931            utiny_int_value_borrowed.to_json_value(),
932            serde_json::Value::Number(42.into())
933        );
934        assert_eq!(format!("{}", utiny_int_value_borrowed), "42");
935        println!("{:?}", utiny_int_value_borrowed.to_str());
936        assert_eq!(utiny_int_value_borrowed.to_bool(), Some(true));
937        assert_eq!(utiny_int_value_borrowed.to_value(), utiny_int_value);
938        assert_eq!(
939            utiny_int_value_borrowed.clone().into_value(),
940            utiny_int_value
941        );
942        assert_eq!(utiny_int_value_borrowed, utiny_int_value);
943        assert_eq!(utiny_int_value, utiny_int_value_borrowed);
944        assert_eq!(utiny_int_value_borrowed, &utiny_int_value);
945    }
946
947    #[test]
948    fn test_usmall_int_value() {
949        let usmall_int_value = Value::USmallInt(1000);
950        assert_eq!(usmall_int_value.ty(), Ty::USmallInt);
951        assert_eq!(usmall_int_value.to_sql_value(), "1000".to_string());
952        assert_eq!(usmall_int_value.to_string(), Ok("1000".to_string()));
953        assert_eq!(
954            usmall_int_value.to_json_value(),
955            serde_json::Value::Number(1000.into())
956        );
957        assert_eq!(format!("{}", usmall_int_value), "1000");
958        let usmall_int_value_borrowed = usmall_int_value.to_borrowed_value();
959        assert_eq!(usmall_int_value_borrowed.ty(), Ty::USmallInt);
960        assert_eq!(usmall_int_value_borrowed.to_sql_value(), "1000".to_string());
961        assert_eq!(
962            usmall_int_value_borrowed.to_string(),
963            Ok("1000".to_string())
964        );
965        assert_eq!(
966            usmall_int_value_borrowed.to_json_value(),
967            serde_json::Value::Number(1000.into())
968        );
969        assert_eq!(format!("{}", usmall_int_value_borrowed), "1000");
970        println!("{:?}", usmall_int_value_borrowed.to_str());
971        assert_eq!(usmall_int_value_borrowed.to_bool(), Some(true));
972        assert_eq!(usmall_int_value_borrowed.to_value(), usmall_int_value);
973        assert_eq!(
974            usmall_int_value_borrowed.clone().into_value(),
975            usmall_int_value
976        );
977        assert_eq!(usmall_int_value_borrowed, usmall_int_value);
978        assert_eq!(usmall_int_value, usmall_int_value_borrowed);
979        assert_eq!(usmall_int_value_borrowed, &usmall_int_value);
980    }
981
982    #[test]
983    fn test_uint_value() {
984        let uint_value = Value::UInt(5000);
985        assert_eq!(uint_value.ty(), Ty::UInt);
986        assert_eq!(uint_value.to_sql_value(), "5000".to_string());
987        assert_eq!(uint_value.to_string(), Ok("5000".to_string()));
988        assert_eq!(
989            uint_value.to_json_value(),
990            serde_json::Value::Number(5000.into())
991        );
992        assert_eq!(format!("{}", uint_value), "5000");
993        let uint_value_borrowed = uint_value.to_borrowed_value();
994        assert_eq!(uint_value_borrowed.ty(), Ty::UInt);
995        assert_eq!(uint_value_borrowed.to_sql_value(), "5000".to_string());
996        assert_eq!(uint_value_borrowed.to_string(), Ok("5000".to_string()));
997        assert_eq!(
998            uint_value_borrowed.to_json_value(),
999            serde_json::Value::Number(5000.into())
1000        );
1001        assert_eq!(format!("{}", uint_value_borrowed), "5000");
1002        println!("{:?}", uint_value_borrowed.to_str());
1003        assert_eq!(uint_value_borrowed.to_bool(), Some(true));
1004        assert_eq!(uint_value_borrowed.to_value(), uint_value);
1005        assert_eq!(uint_value_borrowed.clone().into_value(), uint_value);
1006        assert_eq!(uint_value_borrowed, uint_value);
1007        assert_eq!(uint_value, uint_value_borrowed);
1008        assert_eq!(uint_value_borrowed, &uint_value);
1009    }
1010
1011    #[test]
1012    fn test_ubig_int_value() {
1013        let ubig_int_value = Value::UBigInt(1234567890);
1014        assert_eq!(ubig_int_value.ty(), Ty::UBigInt);
1015        assert_eq!(ubig_int_value.to_sql_value(), "1234567890".to_string());
1016        assert_eq!(ubig_int_value.to_string(), Ok("1234567890".to_string()));
1017        assert_eq!(
1018            ubig_int_value.to_json_value(),
1019            serde_json::Value::Number(1234567890.into())
1020        );
1021        assert_eq!(format!("{}", ubig_int_value), "1234567890");
1022        let ubig_int_value_borrowed = ubig_int_value.to_borrowed_value();
1023        assert_eq!(ubig_int_value_borrowed.ty(), Ty::UBigInt);
1024        assert_eq!(
1025            ubig_int_value_borrowed.to_sql_value(),
1026            "1234567890".to_string()
1027        );
1028        assert_eq!(
1029            ubig_int_value_borrowed.to_string(),
1030            Ok("1234567890".to_string())
1031        );
1032        assert_eq!(
1033            ubig_int_value_borrowed.to_json_value(),
1034            serde_json::Value::Number(1234567890.into())
1035        );
1036        assert_eq!(format!("{}", ubig_int_value_borrowed), "1234567890");
1037        println!("{:?}", ubig_int_value_borrowed.to_str());
1038        assert_eq!(ubig_int_value_borrowed.to_bool(), Some(true));
1039        assert_eq!(ubig_int_value_borrowed.to_value(), ubig_int_value);
1040        assert_eq!(ubig_int_value_borrowed.clone().into_value(), ubig_int_value);
1041        assert_eq!(ubig_int_value_borrowed, ubig_int_value);
1042        assert_eq!(ubig_int_value, ubig_int_value_borrowed);
1043        assert_eq!(ubig_int_value_borrowed, &ubig_int_value);
1044    }
1045
1046    #[test]
1047    fn test_float_value() {
1048        let float_value = Value::Float(3.14);
1049        assert_eq!(float_value.ty(), Ty::Float);
1050        assert_eq!(float_value.to_sql_value(), "3.14".to_string());
1051        assert_eq!(float_value.to_string(), Ok("3.14".to_string()));
1052        println!("{:?}", float_value.to_json_value());
1053        assert_eq!(format!("{}", float_value), "3.14");
1054        let float_value_borrowed = float_value.to_borrowed_value();
1055        assert_eq!(float_value_borrowed.ty(), Ty::Float);
1056        assert_eq!(float_value_borrowed.to_sql_value(), "3.14".to_string());
1057        assert_eq!(float_value_borrowed.to_string(), Ok("3.14".to_string()));
1058        println!("{:?}", float_value_borrowed.to_json_value());
1059        assert_eq!(format!("{}", float_value_borrowed), "3.14");
1060        println!("{:?}", float_value_borrowed.to_str());
1061        assert_eq!(float_value_borrowed.to_bool(), Some(true));
1062        assert_eq!(float_value_borrowed.to_value(), float_value);
1063        assert_eq!(float_value_borrowed.clone().into_value(), float_value);
1064        assert_eq!(float_value_borrowed, float_value);
1065        assert_eq!(float_value, float_value_borrowed);
1066        assert_eq!(float_value_borrowed, &float_value);
1067    }
1068
1069    #[test]
1070    fn test_double_value() {
1071        let double_value = Value::Double(2.71828);
1072        assert_eq!(double_value.ty(), Ty::Double);
1073        assert_eq!(double_value.to_sql_value(), "2.71828".to_string());
1074        assert_eq!(double_value.to_string(), Ok("2.71828".to_string()));
1075        println!("{:?}", double_value.to_json_value());
1076        assert_eq!(format!("{}", double_value), "2.71828");
1077        let double_value_borrowed = double_value.to_borrowed_value();
1078        assert_eq!(double_value_borrowed.ty(), Ty::Double);
1079        assert_eq!(double_value_borrowed.to_sql_value(), "2.71828".to_string());
1080        assert_eq!(double_value_borrowed.to_string(), Ok("2.71828".to_string()));
1081        println!("{:?}", double_value_borrowed.to_json_value());
1082        assert_eq!(format!("{}", double_value_borrowed), "2.71828");
1083        println!("{:?}", double_value_borrowed.to_str());
1084        assert_eq!(double_value_borrowed.to_bool(), Some(true));
1085        assert_eq!(double_value_borrowed.to_value(), double_value);
1086        assert_eq!(double_value_borrowed.clone().into_value(), double_value);
1087        assert_eq!(double_value_borrowed, double_value);
1088        assert_eq!(double_value, double_value_borrowed);
1089        assert_eq!(double_value_borrowed, &double_value);
1090    }
1091
1092    #[test]
1093    fn test_var_char_value() {
1094        let varchar_value = Value::VarChar("hello".to_string());
1095        assert_eq!(varchar_value.ty(), Ty::VarChar);
1096        assert_eq!(varchar_value.to_sql_value(), "\"hello\"".to_string());
1097        assert_eq!(varchar_value.to_string(), Ok("hello".to_string()));
1098        assert_eq!(
1099            varchar_value.to_json_value(),
1100            serde_json::Value::String("hello".to_string())
1101        );
1102        assert_eq!(format!("{}", varchar_value), "hello");
1103        let varchar_value_borrowed = varchar_value.to_borrowed_value();
1104        assert_eq!(varchar_value_borrowed.ty(), Ty::VarChar);
1105        assert_eq!(
1106            varchar_value_borrowed.to_sql_value(),
1107            "\"hello\"".to_string()
1108        );
1109        assert_eq!(varchar_value_borrowed.to_string(), Ok("hello".to_string()));
1110        assert_eq!(
1111            varchar_value_borrowed.to_json_value(),
1112            serde_json::Value::String("hello".to_string())
1113        );
1114        assert_eq!(format!("{}", varchar_value_borrowed), "hello");
1115        println!("{:?}", varchar_value_borrowed.to_str());
1116        assert_eq!(varchar_value_borrowed.to_bool(), Some(true));
1117        assert_eq!(varchar_value_borrowed.to_value(), varchar_value);
1118        assert_eq!(varchar_value_borrowed.clone().into_value(), varchar_value);
1119        assert_eq!(varchar_value_borrowed, varchar_value);
1120        assert_eq!(varchar_value, varchar_value_borrowed);
1121        assert_eq!(varchar_value_borrowed, &varchar_value);
1122    }
1123
1124    #[test]
1125    fn test_timestamp_value() {
1126        let timestamp_value = Value::Timestamp(Timestamp::new(1, Precision::Millisecond));
1127        assert_eq!(timestamp_value.ty(), Ty::Timestamp);
1128        assert_eq!(timestamp_value.to_sql_value(), "1".to_string());
1129        println!("{:?}", timestamp_value.to_string());
1130        assert_eq!(
1131            timestamp_value.to_json_value(),
1132            serde_json::Value::Number(1.into())
1133        );
1134        println!("{}", format!("{}", timestamp_value));
1135        let timestamp_value_borrowed = timestamp_value.to_borrowed_value();
1136        assert_eq!(timestamp_value_borrowed.ty(), Ty::Timestamp);
1137        assert_eq!(timestamp_value_borrowed.to_sql_value(), "1".to_string());
1138        println!("{:?}", timestamp_value_borrowed.to_string());
1139        assert_eq!(
1140            timestamp_value_borrowed.to_json_value(),
1141            serde_json::Value::Number(1.into())
1142        );
1143        println!("{}", format!("{}", timestamp_value_borrowed));
1144        println!("{:?}", timestamp_value_borrowed.to_str());
1145        assert_eq!(timestamp_value_borrowed.to_bool(), Some(true));
1146        assert_eq!(timestamp_value_borrowed.to_value(), timestamp_value);
1147        assert_eq!(
1148            timestamp_value_borrowed.clone().into_value(),
1149            timestamp_value
1150        );
1151        assert_eq!(timestamp_value_borrowed, timestamp_value);
1152        assert_eq!(timestamp_value, timestamp_value_borrowed);
1153        assert_eq!(timestamp_value_borrowed, &timestamp_value);
1154    }
1155
1156    #[test]
1157    fn test_nchar_value() {
1158        let nchar_value = Value::NChar("hello".to_string());
1159        assert_eq!(nchar_value.ty(), Ty::NChar);
1160        assert_eq!(nchar_value.to_sql_value(), "\"hello\"".to_string());
1161        assert_eq!(nchar_value.to_string(), Ok("hello".to_string()));
1162        assert_eq!(
1163            nchar_value.to_json_value(),
1164            serde_json::Value::String("hello".to_string())
1165        );
1166        assert_eq!(format!("{}", nchar_value), "hello");
1167        let nchar_value_borrowed = nchar_value.to_borrowed_value();
1168        assert_eq!(nchar_value_borrowed.ty(), Ty::NChar);
1169        assert_eq!(nchar_value_borrowed.to_sql_value(), "\"hello\"".to_string());
1170        assert_eq!(nchar_value_borrowed.to_string(), Ok("hello".to_string()));
1171        assert_eq!(
1172            nchar_value_borrowed.to_json_value(),
1173            serde_json::Value::String("hello".to_string())
1174        );
1175        assert_eq!(format!("{}", nchar_value_borrowed), "hello");
1176        println!("{:?}", nchar_value_borrowed.to_str());
1177        assert_eq!(nchar_value_borrowed.to_bool(), Some(true));
1178        assert_eq!(nchar_value_borrowed.to_value(), nchar_value);
1179        assert_eq!(nchar_value_borrowed.clone().into_value(), nchar_value);
1180        assert_eq!(nchar_value_borrowed, nchar_value);
1181        assert_eq!(nchar_value, nchar_value_borrowed);
1182        assert_eq!(nchar_value_borrowed, &nchar_value);
1183    }
1184
1185    #[test]
1186    fn test_json_value() {
1187        let json_value = Value::Json(serde_json::json!({"hello": "world"}));
1188        assert_eq!(json_value.ty(), Ty::Json);
1189        assert_eq!(
1190            json_value.to_sql_value(),
1191            "\"{\"hello\":\"world\"}\"".to_string()
1192        );
1193        assert_eq!(
1194            json_value.to_string(),
1195            Ok("{\"hello\":\"world\"}".to_string())
1196        );
1197        assert_eq!(
1198            json_value.to_json_value(),
1199            serde_json::json!({"hello": "world"})
1200        );
1201        assert_eq!(format!("{}", json_value), "{\"hello\":\"world\"}");
1202        let json_value_borrowed = json_value.to_borrowed_value();
1203        assert_eq!(json_value_borrowed.ty(), Ty::Json);
1204        assert_eq!(
1205            json_value_borrowed.to_sql_value(),
1206            "\"{\"hello\":\"world\"}\"".to_string()
1207        );
1208        assert_eq!(
1209            json_value_borrowed.to_string(),
1210            Ok("{\"hello\":\"world\"}".to_string())
1211        );
1212        assert_eq!(
1213            json_value_borrowed.to_json_value(),
1214            serde_json::json!({"hello": "world"})
1215        );
1216        assert_eq!(
1217            format!("{}", json_value_borrowed),
1218            "{\\\"hello\\\":\\\"world\\\"}"
1219        );
1220        println!("{:?}", json_value_borrowed.to_str());
1221        assert_eq!(json_value_borrowed.to_bool(), Some(true));
1222        assert_eq!(json_value_borrowed.to_value(), json_value);
1223        assert_eq!(json_value_borrowed.clone().into_value(), json_value);
1224        assert_eq!(json_value_borrowed, json_value);
1225        assert_eq!(json_value, json_value_borrowed);
1226        assert_eq!(json_value_borrowed, &json_value);
1227    }
1228
1229    #[test]
1230    fn test_ty() {
1231        let null_value = BorrowedValue::Null(Ty::Int);
1232        assert_eq!(null_value.ty(), Ty::Int);
1233
1234        let bool_value = BorrowedValue::Bool(true);
1235        assert_eq!(bool_value.ty(), Ty::Bool);
1236
1237        let tiny_int_value = BorrowedValue::TinyInt(42);
1238        assert_eq!(tiny_int_value.ty(), Ty::TinyInt);
1239
1240        let small_int_value = BorrowedValue::SmallInt(1000);
1241        assert_eq!(small_int_value.ty(), Ty::SmallInt);
1242
1243        let int_value = BorrowedValue::Int(-500);
1244        assert_eq!(int_value.ty(), Ty::Int);
1245
1246        let big_int_value = BorrowedValue::BigInt(1234567890);
1247        assert_eq!(big_int_value.ty(), Ty::BigInt);
1248
1249        let utiny_int_value = BorrowedValue::UTinyInt(42);
1250        assert_eq!(utiny_int_value.ty(), Ty::UTinyInt);
1251
1252        let usmall_int_value = BorrowedValue::USmallInt(1000);
1253        assert_eq!(usmall_int_value.ty(), Ty::USmallInt);
1254
1255        let uint_value = BorrowedValue::UInt(5000);
1256        assert_eq!(uint_value.ty(), Ty::UInt);
1257
1258        let ubig_int_value = BorrowedValue::UBigInt(1234567890);
1259        assert_eq!(ubig_int_value.ty(), Ty::UBigInt);
1260
1261        let float_value = BorrowedValue::Float(3.14);
1262        assert_eq!(float_value.ty(), Ty::Float);
1263
1264        let double_value = BorrowedValue::Double(2.71828);
1265        assert_eq!(double_value.ty(), Ty::Double);
1266
1267        let varchar_value = BorrowedValue::VarChar("hello");
1268        assert_eq!(varchar_value.ty(), Ty::VarChar);
1269
1270        let timestamp_value = BorrowedValue::Timestamp(Timestamp::new(1, Precision::Millisecond));
1271        assert_eq!(timestamp_value.ty(), Ty::Timestamp);
1272
1273        let blob_value = BorrowedValue::Blob(&[1, 2, 3]);
1274        assert_eq!(blob_value.ty(), Ty::Blob);
1275
1276        let medium_blob_value = BorrowedValue::MediumBlob(&[1, 2, 3]);
1277        assert_eq!(medium_blob_value.ty(), Ty::MediumBlob);
1278    }
1279
1280    #[test]
1281    fn test_to_sql_value() {
1282        let null_value = BorrowedValue::Null(Ty::Int);
1283        assert_eq!(null_value.to_sql_value(), "NULL".to_string());
1284
1285        let bool_value = BorrowedValue::Bool(true);
1286        assert_eq!(bool_value.to_sql_value(), "true".to_string());
1287
1288        let tiny_int_value = BorrowedValue::TinyInt(42);
1289        assert_eq!(tiny_int_value.to_sql_value(), "42".to_string());
1290
1291        let small_int_value = BorrowedValue::SmallInt(1000);
1292        assert_eq!(small_int_value.to_sql_value(), "1000".to_string());
1293
1294        let int_value = BorrowedValue::Int(-500);
1295        assert_eq!(int_value.to_sql_value(), "-500".to_string());
1296
1297        let big_int_value = BorrowedValue::BigInt(1234567890);
1298        assert_eq!(big_int_value.to_sql_value(), "1234567890".to_string());
1299
1300        let utiny_int_value = BorrowedValue::UTinyInt(42);
1301        assert_eq!(utiny_int_value.to_sql_value(), "42".to_string());
1302
1303        let usmall_int_value = BorrowedValue::USmallInt(1000);
1304        assert_eq!(usmall_int_value.to_sql_value(), "1000".to_string());
1305
1306        let uint_value = BorrowedValue::UInt(5000);
1307        assert_eq!(uint_value.to_sql_value(), "5000".to_string());
1308
1309        let ubig_int_value = BorrowedValue::UBigInt(1234567890);
1310        assert_eq!(ubig_int_value.to_sql_value(), "1234567890".to_string());
1311
1312        let float_value = BorrowedValue::Float(3.14);
1313        assert_eq!(float_value.to_sql_value(), "3.14".to_string());
1314
1315        let double_value = BorrowedValue::Double(2.71828);
1316        assert_eq!(double_value.to_sql_value(), "2.71828".to_string());
1317
1318        let varchar_value = BorrowedValue::VarChar("hello");
1319        assert_eq!(varchar_value.to_sql_value(), "\"hello\"".to_string());
1320
1321        let timestamp_value = BorrowedValue::Timestamp(Timestamp::new(1, Precision::Millisecond));
1322        assert_eq!(timestamp_value.to_sql_value(), "1".to_string());
1323
1324        let nchar_value = Value::NChar("hello".to_string());
1325        let b_nchar_value = nchar_value.to_borrowed_value();
1326        assert_eq!(b_nchar_value.to_sql_value(), "\"hello\"".to_string());
1327    }
1328
1329    #[test]
1330    fn test_to_json_value() {
1331        let null_value = BorrowedValue::Null(Ty::Int);
1332        assert_eq!(null_value.to_json_value(), serde_json::Value::Null);
1333
1334        let bool_value = BorrowedValue::Bool(true);
1335        assert_eq!(bool_value.to_json_value(), serde_json::Value::Bool(true));
1336
1337        let tiny_int_value = BorrowedValue::TinyInt(42);
1338        assert_eq!(
1339            tiny_int_value.to_json_value(),
1340            serde_json::Value::Number(42.into())
1341        );
1342
1343        let small_int_value = BorrowedValue::SmallInt(1000);
1344        assert_eq!(
1345            small_int_value.to_json_value(),
1346            serde_json::Value::Number(1000.into())
1347        );
1348
1349        let int_value = BorrowedValue::Int(-500);
1350        assert_eq!(
1351            int_value.to_json_value(),
1352            serde_json::Value::Number((-500).into())
1353        );
1354
1355        let big_int_value = BorrowedValue::BigInt(1234567890);
1356        assert_eq!(
1357            big_int_value.to_json_value(),
1358            serde_json::Value::Number(1234567890.into())
1359        );
1360
1361        let utiny_int_value = BorrowedValue::UTinyInt(42);
1362        assert_eq!(
1363            utiny_int_value.to_json_value(),
1364            serde_json::Value::Number(42.into())
1365        );
1366
1367        let usmall_int_value = BorrowedValue::USmallInt(1000);
1368        assert_eq!(
1369            usmall_int_value.to_json_value(),
1370            serde_json::Value::Number(1000.into())
1371        );
1372
1373        let uint_value = BorrowedValue::UInt(5000);
1374        assert_eq!(
1375            uint_value.to_json_value(),
1376            serde_json::Value::Number(5000.into())
1377        );
1378
1379        let ubig_int_value = BorrowedValue::UBigInt(1234567890);
1380        assert_eq!(
1381            ubig_int_value.to_json_value(),
1382            serde_json::Value::Number(1234567890.into())
1383        );
1384
1385        let float_value = BorrowedValue::Float(3.14);
1386        assert_eq!(
1387            float_value.to_json_value(),
1388            serde_json::json!(3.140000104904175)
1389        );
1390
1391        let double_value = BorrowedValue::Double(2.71828);
1392        assert_eq!(double_value.to_json_value(), serde_json::json!(2.71828));
1393
1394        let varchar_value = BorrowedValue::VarChar("hello");
1395        assert_eq!(
1396            varchar_value.to_json_value(),
1397            serde_json::Value::String("hello".to_string())
1398        );
1399
1400        let timestamp_value = BorrowedValue::Timestamp(Timestamp::new(1, Precision::Millisecond));
1401        assert_eq!(
1402            timestamp_value.to_json_value(),
1403            serde_json::Value::Number(1.into())
1404        );
1405
1406        let json_value = Value::Json(serde_json::json!({"hello": "world"}));
1407        let b_json_value = json_value.to_borrowed_value();
1408        assert_eq!(
1409            b_json_value.to_json_value(),
1410            serde_json::json!({"hello": "world"})
1411        );
1412
1413        let nchar_value = Value::NChar("hello".to_string());
1414        let b_nchar_value = nchar_value.to_borrowed_value();
1415        assert_eq!(
1416            b_nchar_value.to_json_value(),
1417            serde_json::Value::String("hello".to_string())
1418        );
1419    }
1420}