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


Public Member Functions | |
| mongodb_backend () | |
| Default constructor. | |
| ~mongodb_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::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. | |
Public Member Functions inherited from database::core::backend_base< mongodb_backend, database_types::mongodb > | |
| 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 |
| virtual kcenon::common::Result< database_result > | select_prepared (const std::string &query, const std::vector< database_value > ¶ms) |
| Execute a parameterized SELECT query (prepared statement) | |
| virtual kcenon::common::VoidResult | execute_prepared (const std::string &query, const std::vector< database_value > ¶ms) |
| Execute a parameterized DML/DDL query (prepared statement) | |
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< mongodb_backend, database_types::mongodb > | |
| 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_uri (const core::connection_config &config) const |
| Convert connection_config to MongoDB connection URI. | |
| bool | parse_query_string (const std::string &query_string, std::string &collection, std::string &filter, std::string &update) const |
| Parse query string format. | |
Private Attributes | |
| void * | client_ {nullptr} |
| MongoDB client (mongocxx::client*) | |
| void * | database_ {nullptr} |
| MongoDB database (mongocxx::database*) | |
| std::string | db_name_ |
| Database name. | |
| std::atomic< bool > | in_transaction_ {false} |
| Transaction state. | |
| std::string | last_error_ |
| Last error message. | |
| core::connection_config | connection_config_ |
| Cached connection config. | |
| std::mutex | mongo_mutex_ |
| Mutex for thread safety. | |
Friends | |
| class | core::backend_base< mongodb_backend, database_types::mongodb > |
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< mongodb_backend, database_types::mongodb > | |
| std::atomic< bool > | initialized_ |
| Initialization state. | |
MongoDB implementation of database_backend interface.
This class implements the database_backend interface for MongoDB via backend_base CRTP template, using the MongoDB C++ driver.
Design Pattern: Strategy pattern with CRTP
Thread Safety:
Usage:
Definition at line 71 of file mongodb_backend.h.
| database::backends::mongodb_backend::mongodb_backend | ( | ) |
Default constructor.
Definition at line 36 of file mongodb_backend.cpp.
|
overridedefault |
Destructor - ensures proper cleanup.
|
inlinestaticconstexpr |
Backend name for error messages.
Definition at line 78 of file mongodb_backend.h.
Referenced by TEST_F().

|
overridevirtual |
Begin a transaction.
Implements database::core::database_backend.
Definition at line 285 of file mongodb_backend.cpp.
References in_transaction_, database::invalid_state, database::core::backend_base< mongodb_backend, database_types::mongodb >::is_initialized(), and last_error_.

|
private |
Convert connection_config to MongoDB connection URI.
| config | Structured connection configuration |
Format: "mongodb://username:password@host:port/database"
Definition at line 381 of file mongodb_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 312 of file mongodb_backend.cpp.
References in_transaction_, database::invalid_state, database::core::backend_base< mongodb_backend, database_types::mongodb >::is_initialized(), and last_error_.

|
overridevirtual |
Get backend-specific connection information.
Example keys: "server_version", "connection_id", "protocol_version"
Implements database::core::database_backend.
Definition at line 368 of file mongodb_backend.cpp.
References connection_config_, database::core::connection_config::database, database::core::connection_config::host, in_transaction_, database::core::backend_base< mongodb_backend, database_types::mongodb >::initialized_, database::core::connection_config::port, and database::core::connection_config::username.
|
protected |
Database-specific initialization logic.
| config | Connection configuration |
Definition at line 41 of file mongodb_backend.cpp.
References build_connection_uri(), client_, connection_config_, database::connection_failed, database::core::connection_config::database, database_, db_name_, last_error_, and mongo_mutex_.

|
protected |
Database-specific shutdown logic.
Definition at line 92 of file mongodb_backend.cpp.
References client_, database_, in_transaction_, last_error_, mongo_mutex_, and rollback_transaction().

|
overridevirtual |
Execute a general SQL query (DDL, DML)
| query_string | SQL statement |
Implements database::core::database_backend.
Definition at line 226 of file mongodb_backend.cpp.
References database::connection_failed, database_, database::invalid_state, database::core::backend_base< mongodb_backend, database_types::mongodb >::is_initialized(), last_error_, mongo_mutex_, and database::query_failed.

|
overridevirtual |
Check if backend is currently in a transaction.
Implements database::core::database_backend.
Definition at line 358 of file mongodb_backend.cpp.
References in_transaction_.
|
overridevirtual |
Get last error message from backend.
Implements database::core::database_backend.
Definition at line 363 of file mongodb_backend.cpp.
References last_error_.
|
private |
Parse query string format.
| query_string | Query string in format "collection:filter:update" |
| collection | Output collection name |
| filter | Output filter JSON |
| update | Output update JSON (optional) |
Definition at line 115 of file mongodb_backend.cpp.
Referenced by select_query().

|
overridevirtual |
Rollback the current transaction.
Implements database::core::database_backend.
Definition at line 337 of file mongodb_backend.cpp.
References in_transaction_, database::invalid_state, database::core::backend_base< mongodb_backend, database_types::mongodb >::is_initialized(), and last_error_.
Referenced by do_shutdown().


|
overridevirtual |
Execute a SELECT query.
| query_string | SQL SELECT statement |
Implements database::core::database_backend.
Definition at line 136 of file mongodb_backend.cpp.
References database::connection_failed, database_, database::invalid_state, database::core::backend_base< mongodb_backend, database_types::mongodb >::is_initialized(), last_error_, mongo_mutex_, parse_query_string(), and database::query_failed.

|
friend |
Definition at line 106 of file mongodb_backend.h.
|
private |
MongoDB client (mongocxx::client*)
Definition at line 147 of file mongodb_backend.h.
Referenced by do_initialize(), and do_shutdown().
|
private |
Cached connection config.
Definition at line 152 of file mongodb_backend.h.
Referenced by connection_info(), and do_initialize().
|
private |
MongoDB database (mongocxx::database*)
Definition at line 148 of file mongodb_backend.h.
Referenced by do_initialize(), do_shutdown(), execute_query(), and select_query().
|
private |
|
private |
Transaction state.
Definition at line 150 of file mongodb_backend.h.
Referenced by begin_transaction(), commit_transaction(), connection_info(), do_shutdown(), in_transaction(), and rollback_transaction().
|
mutableprivate |
Last error message.
Definition at line 151 of file mongodb_backend.h.
Referenced by begin_transaction(), commit_transaction(), do_initialize(), do_shutdown(), execute_query(), last_error(), rollback_transaction(), and select_query().
|
mutableprivate |
Mutex for thread safety.
Definition at line 153 of file mongodb_backend.h.
Referenced by do_initialize(), do_shutdown(), execute_query(), and select_query().