Line data Source code
1 : //! Rust repr for <https://www.postgresql.org/docs/current/errcodes-appendix.html>
2 :
3 : /// A SQLSTATE error code
4 : #[derive(PartialEq, Eq, Clone, Debug)]
5 : pub struct SqlState([u8; 5]);
6 :
7 : impl SqlState {
8 : /// Creates a `SqlState` from its error code.
9 2 : pub fn from_code(s: &str) -> SqlState {
10 2 : let mut code = [b'0'; 5];
11 2 : if s.len() == 5 {
12 2 : code.copy_from_slice(s.as_bytes());
13 2 : }
14 2 : SqlState(code)
15 2 : }
16 :
17 : /// Returns the error code corresponding to the `SqlState`.
18 1 : pub fn code(&self) -> &str {
19 1 : std::str::from_utf8(&self.0).unwrap()
20 1 : }
21 :
22 : // Class 08 - Connection Exception
23 :
24 : /// 08000
25 : pub const CONNECTION_EXCEPTION: SqlState = SqlState(*b"08000");
26 :
27 : /// 08003
28 : pub const CONNECTION_DOES_NOT_EXIST: SqlState = SqlState(*b"08003");
29 :
30 : /// 08006
31 : pub const CONNECTION_FAILURE: SqlState = SqlState(*b"08006");
32 :
33 : /// 08001
34 : pub const SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION: SqlState = SqlState(*b"08001");
35 :
36 : /// 08P01
37 : pub const PROTOCOL_VIOLATION: SqlState = SqlState(*b"08P01");
38 :
39 : // Class 22 - Data Exception
40 :
41 : /// 22023
42 : pub const INVALID_PARAMETER_VALUE: SqlState = SqlState(*b"22023");
43 :
44 : // Class 3D - Invalid Catalog Name
45 :
46 : /// 3D000
47 : pub const INVALID_CATALOG_NAME: SqlState = SqlState(*b"3D000");
48 :
49 : // Class 3F - Invalid Schema Name
50 :
51 : /// 3F000
52 : pub const INVALID_SCHEMA_NAME: SqlState = SqlState(*b"3F000");
53 :
54 : // Class 40 - Transaction Rollback
55 :
56 : /// 40001
57 : pub const T_R_SERIALIZATION_FAILURE: SqlState = SqlState(*b"40001");
58 :
59 : // Class 42 - Syntax Error or Access Rule Violation
60 :
61 : /// 42601
62 : pub const SYNTAX_ERROR: SqlState = SqlState(*b"42601");
63 :
64 : // Class 53 - Insufficient Resources
65 :
66 : /// 53200
67 : pub const OUT_OF_MEMORY: SqlState = SqlState(*b"53200");
68 :
69 : /// 53300
70 : pub const TOO_MANY_CONNECTIONS: SqlState = SqlState(*b"53300");
71 :
72 : // Class 57 - Operator Intervention
73 :
74 : /// 57014
75 : pub const QUERY_CANCELED: SqlState = SqlState(*b"57014");
76 : }
77 :
78 : #[cfg(test)]
79 : mod tests {
80 : use super::SqlState;
81 :
82 : #[test]
83 1 : fn round_trip() {
84 1 : let state = SqlState::from_code("08P01");
85 1 : assert_eq!(state, SqlState::PROTOCOL_VIOLATION);
86 1 : assert_eq!(state.code(), "08P01");
87 1 : }
88 : }
|