pub mod client;
pub mod crypto;
pub mod cursor;
pub mod frame;
pub mod pool;
pub mod result;
pub mod transaction;
pub use client::{ClientConfig, ExecuteResult, DmClient, OUT, OutType, Param, ServerParam};
pub use cursor::Cursor;
pub use pool::{Connection, ConnectionPool, PooledConnection};
pub use result::{Column, DmDate, DmDateTime, DmTime, Value};
pub use transaction::Transaction;
use std::fmt;
#[derive(Debug, Clone)]
pub struct DmError {
pub code: i32,
pub msg: String,
}
impl DmError {
pub fn new(code: i32, msg: impl Into<String>) -> Self {
DmError { code, msg: msg.into() }
}
pub fn server(code: i32, msg: impl Into<String>) -> Self {
DmError { code, msg: msg.into() }
}
pub fn conn(msg: impl Into<String>) -> Self {
DmError { code: -6852, msg: msg.into() }
}
pub fn protocol(msg: impl Into<String>) -> Self {
DmError { code: -6001, msg: msg.into() }
}
pub fn arg(msg: impl Into<String>) -> Self {
DmError { code: -6119, msg: msg.into() }
}
}
impl fmt::Display for DmError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let desc = dm_error_desc(self.code);
if desc.is_empty() {
write!(f, "[CODE:{}] {}", self.code, self.msg)
} else {
write!(f, "[CODE:{}] ({}) {}", self.code, desc, self.msg)
}
}
}
impl std::error::Error for DmError {}
impl From<std::io::Error> for DmError {
fn from(e: std::io::Error) -> Self {
DmError::conn(e.to_string())
}
}
pub fn dm_error_desc(code: i32) -> &'static str {
match code {
-7003 => "无效的语句句柄",
-6119 => "无效的数据类型",
-2106 => "对象不存在",
-2104 | -2124 => "对象已存在",
-6804 => "需要更多的参数",
-2111 => "语法错误",
-2007 => "语法分析出错",
-6852 => "网络异常",
-6002 => "缓冲区溢出",
-6001 => "内部错误",
100 => "通用错误",
_ => "",
}
}
pub type Result<T> = std::result::Result<T, DmError>;