2 个不稳定版本
0.1.0 | 2021 年 9 月 19 日 |
---|---|
0.0.0 | 2021 年 8 月 29 日 |
#1128 在 数据库接口
每月 52 次下载
51KB
1.5K SLoC
SQLSTATE 返回代码的表示和解析逻辑。
示例
根据 SQL 标准解析返回代码
use sqlstate::standard::{
class::{DataException::DivisionByZero, Warning::PrivilegeNotGranted},
SqlState,
};
assert_eq!("00000".parse::<SqlState>()?, SqlState::Success(None));
assert_eq!("01007".parse::<SqlState>()?, SqlState::Warning(Some(PrivilegeNotGranted)));
// Unknown codes are represented as `Other`
assert_eq!("XX001".parse::<SqlState>()?, SqlState::Other(String::from("XX001")));
检查返回代码的各个部分
use sqlstate::standard::{class::Warning::PrivilegeNotGranted, SqlState};
let success = SqlState::Success(None);
let warning = SqlState::Warning(Some(PrivilegeNotGranted));
assert_eq!((success.class(), success.subclass()), ("00", None));
assert_eq!((warning.class(), warning.subclass()), ("01", Some("007")));
解析 PostgreSQL 的特定返回代码
use sqlstate::{
postgres::{
class::{
DataException::InvalidJsonText, InternalError::DataCorrupted,
OperatorIntervention::CrashShutdown,
},
SqlState::*,
},
standard,
PostgresSqlState,
};
assert_eq!("22032".parse::<PostgresSqlState>()?,
PostgresSqlState::Custom(DataException(Some(InvalidJsonText))));
assert_eq!("XX001".parse::<PostgresSqlState>()?,
PostgresSqlState::Custom(InternalError(Some(DataCorrupted))));
// Can also fall back to standard codes
assert_eq!("00000".parse::<PostgresSqlState>()?,
PostgresSqlState::Standard(standard::SqlState::Success(None)));
特性
- MIT 许可证
51KB
1.5K SLoC
SQLSTATE 返回代码的表示和解析逻辑。