dm-pure 1.2.2

纯 Rust 实现的达梦 DM8 数据库驱动:DM 网络协议(握手/登录/执行/参数绑定/LOB/事务/分批 fetch),零外部数据库依赖
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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
//! 达梦结果集解析:列描述符 + 行数据 + 类型解码(对照 Go 驱动 + 抓包实测格式)。

use encoding_rs::Encoding;

use crate::Result;

// 达梦类型常量(zzn.go)
pub const DM_CHAR: i32 = 0;
pub const DM_VARCHAR2: i32 = 1;
pub const DM_VARCHAR: i32 = 2;
pub const DM_BIT: i32 = 3;
pub const DM_TINYINT: i32 = 5;
pub const DM_SMALLINT: i32 = 6;
pub const DM_INT: i32 = 7;
pub const DM_BIGINT: i32 = 8;
pub const DM_DECIMAL: i32 = 9;
pub const DM_REAL: i32 = 10;
pub const DM_DOUBLE: i32 = 11;
pub const DM_BLOB: i32 = 12;
pub const DM_BOOLEAN: i32 = 13;
pub const DM_DATE: i32 = 14;
pub const DM_TIME: i32 = 15;
pub const DM_DATETIME: i32 = 16;
pub const DM_BINARY: i32 = 17;
pub const DM_VARBINARY: i32 = 18;
pub const DM_CLOB: i32 = 19;
pub const DM_INTERVAL_YM: i32 = 20;
pub const DM_INTERVAL_DT: i32 = 21;
pub const DM_TIME_TZ: i32 = 22;
pub const DM_DATETIME_TZ: i32 = 23;
pub const DM_XDEC_INT32: i32 = 24;
pub const DM_XDEC_INT64: i32 = 25;
pub const DM_DATETIME2: i32 = 26;
pub const DM_DATETIME2_TZ: i32 = 27;
// (对照 Go 驱动 zzn.go:TIME_TZ=22, DATETIME_TZ=23, XDEC_INT32=24, XDEC_INT64=25,
//  DATETIME2=26, DATETIME2_TZ=27;旧版误写 DATETIME2=30 已修正)

/// 行数据 NULL 标记
pub const NULL_MARK: u16 = 0xFFFE;
/// LOB/特殊标记(0xFFFF 后跟 int32 真实长度 + 数据,Go zzo.go convertBytes2BaseData)
pub const LOB_MARK: u16 = 0xFFFF;

/// 达梦日期(年可为负/超 9999,避免 chrono/NaiveDate 限制)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DmDate {
    pub year: i32,
    pub month: u8,
    pub day: u8,
}

impl DmDate {
    pub fn new(year: i32, month: u8, day: u8) -> Self {
        DmDate { year, month, day }
    }
}

impl std::fmt::Display for DmDate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:04}-{:02}-{:02}", self.year, self.month, self.day)
    }
}

/// 达梦时间(微秒精度)。`timezone_minutes` 为带时区类型(TIME_TZ)的偏移(分钟,可负)。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DmTime {
    pub hour: u8,
    pub minute: u8,
    pub second: u8,
    pub microsecond: u32,
    /// 时区偏移(分钟,东正西负);非 TZ 类型为 None
    pub timezone_minutes: Option<i16>,
}

impl DmTime {
    pub fn new(hour: u8, minute: u8, second: u8, microsecond: u32) -> Self {
        DmTime { hour, minute, second, microsecond, timezone_minutes: None }
    }
}

fn fmt_tz(tz: i16) -> String {
    let sign = if tz < 0 { "-" } else { "+" };
    let a = tz.unsigned_abs() as i32;
    format!("{sign}{:02}:{:02}", a / 60, a % 60)
}

impl std::fmt::Display for DmTime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{:02}:{:02}:{:02}.{:06}",
            self.hour, self.minute, self.second, self.microsecond
        )?;
        if let Some(tz) = self.timezone_minutes {
            write!(f, " {}", fmt_tz(tz))?;
        }
        Ok(())
    }
}

/// 达梦日期时间。`time.timezone_minutes` 为带时区类型(DATETIME_TZ/DATETIME2_TZ)的偏移。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DmDateTime {
    pub date: DmDate,
    pub time: DmTime,
}

impl DmDateTime {
    pub fn new(date: DmDate, time: DmTime) -> Self {
        DmDateTime { date, time }
    }
}

impl std::fmt::Display for DmDateTime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} {}", self.date, self.time)
    }
}

/// 解码后的单元格值。
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    Null,
    Bool(bool),
    TinyInt(i8),
    SmallInt(i16),
    Int(i32),
    BigInt(i64),
    Real(f32),
    Double(f64),
    /// DECIMAL 保持字符串,避免精度丢失(如 DECIMAL(38))
    Decimal(String),
    Str(String),
    Bytes(Vec<u8>),
    Date(DmDate),
    Time(DmTime),
    DateTime(DmDateTime),
    Interval(String),
    /// 未识别的原始字节
    Raw(Vec<u8>),
}

impl Value {
    pub fn as_str(&self) -> Option<&str> {
        match self {
            Value::Str(s) => Some(s),
            _ => None,
        }
    }
    pub fn as_i64(&self) -> Option<i64> {
        match self {
            Value::Int(v) => Some(*v as i64),
            Value::BigInt(v) => Some(*v),
            Value::SmallInt(v) => Some(*v as i64),
            Value::TinyInt(v) => Some(*v as i64),
            Value::Bool(b) => Some(*b as i64),
            Value::Double(f) => Some(*f as i64),
            Value::Real(f) => Some(*f as i64),
            _ => None,
        }
    }
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            Value::Double(f) => Some(*f),
            Value::Real(f) => Some(*f as f64),
            Value::Int(v) => Some(*v as f64),
            Value::BigInt(v) => Some(*v as f64),
            _ => None,
        }
    }
    pub fn as_bytes(&self) -> Option<&[u8]> {
        match self {
            Value::Bytes(b) => Some(b),
            Value::Raw(b) => Some(b),
            _ => None,
        }
    }
}

/// 列描述符。
#[derive(Debug, Clone, PartialEq)]
pub struct Column {
    pub col_type: i32,
    pub prec: i32,
    pub scale: i32,
    pub nullable: bool,
    pub name: String,
    pub type_name: String,
    pub table_name: String,
    pub schema_name: String,
    pub lob: bool,
}

/// 原始行:每单元格为 Some(bytes) 或 None(NULL)。
pub type Row = Vec<Option<Vec<u8>>>;

fn le_i32(b: &[u8], pos: usize) -> i32 {
    i32::from_le_bytes(b[pos..pos + 4].try_into().unwrap())
}

fn le_u16(b: &[u8], pos: usize) -> u16 {
    u16::from_le_bytes(b[pos..pos + 2].try_into().unwrap())
}

fn read_str_at(body: &[u8], pos: &mut usize, len: usize, enc: &'static Encoding) -> String {
    if *pos + len > body.len() {
        *pos = body.len();
        return String::new();
    }
    let (s, _, _) = enc.decode(&body[*pos..*pos + len]);
    *pos += len;
    s.into_owned()
}

/// 列描述符定位自适应(服务器参数回显区长度不固定)。
///
/// 实测:服务器在 DDL(DROP/CREATE)后首条参数化查询的参数回显区会多带 8B 类型名
/// (如 "BIGINT"),列描述符起点从 `32*n` 变为 `32*n+8`;硬编码 `params.len()*32`
/// 会解析错位成垃圾 col_type(如 1229408578)。
/// 依次尝试 32/32+8/16/16+8/0 字节每参数偏移,取能解析出合法列描述符
/// (列数匹配、col_type 0..=40、prec 合理、列名非空且含可打印字符)的候选。
pub fn parse_cols_robust(
    body: &[u8],
    n_params: usize,
    ncols: usize,
    enc: &'static Encoding,
) -> Result<Vec<Column>> {
    let valid = |cols: &[Column]| -> bool {
        if cols.len() != ncols {
            return false;
        }
        for c in cols {
            if !(0..=40).contains(&c.col_type) {
                return false;
            }
            if !(0..=1_000_000).contains(&c.prec) {
                return false;
            }
            if c.name.is_empty()
                || c.name.chars().any(|ch| ch.is_control())
                || !c.name.chars().any(|ch| ch.is_alphanumeric())
            {
                return false;
            }
        }
        true
    };
    for (per, off) in [(32usize, 0i32), (32, 8), (16, 0), (16, 8), (0, 0), (32, -8), (16, -8)] {
        let pos = (n_params * per) as i64 + off as i64;
        if pos < 0 || pos + 30 > body.len() as i64 {
            continue;
        }
        if let Ok((cols, _)) = parse_columns(body, pos as usize, ncols, enc) {
            if valid(&cols) {
                return Ok(cols);
            }
        }
    }
    // 全范围扫描兜底(每 2 字节步进,应对非常规回显如 32*n+10 等)
    let scan_end = body.len().saturating_sub(30).min(256);
    for pos in (0..scan_end).step_by(2) {
        if let Ok((cols, _)) = parse_columns(body, pos, ncols, enc) {
            if valid(&cols) {
                return Ok(cols);
            }
        }
    }
    // 最后兜底:按 32B/参数
    let (cols, _) = parse_columns(body, n_params * 32, ncols, enc)?;
    Ok(cols)
}

/// 解析 `count` 个列描述符,返回 (列列表, 新位置)。
///
/// 实测结构(Go dm_build_1009):
/// `4×int32(类型/精度/缩放/可空) + 8字节保留 + 4×int16 长度表 + 4 个字符串
///  + baseName(int16 长度 + 数据) + (LOB 列额外 6 字节)`
pub fn parse_columns(
    body: &[u8],
    mut pos: usize,
    count: usize,
    enc: &'static Encoding,
) -> Result<(Vec<Column>, usize)> {
    let mut cols = Vec::with_capacity(count);
    for _ in 0..count {
        if pos + 32 > body.len() {
            break;
        }
        let col_type = le_i32(body, pos);
        let prec = le_i32(body, pos + 4);
        let scale = le_i32(body, pos + 8);
        let nullable = le_i32(body, pos + 12) != 0;
        pos += 24; // 16(4×i32) + 8 保留
        if pos + 8 > body.len() {
            break;
        }
        let nl = le_u16(body, pos) as usize;
        let tl = le_u16(body, pos + 2) as usize;
        let tbl = le_u16(body, pos + 4) as usize;
        let sl = le_u16(body, pos + 6) as usize;
        pos += 8;
        let name = read_str_at(body, &mut pos, nl, enc);
        let type_name = read_str_at(body, &mut pos, tl, enc);
        let table_name = read_str_at(body, &mut pos, tbl, enc);
        let schema_name = read_str_at(body, &mut pos, sl, enc);
        // baseName(int16 长度 + 数据)
        if pos + 2 > body.len() {
            break;
        }
        let bn = le_u16(body, pos) as usize;
        pos += 2;
        pos = (pos + bn).min(body.len());
        let lob = col_type == DM_BLOB || col_type == DM_CLOB;
        if lob {
            // lob 列额外 6 字节(lobTabId int32 + lobColId int16)
            pos += 6;
        }
        cols.push(Column {
            col_type,
            prec,
            scale,
            nullable,
            name,
            type_name,
            table_name,
            schema_name,
            lob,
        });
    }
    Ok((cols, pos))
}

/// 解析普通查询行(`[int32 行长][6B][偏移表 2×ncols][列数据]`)。
///
/// NULL 列:长度 = 0xFFFE;LOB/特殊:长度 = 0xFFFF + int32 真实长度 + 数据
/// (后者为对 Python 驱动 `<LOB>` 占位符的修正,格式依据 Go zzo.go)。
pub fn parse_rows(
    body: &[u8],
    mut pos: usize,
    ncols: usize,
    nrows: usize,
) -> Result<Vec<Row>> {
    let mut rows = Vec::with_capacity(nrows);
    let header = 10; // int32(4) + 6B
    for _ in 0..nrows {
        if pos + header > body.len() {
            break;
        }
        pos += header;
        if pos + 2 * ncols > body.len() {
            break;
        }
        pos += 2 * ncols; // 偏移表
        let mut row = Vec::with_capacity(ncols);
        for _ in 0..ncols {
            if pos + 2 > body.len() {
                row.push(None);
                break;
            }
            let ln = le_u16(body, pos);
            pos += 2;
            match ln {
                NULL_MARK => row.push(None),
                LOB_MARK => {
                    // 0xFFFF → int32 真实长度(Go: valueLen == -1 → 读 int32)
                    if pos + 4 <= body.len() {
                        let real = le_i32(body, pos);
                        pos += 4;
                        if real > 0 && pos + real as usize <= body.len() {
                            row.push(Some(body[pos..pos + real as usize].to_vec()));
                            pos += real as usize;
                        } else {
                            row.push(None);
                        }
                    } else {
                        row.push(None);
                    }
                }
                n => {
                    let n = n as usize;
                    if pos + n > body.len() {
                        row.push(None);
                        break;
                    }
                    row.push(Some(body[pos..pos + n].to_vec()));
                    pos += n;
                }
            }
        }
        rows.push(row);
    }
    Ok(rows)
}

/// 解析参数化查询行(行长循环 `[int16 行长][int16 行号][7B][偏移表 2×ncols][列数据]`)。
pub fn parse_rows_param(body: &[u8], mut pos: usize, ncols: usize) -> Result<Vec<Row>> {
    let mut rows = Vec::new();
    while pos + 2 <= body.len() {
        let rlen = le_u16(body, pos) as usize;
        let row_end = pos + rlen;
        // 行长 = 行长字段(2) + 数据长度(实测:行1 行长26 = 2+24)
        if rlen == 0 || row_end <= pos + 2 || row_end > body.len() {
            break; // 行长=0 或无效(尾部残留)
        }
        let mut p = pos + 2 + 2 + 6; // 行长 + 行号 + 6B
        p += 2 * ncols; // 偏移表
        let mut row = Vec::with_capacity(ncols);
        for _ in 0..ncols {
            if p + 2 > row_end {
                row.push(None);
                break;
            }
            let ln = le_u16(body, p);
            p += 2;
            match ln {
                NULL_MARK => row.push(None),
                LOB_MARK => {
                    if p + 4 <= row_end {
                        let real = le_i32(body, p);
                        p += 4;
                        if real > 0 && p + real as usize <= row_end {
                            row.push(Some(body[p..p + real as usize].to_vec()));
                            p += real as usize;
                        } else {
                            row.push(None);
                        }
                    } else {
                        row.push(None);
                    }
                }
                n => {
                    let n = n as usize;
                    if p + n > row_end {
                        row.push(None);
                        break;
                    }
                    row.push(Some(body[p..p + n].to_vec()));
                    p += n;
                }
            }
        }
        rows.push(row);
        pos = row_end;
    }
    Ok(rows)
}

/// 按列类型解码单个值(LOB 列不在此处理,由 client 走完整读取)。
pub fn decode_scalar(raw: &[u8], col: &Column, enc: &'static Encoding) -> Result<Value> {
    if raw.is_empty() {
        // 空字节串按类型给空值
        match col.col_type {
            DM_CHAR | DM_VARCHAR2 | DM_VARCHAR | DM_CLOB => return Ok(Value::Str(String::new())),
            _ => return Ok(Value::Bytes(Vec::new())),
        }
    }
    let t = col.col_type;
    let v = match t {
        DM_CHAR | DM_VARCHAR2 | DM_VARCHAR => {
            let (s, _, _) = enc.decode(raw);
            Value::Str(s.into_owned())
        }
        DM_BOOLEAN | DM_BIT => Value::Bool(raw[0] != 0),
        DM_TINYINT => Value::TinyInt(raw[0] as i8),
        DM_SMALLINT => Value::SmallInt(i16::from_le_bytes(raw[..2].try_into().unwrap())),
        DM_INT => Value::Int(le_i32(raw, 0)),
        DM_BIGINT => Value::BigInt(i64::from_le_bytes(raw[..8].try_into().unwrap())),
        DM_REAL => Value::Real(f32::from_le_bytes(raw[..4].try_into().unwrap())),
        DM_DOUBLE => Value::Double(f64::from_le_bytes(raw[..8].try_into().unwrap())),
        DM_DECIMAL => Value::Decimal(decode_decimal(raw)),
        DM_DATE => Value::Date(decode_date(raw)),
        DM_TIME => Value::Time(decode_time(raw)),
        DM_TIME_TZ => Value::Time(decode_time_tz(raw)),
        DM_DATETIME => Value::DateTime(decode_datetime(raw)),
        DM_DATETIME2 => Value::DateTime(decode_datetime2(raw)),
        DM_DATETIME_TZ => Value::DateTime(decode_datetime_tz(raw)),
        DM_DATETIME2_TZ => Value::DateTime(decode_datetime2_tz(raw)),
        DM_INTERVAL_YM => Value::Interval(decode_interval_ym(raw)),
        DM_INTERVAL_DT => Value::Interval(decode_interval_dt(raw)),
        DM_BINARY | DM_VARBINARY | DM_BLOB => Value::Bytes(raw.to_vec()),
        _ => Value::Raw(raw.to_vec()),
    };
    Ok(v)
}

/// 达梦私有大数编码解码(Go o.go decodeDecimal,0xc10233=1.5 实测验证)。
///
/// 首字节: 0x80=0;&0x80≠0 → 正数 exp=flag-0xC1;否则负数 exp=0x3E-flag
/// 后续字节: 每字节 2 位十进制,正数=byte-1,负数=101-byte;越界(0/0x66)终止
/// 用字符串构造结果,避免浮点/Decimal 默认精度截断(DECIMAL(38) 需要 40 位)
pub fn decode_decimal(raw: &[u8]) -> String {
    if raw.is_empty() || raw[0] == 0x80 {
        return "0".to_string();
    }
    let (sign, exp): (i32, i32) = if raw[0] & 0x80 != 0 {
        (1, raw[0] as i32 - 0xC1)
    } else {
        (-1, 0x3E - raw[0] as i32)
    };
    let mut digits = String::new();
    for &b in &raw[1..] {
        let d = if sign > 0 { b as i32 - 1 } else { 101 - b as i32 };
        if !(0..=99).contains(&d) {
            break;
        }
        digits.push_str(&format!("{d:02}"));
    }
    if digits.is_empty() {
        return "0".to_string();
    }
    // weight = exp*2 - (len(digits)-2),与 Python 一致
    let weight = exp * 2 - (digits.len() as i32 - 2);
    let s = if weight >= 0 {
        format!("{}{}", digits, "0".repeat(weight as usize))
    } else {
        let n = digits.len() as i32 + weight;
        if n <= 0 {
            format!("0.{}{}", "0".repeat((-n) as usize), digits)
        } else {
            format!("{}.{}", &digits[..n as usize], &digits[n as usize..])
        }
    };
    let s = if sign < 0 {
        format!("-{s}")
    } else {
        s
    };
    py_decimal_str(&s)
}

/// 按 Python `Decimal.__str__` 算法格式化十进制字符串
/// (_pydecimal.py __str__:`exp<=0 且 leftdigits>-6` 用定点,否则科学计数法)。
fn py_decimal_str(s: &str) -> String {
    let (neg, body) = match s.strip_prefix('-') {
        Some(b) => (true, b),
        None => (false, s),
    };
    let (int_part, frac_part) = match body.split_once('.') {
        Some((i, f)) => (i, f),
        None => (body, ""),
    };
    let mut combined = String::with_capacity(int_part.len() + frac_part.len());
    combined.push_str(int_part);
    combined.push_str(frac_part);
    // 去掉系数前导零(保留至少一位)
    let stripped = combined.trim_start_matches('0');
    let coeff = if stripped.is_empty() { "0" } else { stripped };
    // 指数 = 小数位数(符号相反)
    let exp: i64 = -(frac_part.len() as i64);
    // Python: leftdigits = exp + len(coeff)
    let leftdigits = exp + coeff.len() as i64;
    let dotplace: i64 = if exp <= 0 && leftdigits > -6 {
        leftdigits
    } else {
        1
    };
    let (intpart, fracpart) = if dotplace <= 0 {
        (String::from("0"), format!(".{}{}", "0".repeat((-dotplace) as usize), coeff))
    } else if dotplace >= coeff.len() as i64 {
        (
            format!("{}{}", coeff, "0".repeat((dotplace - coeff.len() as i64) as usize)),
            String::new(),
        )
    } else {
        (
            coeff[..dotplace as usize].to_string(),
            format!(".{}", &coeff[dotplace as usize..]),
        )
    };
    let exp_part = if leftdigits == dotplace {
        String::new()
    } else {
        format!("E{:+}", leftdigits - dotplace)
    };
    format!("{}{}{}{}", if neg { "-" } else { "" }, intpart, fracpart, exp_part)
}

/// DATE 位压缩(3 字节):EA 07 64 → 2026-08-12
pub fn decode_date(raw: &[u8]) -> DmDate {
    let b0 = raw[0] as i32;
    let b1 = raw[1] as i32;
    let b2 = raw[2] as i32;
    let mut year = (b0 | (b1 << 8)) & 0x7FFF;
    if year > 9999 {
        year = (year | 0x8000) & 0xFFFF;
        year -= 65536;
    }
    let month = ((b1 >> 7) & 0x1) + ((b2 & 0x07) << 1);
    let day = ((b2 & 0xF8) >> 3) & 0x1F;
    DmDate {
        year,
        month: if month == 0 { 1 } else { month as u8 },
        day: if day == 0 { 1 } else { day as u8 },
    }
}

/// TIME 位压缩(5 字节)
pub fn decode_time(raw: &[u8]) -> DmTime {
    let b0 = raw[0] as u32;
    let b1 = raw[1] as u32;
    let b2 = raw[2] as u32;
    let b3 = raw[3] as u32;
    let b4 = raw[4] as u32;
    let hour = b0 & 0x1F;
    let minute = ((b0 >> 5) & 0x07) + ((b1 & 0x07) << 3);
    let second = ((b1 >> 3) & 0x1F) + ((b2 & 0x01) << 5);
    let nsec = (((b2 >> 1) & 0x7F) + (b3 << 7) + ((b4 & 0x1F) << 15)) * 1000;
    DmTime {
        hour: hour as u8,
        minute: minute as u8,
        second: second as u8,
        microsecond: nsec / 1000,
        timezone_minutes: None,
    }
}

/// TIME_TZ:TIME(5B) + int16 时区偏移(分钟,可负,Go h.go dmdtDecodeFast)
pub fn decode_time_tz(raw: &[u8]) -> DmTime {
    let mut t = decode_time(&raw[..5.min(raw.len())]);
    if raw.len() >= 7 {
        t.timezone_minutes = Some(i16::from_le_bytes([raw[5], raw[6]]));
    }
    t
}

/// DATETIME:DATE(3B) + TIME(5B) 组合
pub fn decode_datetime(raw: &[u8]) -> DmDateTime {
    if raw.len() >= 8 {
        DmDateTime {
            date: decode_date(&raw[..3]),
            time: decode_time(&raw[3..8]),
        }
    } else {
        DmDateTime {
            date: DmDate::new(1, 1, 1),
            time: DmTime::new(0, 0, 0, 0),
        }
    }
}

/// DATETIME2:DATE(3B) + TIME 6B(纳秒 30 位直接存储,非 ×1000,Go h.go DATETIME2 分支)
pub fn decode_datetime2(raw: &[u8]) -> DmDateTime {
    if raw.len() >= 9 {
        let b = |i: usize| raw[3 + i] as u32; // time 部分从 [3] 起
        let nsec = ((b(2) >> 1) & 0x7F) + (b(3) << 7) + (b(4) << 15) + ((b(5) & 0x7F) << 23);
        let date = decode_date(&raw[..3]);
        let mut time = decode_time(&raw[3..8]);
        time.microsecond = nsec / 1000;
        DmDateTime { date, time }
    } else {
        decode_datetime(raw)
    }
}

/// DATETIME_TZ:DATETIME(8B) + int16 时区偏移(分钟,可负)
pub fn decode_datetime_tz(raw: &[u8]) -> DmDateTime {
    let mut dt = decode_datetime(&raw[..8.min(raw.len())]);
    if raw.len() >= 10 {
        dt.time.timezone_minutes = Some(i16::from_le_bytes([raw[8], raw[9]]));
    }
    dt
}

/// DATETIME2_TZ:DATETIME2(9B) + int16 时区偏移(分钟,可负)
pub fn decode_datetime2_tz(raw: &[u8]) -> DmDateTime {
    let mut dt = decode_datetime2(&raw[..9.min(raw.len())]);
    if raw.len() >= 11 {
        dt.time.timezone_minutes = Some(i16::from_le_bytes([raw[9], raw[10]]));
    }
    dt
}

/// INTERVAL YEAR TO MONTH:12 字节 = [int32 年][int32 月][int32 scale][byte type]
pub fn decode_interval_ym(raw: &[u8]) -> String {
    if raw.len() < 8 {
        return String::new();
    }
    let years = le_i32(raw, 0);
    let months = le_i32(raw, 4);
    format!("{years}-{months}")
}

/// INTERVAL DAY TO SECOND:24 字节 = [int32 天][int32 时][int32 分][int32 秒][int32 微秒][...]
pub fn decode_interval_dt(raw: &[u8]) -> String {
    if raw.len() < 20 {
        return String::new();
    }
    let days = le_i32(raw, 0);
    let hours = le_i32(raw, 4);
    let minutes = le_i32(raw, 8);
    let seconds = le_i32(raw, 12);
    let frac = le_i32(raw, 16);
    format!("{days} {hours:02}:{minutes:02}:{seconds:02}.{frac:06}")
}

// ---------- 编码(参数绑定用) ----------

/// DATE 位压缩编码(3 字节,与 decode_date 互逆)
pub fn encode_date(d: &DmDate) -> [u8; 3] {
    let y = d.year;
    let m = d.month as i32;
    let dd = d.day as i32;
    [
        (y & 0xFF) as u8,
        (((y >> 8) & 0x7F) | ((m & 1) << 7)) as u8,
        (((m >> 1) & 0x07) | ((dd & 0x1F) << 3)) as u8,
    ]
}

/// TIME 位压缩编码(5 字节,与 decode_time 互逆)
pub fn encode_time(t: &DmTime) -> [u8; 5] {
    let us = t.microsecond;
    let b0 = (t.hour as u32) | ((t.minute as u32 & 0x07) << 5);
    let b1 = (t.minute as u32 >> 3) | ((t.second as u32) << 3);
    let b2 = (t.second as u32 >> 5) | ((us & 0x7F) << 1);
    let b3 = (us >> 7) & 0xFF;
    let b4 = (us >> 15) & 0x1F;
    [
        (b0 & 0xFF) as u8,
        (b1 & 0xFF) as u8,
        (b2 & 0xFF) as u8,
        b3 as u8,
        b4 as u8,
    ]
}

/// DATETIME:DATE 位压缩 + TIME 位压缩(8 字节)
pub fn encode_datetime(dt: &DmDateTime) -> Vec<u8> {
    let mut out = encode_date(&dt.date).to_vec();
    out.extend_from_slice(&encode_time(&dt.time));
    out
}

/// TIME_TZ 编码:TIME(5B) + int16 时区偏移(分钟,小端,与 decode_time_tz 互逆)
pub fn encode_time_tz(t: &DmTime) -> Vec<u8> {
    let mut out = encode_time(t).to_vec();
    out.extend_from_slice(&t.timezone_minutes.unwrap_or(0).to_le_bytes());
    out
}

/// DATETIME_TZ 编码:DATETIME(8B) + int16 时区偏移(分钟,小端,与 decode_datetime_tz 互逆)
pub fn encode_datetime_tz(dt: &DmDateTime) -> Vec<u8> {
    let mut out = encode_datetime(dt);
    out.extend_from_slice(&dt.time.timezone_minutes.unwrap_or(0).to_le_bytes());
    out
}

/// DATETIME2 编码:DATE(3B) + TIME 6B(纳秒 30 位直接存储,非 ×1000;与 decode_datetime2 互逆)
/// 时间部分 b0/b1/b2-低位 与 5 字节 TIME 一致,b2 高 7 位/b3/b4/b5 存纳秒:
/// nsec = (b2>>1 &0x7F) + (b3<<7) + (b4<<15) + (b5&0x7F)<<23
pub fn encode_datetime2(dt: &DmDateTime) -> Vec<u8> {
    let mut out = encode_date(&dt.date).to_vec();
    let t = &dt.time;
    let nsec = (t.microsecond as u64) * 1000;
    let b0 = (t.hour as u32) | ((t.minute as u32 & 0x07) << 5);
    let b1 = (t.minute as u32 >> 3) | ((t.second as u32) << 3);
    let b2 = (t.second as u32 >> 5) | (((nsec & 0x7F) as u32) << 1);
    out.extend_from_slice(&[
        b0 as u8,
        b1 as u8,
        b2 as u8,
        ((nsec >> 7) & 0xFF) as u8,
        ((nsec >> 15) & 0xFF) as u8,
        ((nsec >> 23) & 0x7F) as u8,
    ]);
    out
}

/// DATETIME2_TZ 编码:DATETIME2(9B) + int16 时区偏移(分钟,小端,与 decode_datetime2_tz 互逆)
pub fn encode_datetime2_tz(dt: &DmDateTime) -> Vec<u8> {
    let mut out = encode_datetime2(dt);
    out.extend_from_slice(&dt.time.timezone_minutes.unwrap_or(0).to_le_bytes());
    out
}

// ---------- 参数/输出值类型转换 ----------

/// OUT 参数值解码(cmd=187 响应)
pub fn decode_out_value(raw: &[u8], col_type: i32, enc: &'static Encoding) -> Value {
    match col_type {
        5..=8 => Value::BigInt(le_i64(raw)),
        10 => Value::Real(f32::from_le_bytes(raw[..4].try_into().unwrap())),
        11 => Value::Double(f64::from_le_bytes(raw[..8].try_into().unwrap())),
        14 => Value::Date(decode_date(raw)),
        15 => Value::Time(decode_time(raw)),
        16 => Value::DateTime(decode_datetime(raw)),
        2 | 19 => {
            let (s, _, _) = enc.decode(raw);
            Value::Str(s.into_owned())
        }
        12 | 17 | 18 => Value::Bytes(raw.to_vec()),
        _ => Value::Raw(raw.to_vec()),
    }
}

fn le_i64(b: &[u8]) -> i64 {
    let mut buf = [0u8; 8];
    let n = b.len().min(8);
    buf[..n].copy_from_slice(&b[..n]);
    i64::from_le_bytes(buf)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn date_decode_reference() {
        let d = decode_date(&[0xEA, 0x07, 0x64]);
        assert_eq!(d, DmDate::new(2026, 8, 12));
        // 最小日期
        let d = decode_date(&encode_date(&DmDate::new(1, 1, 1)));
        assert_eq!(d, DmDate::new(1, 1, 1));
    }

    #[test]
    fn date_encode_reference() {
        assert_eq!(encode_date(&DmDate::new(2026, 8, 12)), [0xEA, 0x07, 0x64]);
        assert_eq!(encode_date(&DmDate::new(1999, 2, 28)), [0xCF, 0x07, 0xE1]);
    }

    #[test]
    fn time_roundtrip_reference() {
        // Python 参考向量
        let t = decode_time(&[0x77, 0xDF, 0x7F, 0x84, 0x1E]);
        assert_eq!(t, DmTime::new(23, 59, 59, 999999));
        let t2 = decode_time(&[0x4C, 0xC4, 0x11, 0x14, 0x18]);
        assert_eq!(t2, DmTime::new(12, 34, 56, 789000));
        // 编码互逆
        for t in [
            DmTime::new(23, 59, 59, 999999),
            DmTime::new(12, 34, 56, 789000),
            DmTime::new(0, 0, 0, 0),
        ] {
            assert_eq!(decode_time(&encode_time(&t)), t);
        }
    }

    #[test]
    fn datetime_roundtrip() {
        let dt = DmDateTime::new(DmDate::new(2030, 6, 15), DmTime::new(8, 9, 10, 111222));
        let b = encode_datetime(&dt);
        assert_eq!(b, vec![0xEE, 0x07, 0x7B, 0x28, 0x51, 0xEC, 0x64, 0x03]);
        assert_eq!(decode_datetime(&b), dt);
        // 最大 datetime
        let max = DmDateTime::new(DmDate::new(9999, 12, 31), DmTime::new(23, 59, 59, 999999));
        assert_eq!(encode_datetime(&max), vec![0x0F, 0x27, 0xFE, 0x77, 0xDF, 0x7F, 0x84, 0x1E]);
        assert_eq!(decode_datetime(&encode_datetime(&max)), max);
    }

    #[test]
    fn decimal_decode_reference() {
        // Python 参考:0xc10233 → 1.50
        assert_eq!(decode_decimal(&[0xC1, 0x02, 0x33]), "1.50");
        assert_eq!(decode_decimal(&[0x80]), "0");
        assert_eq!(decode_decimal(&[0xC1, 0x00]), "0");
        assert_eq!(decode_decimal(&[0xC0, 0x01]), "0.00");
        // 负数
        assert_eq!(decode_decimal(&[0x3D, 0x02, 0x33]), "-9950");
        assert_eq!(decode_decimal(&[0x3E, 0x63, 0x66]), "-2");
    }

    #[test]
    fn interval_decode() {
        let mut b = Vec::new();
        for v in [1i32, 2, 0] {
            b.extend_from_slice(&v.to_le_bytes());
        }
        b.push(0);
        assert_eq!(decode_interval_ym(&b), "1-2");

        let mut b = Vec::new();
        for v in [1i32, 2, 3, 4, 567890, 0] {
            b.extend_from_slice(&v.to_le_bytes());
        }
        b.push(0);
        assert_eq!(decode_interval_dt(&b), "1 02:03:04.567890");
    }

    #[test]
    fn parse_columns_real_capture() {
        // 抓包实测(select 1 响应 cmd=187):INT 列(列名为字面量 "1")
        let body = [
            0x07, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
            0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x07, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x31, 0x49, 0x4e, 0x54, 0x45, 0x47, 0x45, 0x52, 0x00, 0x00,
            0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x04, 0x00,
            0x01, 0x00, 0x00, 0x00,
        ];
        let enc: &'static Encoding = encoding_rs::GB18030;
        let (cols, pos) = parse_columns(&body, 0, 1, enc).unwrap();
        assert_eq!(cols.len(), 1);
        assert_eq!(cols[0].col_type, DM_INT);
        assert_eq!(cols[0].prec, 4);
        assert_eq!(cols[0].name, "1");
        assert_eq!(cols[0].type_name, "INTEGER");
        assert_eq!(cols[0].table_name, "");
        assert_eq!(cols[0].schema_name, "");
        // 行数据
        let rows = parse_rows(&body, pos, 1, 1).unwrap();
        assert_eq!(rows, vec![vec![Some(vec![1, 0, 0, 0])]]);
    }

    #[test]
    fn parse_rows_param_real_capture() {
        // 抓包实测(my_lob SELECT content,1 列 CLOB 行内值 83 字节)
        let body = hex2b(
            "610002000000000000000c00530001020403000000000028000000000000000000000002040000010002000000000000000000000000000000d5e2cac74c4f42b2e2cad4c4dac8dda3acb0fcbaacd6d0cec4bacd456e676c697368bbecbacfa1a3",
        );
        let rows = parse_rows_param(&body, 0, 1).unwrap();
        assert_eq!(rows.len(), 1);
        let cell = rows[0][0].as_ref().unwrap();
        assert_eq!(cell.len(), 83);
        assert_eq!(cell[0], 0x01); // inRow 标志
        // 长度 @9 = 40
        assert_eq!(u32::from_le_bytes(cell[9..13].try_into().unwrap()), 40);
        // 数据 @43:"这是LOB测试内容,包含中文和English混合。"
        let (s, _, _) = encoding_rs::GB18030.decode(&cell[43..]);
        assert_eq!(s, "这是LOB测试内容,包含中文和English混合。");
    }

    fn hex2b(s: &str) -> Vec<u8> {
        (0..s.len())
            .step_by(2)
            .map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap())
            .collect()
    }

    #[test]
    fn gb18030_scalar_decode() {
        let enc: &'static Encoding = encoding_rs::GB18030;
        let (s, _, _) = enc.decode(&[0xb2, 0xe2, 0xca, 0xd4]);
        assert_eq!(s, "测试");
        // GB18030 4 字节字符 U+20000
        let (s, _, _) = enc.decode(&[0x95, 0x32, 0x82, 0x36]);
        assert_eq!(s, "\u{20000}");
    }

    #[test]
    fn tz_and_datetime2_decode() {
        // 向量由 Python 参考实现生成(dmpy._enc_time/_enc_datetime/_enc_date)
        // TIME_TZ:TIME(5B) + int16 时区(分钟,东八区 +480 → e0 01)
        let t = decode_time_tz(&hex2b("4cc4111418e001"));
        assert_eq!((t.hour, t.minute, t.second, t.microsecond), (12, 34, 56, 789000));
        assert_eq!(t.timezone_minutes, Some(480));
        assert!(t.to_string().ends_with("+08:00"), "{}", t);
        let t2 = decode_time_tz(&hex2b("4cc4111418d4fe"));
        assert_eq!(t2.timezone_minutes, Some(-300));
        assert!(t2.to_string().ends_with("-05:00"), "{}", t2);

        // DATETIME_TZ:DATETIME(8B) + int16 时区
        let dt = decode_datetime_tz(&hex2b("ea07744cc4111418e001"));
        assert_eq!((dt.date.year, dt.date.month, dt.date.day), (2026, 8, 14));
        assert_eq!(dt.time.hour, 12);
        assert_eq!(dt.time.timezone_minutes, Some(480));
        assert!(dt.to_string().ends_with("+08:00"), "{}", dt);

        // DATETIME2:9B,纳秒 30 位直接存储(789000us = 789_000_000ns)
        let dt2 = decode_datetime2(&hex2b("ea07744cc4815e0e5e"));
        assert_eq!(dt2.date.year, 2026);
        assert_eq!(dt2.time.microsecond, 789000);
        assert_eq!(dt2.time.timezone_minutes, None);

        // DATETIME2_TZ:11B = DATETIME2(9B) + tz
        let dtz = decode_datetime2_tz(&hex2b("ea07744cc4815e0e5ee001"));
        assert_eq!(dtz.time.microsecond, 789000);
        assert_eq!(dtz.time.timezone_minutes, Some(480));
        assert!(dtz.to_string().ends_with("+08:00"));
    }

    #[test]
    fn tz_and_datetime2_encode_roundtrip() {
        // TIME_TZ:TIME(5B) + int16 时区(东八区 +480 → e0 01)
        let mut t = DmTime::new(12, 34, 56, 789000);
        t.timezone_minutes = Some(480);
        assert_eq!(encode_time_tz(&t), hex2b("4cc4111418e001"));
        assert_eq!(decode_time_tz(&encode_time_tz(&t)), t);

        // DATETIME_TZ:DATETIME(8B) + int16 时区
        let mut dt = DmDateTime::new(DmDate::new(2026, 8, 14), DmTime::new(12, 34, 56, 789000));
        dt.time.timezone_minutes = Some(-300);
        assert_eq!(encode_datetime_tz(&dt), hex2b("ea07744cc4111418d4fe"));
        assert_eq!(decode_datetime_tz(&encode_datetime_tz(&dt)), dt);

        // DATETIME2:9B 纳秒 30 位直接存储(789000us = 789_000_000ns)
        let dt2 = DmDateTime::new(DmDate::new(2026, 8, 14), DmTime::new(12, 34, 56, 789000));
        assert_eq!(encode_datetime2(&dt2), hex2b("ea07744cc4815e0e5e"));
        assert_eq!(decode_datetime2(&encode_datetime2(&dt2)), dt2);

        // DATETIME2_TZ:11B = DATETIME2(9B) + tz
        let mut dtz = dt2;
        dtz.time.timezone_minutes = Some(480);
        assert_eq!(encode_datetime2_tz(&dtz), hex2b("ea07744cc4815e0e5ee001"));
        assert_eq!(decode_datetime2_tz(&encode_datetime2_tz(&dtz)), dtz);

        // 无时区:TZ 编码退化为偏移 0
        let plain = DmTime::new(1, 2, 3, 0);
        assert_eq!(encode_time_tz(&plain).len(), 7);
        assert_eq!(&encode_time_tz(&plain)[5..], &[0u8, 0]);
    }

    #[test]
    fn dml_affected_i64() {
        // 影响行数在帧参数区偏移 24(opts[4..12])为 int64,>65535 不截断
        // (client.rs 的 le_i64(b, pos) 为带偏移版本;此处验证其语义)
        let o = [0u8; 44];
        let _ = &o;
        assert_eq!(le_i64(&100_000i64.to_le_bytes()), 100_000);
    }
}