SQLite Containers is a lightweight header-only C++ library designed to integrate SQLite databases with standard C++ containers, such as std::map
and other associative containers. This library abstracts the complexity of database operations, allowing developers to interact with SQLite databases using familiar container interfaces. It ensures data persistence while providing robust mechanisms for synchronization between in-memory data structures and persistent storage.
Features
- Container Integration: Synchronizes SQLite databases with standard C++ containers, including
std::map
, std::unordered_map
, std::set
, std::vector
, and more.
- Easy-to-Use API: Simplified API for common operations like
insert
, remove
, find
, sync
, and clear
.
- Multiple Classes, Single Database: Supports the use of multiple class instances (e.g.,
KeyDB
, KeyValueDB
, KeyMultiValueDB
) operating on different tables in the same SQLite database file.
- Exception Handling: Exception handling to manage SQLite errors gracefully.
- Thread-Safety: Thread-safe interactions with the database to ensure consistent state in multi-threaded applications.
- Transaction Management: Supports transactions to guarantee data integrity during complex operations.
- Temporary Tables: Uses temporary tables for efficient data synchronization.
- Customizable Configuration: Allows for detailed configuration of database paths, table names, and other settings.
- C++17 Compatible: Designed to work with C++17, leveraging modern language features.
Requirements
The library is compatible with C++17
and requires the SQLite3 library. Ensure that SQLite is compiled with SQLITE_THREADSAFE=1
for multi-threading support.
Installation
To use the SQLite Containers library, include the source files in your project and link against the SQLite3 library.
SQLite Containers will create the database file and all intermediate directories if they do not already exist.
Key Classes
KeyDB
: Manages unique keys in containers like std::set
, std::unordered_set
, std::list
, and std::vector
.
KeyValueDB
: Manages key-value pairs in containers like std::map
, std::unordered_map
.
KeyMultiValueDB
: Manages key-value pairs where each key maps to multiple values. The database follows a many-to-many relationship model. This class allows flexibility in how keys and values are stored and supports operations with containers such as std::multimap
, std::unordered_multimap
, or containers where keys map to collections of values, like std::map<KeyT, std::vector<ValueT>>
.
Example of Using Multiple Classes with a Single Database
Each class can operate on a different table within the same SQLite database file by setting the table_name
in the Config
object:
key_db.connect();
Configuration class for SQLite database settings.
std::string db_path
Path to the SQLite database file.
std::string table_name
Name of the database table.
Template class for managing keys in a SQLite database.
Example of Using Transactions
You can execute multiple operations within a single transaction using the execute_in_transaction
method:
key_value_db.execute_in_transaction([&key_value_db] {
key_value_db.insert("apple", 1.1);
key_value_db.insert("banana", 2.2);
});
Transaction Support for Data Manipulation
Some methods, such as append
, load
, and reconcile
, support an optional transaction mode:
std::set<int> int_keys = {1, 2, 3, 4};
@ IMMEDIATE
Locks the database for writing at the start, allowing only read operations by others.
Configuration Options
The Config
class allows customization of database settings:
class Config {
public:
};
}
int wal_autocheckpoint
WAL auto-checkpoint threshold.
int analysis_limit
Maximum number of rows to analyze.
AutoVacuumMode auto_vacuum_mode
SQLite auto-vacuum mode.
JournalMode journal_mode
SQLite journal mode.
bool read_only
Whether the database is in read-only mode.
int user_version
User-defined version number for the database schema.
SynchronousMode synchronous
SQLite synchronous mode.
bool use_uri
Whether to use URI for opening the database.
bool use_async
Whether to use asynchronous write.
bool in_memory
Whether the database should be in-memory.
TransactionMode default_txn_mode
LockingMode locking_mode
SQLite locking mode.
int busy_timeout
Timeout in milliseconds for busy handler.
int page_size
SQLite page size.
int cache_size
SQLite cache size (in pages).
SynchronousMode
SQLite synchronous modes enumeration.
@ FULL
Full synchronous mode.
TransactionMode
Defines SQLite transaction modes.
LockingMode
SQLite locking modes enumeration.
@ NORMAL
Normal locking mode.
JournalMode
SQLite journal modes enumeration.
@ DELETE_MODE
Delete journal mode.
AutoVacuumMode
SQLite auto-vacuum modes enumeration.
You can configure the default transaction mode, table name, database path, and other important parameters.
Struct Support
For classes that support key-value pairs, the value must be a structure composed of simple data types.