akita_core 0.7.0

Akita - Mini orm for rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/*
 *
 *  *
 *  *      Copyright (c) 2018-2025, SnackCloud All rights reserved.
 *  *
 *  *   Redistribution and use in source and binary forms, with or without
 *  *   modification, are permitted provided that the following conditions are met:
 *  *
 *  *   Redistributions of source code must retain the above copyright notice,
 *  *   this list of conditions and the following disclaimer.
 *  *   Redistributions in binary form must reproduce the above copyright
 *  *   notice, this list of conditions and the following disclaimer in the
 *  *   documentation and/or other materials provided with the distribution.
 *  *   Neither the name of the www.snackcloud.cn developer nor the names of its
 *  *   contributors may be used to endorse or promote products derived from
 *  *   this software without specific prior written permission.
 *  *   Author: SnackCloud
 *  *
 *
 */

use base64::Engine;
use bigdecimal::{BigDecimal};
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
use indexmap::IndexMap;
use serde::ser::SerializeStruct;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value as JsonValue;
use std::collections::HashMap;
use std::{fmt, mem};
use uuid::Uuid;


mod interval;
mod array;
#[allow(non_snake_case, unused)]
mod tuple;
mod blob;
mod expr;
mod to_value;
mod from_value;

use crate::{AkitaDataError, ConversionError, Wrapper};
pub use array::*;
pub use blob::*;
pub use expr::*;
pub use from_value::*;
pub use interval::*;
pub use to_value::*;

#[derive(Debug, Clone, PartialEq)]
pub enum AkitaValue {
    // ========== Underlying Data Type (Value Compatible)==========
    Null,
    Bool(bool),
    Tinyint(i8),
    Smallint(i16),
    Int(i32),
    Bigint(i64),
    Float(f32),
    Double(f64),
    BigDecimal(BigDecimal),
    Blob(Vec<u8>),
    Char(char),
    Text(String),
    Json(JsonValue),
    Uuid(Uuid),
    Date(NaiveDate),
    Time(NaiveTime),
    DateTime(NaiveDateTime),
    Timestamp(DateTime<Utc>),
    Interval(Interval),
    Array(Array),
    Object(IndexMap<String, AkitaValue>),

    // ========== SQL Specific type ==========
    Column(String),
    RawSql(String),
    Wrapper(Box<Wrapper>),

    // ========== Container type ==========
    List(Vec<AkitaValue>),
}

#[derive(Serialize, Deserialize)]
enum AkitaValueType {
    Null,
    Bool,
    Tinyint,
    Smallint,
    Int,
    Bigint,
    Float,
    Double,
    BigDecimal,
    Blob,
    Char,
    Text,
    Json,
    Uuid,
    Array,
    Date,
    Time,
    DateTime,
    Timestamp,
    Object,
    Column,
    RawSql,
    Wrapper,
    List,
    Interval,
}

// Implement Serialize
impl Serialize for AkitaValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        // Use the wrapper structure to include type information
        match self {
            AkitaValue::Null => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValue::Null)?;
                state.serialize_field("value", &())?;
                state.end()
            }
            AkitaValue::Bool(b) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Bool)?;
                state.serialize_field("value", b)?;
                state.end()
            }
            AkitaValue::Tinyint(i) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Tinyint)?;
                state.serialize_field("value", i)?;
                state.end()
            }
            AkitaValue::Smallint(i) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Smallint)?;
                state.serialize_field("value", i)?;
                state.end()
            }
            AkitaValue::Int(i) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Int)?;
                state.serialize_field("value", i)?;
                state.end()
            }
            AkitaValue::Bigint(i) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Bigint)?;
                state.serialize_field("value", i)?;
                state.end()
            }
            AkitaValue::Float(f) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Float)?;
                state.serialize_field("value", f)?;
                state.end()
            }
            AkitaValue::Double(f) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Double)?;
                state.serialize_field("value", f)?;
                state.end()
            }
            AkitaValue::BigDecimal(bd) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::BigDecimal)?;
                state.serialize_field("value", &bd.to_string())?;
                state.end()
            }
            AkitaValue::Blob(blob) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Blob)?;
                state.serialize_field("value", &base64::engine::general_purpose::STANDARD.encode(blob))?;
                state.end()
            }
            AkitaValue::Char(c) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Char)?;
                state.serialize_field("value", &c.to_string())?;
                state.end()
            }
            AkitaValue::Text(s) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Text)?;
                state.serialize_field("value", s)?;
                state.end()
            }
            AkitaValue::Json(value) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Json)?;
                state.serialize_field("value", value)?;
                state.end()
            }
            AkitaValue::Uuid(uuid) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Uuid)?;
                state.serialize_field("value", &uuid.to_string())?;
                state.end()
            }
            AkitaValue::Date(date) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Date)?;
                state.serialize_field("value", &date.to_string())?;
                state.end()
            }
            AkitaValue::Time(time) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Time)?;
                state.serialize_field("value", &time.to_string())?;
                state.end()
            }
            AkitaValue::DateTime(dt) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::DateTime)?;
                state.serialize_field("value", &dt.to_string())?;
                state.end()
            }
            AkitaValue::Timestamp(ts) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Timestamp)?;
                state.serialize_field("value", &ts.to_rfc3339())?;
                state.end()
            }
            AkitaValue::Object(map) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Object)?;
                let data = map.iter().collect::<HashMap<_, _>>();
                state.serialize_field("value", &data)?;
                state.end()
            }
            AkitaValue::Column(c) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Column)?;
                state.serialize_field("value", c)?;
                state.end()
            }
            AkitaValue::RawSql(sql) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::RawSql)?;
                state.serialize_field("value", sql)?;
                state.end()
            }
            AkitaValue::Wrapper(w) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Wrapper)?;
                // 对于 Wrapper,我们序列化其 SQL 表示
                state.serialize_field("value", &w.to_string())?;
                state.end()
            }
            AkitaValue::List(items) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::List)?;
                state.serialize_field("value", items)?;
                state.end()
            }
            AkitaValue::Interval(iv) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Interval)?;
                state.serialize_field("value", &iv.to_string())?;
                state.end()
            }
            AkitaValue::Array(arr) => {
                let mut state = serializer.serialize_struct("AkitaValue", 2)?;
                state.serialize_field("type", &AkitaValueType::Array)?;
                state.serialize_field("value", &arr.to_string())?;
                state.end()
            }
        }
    }
}

impl<'de> Deserialize<'de> for AkitaValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        // 期望的结构:{"type": "TypeName", "value": ...}
        #[derive(Deserialize)]
        struct AkitaValueHelper {
            r#type: AkitaValueType,
            value: serde_json::Value,
        }

        let helper = AkitaValueHelper::deserialize(deserializer)?;

        match helper.r#type {
            AkitaValueType::Null => Ok(AkitaValue::Null),
            AkitaValueType::Bool => {
                let b = bool::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Bool(b))
            }
            AkitaValueType::Tinyint => {
                let i = i8::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Tinyint(i))
            }
            AkitaValueType::Smallint => {
                let i = i16::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Smallint(i))
            }
            AkitaValueType::Int => {
                let i = i32::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Int(i))
            }
            AkitaValueType::Bigint => {
                let i = i64::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Bigint(i))
            }
            AkitaValueType::Float => {
                let f = f32::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Float(f))
            }
            AkitaValueType::Double => {
                let f = f64::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Double(f))
            }
            AkitaValueType::BigDecimal => {
                let s = String::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                let bd = s.parse::<BigDecimal>()
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::BigDecimal(bd))
            }
            AkitaValueType::Blob => {
                let s = String::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                let bytes = base64::engine::general_purpose::STANDARD.decode(&s)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Blob(bytes))
            }
            AkitaValueType::Char => {
                let s = String::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                let c = s.chars().next()
                    .ok_or_else(|| de::Error::custom("Empty string for char"))?;
                Ok(AkitaValue::Char(c))
            }
            AkitaValueType::Text => {
                let s = String::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Text(s))
            }
            AkitaValueType::Json => {
                let json = JsonValue::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Json(json))
            }
            AkitaValueType::Uuid => {
                let s = String::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                let uuid = Uuid::parse_str(&s)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Uuid(uuid))
            }
            AkitaValueType::Date => {
                let s = String::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                let date = NaiveDate::parse_from_str(&s, "%Y-%m-%d")
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Date(date))
            }
            AkitaValueType::Time => {
                let s = String::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                let time = NaiveTime::parse_from_str(&s, "%H:%M:%S")
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Time(time))
            }
            AkitaValueType::DateTime => {
                let s = String::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                let dt = NaiveDateTime::parse_from_str(&s, "%Y-%m-%d %H:%M:%S")
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::DateTime(dt))
            }
            AkitaValueType::Timestamp => {
                let s = String::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                let ts = DateTime::parse_from_rfc3339(&s)
                    .map_err(de::Error::custom)?
                    .with_timezone(&Utc);
                Ok(AkitaValue::Timestamp(ts))
            }
            AkitaValueType::Object => {
                let map: HashMap<String, AkitaValue> = HashMap::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                let mut im = IndexMap::new();
                // 遍历 HashMap 并按顺序插入
                for (k, v) in map {
                    im.insert(k.clone(), v.clone());
                }
                Ok(AkitaValue::Object(im))
            }
            AkitaValueType::Column => {
                let s = String::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::Column(s))
            }
            AkitaValueType::RawSql => {
                let s = String::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::RawSql(s))
            }
            AkitaValueType::Wrapper => {
                // Wrapper 无法完整反序列化,返回默认值
                Ok(AkitaValue::Wrapper(Box::new(Wrapper::new())))
            }
            AkitaValueType::List => {
                let items: Vec<AkitaValue> = Vec::deserialize(helper.value)
                    .map_err(de::Error::custom)?;
                Ok(AkitaValue::List(items))
            }
            AkitaValueType::Array => {
                Ok(AkitaValue::Array(Array::Text(vec![])))
            }
            AkitaValueType::Interval => {
                Ok(AkitaValue::Interval(Interval::new(0,0,0)))
            }
        }
    }
}


impl Default for AkitaValue {
    fn default() -> Self {
        AkitaValue::Null
    }
}

impl <'a> From<&'a AkitaValue> for AkitaValue {
    fn from(v: &'a AkitaValue) -> AkitaValue {
        v.to_owned()
    }
}

impl<T> From<T> for AkitaValue
where
    T: IntoAkitaValue,
{
    fn from(v: T) -> AkitaValue {
        v.into_value()
    }
}


impl AkitaValue {

    // ========== Type check method ==========

    pub fn is_string(&self) -> bool {
        self.as_str().is_some()
    }

    pub fn is_object(&self) -> bool {
        self.as_object().is_some()
    }

    pub fn is_array(&self) -> bool {
        self.as_array().is_some()
    }

    pub fn is_number(&self) -> bool {
        matches!(
            *self,
            AkitaValue::Tinyint(_) 
            | AkitaValue::Smallint(_) 
            | AkitaValue::Int(_)
            | AkitaValue::Bigint(_)  
            | AkitaValue::Float(_) 
            | AkitaValue::Double(_)
            | AkitaValue::BigDecimal(_)
        )
    }

    pub fn is_boolean(&self) -> bool {
        self.as_bool().is_some()
    }

    pub fn is_null(&self) -> bool {
        self.as_null().is_some()
    }

    pub fn is_zero(&self) -> bool {
        match self {
            AkitaValue::Tinyint(v) => *v == 0,
            AkitaValue::Smallint(v) => *v == 0,
            AkitaValue::Int(v) => *v == 0,
            AkitaValue::Bigint(v) => *v == 0,
            AkitaValue::Float(v) => *v == 0.0,
            AkitaValue::Double(v) => *v == 0.0,
            AkitaValue::Text(v) => v.eq("0"),
            _ => false,
        }
    }

    // ========== Type conversion method ==========

    pub fn as_object(&self) -> Option<&IndexMap<String, AkitaValue>> {
        match self {
            AkitaValue::Object(map) => Some(map),
            _ => None,
        }
    }

    pub fn as_object_mut(&mut self) -> Option<&mut IndexMap<String, AkitaValue>> {
        match self {
            AkitaValue::Object(map) => Some(map),
            _ => None,
        }
    }

    pub fn as_array(&self) -> Option<&Vec<AkitaValue>> {
        match self {
            AkitaValue::List(array) => Some(array),
            _ => None,
        }
    }

    pub fn as_array_mut(&mut self) -> Option<&mut Vec<AkitaValue>> {
        match self {
            AkitaValue::List(array) => Some(array),
            _ => None,
        }
    }

    pub fn as_str(&self) -> Option<&str> {
        match self {
            AkitaValue::Text(s) => Some(s.as_str()),
            AkitaValue::Char(c) => {
                Some(Box::leak(c.to_string().into_boxed_str()))
            }
            _ => None,
        }
    }

    pub fn to_str(&self) -> Option<&str> {
        match self {
            AkitaValue::Text(s) => Some(s.as_str()),
            AkitaValue::Char(c) => {
                Some(Box::leak(c.to_string().into_boxed_str()))
            }
            AkitaValue::Bool(b) => Some(Box::leak(b.to_string().into_boxed_str())),
            AkitaValue::Tinyint(v) => Some(Box::leak(v.to_string().into_boxed_str())),
            AkitaValue::Smallint(v) => Some(Box::leak(v.to_string().into_boxed_str())),
            AkitaValue::Int(v) => Some(Box::leak(v.to_string().into_boxed_str())),
            AkitaValue::Bigint(v) => Some(Box::leak(v.to_string().into_boxed_str())),
            AkitaValue::Float(v) => Some(Box::leak(v.to_string().into_boxed_str())),
            AkitaValue::Double(v) => Some(Box::leak(v.to_string().into_boxed_str())),
            AkitaValue::BigDecimal(v) => Some(Box::leak(v.to_string().into_boxed_str())),
            AkitaValue::Json(v) => Some(Box::leak(v.to_string().into_boxed_str())),
            AkitaValue::Uuid(v) => Some(Box::leak(v.to_string().into_boxed_str())),
            AkitaValue::Date(v) => Some(Box::leak(v.to_string().into_boxed_str())),
            AkitaValue::Time(v) => Some(Box::leak(v.to_string().into_boxed_str())),
            AkitaValue::DateTime(v) => Some(Box::leak(v.format("%Y-%m-%d %H:%M:%S").to_string().into_boxed_str())),
            AkitaValue::Timestamp(v) => Some(Box::leak(v.to_string().into_boxed_str())),
            _ => None,
        }
    }

    pub fn as_bool(&self) -> Option<bool> {
        match self {
            AkitaValue::Bool(b) => Some(*b),
            _ => None,
        }
    }

    pub fn as_null(&self) -> Option<()> {
        match self {
            AkitaValue::Null => Some(()),
            _ => None,
        }
    }

    // ========== Numeric type conversion ==========

    pub fn as_i8(&self) -> Option<i8> {
        match self {
            AkitaValue::Tinyint(v) => Some(*v),
            AkitaValue::Smallint(v) => Some(*v as i8),
            AkitaValue::Int(v) => Some(*v as i8),
            AkitaValue::Bigint(v) => Some(*v as i8),
            AkitaValue::Float(v) => Some(*v as i8),
            AkitaValue::Double(v) => Some(*v as i8),
            _ => None,
        }
    }

    pub fn as_i16(&self) -> Option<i16> {
        match self {
            AkitaValue::Tinyint(v) => Some(*v as i16),
            AkitaValue::Smallint(v) => Some(*v),
            AkitaValue::Int(v) => Some(*v as i16),
            AkitaValue::Bigint(v) => Some(*v as i16),
            AkitaValue::Float(v) => Some(*v as i16),
            AkitaValue::Double(v) => Some(*v as i16),
            _ => None,
        }
    }

    pub fn as_i32(&self) -> Option<i32> {
        match self {
            AkitaValue::Tinyint(v) => Some(*v as i32),
            AkitaValue::Smallint(v) => Some(*v as i32),
            AkitaValue::Int(v) => Some(*v),
            AkitaValue::Bigint(v) => Some(*v as i32),
            AkitaValue::Float(v) => Some(*v as i32),
            AkitaValue::Double(v) => Some(*v as i32),
            _ => None,
        }
    }

    pub fn as_i64(&self) -> Option<i64> {
        match self {
            AkitaValue::Tinyint(v) => Some(*v as i64),
            AkitaValue::Smallint(v) => Some(*v as i64),
            AkitaValue::Int(v) => Some(*v as i64),
            AkitaValue::Bigint(v) => Some(*v),
            AkitaValue::Float(v) => Some(*v as i64),
            AkitaValue::Double(v) => Some(*v as i64),
            _ => None,
        }
    }

    pub fn as_f32(&self) -> Option<f32> {
        match self {
            AkitaValue::Tinyint(v) => Some(*v as f32),
            AkitaValue::Smallint(v) => Some(*v as f32),
            AkitaValue::Int(v) => Some(*v as f32),
            AkitaValue::Bigint(v) => Some(*v as f32),
            AkitaValue::Float(v) => Some(*v),
            AkitaValue::Double(v) => Some(*v as f32),
            _ => None,
        }
    }

    pub fn as_f64(&self) -> Option<f64> {
        match self {
            AkitaValue::Tinyint(v) => Some(*v as f64),
            AkitaValue::Smallint(v) => Some(*v as f64),
            AkitaValue::Int(v) => Some(*v as f64),
            AkitaValue::Bigint(v) => Some(*v as f64),
            AkitaValue::Float(v) => Some(*v as f64),
            AkitaValue::Double(v) => Some(*v),
            _ => None,
        }
    }

    // ========== Object manipulation method ==========

    pub fn take(&mut self) -> AkitaValue {
        mem::replace(self, AkitaValue::Null)
    }

    pub fn new_object() -> Self {
        AkitaValue::Object(IndexMap::new())
    }

    pub fn insert_obj<K, V>(&mut self, k: K, v: V)
    where
        K: ToString,
        V: IntoAkitaValue,
    {
        match self {
            AkitaValue::Object(data) => {
                data.insert(k.to_string().replace("r#",""), v.into_value());
            },
            _ => (),
        }
    }

    pub fn insert_obj_value<K>(&mut self, k: K, value: &AkitaValue)
    where
        K: ToString,
    {
        match self {
            AkitaValue::Object(v) => {
                v.insert(k.to_string(), value.clone());
            },
            _ => (),
        }
    }

    pub fn get_obj<T>(&self, s: &str) -> Result<T, AkitaDataError>
    where
        T: FromAkitaValue,
    {
        match self {
            AkitaValue::Object(data) => match data.get(&s.replace("r#","")) {
                Some(v) => FromAkitaValue::from_value_opt(v),
                None => Err(AkitaDataError::ConversionError(ConversionError::ConversionError {
                    message: format!("No such key: {}", s),
                })),
            },
            _ => Err(AkitaDataError::ConversionError(ConversionError::ConversionError {
                message: "Not an object".to_string(),
            })),
        }
    }

    pub fn get_obj_len(&self) -> usize {
        match self {
            AkitaValue::Object(data) => data.len(),
            _ => 0,
        }
    }

    pub fn get_obj_opt<T>(&self, s: &str) -> Result<Option<T>, AkitaDataError>
    where
        T: FromAkitaValue,
    {
        match self {
            AkitaValue::Object(data) => match data.get(&s.replace("r#","")) {
                Some(v) => {
                    match v {
                        AkitaValue::Null => Ok(None),
                        _ => Ok(Some(FromAkitaValue::from_value_opt(v)?)),
                    }
                }
                None => Ok(None),
            },
            _ => Ok(None),
        }
    }

    pub fn get_obj_value(&self, s: &str) -> Option<&AkitaValue> {
        match self {
            AkitaValue::Object(data) => data.get(s),
            _ => None,
        }
    }

    pub fn get_obj_value_mut(&mut self, s: &str) -> Option<&mut AkitaValue> {
        match self {
            AkitaValue::Object(data) => data.get_mut(s),
            _ => None,
        }
    }

    pub fn remove_obj(&mut self, s: &str) -> Option<AkitaValue> {
        match self {
            AkitaValue::Object(v) => v.shift_remove(s),
            _ => None,
        }
    }

    /// Will take value of a column with index `index` if it exists and wasn't taken earlier then
    /// will converts it to `T`.
    pub fn take_obj_raw<T>(&self, index: usize) -> Option<T>
    where
        T: FromAkitaValue,
    {
        match self {
            AkitaValue::Object(v) => {
                v.values().nth(index).and_then(|v| {
                    FromAkitaValue::from_value_opt(v).ok()
                })
            },
            _ => None,
        }
    }

    /// Will take value of a column with index `index` if it exists and wasn't taken earlier then
    /// will attempt to convert it to `T`. Unlike `Row::take`, `Row::take_opt` will allow you to
    /// directly handle errors if the value could not be converted to `T`.
    pub fn take_obj_raw_opt<T>(&self, index: usize) -> Option<Result<T, AkitaDataError>>
    where
        T: FromAkitaValue,
    {
        match self {
            AkitaValue::Object(v) => {
                v.values().nth(index).map(|v| {
                    FromAkitaValue::from_value_opt(v)
                })
            },
            _ => Some(Err(AkitaDataError::ConversionError(ConversionError::ConversionError {
                message: "Not an object".to_string(),
            }))),
        }
    }

    #[doc(hidden)]
    pub fn place(&mut self, index: usize, value: AkitaValue) {
        match self {
            AkitaValue::Object(v) => {
                if let Some(key) = v.keys().nth(index).cloned() {
                    v.insert(key, value);
                }
            }
            _ => (),
        }
    }

    // ========== Added a convenient method ==========

    /// Create a new array
    pub fn new_array() -> Self {
        AkitaValue::List(Vec::new())
    }

    /// Add elements to the array
    pub fn push_array<V: IntoAkitaValue>(&mut self, value: V) {
        match self {
            AkitaValue::List(vec) => {
                vec.push(value.into_value());
            }
            _ => (),
        }
    }

    /// Pop elements from the array
    pub fn pop_array(&mut self) -> Option<AkitaValue> {
        match self {
            AkitaValue::List(vec) => vec.pop(),
            _ => None,
        }
    }

    /// Get the array length
    pub fn array_len(&self) -> usize {
        match self {
            AkitaValue::List(vec) => vec.len(),
            _ => 0,
        }
    }

    /// Check if it is empty
    pub fn is_empty(&self) -> bool {
        match self {
            AkitaValue::Null => true,
            AkitaValue::Text(s) => s.is_empty(),
            AkitaValue::List(vec) => vec.is_empty(),
            AkitaValue::Object(map) => map.is_empty(),
            AkitaValue::Blob(data) => data.is_empty(),
            _ => false,
        }
    }

    /// Convert to JSON values
    pub fn to_json(&self) -> Result<JsonValue, AkitaDataError> {
        JsonValue::from_value_opt(self)
    }

    /// Created from a JSON value
    pub fn from_json(json: JsonValue) -> Self {
        AkitaValue::Json(json)
    }

    /// Secure type conversion (no loss of information)
    pub fn coerce_to_string(&self) -> String {
        match self {
            AkitaValue::Text(s) => s.clone(),
            AkitaValue::Char(c) => c.to_string(),
            AkitaValue::Int(i) => i.to_string(),
            AkitaValue::Bigint(i) => i.to_string(),
            AkitaValue::Float(f) => f.to_string(),
            AkitaValue::Double(f) => f.to_string(),
            AkitaValue::Bool(b) => b.to_string(),
            AkitaValue::Date(d) => d.to_string(),
            AkitaValue::Time(t) => t.to_string(),
            AkitaValue::DateTime(dt) => dt.to_string(),
            AkitaValue::Timestamp(ts) => ts.to_rfc3339(),
            AkitaValue::Uuid(u) => u.to_string(),
            AkitaValue::BigDecimal(bd) => bd.to_string(),
            AkitaValue::Null => "".to_string(),
            AkitaValue::Blob(_) => "[BLOB]".to_string(),
            AkitaValue::List(_) => "[ARRAY]".to_string(),
            AkitaValue::Object(_) => "[OBJECT]".to_string(),
            AkitaValue::Column(c) => c.clone(),
            AkitaValue::RawSql(sql) => sql.clone(),
            AkitaValue::Wrapper(_) => "[WRAPPER]".to_string(),
            AkitaValue::Tinyint(v) => v.to_string(),
            AkitaValue::Smallint(v) => v.to_string(),
            AkitaValue::Json(v) => v.to_string(),
            AkitaValue::Interval(v) => v.to_string(),
            AkitaValue::Array(v) => v.to_string(),
        }
    }

    /// Deep cloning (can be slower for large objects)
    pub fn deep_clone(&self) -> Self {
        match self {
            AkitaValue::Object(map) => {
                AkitaValue::Object(map.clone())
            }
            AkitaValue::List(vec) => {
                let cloned_vec: Vec<AkitaValue> = vec
                    .iter()
                    .map(|v| v.deep_clone())
                    .collect();
                AkitaValue::List(cloned_vec)
            }
            other => other.clone(),
        }
    }
}

impl fmt::Display for AkitaValue {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            // 保持原有实现,但可以优化一些显示
            AkitaValue::Null => write!(f, "null"),
            AkitaValue::Bool(v) => write!(f, "{}", v),
            AkitaValue::Tinyint(v) => write!(f, "{}", v),
            AkitaValue::Smallint(v) => write!(f, "{}", v),
            AkitaValue::Int(v) => write!(f, "{}", v),
            AkitaValue::Bigint(v) => write!(f, "{}", v),
            AkitaValue::Float(v) => write!(f, "{:.6}", v),
            AkitaValue::Double(v) => write!(f, "{:.6}", v),
            AkitaValue::BigDecimal(v) => write!(f, "{}", v),
            AkitaValue::Char(v) => write!(f, "'{}'", v),
            AkitaValue::Text(v) => write!(f, "'{}'", v.replace("'", "''")),
            AkitaValue::Json(v) => {
                let json_str = serde_json::to_string(v).unwrap_or_default();
                if json_str.len() > 100 {
                    write!(f, "JSON(truncated)")
                } else {
                    write!(f, "{}", json_str)
                }
            }
            AkitaValue::Uuid(v) => write!(f, "'{}'", v),
            AkitaValue::Date(v) => write!(f, "DATE '{}'", v),
            AkitaValue::Time(v) => write!(f, "TIME '{}'", v.format("%H:%M:%S%.3f")),
            AkitaValue::DateTime(v) => write!(f, "'{}'", v.format("%Y-%m-%d %H:%M:%S")),
            AkitaValue::Timestamp(v) => write!(f, "'{}'", v.to_rfc3339()),
            AkitaValue::Array(array) => write!(f, "ARRAY({})", array),
            AkitaValue::Blob(v) => {
                if v.len() > 20 {
                    write!(f, "BLOB({} bytes, truncated)", v.len())
                } else {
                    write!(f, "{}", String::from_utf8_lossy(v))
                }
            }
            AkitaValue::Interval(v) => write!(f, "{}", v.to_string()),
            AkitaValue::Object(v) => {
                let map: HashMap<_, _> = v.iter().collect();
                write!(f, "OBJECT({} fields)", map.len())
            }
            AkitaValue::Column(v) => write!(f, "[COLUMN] {}", v),
            AkitaValue::RawSql(v) => write!(f, "[RAW_SQL] {}", v),
            AkitaValue::Wrapper(_v) => write!(f, "[WRAPPER]"),
            AkitaValue::List(v) => {
                if v.is_empty() {
                    write!(f, "[]")
                } else if v.len() <= 5 {
                    let items: Vec<String> = v.iter().map(|item| item.to_string()).collect();
                    write!(f, "[{}]", items.join(", "))
                } else {
                    write!(f, "[{} items]", v.len())
                }
            }
        }
    }
}

#[inline]
pub fn from_akita_value<T: FromAkitaValue>(v: AkitaValue) -> T {
    FromAkitaValue::from_value(&v)
}


#[inline]
pub fn from_akita_value_opt<T: FromAkitaValue>(v: AkitaValue) -> Result<T, AkitaDataError> {
    FromAkitaValue::from_value_opt(&v)
}