taos_query/common/
itypes.rs

1use derive_more::{Deref, DerefMut, Display, From};
2
3use rust_decimal::prelude::*;
4use serde::{Deserialize, Serialize};
5use serde_json::Value as Json;
6
7use super::{Ty, Value};
8
9pub type INull = ();
10pub type IBool = bool;
11pub type ITinyInt = i8;
12pub type ISmallInt = i16;
13pub type IInt = i32;
14pub type IBigInt = i64;
15pub type IUTinyInt = u8;
16pub type IUSmallInt = u16;
17pub type IUInt = u32;
18pub type IUBigInt = u64;
19pub type IFloat = f32;
20pub type IDouble = f64;
21pub type IJson = Json;
22pub type IDecimal = Decimal;
23
24#[derive(Debug, Clone, Copy, Deref, DerefMut, Deserialize, Serialize, Display, From)]
25pub struct ITimestamp(pub i64);
26
27#[derive(Debug, Deref, DerefMut, Clone, Deserialize, Serialize)]
28pub struct IVarChar(String);
29
30impl AsRef<str> for IVarChar {
31    fn as_ref(&self) -> &str {
32        self.0.as_str()
33    }
34}
35impl AsRef<[u8]> for IVarChar {
36    fn as_ref(&self) -> &[u8] {
37        self.0.as_ref()
38    }
39}
40
41/// Alias of [IVarChar].
42pub type IBinary = IVarChar;
43
44#[derive(Debug, Deref, DerefMut, Clone, From, Deserialize, Serialize)]
45pub struct INChar(String);
46
47impl AsRef<str> for INChar {
48    fn as_ref(&self) -> &str {
49        self.0.as_str()
50    }
51}
52impl AsRef<[u8]> for INChar {
53    fn as_ref(&self) -> &[u8] {
54        self.0.as_ref()
55    }
56}
57
58#[derive(Debug, Deref, DerefMut, Clone, From, Deserialize, Serialize)]
59pub struct IVarBinary(Vec<u8>);
60#[allow(dead_code)]
61pub struct IGeometry(Vec<u8>);
62
63#[derive(Debug, Deref, DerefMut, Clone, From, Deserialize, Serialize)]
64pub struct IMediumBlob(Vec<u8>);
65
66#[derive(Debug, Deref, DerefMut, Clone, From, Deserialize, Serialize)]
67pub struct IBlob(Vec<u8>);
68
69impl From<String> for IVarChar {
70    fn from(v: String) -> Self {
71        Self(v)
72    }
73}
74impl From<&str> for IVarChar {
75    fn from(v: &str) -> Self {
76        Self(v.to_string())
77    }
78}
79
80impl From<IVarChar> for String {
81    fn from(v: IVarChar) -> Self {
82        v.0
83    }
84}
85
86impl From<INChar> for String {
87    fn from(v: INChar) -> Self {
88        v.0
89    }
90}
91
92// impl From<IJson> for String {
93//     fn from(v: IJson) -> Self {
94//         v.0.to_string()
95//     }
96// }
97
98impl IVarChar {
99    pub const fn new() -> Self {
100        Self(String::new())
101    }
102    pub fn with_capacity(cap: usize) -> Self {
103        Self(String::with_capacity(cap))
104    }
105}
106pub trait IsValue: Sized + Clone {
107    const TY: Ty;
108
109    fn is_null(&self) -> bool {
110        false
111    }
112
113    fn is_primitive(&self) -> bool {
114        std::mem::size_of::<Self>() == Self::TY.fixed_length()
115    }
116
117    fn fixed_length(&self) -> usize {
118        std::mem::size_of::<Self>()
119    }
120
121    fn as_timestamp(&self) -> i64 {
122        debug_assert!(Self::TY == Ty::Timestamp);
123        unimplemented!()
124    }
125
126    fn as_var_char(&self) -> &str {
127        debug_assert!(Self::TY == Ty::VarChar);
128        unimplemented!()
129    }
130
131    fn as_nchar(&self) -> &str {
132        debug_assert!(Self::TY == Ty::NChar);
133        unimplemented!()
134    }
135
136    fn as_medium_blob(&self) -> &[u8] {
137        debug_assert!(Self::TY == Ty::MediumBlob);
138        unimplemented!()
139    }
140
141    fn as_blob(&self) -> &[u8] {
142        debug_assert!(Self::TY == Ty::Blob);
143        unimplemented!()
144    }
145}
146
147impl<T> IsValue for Option<T>
148where
149    T: IsValue,
150{
151    const TY: Ty = T::TY;
152
153    fn is_null(&self) -> bool {
154        self.is_none()
155    }
156
157    fn is_primitive(&self) -> bool {
158        self.as_ref().unwrap().is_primitive()
159    }
160
161    fn as_timestamp(&self) -> i64 {
162        self.as_ref().unwrap().as_timestamp()
163    }
164
165    fn as_var_char(&self) -> &str {
166        self.as_ref().unwrap().as_var_char()
167    }
168    fn as_nchar(&self) -> &str {
169        self.as_ref().unwrap().as_nchar()
170    }
171    fn as_medium_blob(&self) -> &[u8] {
172        self.as_ref().unwrap().as_medium_blob()
173    }
174    fn as_blob(&self) -> &[u8] {
175        self.as_ref().unwrap().as_blob()
176    }
177}
178
179pub trait IValue: Sized {
180    const TY: Ty;
181
182    type Inner: Sized;
183
184    fn is_null(&self) -> bool {
185        false
186    }
187
188    fn into_value(self) -> Value;
189
190    fn into_inner(self) -> Self::Inner;
191}
192
193impl IValue for INull {
194    const TY: Ty = Ty::Null;
195
196    fn is_null(&self) -> bool {
197        true
198    }
199    fn into_value(self) -> Value {
200        Value::Null(Ty::Null)
201    }
202
203    type Inner = ();
204
205    fn into_inner(self) -> Self::Inner {}
206}
207
208/// Primitive type to TDengine data type.
209macro_rules! impl_prim {
210    ($($ty:ident = $inner:ty)*) => {
211        $(paste::paste! {
212            impl IValue for [<I $ty>] {
213                const TY: Ty = Ty::$ty;
214                type Inner = $inner;
215
216                #[inline]
217                fn is_null(&self) -> bool {
218                    false
219                }
220
221                #[inline]
222                fn into_value(self) -> Value {
223                    Value::$ty(self)
224                }
225
226                #[inline]
227                fn into_inner(self) -> Self::Inner {
228                    self
229                }
230            }
231        })*
232    };
233}
234
235impl_prim!(
236    Bool = bool
237    TinyInt = i8
238    SmallInt =  i16
239    Int = i32
240    BigInt = i64
241    UTinyInt = u8
242    USmallInt = u16
243    UInt = u32
244    UBigInt = u64
245    Float = f32
246    Double = f64
247    Decimal = Decimal
248    Json = Json
249);
250
251pub trait IsPrimitive: Copy {
252    const TY: Ty;
253    fn is_primitive(&self) -> bool {
254        std::mem::size_of::<Self>() == Self::TY.fixed_length()
255    }
256}
257
258macro_rules! impl_is_primitive {
259    ($($ty:ident) *) => {
260        $(paste::paste! {
261            impl IsPrimitive for [<I $ty>] {
262                const TY: Ty = Ty::$ty;
263            }
264            impl IsValue for [<I $ty>] {
265                const TY: Ty = Ty::$ty;
266            }
267        })*
268    };
269}
270
271impl_is_primitive!(
272    Bool TinyInt SmallInt Int BigInt
273    UTinyInt USmallInt UInt UBigInt
274    Float Double Decimal
275);
276
277impl IsValue for ITimestamp {
278    const TY: Ty = Ty::Timestamp;
279
280    #[inline]
281    fn as_timestamp(&self) -> i64 {
282        self.0
283    }
284}
285
286impl IsValue for IVarChar {
287    const TY: Ty = Ty::VarChar;
288
289    #[inline]
290    fn as_var_char(&self) -> &str {
291        &self.0
292    }
293}
294
295impl IsValue for INChar {
296    const TY: Ty = Ty::NChar;
297
298    #[inline]
299    fn as_nchar(&self) -> &str {
300        &self.0
301    }
302}
303
304pub trait IsVarChar {
305    fn as_var_char(&self) -> &str;
306}
307
308impl IsVarChar for IVarChar {
309    fn as_var_char(&self) -> &str {
310        &self.0
311    }
312}
313
314pub trait IsNChar {
315    fn as_nchar(&self) -> &str;
316}
317
318impl IsNChar for INChar {
319    fn as_nchar(&self) -> &str {
320        &self.0
321    }
322}
323
324pub trait IsJson {
325    fn to_json(&self) -> String;
326}
327
328impl IsJson for IJson {
329    fn to_json(&self) -> String {
330        self.to_string()
331    }
332}
333
334pub trait IsMediumBlob {
335    fn as_medium_blob(&self) -> &[u8];
336}
337
338impl IsMediumBlob for IMediumBlob {
339    fn as_medium_blob(&self) -> &[u8] {
340        &self.0
341    }
342}
343
344pub trait IsBlob {
345    fn as_blob(&self) -> &[u8];
346}
347
348impl IsBlob for IBlob {
349    fn as_blob(&self) -> &[u8] {
350        &self.0
351    }
352}
353
354impl IValue for ITimestamp {
355    const TY: Ty = Ty::Timestamp;
356
357    type Inner = i64;
358
359    fn into_value(self) -> Value {
360        todo!()
361    }
362
363    fn into_inner(self) -> Self::Inner {
364        self.0
365    }
366}
367
368// macro_rules! impl_wrapper_struct {
369//     ($($ty:ident)*) => {
370//         $(paste::paste! {
371//             impl IValue for [<I $ty>] {
372//                 const TY: Ty = Ty::$ty;
373//                 #[inline]
374//                 fn into_value(self) -> Value {
375//                     Value::$ty(self.0)
376//                 }
377//             }
378//         })*
379//     };
380//     ($($ty:ident, $inner:ty;)*) => {
381//         $(paste::paste! {
382//             #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Deserialize, Serialize)]
383//             pub struct [<I $ty>]($inner);
384
385//             impl Deref for [<I $ty>] {
386//                 type Target = $inner;
387
388//                 #[inline]
389//                 fn deref(&self) -> &Self::Target {
390//                         &self.0
391//                 }
392//             }
393
394//             impl IValue for [<I $ty>] {
395//                 const TY: Ty = Ty::$ty;
396//                 #[inline]
397//                 fn into_value(self) -> Value {
398//                     Value::$ty(self.0)
399//                 }
400//             }
401//         })*
402//     };
403// }