Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
database::core::backend_base< Derived, Type > Class Template Reference

CRTP template base class for database backends. More...

#include <backend_base.h>

Inheritance diagram for database::core::backend_base< Derived, Type >:
Inheritance graph
Collaboration diagram for database::core::backend_base< Derived, Type >:
Collaboration graph

Public Member Functions

 backend_base ()=default
 Default constructor.
 
 ~backend_base () override
 Virtual destructor.
 
 backend_base (const backend_base &)=delete
 
backend_baseoperator= (const backend_base &)=delete
 
 backend_base (backend_base &&) noexcept=delete
 
backend_baseoperator= (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_resultselect_query (const std::string &query_string)=0
 Execute a SELECT query.
 
virtual kcenon::common::VoidResult execute_query (const std::string &query_string)=0
 Execute a general SQL query (DDL, DML)
 
virtual kcenon::common::Result< database_resultselect_prepared (const std::string &query, const std::vector< database_value > &params)
 Execute a parameterized SELECT query (prepared statement)
 
virtual kcenon::common::VoidResult execute_prepared (const std::string &query, const std::vector< database_value > &params)
 Execute a parameterized DML/DDL query (prepared statement)
 
virtual kcenon::common::VoidResult begin_transaction ()=0
 Begin a transaction.
 
virtual kcenon::common::VoidResult commit_transaction ()=0
 Commit the current transaction.
 
virtual kcenon::common::VoidResult rollback_transaction ()=0
 Rollback the current transaction.
 
virtual bool in_transaction () const =0
 Check if backend is currently in a transaction.
 
virtual std::string last_error () const =0
 Get last error message from backend.
 
virtual std::map< std::string, std::string > connection_info () const =0
 Get backend-specific connection information.
 

Static Public Member Functions

static std::unique_ptr< database_backendcreate ()
 Factory method for backend_registry.
 

Protected Attributes

std::atomic< bool > initialized_ {false}
 Initialization state.
 

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 > &params)
 Expand positional parameters into a SQL string (fallback)
 

Detailed Description

template<typename Derived, database_types Type>
class database::core::backend_base< Derived, Type >

CRTP template base class for database backends.

This template class implements the common lifecycle pattern shared by all database backends, reducing code duplication and ensuring consistent behavior.

Template Parameters
DerivedThe derived backend class (CRTP pattern)
TypeThe database_types enum value for this backend

Template Parameters:

  • Derived: Must implement:
    • static constexpr const char* backend_name()
    • VoidResult do_initialize(const connection_config&)
    • VoidResult do_shutdown()

Thread Safety:

  • initialized_ is atomic for thread-safe state queries
  • Derived classes must handle their own synchronization for connection access

Definition at line 73 of file backend_base.h.

Constructor & Destructor Documentation

◆ backend_base() [1/3]

template<typename Derived , database_types Type>
database::core::backend_base< Derived, Type >::backend_base ( )
default

Default constructor.

Initializes the backend in an uninitialized state.

◆ ~backend_base()

template<typename Derived , database_types Type>
database::core::backend_base< Derived, Type >::~backend_base ( )
inlineoverride

Virtual destructor.

Calls shutdown() to ensure proper cleanup of derived class resources.

Definition at line 88 of file backend_base.h.

89 {
90 shutdown();
91 }
kcenon::common::VoidResult shutdown() override
Shutdown the database backend gracefully.

References database::core::backend_base< Derived, Type >::shutdown().

Here is the call graph for this function:

◆ backend_base() [2/3]

template<typename Derived , database_types Type>
database::core::backend_base< Derived, Type >::backend_base ( const backend_base< Derived, Type > & )
delete

◆ backend_base() [3/3]

template<typename Derived , database_types Type>
database::core::backend_base< Derived, Type >::backend_base ( backend_base< Derived, Type > && )
deletenoexcept

Member Function Documentation

◆ create()

template<typename Derived , database_types Type>
static std::unique_ptr< database_backend > database::core::backend_base< Derived, Type >::create ( )
inlinestatic

Factory method for backend_registry.

Returns
Unique pointer to new backend instance

Definition at line 105 of file backend_base.h.

106 {
107 return std::make_unique<Derived>();
108 }

◆ initialize()

template<typename Derived , database_types Type>
kcenon::common::VoidResult database::core::backend_base< Derived, Type >::initialize ( const connection_config & config)
inlineoverridevirtual

Initialize the database backend.

Parameters
configConnection configuration
Returns
VoidResult::ok() on success, error on failure

Performs initialization guard check, then delegates to derived class. Derived class must implement do_initialize() for database-specific logic.

Implements database::core::database_backend.

Definition at line 127 of file backend_base.h.

128 {
129 if (initialized_) {
130 return kcenon::common::error_info{
131 static_cast<int>(database::error_code::invalid_state),
132 "Backend already initialized",
133 Derived::backend_name()
134 };
135 }
136
137 auto result = static_cast<Derived*>(this)->do_initialize(config);
138 if (result.is_ok()) {
139 initialized_ = true;
140 }
141 return result;
142 }
std::atomic< bool > initialized_
Initialization state.

References database::core::backend_base< Derived, Type >::initialized_, and database::invalid_state.

◆ is_initialized()

template<typename Derived , database_types Type>
bool database::core::backend_base< Derived, Type >::is_initialized ( ) const
inlineoverridevirtual

Check if backend is initialized and ready.

Returns
true if initialized and can accept queries

Implements database::core::database_backend.

Definition at line 166 of file backend_base.h.

167 {
168 return initialized_;
169 }

References database::core::backend_base< Derived, Type >::initialized_.

◆ operator=() [1/2]

template<typename Derived , database_types Type>
backend_base & database::core::backend_base< Derived, Type >::operator= ( backend_base< Derived, Type > && )
deletenoexcept

◆ operator=() [2/2]

template<typename Derived , database_types Type>
backend_base & database::core::backend_base< Derived, Type >::operator= ( const backend_base< Derived, Type > & )
delete

◆ shutdown()

template<typename Derived , database_types Type>
kcenon::common::VoidResult database::core::backend_base< Derived, Type >::shutdown ( )
inlineoverridevirtual

Shutdown the database backend gracefully.

Returns
VoidResult::ok() on success, error on failure

Performs shutdown guard check, then delegates to derived class. Derived class must implement do_shutdown() for database-specific cleanup.

Implements database::core::database_backend.

Definition at line 151 of file backend_base.h.

152 {
153 if (!initialized_) {
154 return kcenon::common::ok();
155 }
156
157 auto result = static_cast<Derived*>(this)->do_shutdown();
158 initialized_ = false;
159 return result;
160 }

References database::core::backend_base< Derived, Type >::initialized_.

Referenced by database::core::backend_base< Derived, Type >::~backend_base().

Here is the caller graph for this function:

◆ type()

template<typename Derived , database_types Type>
database_types database::core::backend_base< Derived, Type >::type ( ) const
inlineoverridevirtual

Get the database type of this backend.

Returns
Database type identifier from template parameter

Implements database::core::database_backend.

Definition at line 114 of file backend_base.h.

115 {
116 return Type;
117 }

Member Data Documentation

◆ initialized_

template<typename Derived , database_types Type>
std::atomic<bool> database::core::backend_base< Derived, Type >::initialized_ {false}
protected

The documentation for this class was generated from the following file: