relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
postgresql_errors.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <unordered_map>
5
6namespace relx::connection {
7
10 // Connection errors
11 ConnectionFailed = 1000,
12 ConnectionClosed = 1001,
13 ConnectionTimeout = 1002,
14
15 // Transaction errors
16 TransactionError = 2000,
19
20 // Query errors
21 QueryFailed = 3000,
22 InvalidParameters = 3001,
23 EmptyResult = 3002,
24
25 // SQLSTATE errors
26 DuplicateKey = 23505, // unique_violation
27 ForeignKeyViolation = 23503, // foreign_key_violation
28 CheckConstraintViolation = 23514, // check_violation
29 NotNullViolation = 23502, // not_null_violation
30
31 // Generic errors
32 Unknown = 9999
33};
34
37 std::string message;
39 std::string sql_state;
40 std::string constraint_name;
41 std::string table_name;
42 std::string column_name;
43 std::string detail;
44 std::string hint;
45
49 static PostgreSQLError from_libpq(int pg_error_code, std::string_view error_msg);
50
54 static PostgreSQLError from_sql_state(std::string_view sql_state, std::string_view error_msg);
55
58 std::string formatted_message() const;
59
63
69
75
79};
80
82inline const std::unordered_map<std::string, PostgreSQLErrorCode> sql_state_map = {
83 // Class 23 — Integrity Constraint Violation
88
89 // Add more mappings as needed
90};
91
92} // namespace relx::connection
PostgreSQLErrorCode
PostgreSQL specific error codes.
const std::unordered_map< std::string, PostgreSQLErrorCode > sql_state_map
Map of SQL STATE codes to PostgreSQLErrorCode values.
Specialized ConnectionError for PostgreSQL with detailed error information.
bool is_not_null_violation() const
Check if this is a not-null violation.
bool is_foreign_key_violation() const
Check if this is a foreign key violation.
bool is_check_constraint_violation() const
Check if this is a check constraint violation.
static PostgreSQLError from_libpq(int pg_error_code, std::string_view error_msg)
Create an error from a libpq error code and message.
bool is_duplicate_key_error() const
Check if this is a duplicate key error.
static PostgreSQLError from_sql_state(std::string_view sql_state, std::string_view error_msg)
Create an error from a SQLSTATE code.
std::string formatted_message() const
Get a user-friendly error message.