|
Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
|
PostgreSQL implementation of database_backend interface. More...
#include <postgresql_backend.h>


Public Member Functions | |
| postgresql_backend () | |
| Default constructor. | |
| ~postgresql_backend () override=default | |
| Destructor - ensures proper cleanup. | |
| kcenon::common::Result< core::database_result > | select_query (const std::string &query_string) override |
| Execute a SELECT query. | |
| kcenon::common::VoidResult | execute_query (const std::string &query_string) override |
| Execute a general SQL query (DDL, DML) | |
| kcenon::common::Result< core::database_result > | select_prepared (const std::string &query, const std::vector< core::database_value > ¶ms) override |
| Execute a parameterized SELECT query (prepared statement) | |
| kcenon::common::VoidResult | execute_prepared (const std::string &query, const std::vector< core::database_value > ¶ms) override |
| Execute a parameterized DML/DDL query (prepared statement) | |
| kcenon::common::VoidResult | begin_transaction () override |
| Begin a transaction. | |
| kcenon::common::VoidResult | commit_transaction () override |
| Commit the current transaction. | |
| kcenon::common::VoidResult | rollback_transaction () override |
| Rollback the current transaction. | |
| bool | in_transaction () const override |
| Check if backend is currently in a transaction. | |
| std::string | last_error () const override |
| Get last error message from backend. | |
| std::map< std::string, std::string > | connection_info () const override |
| Get backend-specific connection information. | |
| kcenon::common::Result< uint64_t > | execute_batch (const std::vector< std::string > &queries) |
| Execute multiple queries in a single transaction (batch mode). | |
Public Member Functions inherited from database::core::backend_base< postgresql_backend, database_types::postgres > | |
| backend_base ()=default | |
| Default constructor. | |
| backend_base (const backend_base &)=delete | |
| backend_base (backend_base &&) noexcept=delete | |
| ~backend_base () override | |
| Virtual destructor. | |
| backend_base & | operator= (const backend_base &)=delete |
| backend_base & | operator= (backend_base &&) noexcept=delete |
| database_types | type () const override |
| Get the database type of this backend. | |
| kcenon::common::VoidResult | initialize (const connection_config &config) override |
| Initialize the database backend. | |
| kcenon::common::VoidResult | shutdown () override |
| Shutdown the database backend gracefully. | |
| bool | is_initialized () const override |
| Check if backend is initialized and ready. | |
Public Member Functions inherited from database::core::database_backend | |
| virtual | ~database_backend ()=default |
Static Public Member Functions | |
| static constexpr const char * | backend_name () |
| Backend name for error messages. | |
Static Public Member Functions inherited from database::core::backend_base< postgresql_backend, database_types::postgres > | |
| static std::unique_ptr< database_backend > | create () |
| Factory method for backend_registry. | |
Protected Member Functions | |
| kcenon::common::VoidResult | do_initialize (const core::connection_config &config) |
| Database-specific initialization logic. | |
| kcenon::common::VoidResult | do_shutdown () |
| Database-specific shutdown logic. | |
Private Member Functions | |
| std::string | build_connection_string (const core::connection_config &config) const |
| Convert connection_config to PostgreSQL connection string. | |
| std::string | build_safe_connection_string (const core::connection_config &config) const |
| Build a connection string with password masked for safe logging. | |
| std::string | sanitize_error (const std::string &error_message) const |
| Remove password from an error message that may contain the connection string. | |
| unsigned int | execute_modification_query (const std::string &query_string) |
| Execute a modification query (INSERT, UPDATE, DELETE) | |
Private Attributes | |
| void * | connection_ {nullptr} |
| PostgreSQL connection (PGconn* or pqxx::connection*) | |
| std::atomic< bool > | in_transaction_ {false} |
| Transaction state. | |
| std::string | last_error_ |
| Last error message. | |
| core::connection_config | connection_config_ |
| Cached connection config. | |
Friends | |
| class | core::backend_base< postgresql_backend, database_types::postgres > |
Additional Inherited Members | |
Static Protected Member Functions inherited from database::core::database_backend | |
| static std::string | expand_params (const std::string &query, const std::vector< database_value > ¶ms) |
| Expand positional parameters into a SQL string (fallback) | |
Protected Attributes inherited from database::core::backend_base< postgresql_backend, database_types::postgres > | |
| std::atomic< bool > | initialized_ |
| Initialization state. | |
PostgreSQL implementation of database_backend interface.
This class implements the database_backend interface for PostgreSQL via backend_base CRTP template, using libpq/pqxx libraries.
Design Pattern: Strategy pattern with CRTP
Thread Safety:
Usage:
Definition at line 71 of file postgresql_backend.h.
| database::backends::postgresql_backend::postgresql_backend | ( | ) |
Default constructor.
Definition at line 38 of file postgresql_backend.cpp.
|
overridedefault |
Destructor - ensures proper cleanup.
|
inlinestaticconstexpr |
Backend name for error messages.
Definition at line 78 of file postgresql_backend.h.
Referenced by TEST_F().

|
overridevirtual |
Begin a transaction.
Implements database::core::database_backend.
Definition at line 712 of file postgresql_backend.cpp.
References execute_query(), in_transaction_, database::invalid_state, database::core::backend_base< postgresql_backend, database_types::postgres >::is_initialized(), and last_error_.
Referenced by execute_batch().


|
private |
Convert connection_config to PostgreSQL connection string.
| config | Structured connection configuration |
Format: "host=... port=... dbname=... user=... password=..."
Definition at line 878 of file postgresql_backend.cpp.
References database::core::connection_config::database, database::core::connection_config::host, database::core::connection_config::options, database::core::connection_config::password, database::core::connection_config::port, and database::core::connection_config::username.
Referenced by do_initialize().

|
private |
Build a connection string with password masked for safe logging.
| config | Structured connection configuration |
Definition at line 910 of file postgresql_backend.cpp.
References database::core::connection_config::database, database::core::connection_config::host, database::core::connection_config::options, database::core::connection_config::password, database::core::connection_config::port, and database::core::connection_config::username.
Referenced by do_initialize().

|
overridevirtual |
Commit the current transaction.
Implements database::core::database_backend.
Definition at line 742 of file postgresql_backend.cpp.
References execute_query(), in_transaction_, database::invalid_state, database::core::backend_base< postgresql_backend, database_types::postgres >::is_initialized(), and last_error_.
Referenced by execute_batch().


|
overridevirtual |
Get backend-specific connection information.
Example keys: "server_version", "connection_id", "protocol_version"
Implements database::core::database_backend.
Definition at line 865 of file postgresql_backend.cpp.
References connection_config_, database::core::connection_config::database, database::core::connection_config::host, in_transaction_, database::core::backend_base< postgresql_backend, database_types::postgres >::initialized_, database::core::connection_config::port, and database::core::connection_config::username.
|
protected |
Database-specific initialization logic.
| config | Connection configuration |
Definition at line 43 of file postgresql_backend.cpp.
References build_connection_string(), build_safe_connection_string(), connection_, connection_config_, database::connection_failed, last_error_, and sanitize_error().

|
protected |
Database-specific shutdown logic.
Definition at line 91 of file postgresql_backend.cpp.
References connection_, database::connection_failed, in_transaction_, last_error_, and rollback_transaction().

| kcenon::common::Result< uint64_t > database::backends::postgresql_backend::execute_batch | ( | const std::vector< std::string > & | queries | ) |
Execute multiple queries in a single transaction (batch mode).
| queries | Vector of SQL statements to execute atomically. |
All queries run inside a single BEGIN/COMMIT block. If any query fails, the entire batch is rolled back and the error is returned.
Definition at line 799 of file postgresql_backend.cpp.
References begin_transaction(), commit_transaction(), execute_modification_query(), database::invalid_state, database::core::backend_base< postgresql_backend, database_types::postgres >::is_initialized(), last_error_, database::query_failed, and rollback_transaction().

|
private |
Execute a modification query (INSERT, UPDATE, DELETE)
| query_string | SQL query to execute |
Definition at line 132 of file postgresql_backend.cpp.
References connection_, and last_error_.
Referenced by execute_batch().

|
nodiscardoverridevirtual |
Execute a parameterized DML/DDL query (prepared statement)
| query | SQL with positional placeholders ($1, $2, ... or ?, ?, ...) |
| params | Parameter values to bind |
Reimplemented from database::core::database_backend.
Definition at line 575 of file postgresql_backend.cpp.
References connection_, database::connection_failed, database::invalid_state, database::core::backend_base< postgresql_backend, database_types::postgres >::is_initialized(), last_error_, database::query_failed, and database::success.

|
overridevirtual |
Execute a general SQL query (DDL, DML)
| query_string | SQL statement |
Implements database::core::database_backend.
Definition at line 309 of file postgresql_backend.cpp.
References connection_, database::connection_failed, database::invalid_state, database::core::backend_base< postgresql_backend, database_types::postgres >::is_initialized(), last_error_, database::query_failed, and database::success.
Referenced by begin_transaction(), commit_transaction(), and rollback_transaction().


|
overridevirtual |
Check if backend is currently in a transaction.
Implements database::core::database_backend.
Definition at line 855 of file postgresql_backend.cpp.
References in_transaction_.
|
overridevirtual |
Get last error message from backend.
Implements database::core::database_backend.
Definition at line 860 of file postgresql_backend.cpp.
References last_error_.
|
overridevirtual |
Rollback the current transaction.
Implements database::core::database_backend.
Definition at line 772 of file postgresql_backend.cpp.
References execute_query(), in_transaction_, database::invalid_state, database::core::backend_base< postgresql_backend, database_types::postgres >::is_initialized(), and last_error_.
Referenced by do_shutdown(), and execute_batch().


|
private |
Remove password from an error message that may contain the connection string.
| error_message | The error message to sanitize |
Definition at line 941 of file postgresql_backend.cpp.
References connection_config_, and database::core::connection_config::password.
Referenced by do_initialize().

|
nodiscardoverridevirtual |
Execute a parameterized SELECT query (prepared statement)
Parameters are bound at the wire-protocol level, providing stronger SQL injection protection than string escaping. Backends that support native prepared statements (PostgreSQL, SQLite) should override this.
| query | SQL with positional placeholders ($1, $2, ... or ?, ?, ...) |
| params | Parameter values to bind |
Reimplemented from database::core::database_backend.
Definition at line 393 of file postgresql_backend.cpp.
References connection_, database::connection_failed, database::invalid_state, database::core::backend_base< postgresql_backend, database_types::postgres >::is_initialized(), last_error_, database::query_failed, and database::core::backend_base< postgresql_backend, database_types::postgres >::type().

|
overridevirtual |
Execute a SELECT query.
| query_string | SQL SELECT statement |
Implements database::core::database_backend.
Definition at line 175 of file postgresql_backend.cpp.
References connection_, database::connection_failed, database::invalid_state, database::core::backend_base< postgresql_backend, database_types::postgres >::is_initialized(), last_error_, database::query_failed, and database::core::backend_base< postgresql_backend, database_types::postgres >::type().

|
friend |
Definition at line 124 of file postgresql_backend.h.
|
private |
PostgreSQL connection (PGconn* or pqxx::connection*)
Definition at line 173 of file postgresql_backend.h.
Referenced by do_initialize(), do_shutdown(), execute_modification_query(), execute_prepared(), execute_query(), select_prepared(), and select_query().
|
private |
Cached connection config.
Definition at line 176 of file postgresql_backend.h.
Referenced by connection_info(), do_initialize(), and sanitize_error().
|
private |
Transaction state.
Definition at line 174 of file postgresql_backend.h.
Referenced by begin_transaction(), commit_transaction(), connection_info(), do_shutdown(), in_transaction(), and rollback_transaction().
|
mutableprivate |
Last error message.
Definition at line 175 of file postgresql_backend.h.
Referenced by begin_transaction(), commit_transaction(), do_initialize(), do_shutdown(), execute_batch(), execute_modification_query(), execute_prepared(), execute_query(), last_error(), rollback_transaction(), select_prepared(), and select_query().