use encoding_rs::Encoding;
use crate::Result;
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;
pub const NULL_MARK: u16 = 0xFFFE;
pub const LOB_MARK: u16 = 0xFFFF;
#[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)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DmTime {
pub hour: u8,
pub minute: u8,
pub second: u8,
pub microsecond: u32,
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(())
}
}
#[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(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,
}
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()
}
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);
}
}
}
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);
}
}
}
let (cols, _) = parse_columns(body, n_params * 32, ncols, enc)?;
Ok(cols)
}
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; 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);
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 {
pos += 6;
}
cols.push(Column {
col_type,
prec,
scale,
nullable,
name,
type_name,
table_name,
schema_name,
lob,
});
}
Ok((cols, pos))
}
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; 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 => {
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)
}
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;
if rlen == 0 || row_end <= pos + 2 || row_end > body.len() {
break; }
let mut p = pos + 2 + 2 + 6; 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)
}
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)
}
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();
}
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)
}
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);
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)
}
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 },
}
}
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,
}
}
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
}
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),
}
}
}
pub fn decode_datetime2(raw: &[u8]) -> DmDateTime {
if raw.len() >= 9 {
let b = |i: usize| raw[3 + i] as u32; 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)
}
}
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
}
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
}
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}")
}
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}")
}
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,
]
}
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,
]
}
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
}
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
}
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
}
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
}
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
}
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() {
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);
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() {
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() {
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() {
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); assert_eq!(u32::from_le_bytes(cell[9..13].try_into().unwrap()), 40);
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, "测试");
let (s, _, _) = enc.decode(&[0x95, 0x32, 0x82, 0x36]);
assert_eq!(s, "\u{20000}");
}
#[test]
fn tz_and_datetime2_decode() {
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);
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);
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);
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() {
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);
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);
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);
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);
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() {
let o = [0u8; 44];
let _ = &o;
assert_eq!(le_i64(&100_000i64.to_le_bytes()), 100_000);
}
}