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>(
70 SqlStateClass::kSuccessfulCompletion), //!< 00000
71 //@}
72 //@{
73 /** @name Class 01 — Warning */
74 kWarning = static_cast<std::int64_t>(SqlStateClass::kWarning), //!< 01000
76 kImplicitZeroBitPadding, //!< 01008
78 kPrivilegeNotGranted, //!< 01007
79 kPrivilegeNotRevoked, //!< 01006
81 kDeprecatedFeature, //!< 01P01
82 //@}
83 //@{
84 /** @name Class 02 — No Data (this is also a warning class per the SQL
85 standard) */
86 kNoData = static_cast<std::int64_t>(SqlStateClass::kNoData), //!< 02000
88 //@}
89 //@{
90 /** @name Class 03 — SQL Statement Not Yet Complete */
91 kSqlStatementNotYetComplete = static_cast<std::int64_t>(
92 SqlStateClass::kSqlStatementNotYetComplete), //!< 03000
93 //@}
94 //@{
95 /** @name Class 08 — Connection Exception */
96 kConnectionException = static_cast<std::int64_t>(
97 SqlStateClass::kConnectionException), //!< 08000
98 kConnectionDoesNotExist, //!< 08003
99 kConnectionFailure, //!< 08006
103 kProtocolViolation, //!< 08P01
104 //@}
105 //@{
106 /** @name Class 09 — Triggered Action Exception */
107 kTriggeredActionException = static_cast<std::int64_t>(
108 SqlStateClass::kTriggeredActionException), //!< 09000
109 //@}
110 //@{
111 /** @name Class 0A — Feature Not Supported */
112 kFeatureNotSupported = static_cast<std::int64_t>(
113 SqlStateClass::kFeatureNotSupported), //!< 0A000
114 //@}
115 //@{
116 /** @name Class 0B — Invalid Transaction Initiation */
117 kInvalidTransactionInitiation = static_cast<std::int64_t>(
118 SqlStateClass::kInvalidTransactionInitiation), //!< 0B000
119 //@}
120 //@{
121 /** @name Class 0F — Locator Exception */
123 static_cast<std::int64_t>(SqlStateClass::kLocatorException), //!< 0F000
125 //@}
126 //@{
127 /** @name Class 0L — Invalid Grantor */
129 static_cast<std::int64_t>(SqlStateClass::kInvalidGrantor), //!< 0L000
130 kInvalidGrantOperation, //!< 0LP01
131 //@}
132 //@{
133 /** @name Class 0P — Invalid Role Specification */
134 kInvalidRoleSpecification = static_cast<std::int64_t>(
135 SqlStateClass::kInvalidRoleSpecification), //!< 0P000
136 //@}
137 //@{
138 /** @name Class 0Z — Diagnostics Exception */
139 kDiagnosticsException = static_cast<std::int64_t>(
140 SqlStateClass::kDiagnosticsException), //!< 0Z000
142 //@}
143 //@{
144 /** @name Class 20 — Case Not Found */
146 static_cast<std::int64_t>(SqlStateClass::kCaseNotFound), //!< 20000
147 //@}
148 //@{
149 /** @name Class 21 — Cardinality Violation */
150 kCardinalityViolation = static_cast<std::int64_t>(
151 SqlStateClass::kCardinalityViolation), //!< 21000
152 //@}
153 //@{
154 /** @name Class 22 — Data Exception */
156 static_cast<std::int64_t>(SqlStateClass::kDataException), //!< 22000
157 kArraySubscriptError, //!< 2202E
158 kCharacterNotInRepertoire, //!< 22021
159 kDatetimeFieldOverflow, //!< 22008
160 kDivisionByZero, //!< 22012
161 kErrorInAssignment, //!< 22005
162 kEscapeCharacterConflict, //!< 2200B
163 kIndicatorOverflow, //!< 22022
164 kIntervalFieldOverflow, //!< 22015
171 kInvalidDatetimeFormat, //!< 22007
172 kInvalidEscapeCharacter, //!< 22019
173 kInvalidEscapeOctet, //!< 2200D
174 kInvalidEscapeSequence, //!< 22025
177 kInvalidParameterValue, //!< 22023
179 kInvalidRegularExpression, //!< 2201B
183 kInvalidTablesampleRepeat, //!< 2202G
186 kMostSpecificTypeMismatch, //!< 2200G
187 kNullValueNotAllowed, //!< 22004
189 kNumericValueOutOfRange, //!< 22003
191 kStringDataLengthMismatch, //!< 22026
193 kSubstringError, //!< 22011
194 kTrimError, //!< 22027
195 kUnterminatedCString, //!< 22024
197 kFloatingPointException, //!< 22P01
200 kBadCopyFileFormat, //!< 22P04
201 kUntranslatableCharacter, //!< 22P05
202 kNotAnXmlDocument, //!< 2200L
203 kInvalidXmlDocument, //!< 2200M
204 kInvalidXmlContent, //!< 2200N
205 kInvalidXmlComment, //!< 2200S
208 kInvalidJsonText, //!< 22032
209 kInvalidSqlJsonSubscript, //!< 22033
210 kMoreThanOneSqlJsonItem, //!< 22034
211 kNoSqlJsonItem, //!< 22035
212 kNonNumericSqlJsonItem, //!< 22036
215 kSqlJsonArrayNotFound, //!< 22039
216 kSqlJsonMemberNotFound, //!< 2203A
217 kSqlJsonNumberNotFound, //!< 2203B
218 kSqlJsonObjectNotFound, //!< 2203C
219 kTooManyJsonArrayElements, //!< 2203D
220 kTooManyJsonObjectMembers, //!< 2203E
221 kSqlJsonScalarRequired, //!< 2203F
222 //@}
223 //@{
224 /** @name Class 23 — Integrity Constraint Violation */
225 kIntegrityConstraintViolation = static_cast<std::int64_t>(
226 SqlStateClass::kIntegrityConstraintViolation), //!< 23000
227 kRestrictViolation, //!< 23001
228 kNotNullViolation, //!< 23502
229 kForeignKeyViolation, //!< 23503
230 kUniqueViolation, //!< 23505
231 kCheckViolation, //!< 23514
232 kExclusionViolation, //!< 23P01
233 //@}
234 //@{
235 /** @name Class 24 — Invalid Cursor State */
237 static_cast<std::int64_t>(SqlStateClass::kInvalidCursorState), //!< 24000
238 //@}
239 //@{
240 /** @name Class 25 — Invalid Transaction State */
241 kInvalidTransactionState = static_cast<std::int64_t>(
242 SqlStateClass::kInvalidTransactionState), //!< 25000
243 kActiveSqlTransaction, //!< 25001
249 kReadOnlySqlTransaction, //!< 25006
251 kNoActiveSqlTransaction, //!< 25P01
252 kInFailedSqlTransaction, //!< 25P02
254 //@}
255 //@{
256 /** @name Class 26 — Invalid SQL Statement Name */
257 kInvalidSqlStatementName = static_cast<std::int64_t>(
258 SqlStateClass::kInvalidSqlStatementName), //!< 26000
259 //@}
260 //@{
261 /** @name Class 27 — Triggered Data Change Violation */
262 kTriggeredDataChangeViolation = static_cast<std::int64_t>(
263 SqlStateClass::kTriggeredDataChangeViolation), //!< 27000
264 //@}
265 //@{
266 /** @name Class 28 — Invalid Authorization Specification */
267 kInvalidAuthorizationSpecification = static_cast<std::int64_t>(
268 SqlStateClass::kInvalidAuthorizationSpecification), //!< 28000
269 kInvalidPassword, //!< 28P01
270 //@}
271 //@{
272 /** @name Class 2B — Dependent Privilege Descriptors Still Exist */
273 kDependentPrivilegeDescriptorsStillExist = static_cast<std::int64_t>(
274 SqlStateClass::kDependentPrivilegeDescriptorsStillExist), //!< 2B000
276 //@}
277 //@{
278 /** @name Class 2D — Invalid Transaction Termination */
279 kInvalidTransactionTermination = static_cast<std::int64_t>(
280 SqlStateClass::kInvalidTransactionTermination), //!< 2D000
281 //@}
282 //@{
283 /** @name Class 2F — SQL Routine Exception */
284 kSqlRoutineException = static_cast<std::int64_t>(
285 SqlStateClass::kSqlRoutineException), //!< 2F000
290 //@}
291 //@{
292 /** @name Class 34 — Invalid Cursor Name */
294 static_cast<std::int64_t>(SqlStateClass::kInvalidCursorName), //!< 34000
295 //@}
296 //@{
297 /** @name Class 38 — External Routine Exception */
298 kExternalRoutineException = static_cast<std::int64_t>(
299 SqlStateClass::kExternalRoutineException), //!< 38000
304 //@}
305 //@{
306 /** @name Class 39 — External Routine Invocation Exception */
307 kExternalRoutineInvocationException = static_cast<std::int64_t>(
308 SqlStateClass::kExternalRoutineInvocationException), //!< 39000
309 kInvalidSqlstateReturned, //!< 39001
310 kNullValueNotAllowedEx, //!< 39004
311 kTriggerProtocolViolated, //!< 39P01
312 kSrfProtocolViolated, //!< 39P02
314 //@}
315 //@{
316 /** @name Class 3B — Savepoint Exception */
318 static_cast<std::int64_t>(SqlStateClass::kSavepointException), //!< 3B000
320 //@}
321 //@{
322 /** @name Class 3D — Invalid Catalog Name */
324 static_cast<std::int64_t>(SqlStateClass::kInvalidCatalogName), //!< 3D000
325 //@}
326 //@{
327 /** @name Class 3F — Invalid Schema Name */
329 static_cast<std::int64_t>(SqlStateClass::kInvalidSchemaName), //!< 3F000
330 //@}
331 //@{
332 /** @name Class 40 — Transaction Rollback */
333 kTransactionRollback = static_cast<std::int64_t>(
334 SqlStateClass::kTransactionRollback), //!< 40000
336 kSerializationFailure, //!< 40001
338 kDeadlockDetected, //!< 40P01
339 //@}
340 //@{
341 /** @name Class 42 — Syntax Error or Access Rule Violation */
342 kSyntaxErrorOrAccessRuleViolation = static_cast<std::int64_t>(
343 SqlStateClass::kSyntaxErrorOrAccessRuleViolation), //!< 42000
344 kSyntaxError, //!< 42601
345 kInsufficientPrivilege, //!< 42501
346 kCannotCoerce, //!< 42846
347 kGroupingError, //!< 42803
348 kWindowingError, //!< 42P20
349 kInvalidRecursion, //!< 42P19
350 kInvalidForeignKey, //!< 42830
351 kInvalidName, //!< 42602
352 kNameTooLong, //!< 42622
353 kReservedName, //!< 42939
354 kDatatypeMismatch, //!< 42804
355 kIndeterminateDatatype, //!< 42P18
356 kCollationMismatch, //!< 42P21
357 kIndeterminateCollation, //!< 42P22
358 kWrongObjectType, //!< 42809
359 kGeneratedAlways, //!< 428C9
360 kUndefinedColumn, //!< 42703
361 kUndefinedFunction, //!< 42883
362 kUndefinedTable, //!< 42P01
363 kUndefinedParameter, //!< 42P02
364 kUndefinedObject, //!< 42704
365 kDuplicateColumn, //!< 42701
366 kDuplicateCursor, //!< 42P03
367 kDuplicateDatabase, //!< 42P04
368 kDuplicateFunction, //!< 42723
370 kDuplicateSchema, //!< 42P06
371 kDuplicateTable, //!< 42P07
372 kDuplicateAlias, //!< 42712
373 kDuplicateObject, //!< 42710
374 kAmbiguousColumn, //!< 42702
375 kAmbiguousFunction, //!< 42725
376 kAmbiguousParameter, //!< 42P08
377 kAmbiguousAlias, //!< 42P09
378 kInvalidColumnReference, //!< 42P10
379 kInvalidColumnDefinition, //!< 42611
380 kInvalidCursorDefinition, //!< 42P11
384 kInvalidSchemaDefinition, //!< 42P15
385 kInvalidTableDefinition, //!< 42P16
386 kInvalidObjectDefinition, //!< 42P17
387 //@}
388 //@{
389 /** @name Class 44 — WITH CHECK OPTION Violation */
390 kWithCheckOptionViolation = static_cast<std::int64_t>(
391 SqlStateClass::kWithCheckOptionViolation), //!< 44000
392 //@}
393 //@{
394 /** @name Class 53 — Insufficient Resources */
395 kInsufficientResources = static_cast<std::int64_t>(
396 SqlStateClass::kInsufficientResources), //!< 53000
397 kDiskFull, //!< 53100
398 kOutOfMemory, //!< 53200
399 kTooManyConnections, //!< 53300
401 //@}
402 //@{
403 /** @name Class 54 — Program Limit Exceeded */
404 kProgramLimitExceeded = static_cast<std::int64_t>(
405 SqlStateClass::kProgramLimitExceeded), //!< 54000
406 kStatementTooComplex, //!< 54001
407 kTooManyColumns, //!< 54011
408 kTooManyArguments, //!< 54023
409 //@}
410 //@{
411 /** @name Class 55 — Object Not In Prerequisite State */
412 kObjectNotInPrerequisiteState = static_cast<std::int64_t>(
413 SqlStateClass::kObjectNotInPrerequisiteState), //!< 55000
414 kObjectInUse, //!< 55006
415 kCantChangeRuntimeParam, //!< 55P02
416 kLockNotAvailable, //!< 55P03
417 kUnsafeNewEnumValueUsage, //!< 55P04
418 //@}
419 //@{
420 /** @name Class 57 — Operator Intervention */
421 kOperatorIntervention = static_cast<std::int64_t>(
422 SqlStateClass::kOperatorIntervention), //!< 57000
423 kQueryCancelled, //!< 57014
424 kAdminShutdown, //!< 57P01
425 kCrashShutdown, //!< 57P02
426 kCannotConnectNow, //!< 57P03
427 kDatabaseDropped, //!< 57P04
428 //@}
429 //@{
430 /** @name Class 58 — System Error (errors external to Postgres itself) */
432 static_cast<std::int64_t>(SqlStateClass::kSystemError), //!< 58000
433 kIoError, //!< 58030
434 kUndefinedFile, //!< 58P01
435 kDuplicateFile, //!< 58P02
436 //@}
437 //@{
438 /** @name Class 72 — Snapshot Failure */
440 static_cast<std::int64_t>(SqlStateClass::kSnapshotTooOld), //!< 72000
441 //@}
442 //@{
443 /** @name Class F0 — Configuration File Error */
445 static_cast<std::int64_t>(SqlStateClass::kConfigFileError), //!< F0000
446 kLockFileExists, //!< F0001
447 //@}
448 //@{
449 /** @name Class HV — Foreign Data Wrapper Error (SQL/MED) */
450 kFdwError = static_cast<std::int64_t>(SqlStateClass::kFdwError), //!< HV000
451 kFdwColumnNameNotFound, //!< HV005
453 kFdwFunctionSequenceError, //!< HV010
455 kFdwInvalidAttributeValue, //!< HV024
456 kFdwInvalidColumnName, //!< HV007
457 kFdwInvalidColumnNumber, //!< HV008
458 kFdwInvalidDataType, //!< HV004
461 kFdwInvalidHandle, //!< HV00B
462 kFdwInvalidOptionIndex, //!< HV00C
463 kFdwInvalidOptionName, //!< HV00D
465 kFdwInvalidStringFormat, //!< HV00A
467 kFdwTooManyHandles, //!< HV014
468 kFdwOutOfMemory, //!< HV001
469 kFdwNoSchemas, //!< HV00P
470 kFdwOptionNameNotFound, //!< HV00J
471 kFdwReplyHandle, //!< HV00K
472 kFdwSchemaNotFound, //!< HV00Q
473 kFdwTableNotFound, //!< HV00R
475 kFdwUnableToCreateReply, //!< HV00M
477 //@}
478 //@{
479 /** @name Class P0 — PL/pgSQL Error */
481 static_cast<std::int64_t>(SqlStateClass::kPlpgsqlError), //!< P0000
482 kRaiseException, //!< P0001
483 kNoDataFound, //!< P0002
484 kTooManyRows, //!< P0003
485 kAssertFailure, //!< P0004
486 //@}
487 //@{
488 /** @name Class XX — Internal Error */
490 static_cast<std::int64_t>(SqlStateClass::kInternalError), //!< XX000
491 kDataCorrupted, //!< XX001
492 kIndexCorrupted, //!< XX002
493 //@}
494};
495
496SqlStateClass GetSqlStateClass(SqlState);
497SqlState SqlStateFromString(std::string_view);
498
499bool IsWhitelistedState(SqlState);
500
501} // namespace storages::postgres
502
503USERVER_NAMESPACE_END
504
505namespace std {
506/// @brief Specialization of std::hash template for SqlState.
507template <>
508struct hash<USERVER_NAMESPACE::storages::postgres::SqlState> {
509 std::size_t operator()(
510 USERVER_NAMESPACE::storages::postgres::SqlState v) const {
511 return static_cast<std::size_t>(v);
512 }
513};
514} // namespace std