Base class for SQLite database management.
More...
#include <BaseDB.hpp>
|
| 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).
|
|
|
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.
|
|
|
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.
|
|
Base class for SQLite database management.
Definition at line 23 of file BaseDB.hpp.
◆ BaseDB()
sqlite_containers::BaseDB::BaseDB |
( |
| ) |
|
|
default |
◆ ~BaseDB()
virtual sqlite_containers::BaseDB::~BaseDB |
( |
| ) |
|
|
inlinevirtual |
Destructor. Disconnects from the database if connected.
Definition at line 31 of file BaseDB.hpp.
◆ begin()
Begins a database transaction.
- Parameters
-
mode | Transaction mode (default: DEFERRED). |
- Exceptions
-
Definition at line 124 of file BaseDB.hpp.
◆ commit()
void sqlite_containers::BaseDB::commit |
( |
| ) |
|
|
inline |
Commits the current transaction.
- Exceptions
-
Definition at line 131 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
-
Definition at line 53 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
-
config | Configuration settings for the database. |
- Exceptions
-
Definition at line 90 of file BaseDB.hpp.
◆ db_begin()
Begins a transaction with the given mode.
- Parameters
-
mode | Transaction mode (defaults to DEFERRED). |
- Exceptions
-
Definition at line 176 of file BaseDB.hpp.
◆ db_commit()
void sqlite_containers::BaseDB::db_commit |
( |
| ) |
|
|
inlineprotected |
Commits the current transaction.
- Exceptions
-
Definition at line 182 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
-
config | Configuration settings, including the path to the database file. |
- Exceptions
-
Definition at line 235 of file BaseDB.hpp.
◆ db_create_table()
virtual void sqlite_containers::BaseDB::db_create_table |
( |
const Config & | config | ) |
|
|
protectedpure virtual |
◆ 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
-
ex | A pointer to the current exception (std::exception_ptr ) that needs to be handled. |
stmts | A vector of pointers to SqliteStmt objects, which will be reset and cleared. |
message | A custom error message used when an unknown exception is encountered. Defaults to "Unknown error occurred." |
- Exceptions
-
Definition at line 197 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
-
config | Configuration settings. |
Definition at line 270 of file BaseDB.hpp.
◆ db_open()
void sqlite_containers::BaseDB::db_open |
( |
const Config & | config | ) |
|
|
inlineprivate |
Opens the database with the specified configuration.
- Parameters
-
config | Configuration settings. |
- Exceptions
-
Definition at line 249 of file BaseDB.hpp.
◆ db_rollback()
void sqlite_containers::BaseDB::db_rollback |
( |
| ) |
|
|
inlineprotected |
Rolls back the current transaction.
- Exceptions
-
Definition at line 188 of file BaseDB.hpp.
◆ disconnect()
void sqlite_containers::BaseDB::disconnect |
( |
| ) |
|
|
inline |
Disconnects from the database.
- Exceptions
-
Definition at line 97 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
-
operation | The operation to execute. |
mode | The transaction mode. |
- Exceptions
-
Definition at line 148 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 45 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 311 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 307 of file BaseDB.hpp.
◆ process()
virtual void sqlite_containers::BaseDB::process |
( |
| ) |
|
|
inlinevirtual |
Processes asynchronous database requests (can be overridden).
Definition at line 167 of file BaseDB.hpp.
◆ rollback()
void sqlite_containers::BaseDB::rollback |
( |
| ) |
|
|
inline |
Rolls back the current transaction.
- Exceptions
-
Definition at line 138 of file BaseDB.hpp.
◆ set_config()
void sqlite_containers::BaseDB::set_config |
( |
const Config & | config | ) |
|
|
inline |
Sets the configuration for the database.
- Parameters
-
config | Configuration settings for the database. |
Definition at line 37 of file BaseDB.hpp.
◆ m_config
Config sqlite_containers::BaseDB::m_config |
|
private |
◆ m_config_mutex
std::mutex sqlite_containers::BaseDB::m_config_mutex |
|
mutableprivate |
◆ m_config_new
Config sqlite_containers::BaseDB::m_config_new |
|
private |
◆ m_config_update
std::atomic<bool> sqlite_containers::BaseDB::m_config_update = ATOMIC_VAR_INIT(false) |
|
private |
◆ m_future
std::shared_future<void> sqlite_containers::BaseDB::m_future |
|
private |
◆ m_sqlite_db
sqlite3* sqlite_containers::BaseDB::m_sqlite_db = nullptr |
|
protected |
◆ m_sqlite_mutex
std::mutex sqlite_containers::BaseDB::m_sqlite_mutex |
|
mutableprotected |
◆ m_stmt_begin
std::array<SqliteStmt, 3> sqlite_containers::BaseDB::m_stmt_begin |
|
private |
◆ m_stmt_commit
SqliteStmt sqlite_containers::BaseDB::m_stmt_commit |
|
private |
◆ m_stmt_rollback
SqliteStmt sqlite_containers::BaseDB::m_stmt_rollback |
|
private |
The documentation for this class was generated from the following file: