SQLite Containers
Loading...
Searching...
No Matches
sqlite_containers::BaseDB Class Referenceabstract

Base class for SQLite database management. More...

#include <BaseDB.hpp>

Inheritance diagram for sqlite_containers::BaseDB:
sqlite_containers::KeyDB< KeyT > sqlite_containers::KeyMultiValueDB< KeyT, ValueT > sqlite_containers::KeyValueDB< KeyT, ValueT >

Public Member Functions

 BaseDB ()=default
 Default constructor.
 
virtual ~BaseDB ()
 Destructor. Disconnects from the database if connected.
 
void set_config (const Config &config)
 Sets the configuration for the database.
 
Config get_config () const
 Gets the current configuration of the database.
 
void connect ()
 Connects to the database using the current configuration. Initializes a connection to the database by creating necessary directories, opening the database, creating tables, and setting up database parameters.
 
void connect (const Config &config)
 Connects to the database with the given configuration.
 
void disconnect ()
 Disconnects from the database.
 
void begin (const TransactionMode &mode=TransactionMode::DEFERRED)
 Begins a database transaction.
 
void commit ()
 Commits the current transaction.
 
void rollback ()
 Rolls back the current transaction.
 
template<typename Func>
void execute_in_transaction (Func operation, const TransactionMode &mode)
 Executes an operation inside a transaction.
 
virtual void process ()
 Processes asynchronous database requests (can be overridden).
 

Protected Member Functions

void db_begin (const TransactionMode &mode=TransactionMode::DEFERRED)
 Begins a transaction with the given mode.
 
void db_commit ()
 Commits the current transaction.
 
void db_rollback ()
 Rolls back the current transaction.
 
void db_handle_exception (std::exception_ptr ex, const std::vector< SqliteStmt * > &stmts, const std::string &message="Unknown error occurred.") const
 Handles an exception by resetting and clearing bindings of prepared SQL statements.
 
virtual void db_create_table (const Config &config)=0
 Creates tables in the database. Must be implemented in derived classes.
 
virtual void on_db_open ()
 Called after the database is opened. Can be overridden in derived classes.
 
virtual void on_db_close ()
 Called before the database is closed. Can be overridden in derived classes.
 

Protected Attributes

sqlite3 * m_sqlite_db = nullptr
 
std::mutex m_sqlite_mutex
 

Private Member Functions

void db_create_directories (const Config &config)
 Creates necessary directories for the database. This method checks if the parent directory of the database file exists, and if not, attempts to create it.
 
void db_open (const Config &config)
 Opens the database with the specified configuration.
 
void db_init (const Config &config)
 Initializes the database with the given configuration. Sets database parameters such as busy timeout, page size, cache size, journal mode, and other settings.
 

Private Attributes

std::array< SqliteStmt, 3 > m_stmt_begin
 
SqliteStmt m_stmt_commit
 
SqliteStmt m_stmt_rollback
 
Config m_config_new
 
Config m_config
 
std::mutex m_config_mutex
 
std::atomic< bool > m_config_update = ATOMIC_VAR_INIT(false)
 
std::shared_future< void > m_future
 

Detailed Description

Base class for SQLite database management.

Definition at line 27 of file BaseDB.hpp.

Constructor & Destructor Documentation

◆ BaseDB()

sqlite_containers::BaseDB::BaseDB ( )
default

Default constructor.

◆ ~BaseDB()

virtual sqlite_containers::BaseDB::~BaseDB ( )
inlinevirtual

Destructor. Disconnects from the database if connected.

Definition at line 35 of file BaseDB.hpp.

Member Function Documentation

◆ begin()

void sqlite_containers::BaseDB::begin ( const TransactionMode & mode = TransactionMode::DEFERRED)
inline

Begins a database transaction.

Parameters
modeTransaction mode (default: DEFERRED).
Exceptions
sqlite_exceptionif the transaction fails.

Definition at line 128 of file BaseDB.hpp.

◆ commit()

void sqlite_containers::BaseDB::commit ( )
inline

Commits the current transaction.

Exceptions
sqlite_exceptionif the commit fails.

Definition at line 135 of file BaseDB.hpp.

◆ connect() [1/2]

void sqlite_containers::BaseDB::connect ( )
inline

Connects to the database using the current configuration. Initializes a connection to the database by creating necessary directories, opening the database, creating tables, and setting up database parameters.

Exceptions
sqlite_exceptionif connection fails.

Definition at line 57 of file BaseDB.hpp.

◆ connect() [2/2]

void sqlite_containers::BaseDB::connect ( const Config & config)
inline

Connects to the database with the given configuration.

Parameters
configConfiguration settings for the database.
Exceptions
sqlite_exceptionif connection fails.

Definition at line 94 of file BaseDB.hpp.

◆ db_begin()

void sqlite_containers::BaseDB::db_begin ( const TransactionMode & mode = TransactionMode::DEFERRED)
inlineprotected

Begins a transaction with the given mode.

Parameters
modeTransaction mode (defaults to DEFERRED).
Exceptions
sqlite_exceptionif the transaction fails.

Definition at line 180 of file BaseDB.hpp.

◆ db_commit()

void sqlite_containers::BaseDB::db_commit ( )
inlineprotected

Commits the current transaction.

Exceptions
sqlite_exceptionif the commit fails.

Definition at line 186 of file BaseDB.hpp.

◆ db_create_directories()

void sqlite_containers::BaseDB::db_create_directories ( const Config & config)
inlineprivate

Creates necessary directories for the database. This method checks if the parent directory of the database file exists, and if not, attempts to create it.

Parameters
configConfiguration settings, including the path to the database file.
Exceptions
sqlite_exceptionIf the directories cannot be created.

Definition at line 239 of file BaseDB.hpp.

◆ db_create_table()

virtual void sqlite_containers::BaseDB::db_create_table ( const Config & config)
protectedpure virtual

Creates tables in the database. Must be implemented in derived classes.

Parameters
configConfiguration settings.

Implemented in sqlite_containers::KeyDB< KeyT >, sqlite_containers::KeyMultiValueDB< KeyT, ValueT >, and sqlite_containers::KeyValueDB< KeyT, ValueT >.

◆ db_handle_exception()

void sqlite_containers::BaseDB::db_handle_exception ( std::exception_ptr ex,
const std::vector< SqliteStmt * > & stmts,
const std::string & message = "Unknown error occurred." ) const
inlineprotected

Handles an exception by resetting and clearing bindings of prepared SQL statements.

Parameters
exA pointer to the current exception (std::exception_ptr) that needs to be handled.
stmtsA vector of pointers to SqliteStmt objects, which will be reset and cleared.
messageA custom error message used when an unknown exception is encountered. Defaults to "Unknown error occurred."
Exceptions
sqlite_exceptionIf an unknown exception or std::exception is encountered, it will be rethrown as sqlite_exception. If the caught exception is already sqlite_exception, it will be rethrown as is.

Definition at line 201 of file BaseDB.hpp.

◆ db_init()

void sqlite_containers::BaseDB::db_init ( const Config & config)
inlineprivate

Initializes the database with the given configuration. Sets database parameters such as busy timeout, page size, cache size, journal mode, and other settings.

Parameters
configConfiguration settings.

Definition at line 281 of file BaseDB.hpp.

◆ db_open()

void sqlite_containers::BaseDB::db_open ( const Config & config)
inlineprivate

Opens the database with the specified configuration.

Parameters
configConfiguration settings.
Exceptions
sqlite_exceptionif the database fails to open.

Definition at line 260 of file BaseDB.hpp.

◆ db_rollback()

void sqlite_containers::BaseDB::db_rollback ( )
inlineprotected

Rolls back the current transaction.

Exceptions
sqlite_exceptionif the rollback fails.

Definition at line 192 of file BaseDB.hpp.

◆ disconnect()

void sqlite_containers::BaseDB::disconnect ( )
inline

Disconnects from the database.

Exceptions
sqlite_exceptionif disconnect fails.

Definition at line 101 of file BaseDB.hpp.

◆ execute_in_transaction()

template<typename Func>
void sqlite_containers::BaseDB::execute_in_transaction ( Func operation,
const TransactionMode & mode )
inline

Executes an operation inside a transaction.

Parameters
operationThe operation to execute.
modeThe transaction mode.
Exceptions
sqlite_exceptionif an error occurs during execution.

Definition at line 152 of file BaseDB.hpp.

◆ get_config()

Config sqlite_containers::BaseDB::get_config ( ) const
inline

Gets the current configuration of the database.

Returns
The current configuration settings.

Definition at line 49 of file BaseDB.hpp.

◆ on_db_close()

virtual void sqlite_containers::BaseDB::on_db_close ( )
inlineprotectedvirtual

Called before the database is closed. Can be overridden in derived classes.

Definition at line 322 of file BaseDB.hpp.

◆ on_db_open()

virtual void sqlite_containers::BaseDB::on_db_open ( )
inlineprotectedvirtual

Called after the database is opened. Can be overridden in derived classes.

Definition at line 318 of file BaseDB.hpp.

◆ process()

virtual void sqlite_containers::BaseDB::process ( )
inlinevirtual

Processes asynchronous database requests (can be overridden).

Definition at line 171 of file BaseDB.hpp.

◆ rollback()

void sqlite_containers::BaseDB::rollback ( )
inline

Rolls back the current transaction.

Exceptions
sqlite_exceptionif the rollback fails.

Definition at line 142 of file BaseDB.hpp.

◆ set_config()

void sqlite_containers::BaseDB::set_config ( const Config & config)
inline

Sets the configuration for the database.

Parameters
configConfiguration settings for the database.

Definition at line 41 of file BaseDB.hpp.

Member Data Documentation

◆ m_config

Config sqlite_containers::BaseDB::m_config
private

Definition at line 229 of file BaseDB.hpp.

◆ m_config_mutex

std::mutex sqlite_containers::BaseDB::m_config_mutex
mutableprivate

Definition at line 230 of file BaseDB.hpp.

◆ m_config_new

Config sqlite_containers::BaseDB::m_config_new
private

Definition at line 228 of file BaseDB.hpp.

◆ m_config_update

std::atomic<bool> sqlite_containers::BaseDB::m_config_update = ATOMIC_VAR_INIT(false)
private

Definition at line 231 of file BaseDB.hpp.

◆ m_future

std::shared_future<void> sqlite_containers::BaseDB::m_future
private

Definition at line 233 of file BaseDB.hpp.

◆ m_sqlite_db

sqlite3* sqlite_containers::BaseDB::m_sqlite_db = nullptr
protected

Definition at line 174 of file BaseDB.hpp.

◆ m_sqlite_mutex

std::mutex sqlite_containers::BaseDB::m_sqlite_mutex
mutableprotected

Definition at line 175 of file BaseDB.hpp.

◆ m_stmt_begin

std::array<SqliteStmt, 3> sqlite_containers::BaseDB::m_stmt_begin
private

Definition at line 224 of file BaseDB.hpp.

◆ m_stmt_commit

SqliteStmt sqlite_containers::BaseDB::m_stmt_commit
private

Definition at line 225 of file BaseDB.hpp.

◆ m_stmt_rollback

SqliteStmt sqlite_containers::BaseDB::m_stmt_rollback
private

Definition at line 226 of file BaseDB.hpp.


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