6template <
typename MapType>
7void print_map(
const MapType& map,
const std::string& header) {
8 std::cout << header << std::endl;
9 for (
const auto& pair : map) {
10 std::cout <<
"Key: " << pair.first <<
", Value: " << pair.second << std::endl;
17 config.
db_path =
"example-map.db";
24 map_db.
insert(1,
"value1");
25 map_db.
insert(2,
"value2");
26 map_db.
insert(3,
"value3");
30 if (map_db.
find(2, value)) {
31 std::cout <<
"Found value for key 2: " << value << std::endl;
33 std::cout <<
"Key 2 not found." << std::endl;
37 std::map<int, std::string> my_map;
39 print_map(my_map,
"Contents of my_map after using operator():");
42 std::map<int, std::string> my_map2;
44 print_map(my_map2,
"Contents of my_map2 after using load:");
47 std::map<int, std::string> all_entries = map_db.
retrieve_all<std::map>();
48 print_map(all_entries,
"Contents of database using retrieve_all:");
53 print_map(all_entries,
"Contents of database after removing key 3:");
56 map_db.
insert(4,
"value4");
58 print_map(all_entries,
"Contents of database after inserting key 4:");
64 print_map(all_entries,
"Contents of database after append:");
71 print_map(all_entries,
"Contents of database after reconcile:");
78 print_map(all_entries,
"Contents of database after using operator= to reconcile:");
81 std::cout <<
"count: " << map_db.
count() << std::endl;
82 std::cout <<
"empty: " << map_db.
empty() << std::endl;
87 if (all_entries.empty()) {
88 std::cout <<
"Database is empty after clear." << std::endl;
90 print_map(all_entries,
"Contents of database after clear:");
94 std::cout <<
"empty: " << map_db.
empty() << std::endl;
Declaration of the KeyValueDB class for managing key-value pairs in a SQLite database.
void connect()
Connects to the database using the current configuration. Initializes a connection to the database by...
Configuration class for SQLite database settings.
std::string db_path
Path to the SQLite database file.
Template class for managing key-value pairs in a SQLite database.
std::size_t count() const
Returns the number of elements in the database.
void clear()
Clears all key-value pairs from the database.
void remove(const KeyT &key)
Removes a key-value pair from the database.
void reconcile(const ContainerT< KeyT, ValueT > &container)
Reconciles the database with the container.
void insert(const KeyT &key, const ValueT &value)
Inserts a key-value pair into the database.
ContainerT< KeyT, ValueT > retrieve_all()
Retrieves all key-value pairs.
bool empty() const
Checks if the database is empty.
bool find(const KeyT &key, ValueT &value)
Finds a value by key.
void append(const ContainerT< KeyT, ValueT > &container)
Appends data to the database.
void load(ContainerT< KeyT, ValueT > &container)
Loads data from the database into the container.
void print_map(const MapType &map, const std::string &header)