Crates.io | sqlstate |
lib.rs | sqlstate |
version | 0.1.0 |
source | src |
created_at | 2021-08-29 01:39:31.975451 |
updated_at | 2021-09-19 22:26:49.781133 |
description | Representations and parsing logic for SQLSTATE return codes. |
homepage | |
repository | https://github.com/lucis-fluxum/sqlstate-rs |
max_upload_size | |
id | 443611 |
size | 49,673 |
Representations and parsing logic for SQLSTATE
return codes.
Parsing return codes according to the SQL standard:
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")));
Examining the pieces of a return code:
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")));
Parsing return codes specific to 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)));
postgres
: Enables PostgreSQL-specific types.