Skip to main content

sqlstate/
lib.rs

1#![doc = include_str!("../README.md")]
2#![no_std]
3#![warn(
4    unreachable_pub,
5    missing_debug_implementations,
6    missing_copy_implementations,
7    missing_docs,
8    clippy::unwrap_used
9)]
10#![cfg_attr(test, allow(clippy::unwrap_used))]
11
12use core::error::Error as StdError;
13use core::fmt;
14use core::str::FromStr;
15
16macro_rules! codes {
17    ($($(#[$doc:meta])* $name:ident = $code:literal;)*) => {
18        $(
19            $(#[$doc])*
20            pub const $name: $crate::SqlState =
21                $crate::SqlState::new(*$code).expect("invalid SQLSTATE");
22        )*
23    };
24}
25
26pub mod mysql_like;
27pub mod pg;
28
29/// A [SQLSTATE](https://en.wikipedia.org/wiki/SQLSTATE) error code, including
30/// custom codes outside the standard, usable as a `match` pattern.
31///
32/// ```rust
33/// use sqlstate::{SqlState, SqlStateClass};
34///
35/// const CUSTOM: SqlState = SqlState::new(*b"P1001").unwrap();
36///
37/// let code: SqlState = "P1001".parse().unwrap();
38/// assert!(matches!(code, CUSTOM));
39/// assert_eq!(code.as_str(), "P1001");
40/// assert_eq!(code.class().as_str(), "P1");
41/// assert!(!code.class().is_standard());
42/// assert_eq!(code.subclass(), "001");
43///
44/// let code: SqlState = "23505".parse().unwrap();
45/// assert_eq!(code.class(), SqlStateClass::INTEGRITY_CONSTRAINT_VIOLATION);
46/// ```
47#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
48pub struct SqlState([u8; 5]);
49
50impl SqlState {
51    /// Returns `None` unless every byte is an ASCII digit or uppercase letter.
52    pub const fn new(code: [u8; 5]) -> Option<Self> {
53        let mut i = 0;
54        while i < code.len() {
55            if !matches!(code[i], b'0'..=b'9' | b'A'..=b'Z') {
56                return None;
57            }
58            i += 1;
59        }
60        Some(Self(code))
61    }
62
63    /// The five character code.
64    pub fn as_str(&self) -> &str {
65        core::str::from_utf8(&self.0).expect("a SQLSTATE consists of ASCII bytes")
66    }
67
68    /// The [`SqlStateClass`], made of the first two characters.
69    pub fn class(&self) -> SqlStateClass {
70        SqlStateClass([self.0[0], self.0[1]])
71    }
72
73    /// The subclass, made of the last three characters, where `000` means none.
74    pub fn subclass(&self) -> &str {
75        &self.as_str()[2..]
76    }
77}
78
79impl fmt::Debug for SqlState {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        f.debug_tuple("SqlState").field(&self.as_str()).finish()
82    }
83}
84
85impl fmt::Display for SqlState {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        f.write_str(self.as_str())
88    }
89}
90
91impl FromStr for SqlState {
92    type Err = InvalidSqlState;
93
94    fn from_str(s: &str) -> Result<Self, Self::Err> {
95        <[u8; 5]>::try_from(s.as_bytes())
96            .ok()
97            .and_then(Self::new)
98            .ok_or(InvalidSqlState)
99    }
100}
101
102/// The error parsing a [`SqlState`] from a string that is not five characters
103/// from `0`-`9` and `A`-`Z`.
104#[derive(Debug, Clone, Copy, PartialEq, Eq)]
105pub struct InvalidSqlState;
106
107impl fmt::Display for InvalidSqlState {
108    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109        f.write_str("a SQLSTATE consists of five characters from `0`-`9` and `A`-`Z`")
110    }
111}
112
113impl StdError for InvalidSqlState {}
114
115/// The class of a [`SqlState`], with a constant for each class
116/// [SQL:2011](https://en.wikipedia.org/wiki/SQLSTATE) defines.
117#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
118pub struct SqlStateClass([u8; 2]);
119
120impl SqlStateClass {
121    /// The two character code of the class.
122    pub fn as_str(&self) -> &str {
123        core::str::from_utf8(&self.0).expect("a SQLSTATE consists of ASCII bytes")
124    }
125
126    /// Whether the class starts with `0`-`4` or `A`-`H`, which the SQL standard reserves.
127    pub fn is_standard(&self) -> bool {
128        matches!(self.0[0], b'0'..=b'4' | b'A'..=b'H')
129    }
130}
131
132impl fmt::Debug for SqlStateClass {
133    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134        f.debug_tuple("SqlStateClass")
135            .field(&self.as_str())
136            .finish()
137    }
138}
139
140impl fmt::Display for SqlStateClass {
141    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142        f.write_str(self.as_str())
143    }
144}
145
146macro_rules! sql_state_classes {
147    ($($name:ident = $code:literal, $condition:literal;)*) => {
148        impl SqlStateClass {
149            $(
150                #[doc = concat!("`", $code, "`, ", $condition, ".")]
151                pub const $name: Self = {
152                    let code = $code.as_bytes();
153                    Self([code[0], code[1]])
154                };
155            )*
156        }
157    };
158}
159
160sql_state_classes! {
161    SUCCESSFUL_COMPLETION = "00", "successful completion";
162    WARNING = "01", "warning";
163    NO_DATA = "02", "no data";
164    DYNAMIC_SQL_ERROR = "07", "dynamic SQL error";
165    CONNECTION_EXCEPTION = "08", "connection exception";
166    TRIGGERED_ACTION_EXCEPTION = "09", "triggered action exception";
167    FEATURE_NOT_SUPPORTED = "0A", "feature not supported";
168    INVALID_TARGET_TYPE_SPECIFICATION = "0D", "invalid target type specification";
169    INVALID_SCHEMA_NAME_LIST_SPECIFICATION = "0E", "invalid schema name list specification";
170    LOCATOR_EXCEPTION = "0F", "locator exception";
171    RESIGNAL_WHEN_HANDLER_NOT_ACTIVE = "0K", "resignal when handler not active";
172    INVALID_GRANTOR = "0L", "invalid grantor";
173    INVALID_SQL_INVOKED_PROCEDURE_REFERENCE = "0M", "invalid SQL-invoked procedure reference";
174    SQL_XML_MAPPING_ERROR = "0N", "SQL/XML mapping error";
175    INVALID_ROLE_SPECIFICATION = "0P", "invalid role specification";
176    INVALID_TRANSFORM_GROUP_NAME_SPECIFICATION = "0S", "invalid transform group name specification";
177    TARGET_TABLE_DISAGREES_WITH_CURSOR_SPECIFICATION = "0T", "target table disagrees with cursor specification";
178    ATTEMPT_TO_ASSIGN_TO_NON_UPDATABLE_COLUMN = "0U", "attempt to assign to non-updatable column";
179    ATTEMPT_TO_ASSIGN_TO_ORDERING_COLUMN = "0V", "attempt to assign to ordering column";
180    PROHIBITED_STATEMENT_ENCOUNTERED_DURING_TRIGGER_EXECUTION = "0W", "prohibited statement encountered during trigger execution";
181    INVALID_FOREIGN_SERVER_SPECIFICATION = "0X", "invalid foreign server specification";
182    PASS_THROUGH_SPECIFIC_CONDITION = "0Y", "pass-through specific condition";
183    DIAGNOSTICS_EXCEPTION = "0Z", "diagnostics exception";
184    XQUERY_ERROR = "10", "XQuery error";
185    CASE_NOT_FOUND_FOR_CASE_STATEMENT = "20", "case not found for case statement";
186    CARDINALITY_VIOLATION = "21", "cardinality violation";
187    DATA_EXCEPTION = "22", "data exception";
188    INTEGRITY_CONSTRAINT_VIOLATION = "23", "integrity constraint violation";
189    INVALID_CURSOR_STATE = "24", "invalid cursor state";
190    INVALID_TRANSACTION_STATE = "25", "invalid transaction state";
191    INVALID_SQL_STATEMENT_NAME = "26", "invalid SQL statement name";
192    TRIGGERED_DATA_CHANGE_VIOLATION = "27", "triggered data change violation";
193    INVALID_AUTHORIZATION_SPECIFICATION = "28", "invalid authorization specification";
194    DEPENDENT_PRIVILEGE_DESCRIPTORS_STILL_EXIST = "2B", "dependent privilege descriptors still exist";
195    INVALID_CHARACTER_SET_NAME = "2C", "invalid character set name";
196    INVALID_TRANSACTION_TERMINATION = "2D", "invalid transaction termination";
197    INVALID_CONNECTION_NAME = "2E", "invalid connection name";
198    SQL_ROUTINE_EXCEPTION = "2F", "SQL routine exception";
199    INVALID_COLLATION_NAME = "2H", "invalid collation name";
200    INVALID_SQL_STATEMENT_IDENTIFIER = "30", "invalid SQL statement identifier";
201    INVALID_SQL_DESCRIPTOR_NAME = "33", "invalid SQL descriptor name";
202    INVALID_CURSOR_NAME = "34", "invalid cursor name";
203    INVALID_CONDITION_NUMBER = "35", "invalid condition number";
204    CURSOR_SENSITIVITY_EXCEPTION = "36", "cursor sensitivity exception";
205    EXTERNAL_ROUTINE_EXCEPTION = "38", "external routine exception";
206    EXTERNAL_ROUTINE_INVOCATION_EXCEPTION = "39", "external routine invocation exception";
207    SAVEPOINT_EXCEPTION = "3B", "savepoint exception";
208    AMBIGUOUS_CURSOR_NAME = "3C", "ambiguous cursor name";
209    INVALID_CATALOG_NAME = "3D", "invalid catalog name";
210    INVALID_SCHEMA_NAME = "3F", "invalid schema name";
211    TRANSACTION_ROLLBACK = "40", "transaction rollback";
212    SYNTAX_ERROR_OR_ACCESS_RULE_VIOLATION = "42", "syntax error or access rule violation";
213    WITH_CHECK_OPTION_VIOLATION = "44", "with check option violation";
214    UNHANDLED_USER_DEFINED_EXCEPTION = "45", "unhandled user-defined exception";
215    OLB_SPECIFIC_ERROR = "46", "OLB-specific error";
216    FDW_SPECIFIC_CONDITION = "HV", "FDW-specific condition";
217    DATALINK_EXCEPTION = "HW", "datalink exception";
218    CLI_SPECIFIC_CONDITION = "HY", "CLI-specific condition";
219    REMOTE_DATABASE_ACCESS = "HZ", "remote database access";
220}
221
222#[cfg(test)]
223mod tests {
224    use super::*;
225
226    #[test]
227    fn new_accepts_only_digits_and_uppercase_letters() {
228        assert!(SqlState::new(*b"09AZ0").is_some());
229        for invalid in [b'/', b':', b'@', b'[', b'a', b'z', b' ', 0, 0xC3] {
230            let mut code = *b"23505";
231            code[4] = invalid;
232            assert_eq!(SqlState::new(code), None, "byte {invalid:#04x}");
233        }
234    }
235
236    #[test]
237    fn from_str_requires_exactly_five_bytes() {
238        assert_eq!("23505".parse::<SqlState>().unwrap().as_str(), "23505");
239        for invalid in ["", "2350", "235050", "2350\u{e9}", "2350\u{0}", "23505 "] {
240            assert_eq!(
241                invalid.parse::<SqlState>(),
242                Err(InvalidSqlState),
243                "{invalid:?}"
244            );
245        }
246    }
247
248    #[test]
249    fn class_and_subclass_split_after_two_characters() {
250        let code: SqlState = "40P01".parse().unwrap();
251        assert_eq!(code.class(), SqlStateClass::TRANSACTION_ROLLBACK);
252        assert_eq!(code.class().as_str(), "40");
253        assert_eq!(code.subclass(), "P01");
254    }
255
256    #[test]
257    fn standard_classes_start_with_0_to_4_or_a_to_h() {
258        for code in [
259            "00000", "40001", "0A000", "B0000", "G0000", "HV000", "H0000",
260        ] {
261            let code: SqlState = code.parse().unwrap();
262            assert!(code.class().is_standard(), "{code}");
263        }
264        for code in ["50000", "9Z000", "I0000", "P0001", "XX000"] {
265            let code: SqlState = code.parse().unwrap();
266            assert!(!code.class().is_standard(), "{code}");
267        }
268    }
269
270    #[test]
271    fn constants_match_as_patterns() {
272        const UNIQUE_VIOLATION: SqlState = SqlState::new(*b"23505").unwrap();
273        let code: SqlState = "23505".parse().unwrap();
274        assert!(matches!(code, UNIQUE_VIOLATION));
275        assert!(matches!(
276            code.class(),
277            SqlStateClass::INTEGRITY_CONSTRAINT_VIOLATION
278        ));
279        let other: SqlState = "22012".parse().unwrap();
280        assert!(!matches!(other, UNIQUE_VIOLATION));
281        assert!(!matches!(
282            other.class(),
283            SqlStateClass::INTEGRITY_CONSTRAINT_VIOLATION
284        ));
285    }
286}