userver: userver/storages/postgres/sql_state.hpp Source File
Loading...
Searching...
No Matches
sql_state.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/postgres/sql_state.hpp
4/// @brief PostgreSQL error codes
5
6#include <cstdint>
7#include <string_view>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace storages::postgres {
12
13// PostgreSQL error classes
14/// Docs: https://www.postgresql.org/docs/12/static/errcodes-appendix.html
15/// Enumeration was generated by userver/scripts/postgres/pg_sql_codes.py
16enum class SqlStateClass : std::int64_t {
17 kSuccessfulCompletion = 0x01,
18 kWarning = 0x02,
19 kNoData = 0x10,
20 kSqlStatementNotYetComplete = 0x20,
21 kConnectionException = 0x40,
22 kTriggeredActionException = 0x80,
23 kFeatureNotSupported = 0x100,
24 kInvalidTransactionInitiation = 0x200,
25 kLocatorException = 0x400,
26 kInvalidGrantor = 0x800,
27 kInvalidRoleSpecification = 0x1000,
28 kDiagnosticsException = 0x2000,
29 kCaseNotFound = 0x4000,
30 kCardinalityViolation = 0x8000,
31 kDataException = 0x10000,
32 kIntegrityConstraintViolation = 0x20000,
33 kInvalidCursorState = 0x40000,
34 kInvalidTransactionState = 0x80000,
35 kInvalidSqlStatementName = 0x100000,
36 kTriggeredDataChangeViolation = 0x200000,
37 kInvalidAuthorizationSpecification = 0x400000,
38 kDependentPrivilegeDescriptorsStillExist = 0x800000,
39 kInvalidTransactionTermination = 0x1000000,
40 kSqlRoutineException = 0x2000000,
41 kInvalidCursorName = 0x4000000,
42 kExternalRoutineException = 0x8000000,
43 kExternalRoutineInvocationException = 0x10000000,
44 kSavepointException = 0x20000000,
45 kInvalidCatalogName = 0x40000000,
46 kInvalidSchemaName = 0x80000000,
47 kTransactionRollback = 0x100000000,
48 kSyntaxErrorOrAccessRuleViolation = 0x200000000,
49 kWithCheckOptionViolation = 0x400000000,
50 kInsufficientResources = 0x800000000,
51 kProgramLimitExceeded = 0x1000000000,
52 kObjectNotInPrerequisiteState = 0x2000000000,
53 kOperatorIntervention = 0x4000000000,
54 kSystemError = 0x8000000000,
55 kSnapshotTooOld = 0x10000000000,
56 kConfigFileError = 0x20000000000,
57 kFdwError = 0x40000000000,
58 kPlpgsqlError = 0x80000000000,
59 kInternalError = 0x100000000000,
60};
61
62/// PostgreSQL error codes
63/// Docs: https://www.postgresql.org/docs/12/static/errcodes-appendix.html
64/// Enumeration was generated by userver/scripts/postgres/pg_sql_codes.py
65enum class SqlState : std::int64_t {
66 kUnknownState, ///!< Unknown state, not in PostgreSQL docs
67 //@{
68 /** @name Class 00 — Successful Completion */
69 kSuccessfulCompletion = static_cast<std::int64_t>(SqlStateClass::kSuccessfulCompletion), //!< 00000
70 //@}
71 //@{
72 /** @name Class 01 — Warning */
73 kWarning = static_cast<std::int64_t>(SqlStateClass::kWarning), //!< 01000
75 kImplicitZeroBitPadding, //!< 01008
77 kPrivilegeNotGranted, //!< 01007
78 kPrivilegeNotRevoked, //!< 01006
80 kDeprecatedFeature, //!< 01P01
81 //@}
82 //@{
83 /** @name Class 02 — No Data (this is also a warning class per the SQL
84 standard) */
85 kNoData = static_cast<std::int64_t>(SqlStateClass::kNoData), //!< 02000
87 //@}
88 //@{
89 /** @name Class 03 — SQL Statement Not Yet Complete */
90 kSqlStatementNotYetComplete = static_cast<std::int64_t>(SqlStateClass::kSqlStatementNotYetComplete), //!< 03000
91 //@}
92 //@{
93 /** @name Class 08 — Connection Exception */
94 kConnectionException = static_cast<std::int64_t>(SqlStateClass::kConnectionException), //!< 08000
95 kConnectionDoesNotExist, //!< 08003
96 kConnectionFailure, //!< 08006
100 kProtocolViolation, //!< 08P01
101 //@}
102 //@{
103 /** @name Class 09 — Triggered Action Exception */
104 kTriggeredActionException = static_cast<std::int64_t>(SqlStateClass::kTriggeredActionException), //!< 09000
105 //@}
106 //@{
107 /** @name Class 0A — Feature Not Supported */
108 kFeatureNotSupported = static_cast<std::int64_t>(SqlStateClass::kFeatureNotSupported), //!< 0A000
109 //@}
110 //@{
111 /** @name Class 0B — Invalid Transaction Initiation */
112 kInvalidTransactionInitiation = static_cast<std::int64_t>(SqlStateClass::kInvalidTransactionInitiation), //!< 0B000
113 //@}
114 //@{
115 /** @name Class 0F — Locator Exception */
116 kLocatorException = static_cast<std::int64_t>(SqlStateClass::kLocatorException), //!< 0F000
118 //@}
119 //@{
120 /** @name Class 0L — Invalid Grantor */
121 kInvalidGrantor = static_cast<std::int64_t>(SqlStateClass::kInvalidGrantor), //!< 0L000
122 kInvalidGrantOperation, //!< 0LP01
123 //@}
124 //@{
125 /** @name Class 0P — Invalid Role Specification */
126 kInvalidRoleSpecification = static_cast<std::int64_t>(SqlStateClass::kInvalidRoleSpecification), //!< 0P000
127 //@}
128 //@{
129 /** @name Class 0Z — Diagnostics Exception */
130 kDiagnosticsException = static_cast<std::int64_t>(SqlStateClass::kDiagnosticsException), //!< 0Z000
132 //@}
133 //@{
134 /** @name Class 20 — Case Not Found */
135 kCaseNotFound = static_cast<std::int64_t>(SqlStateClass::kCaseNotFound), //!< 20000
136 //@}
137 //@{
138 /** @name Class 21 — Cardinality Violation */
139 kCardinalityViolation = static_cast<std::int64_t>(SqlStateClass::kCardinalityViolation), //!< 21000
140 //@}
141 //@{
142 /** @name Class 22 — Data Exception */
143 kDataException = static_cast<std::int64_t>(SqlStateClass::kDataException), //!< 22000
144 kArraySubscriptError, //!< 2202E
145 kCharacterNotInRepertoire, //!< 22021
146 kDatetimeFieldOverflow, //!< 22008
147 kDivisionByZero, //!< 22012
148 kErrorInAssignment, //!< 22005
149 kEscapeCharacterConflict, //!< 2200B
150 kIndicatorOverflow, //!< 22022
151 kIntervalFieldOverflow, //!< 22015
158 kInvalidDatetimeFormat, //!< 22007
159 kInvalidEscapeCharacter, //!< 22019
160 kInvalidEscapeOctet, //!< 2200D
161 kInvalidEscapeSequence, //!< 22025
164 kInvalidParameterValue, //!< 22023
166 kInvalidRegularExpression, //!< 2201B
170 kInvalidTablesampleRepeat, //!< 2202G
173 kMostSpecificTypeMismatch, //!< 2200G
174 kNullValueNotAllowed, //!< 22004
176 kNumericValueOutOfRange, //!< 22003
178 kStringDataLengthMismatch, //!< 22026
180 kSubstringError, //!< 22011
181 kTrimError, //!< 22027
182 kUnterminatedCString, //!< 22024
184 kFloatingPointException, //!< 22P01
187 kBadCopyFileFormat, //!< 22P04
188 kUntranslatableCharacter, //!< 22P05
189 kNotAnXmlDocument, //!< 2200L
190 kInvalidXmlDocument, //!< 2200M
191 kInvalidXmlContent, //!< 2200N
192 kInvalidXmlComment, //!< 2200S
195 kInvalidJsonText, //!< 22032
196 kInvalidSqlJsonSubscript, //!< 22033
197 kMoreThanOneSqlJsonItem, //!< 22034
198 kNoSqlJsonItem, //!< 22035
199 kNonNumericSqlJsonItem, //!< 22036
202 kSqlJsonArrayNotFound, //!< 22039
203 kSqlJsonMemberNotFound, //!< 2203A
204 kSqlJsonNumberNotFound, //!< 2203B
205 kSqlJsonObjectNotFound, //!< 2203C
206 kTooManyJsonArrayElements, //!< 2203D
207 kTooManyJsonObjectMembers, //!< 2203E
208 kSqlJsonScalarRequired, //!< 2203F
209 //@}
210 //@{
211 /** @name Class 23 — Integrity Constraint Violation */
212 kIntegrityConstraintViolation = static_cast<std::int64_t>(SqlStateClass::kIntegrityConstraintViolation), //!< 23000
213 kRestrictViolation, //!< 23001
214 kNotNullViolation, //!< 23502
215 kForeignKeyViolation, //!< 23503
216 kUniqueViolation, //!< 23505
217 kCheckViolation, //!< 23514
218 kExclusionViolation, //!< 23P01
219 //@}
220 //@{
221 /** @name Class 24 — Invalid Cursor State */
222 kInvalidCursorState = static_cast<std::int64_t>(SqlStateClass::kInvalidCursorState), //!< 24000
223 //@}
224 //@{
225 /** @name Class 25 — Invalid Transaction State */
226 kInvalidTransactionState = static_cast<std::int64_t>(SqlStateClass::kInvalidTransactionState), //!< 25000
227 kActiveSqlTransaction, //!< 25001
233 kReadOnlySqlTransaction, //!< 25006
235 kNoActiveSqlTransaction, //!< 25P01
236 kInFailedSqlTransaction, //!< 25P02
238 //@}
239 //@{
240 /** @name Class 26 — Invalid SQL Statement Name */
241 kInvalidSqlStatementName = static_cast<std::int64_t>(SqlStateClass::kInvalidSqlStatementName), //!< 26000
242 //@}
243 //@{
244 /** @name Class 27 — Triggered Data Change Violation */
245 kTriggeredDataChangeViolation = static_cast<std::int64_t>(SqlStateClass::kTriggeredDataChangeViolation), //!< 27000
246 //@}
247 //@{
248 /** @name Class 28 — Invalid Authorization Specification */
250 static_cast<std::int64_t>(SqlStateClass::kInvalidAuthorizationSpecification), //!< 28000
251 kInvalidPassword, //!< 28P01
252 //@}
253 //@{
254 /** @name Class 2B — Dependent Privilege Descriptors Still Exist */
256 static_cast<std::int64_t>(SqlStateClass::kDependentPrivilegeDescriptorsStillExist), //!< 2B000
258 //@}
259 //@{
260 /** @name Class 2D — Invalid Transaction Termination */
262 static_cast<std::int64_t>(SqlStateClass::kInvalidTransactionTermination), //!< 2D000
263 //@}
264 //@{
265 /** @name Class 2F — SQL Routine Exception */
266 kSqlRoutineException = static_cast<std::int64_t>(SqlStateClass::kSqlRoutineException), //!< 2F000
271 //@}
272 //@{
273 /** @name Class 34 — Invalid Cursor Name */
274 kInvalidCursorName = static_cast<std::int64_t>(SqlStateClass::kInvalidCursorName), //!< 34000
275 //@}
276 //@{
277 /** @name Class 38 — External Routine Exception */
278 kExternalRoutineException = static_cast<std::int64_t>(SqlStateClass::kExternalRoutineException), //!< 38000
283 //@}
284 //@{
285 /** @name Class 39 — External Routine Invocation Exception */
287 static_cast<std::int64_t>(SqlStateClass::kExternalRoutineInvocationException), //!< 39000
288 kInvalidSqlstateReturned, //!< 39001
289 kNullValueNotAllowedEx, //!< 39004
290 kTriggerProtocolViolated, //!< 39P01
291 kSrfProtocolViolated, //!< 39P02
293 //@}
294 //@{
295 /** @name Class 3B — Savepoint Exception */
296 kSavepointException = static_cast<std::int64_t>(SqlStateClass::kSavepointException), //!< 3B000
298 //@}
299 //@{
300 /** @name Class 3D — Invalid Catalog Name */
301 kInvalidCatalogName = static_cast<std::int64_t>(SqlStateClass::kInvalidCatalogName), //!< 3D000
302 //@}
303 //@{
304 /** @name Class 3F — Invalid Schema Name */
305 kInvalidSchemaName = static_cast<std::int64_t>(SqlStateClass::kInvalidSchemaName), //!< 3F000
306 //@}
307 //@{
308 /** @name Class 40 — Transaction Rollback */
309 kTransactionRollback = static_cast<std::int64_t>(SqlStateClass::kTransactionRollback), //!< 40000
311 kSerializationFailure, //!< 40001
313 kDeadlockDetected, //!< 40P01
314 //@}
315 //@{
316 /** @name Class 42 — Syntax Error or Access Rule Violation */
318 static_cast<std::int64_t>(SqlStateClass::kSyntaxErrorOrAccessRuleViolation), //!< 42000
319 kSyntaxError, //!< 42601
320 kInsufficientPrivilege, //!< 42501
321 kCannotCoerce, //!< 42846
322 kGroupingError, //!< 42803
323 kWindowingError, //!< 42P20
324 kInvalidRecursion, //!< 42P19
325 kInvalidForeignKey, //!< 42830
326 kInvalidName, //!< 42602
327 kNameTooLong, //!< 42622
328 kReservedName, //!< 42939
329 kDatatypeMismatch, //!< 42804
330 kIndeterminateDatatype, //!< 42P18
331 kCollationMismatch, //!< 42P21
332 kIndeterminateCollation, //!< 42P22
333 kWrongObjectType, //!< 42809
334 kGeneratedAlways, //!< 428C9
335 kUndefinedColumn, //!< 42703
336 kUndefinedFunction, //!< 42883
337 kUndefinedTable, //!< 42P01
338 kUndefinedParameter, //!< 42P02
339 kUndefinedObject, //!< 42704
340 kDuplicateColumn, //!< 42701
341 kDuplicateCursor, //!< 42P03
342 kDuplicateDatabase, //!< 42P04
343 kDuplicateFunction, //!< 42723
345 kDuplicateSchema, //!< 42P06
346 kDuplicateTable, //!< 42P07
347 kDuplicateAlias, //!< 42712
348 kDuplicateObject, //!< 42710
349 kAmbiguousColumn, //!< 42702
350 kAmbiguousFunction, //!< 42725
351 kAmbiguousParameter, //!< 42P08
352 kAmbiguousAlias, //!< 42P09
353 kInvalidColumnReference, //!< 42P10
354 kInvalidColumnDefinition, //!< 42611
355 kInvalidCursorDefinition, //!< 42P11
359 kInvalidSchemaDefinition, //!< 42P15
360 kInvalidTableDefinition, //!< 42P16
361 kInvalidObjectDefinition, //!< 42P17
362 //@}
363 //@{
364 /** @name Class 44 — WITH CHECK OPTION Violation */
365 kWithCheckOptionViolation = static_cast<std::int64_t>(SqlStateClass::kWithCheckOptionViolation), //!< 44000
366 //@}
367 //@{
368 /** @name Class 53 — Insufficient Resources */
369 kInsufficientResources = static_cast<std::int64_t>(SqlStateClass::kInsufficientResources), //!< 53000
370 kDiskFull, //!< 53100
371 kOutOfMemory, //!< 53200
372 kTooManyConnections, //!< 53300
374 //@}
375 //@{
376 /** @name Class 54 — Program Limit Exceeded */
377 kProgramLimitExceeded = static_cast<std::int64_t>(SqlStateClass::kProgramLimitExceeded), //!< 54000
378 kStatementTooComplex, //!< 54001
379 kTooManyColumns, //!< 54011
380 kTooManyArguments, //!< 54023
381 //@}
382 //@{
383 /** @name Class 55 — Object Not In Prerequisite State */
384 kObjectNotInPrerequisiteState = static_cast<std::int64_t>(SqlStateClass::kObjectNotInPrerequisiteState), //!< 55000
385 kObjectInUse, //!< 55006
386 kCantChangeRuntimeParam, //!< 55P02
387 kLockNotAvailable, //!< 55P03
388 kUnsafeNewEnumValueUsage, //!< 55P04
389 //@}
390 //@{
391 /** @name Class 57 — Operator Intervention */
392 kOperatorIntervention = static_cast<std::int64_t>(SqlStateClass::kOperatorIntervention), //!< 57000
393 kQueryCancelled, //!< 57014
394 kAdminShutdown, //!< 57P01
395 kCrashShutdown, //!< 57P02
396 kCannotConnectNow, //!< 57P03
397 kDatabaseDropped, //!< 57P04
398 //@}
399 //@{
400 /** @name Class 58 — System Error (errors external to Postgres itself) */
401 kSystemError = static_cast<std::int64_t>(SqlStateClass::kSystemError), //!< 58000
402 kIoError, //!< 58030
403 kUndefinedFile, //!< 58P01
404 kDuplicateFile, //!< 58P02
405 //@}
406 //@{
407 /** @name Class 72 — Snapshot Failure */
408 kSnapshotTooOld = static_cast<std::int64_t>(SqlStateClass::kSnapshotTooOld), //!< 72000
409 //@}
410 //@{
411 /** @name Class F0 — Configuration File Error */
412 kConfigFileError = static_cast<std::int64_t>(SqlStateClass::kConfigFileError), //!< F0000
413 kLockFileExists, //!< F0001
414 //@}
415 //@{
416 /** @name Class HV — Foreign Data Wrapper Error (SQL/MED) */
417 kFdwError = static_cast<std::int64_t>(SqlStateClass::kFdwError), //!< HV000
418 kFdwColumnNameNotFound, //!< HV005
420 kFdwFunctionSequenceError, //!< HV010
422 kFdwInvalidAttributeValue, //!< HV024
423 kFdwInvalidColumnName, //!< HV007
424 kFdwInvalidColumnNumber, //!< HV008
425 kFdwInvalidDataType, //!< HV004
428 kFdwInvalidHandle, //!< HV00B
429 kFdwInvalidOptionIndex, //!< HV00C
430 kFdwInvalidOptionName, //!< HV00D
432 kFdwInvalidStringFormat, //!< HV00A
434 kFdwTooManyHandles, //!< HV014
435 kFdwOutOfMemory, //!< HV001
436 kFdwNoSchemas, //!< HV00P
437 kFdwOptionNameNotFound, //!< HV00J
438 kFdwReplyHandle, //!< HV00K
439 kFdwSchemaNotFound, //!< HV00Q
440 kFdwTableNotFound, //!< HV00R
442 kFdwUnableToCreateReply, //!< HV00M
444 //@}
445 //@{
446 /** @name Class P0 — PL/pgSQL Error */
447 kPlpgsqlError = static_cast<std::int64_t>(SqlStateClass::kPlpgsqlError), //!< P0000
448 kRaiseException, //!< P0001
449 kNoDataFound, //!< P0002
450 kTooManyRows, //!< P0003
451 kAssertFailure, //!< P0004
452 //@}
453 //@{
454 /** @name Class XX — Internal Error */
455 kInternalError = static_cast<std::int64_t>(SqlStateClass::kInternalError), //!< XX000
456 kDataCorrupted, //!< XX001
457 kIndexCorrupted, //!< XX002
458 //@}
459};
460
461SqlStateClass GetSqlStateClass(SqlState);
462SqlState SqlStateFromString(std::string_view);
463
464bool IsWhitelistedState(SqlState);
465
466} // namespace storages::postgres
467
468USERVER_NAMESPACE_END
469
470namespace std {
471/// @brief Specialization of std::hash template for SqlState.
472template <>
473struct hash<USERVER_NAMESPACE::storages::postgres::SqlState> {
474 std::size_t operator()(USERVER_NAMESPACE::storages::postgres::SqlState v) const {
475 return static_cast<std::size_t>(v);
476 }
477};
478} // namespace std