Skip to main content

dm_pure/
lib.rs

1//! # dm-pure:纯 Rust 达梦 DM8 数据库驱动
2//!
3//! 纯 Rust 实现的达梦 DM8 驱动,零数据库外部依赖。实现 DM 网络协议(握手 / 登录 /
4//! 执行 / 参数绑定 / LOB / 事务 / 分批 fetch)。
5//!
6//! - crates.io: <https://crates.io/crates/dm-pure>
7//! - 源码仓库: <https://github.com/zhangtao103239/dm8_pure_rs>
8//!
9//! ```no_run
10//! use dm_pure::{DmClient, Param};
11//!
12//! let mut c = DmClient::new("127.0.0.1", 5236, "SYSDBA", "password");
13//! c.connect()?;
14//! // 查询(参数化)
15//! let r = c.execute("SELECT id, name FROM t WHERE id >= ?", &[Param::Int(1)])?;
16//! if let dm_pure::ExecuteResult::Query { columns, rows } = r {
17//!     println!("cols: {:?}, first row: {:?}", columns, rows.first());
18//! }
19//! // 写入
20//! c.execute("INSERT INTO t VALUES (?, ?)", &[Param::Int(1), Param::Str("x".into())])?;
21//! # Ok::<(), dm_pure::DmError>(())
22//! ```
23//!
24//! 更上层的 `Cursor`(DB-API 风格惰性游标)与连接池见 [`cursor`] / [`pool`] 模块。
25
26pub mod client;
27pub mod crypto;
28pub mod cursor;
29pub mod frame;
30pub mod pool;
31pub mod result;
32pub mod transaction;
33
34pub use client::{ClientConfig, ExecuteResult, DmClient, OUT, OutType, Param, ServerParam};
35pub use cursor::Cursor;
36pub use pool::{Connection, ConnectionPool, PooledConnection};
37pub use result::{Column, DmDate, DmDateTime, DmTime, Value};
38pub use transaction::Transaction;
39
40use std::fmt;
41
42/// 达梦驱动错误。`code` 为服务器返回码(0 表示非协议错误),`msg` 为描述信息。
43#[derive(Debug, Clone)]
44pub struct DmError {
45    pub code: i32,
46    pub msg: String,
47}
48
49impl DmError {
50    pub fn new(code: i32, msg: impl Into<String>) -> Self {
51        DmError { code, msg: msg.into() }
52    }
53
54    /// 服务器返回码错误(code != 0)
55    pub fn server(code: i32, msg: impl Into<String>) -> Self {
56        DmError { code, msg: msg.into() }
57    }
58
59    /// 连接层错误(网络断开 / 超时等)
60    pub fn conn(msg: impl Into<String>) -> Self {
61        DmError { code: -6852, msg: msg.into() }
62    }
63
64    /// 协议 / 解析错误
65    pub fn protocol(msg: impl Into<String>) -> Self {
66        DmError { code: -6001, msg: msg.into() }
67    }
68
69    /// 参数错误
70    pub fn arg(msg: impl Into<String>) -> Self {
71        DmError { code: -6119, msg: msg.into() }
72    }
73}
74
75impl fmt::Display for DmError {
76    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77        let desc = dm_error_desc(self.code);
78        if desc.is_empty() {
79            write!(f, "[CODE:{}] {}", self.code, self.msg)
80        } else {
81            write!(f, "[CODE:{}] ({}) {}", self.code, desc, self.msg)
82        }
83    }
84}
85
86impl std::error::Error for DmError {}
87
88impl From<std::io::Error> for DmError {
89    fn from(e: std::io::Error) -> Self {
90        DmError::conn(e.to_string())
91    }
92}
93
94/// 常见达梦错误码描述表(与 Python 驱动一致)
95pub fn dm_error_desc(code: i32) -> &'static str {
96    match code {
97        -7003 => "无效的语句句柄",
98        -6119 => "无效的数据类型",
99        -2106 => "对象不存在",
100        -2104 | -2124 => "对象已存在",
101        -6804 => "需要更多的参数",
102        -2111 => "语法错误",
103        -2007 => "语法分析出错",
104        -6852 => "网络异常",
105        -6002 => "缓冲区溢出",
106        -6001 => "内部错误",
107        100 => "通用错误",
108        _ => "",
109    }
110}
111
112/// 统一 Result 别名
113pub type Result<T> = std::result::Result<T, DmError>;