1pub 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#[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 pub fn server(code: i32, msg: impl Into<String>) -> Self {
56 DmError { code, msg: msg.into() }
57 }
58
59 pub fn conn(msg: impl Into<String>) -> Self {
61 DmError { code: -6852, msg: msg.into() }
62 }
63
64 pub fn protocol(msg: impl Into<String>) -> Self {
66 DmError { code: -6001, msg: msg.into() }
67 }
68
69 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
94pub 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
112pub type Result<T> = std::result::Result<T, DmError>;