SQLite Containers
|
Template class for managing key-value pairs in a SQLite database. More...
#include <KeyValueDB.hpp>
Public Member Functions | |
KeyValueDB () | |
Default constructor. | |
KeyValueDB (const Config &config) | |
Constructor with configuration. | |
~KeyValueDB () override final=default | |
Destructor. | |
template<template< class... > class ContainerT> | |
KeyValueDB & | operator= (const ContainerT< KeyT, ValueT > &container) |
Assigns a container (e.g., std::map or std::unordered_map) to the database. | |
template<template< class... > class ContainerT = std::map> | |
ContainerT< KeyT, ValueT > | operator() () |
Loads all key-value pairs from the database into a container (e.g., std::map or std::unordered_map). | |
template<template< class... > class ContainerT> | |
void | load (ContainerT< KeyT, ValueT > &container) |
Loads data from the database into the container. | |
template<template< class... > class ContainerT> | |
void | load (ContainerT< KeyT, ValueT > &container, const TransactionMode &mode) |
Loads data with a transaction. | |
template<template< class... > class ContainerT> | |
ContainerT< KeyT, ValueT > | retrieve_all () |
Retrieves all key-value pairs. | |
template<template< class... > class ContainerT> | |
ContainerT< KeyT, ValueT > | retrieve_all (const TransactionMode &mode) |
Retrieves all key-value pairs with a transaction. | |
template<template< class... > class ContainerT> | |
void | append (const ContainerT< KeyT, ValueT > &container) |
Appends data to the database. | |
template<template< class... > class ContainerT> | |
void | append (const ContainerT< KeyT, ValueT > &container, const TransactionMode &mode) |
Appends data with a transaction. | |
template<template< class... > class ContainerT> | |
void | reconcile (const ContainerT< KeyT, ValueT > &container) |
Reconciles the database with the container. | |
template<template< class... > class ContainerT> | |
void | reconcile (const ContainerT< KeyT, ValueT > &container, const TransactionMode &mode) |
Reconciles the database with the container using a transaction. | |
void | insert (const KeyT &key, const ValueT &value) |
Inserts a key-value pair into the database. | |
void | insert (const std::pair< KeyT, ValueT > &pair) |
Inserts a key-value pair into the database. | |
bool | find (const KeyT &key, ValueT &value) |
Finds a value by key. | |
std::size_t | count () const |
Returns the number of elements in the database. | |
bool | empty () const |
Checks if the database is empty. | |
void | remove (const KeyT &key) |
Removes a key-value pair from the database. | |
void | clear () |
Clears all key-value pairs from the database. | |
Public Member Functions inherited from sqlite_containers::BaseDB | |
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). | |
Private Member Functions | |
void | db_create_table (const Config &config) override final |
Creates the main and temporary tables in the database. This method creates both the main key-value table and a temporary table for handling synchronization. | |
template<template< class... > class ContainerT> | |
void | db_load (ContainerT< KeyT, ValueT > &container) |
Loads data from the database into the container. | |
bool | db_find (const KeyT &key, ValueT &value) |
Finds a value by key in the database. | |
std::size_t | db_count () const |
Returns the number of elements in the database. | |
template<template< class... > class ContainerT> | |
void | db_append (const ContainerT< KeyT, ValueT > &container) |
Appends the content of the container to the database. | |
template<template< class... > class ContainerT> | |
void | db_reconcile (const ContainerT< KeyT, ValueT > &container) |
Reconciles the content of the database with the container. Synchronizes the main table with the content of the container by using a temporary table. Clears old data, inserts new data, and updates existing records in the main table. | |
void | db_insert (const KeyT &key, const ValueT &value) |
Inserts a key-value pair into the database. | |
void | db_remove (const KeyT &key) |
Removes a key-value pair from the database. | |
void | db_clear () |
Clears all key-value pairs from the database. | |
Private Attributes | |
SqliteStmt | m_stmt_load |
Statement for loading data from the database. | |
SqliteStmt | m_stmt_replace |
Statement for replacing key-value pairs in the database. | |
SqliteStmt | m_stmt_get_value |
Statement for retrieving value by key from the database. | |
SqliteStmt | m_stmt_count |
SqliteStmt | m_stmt_remove |
Statement for removing key-value pair from the database. | |
SqliteStmt | m_stmt_clear_main |
Statement for clearing the main table. | |
SqliteStmt | m_stmt_insert_temp |
Statement for inserting data into the temporary table. | |
SqliteStmt | m_stmt_purge_main |
Statement for purging stale data from the main table. | |
SqliteStmt | m_stmt_merge_temp |
Statement for merging data from the temporary table into the main table. | |
SqliteStmt | m_stmt_clear_temp |
Statement for clearing the temporary table. | |
Additional Inherited Members | |
Protected Member Functions inherited from sqlite_containers::BaseDB | |
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 | 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 inherited from sqlite_containers::BaseDB | |
sqlite3 * | m_sqlite_db = nullptr |
std::mutex | m_sqlite_mutex |
Template class for managing key-value pairs in a SQLite database.
KeyT | Type of the keys. |
ValueT | Type of the values. |
This class provides functionality to store, retrieve, and manipulate key-value pairs in a SQLite database. It supports various container types, such as std::map
, std::unordered_map
, std::vector
, and std::list
. Key-value pairs can be inserted, reconciled, retrieved, and removed with transaction support. The class includes methods for bulk loading and appending data with transactional integrity, ensuring that operations are safely executed in a database environment. Additionally, temporary tables are used during reconciliation to ensure consistent data synchronization. This class also provides methods for checking the count and emptiness of the database, and efficiently handles database errors with detailed exception handling.
Definition at line 22 of file KeyValueDB.hpp.
|
inline |
Default constructor.
Definition at line 26 of file KeyValueDB.hpp.
|
inlineexplicit |
Constructor with configuration.
config | Configuration settings for the database. |
Definition at line 30 of file KeyValueDB.hpp.
|
finaloverridedefault |
Destructor.
|
inline |
Appends data to the database.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
container | Container with content to be synchronized. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 129 of file KeyValueDB.hpp.
|
inline |
Appends data with a transaction.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
container | Container with content to be synchronized. |
mode | Transaction mode. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 140 of file KeyValueDB.hpp.
|
inline |
Clears all key-value pairs from the database.
sqlite_exception | if an SQLite error occurs. |
Definition at line 224 of file KeyValueDB.hpp.
|
inline |
Returns the number of elements in the database.
sqlite_exception | if an SQLite error occurs. |
Definition at line 202 of file KeyValueDB.hpp.
|
inlineprivate |
Appends the content of the container to the database.
ContainerT | Template for the container type (map or unordered_map). |
container | Container with content to |
sqlite_exception | if an SQLite error occurs. |
Definition at line 402 of file KeyValueDB.hpp.
|
inlineprivate |
Clears all key-value pairs from the database.
sqlite_exception | if an SQLite error occurs. |
Definition at line 501 of file KeyValueDB.hpp.
|
inlineprivate |
Returns the number of elements in the database.
sqlite_exception | if an SQLite error occurs. |
Definition at line 363 of file KeyValueDB.hpp.
|
inlinefinaloverrideprivatevirtual |
Creates the main and temporary tables in the database. This method creates both the main key-value table and a temporary table for handling synchronization.
config | Configuration settings for the database, such as table names. |
Implements sqlite_containers::BaseDB.
Definition at line 245 of file KeyValueDB.hpp.
|
inlineprivate |
Finds a value by key in the database.
key | The key to search for. |
value | The value associated with the key. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 322 of file KeyValueDB.hpp.
|
inlineprivate |
Inserts a key-value pair into the database.
key | The key to be inserted. |
value | The value to be inserted. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 467 of file KeyValueDB.hpp.
|
inlineprivate |
Loads data from the database into the container.
ContainerT | Template for the container type. |
container | Container to be synchronized with database content. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 283 of file KeyValueDB.hpp.
|
inlineprivate |
Reconciles the content of the database with the container. Synchronizes the main table with the content of the container by using a temporary table. Clears old data, inserts new data, and updates existing records in the main table.
ContainerT | Template for the container type (map or unordered_map). |
container | Container with key-value pairs to be reconciled with the database. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 426 of file KeyValueDB.hpp.
|
inlineprivate |
Removes a key-value pair from the database.
key | The key of the pair to be removed. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 485 of file KeyValueDB.hpp.
|
inline |
Checks if the database is empty.
sqlite_exception | if an SQLite error occurs. |
Definition at line 210 of file KeyValueDB.hpp.
|
inline |
Finds a value by key.
key | The key to search for. |
value | The value associated with the key. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 194 of file KeyValueDB.hpp.
|
inline |
Inserts a key-value pair into the database.
key | The key to be inserted. |
value | The value to be inserted. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 176 of file KeyValueDB.hpp.
|
inline |
Inserts a key-value pair into the database.
pair | The key-value pair to be inserted. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 184 of file KeyValueDB.hpp.
|
inline |
Loads data from the database into the container.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
container | Container to be synchronized with database content. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 79 of file KeyValueDB.hpp.
|
inline |
Loads data with a transaction.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
container | Container to be synchronized with database content. |
mode | Transaction mode. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 90 of file KeyValueDB.hpp.
|
inline |
Loads all key-value pairs from the database into a container (e.g., std::map or std::unordered_map).
ContainerT | The type of the container (e.g., std::map or std::unordered_map). |
sqlite_exception | if an SQLite error occurs. |
Definition at line 61 of file KeyValueDB.hpp.
|
inline |
Assigns a container (e.g., std::map or std::unordered_map) to the database.
container | The container with key-value pairs. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 45 of file KeyValueDB.hpp.
|
inline |
Reconciles the database with the container.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
container | Container to be reconciled with the database. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 153 of file KeyValueDB.hpp.
|
inline |
Reconciles the database with the container using a transaction.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
container | Container to be reconciled with the database. |
mode | Transaction mode. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 164 of file KeyValueDB.hpp.
|
inline |
Removes a key-value pair from the database.
key | The key of the pair to be removed. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 217 of file KeyValueDB.hpp.
|
inline |
Retrieves all key-value pairs.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
sqlite_exception | if an SQLite error occurs. |
Definition at line 103 of file KeyValueDB.hpp.
|
inline |
Retrieves all key-value pairs with a transaction.
ContainerT | Container type (e.g., std::map or std::unordered_map). |
mode | Transaction mode. |
sqlite_exception | if an SQLite error occurs. |
Definition at line 116 of file KeyValueDB.hpp.
|
private |
Statement for clearing the main table.
Definition at line 235 of file KeyValueDB.hpp.
|
private |
Statement for clearing the temporary table.
Definition at line 240 of file KeyValueDB.hpp.
|
mutableprivate |
Definition at line 233 of file KeyValueDB.hpp.
|
private |
Statement for retrieving value by key from the database.
Definition at line 232 of file KeyValueDB.hpp.
|
private |
Statement for inserting data into the temporary table.
Definition at line 237 of file KeyValueDB.hpp.
|
private |
Statement for loading data from the database.
Definition at line 230 of file KeyValueDB.hpp.
|
private |
Statement for merging data from the temporary table into the main table.
Definition at line 239 of file KeyValueDB.hpp.
|
private |
Statement for purging stale data from the main table.
Definition at line 238 of file KeyValueDB.hpp.
|
private |
Statement for removing key-value pair from the database.
Definition at line 234 of file KeyValueDB.hpp.
|
private |
Statement for replacing key-value pairs in the database.
Definition at line 231 of file KeyValueDB.hpp.