dameng_rust_sdk 0.1.3

A Rust SDK for Dameng Database (DM8) with ODBC support
Documentation
//! Utility functions for Dameng Rust SDK

use chrono::{NaiveDate, NaiveDateTime};
use encoding_rs::GBK;

use crate::error::{Error, Result as SdkResult};

/// Convert GBK encoded bytes to UTF-8 string
pub fn gbk_to_utf8(bytes: &[u8]) -> SdkResult<String> {
    let (decoded, _, had_errors) = GBK.decode(bytes);
    if had_errors {
        return Err(Error::Encoding("Failed to decode GBK string".to_string()));
    }
    Ok(decoded.to_string())
}

/// Convert UTF-8 string to GBK encoded bytes
pub fn utf8_to_gbk(s: &str) -> SdkResult<Vec<u8>> {
    let (encoded, _, had_errors) = GBK.encode(s);
    if had_errors {
        return Err(Error::Encoding("Failed to encode string to GBK".to_string()));
    }
    Ok(encoded.into_owned())
}

/// Parse date string from Dameng database
pub fn parse_dameng_date(s: &str) -> SdkResult<NaiveDate> {
    NaiveDate::parse_from_str(s, "%Y-%m-%d")
        .map_err(|e| Error::Parse(e))
}

/// Parse datetime string from Dameng database
pub fn parse_dameng_datetime(s: &str) -> SdkResult<NaiveDateTime> {
    NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S")
        .map_err(|e| Error::Parse(e))
}

/// Format date for Dameng database
pub fn format_dameng_date(date: &NaiveDate) -> String {
    date.format("%Y-%m-%d").to_string()
}

/// Format datetime for Dameng database
pub fn format_dameng_datetime(datetime: &NaiveDateTime) -> String {
    datetime.format("%Y-%m-%d %H:%M:%S").to_string()
}

/// Escape SQL string
pub fn escape_sql_string(s: &str) -> String {
    s.replace('\'', "''")
}