SQLite Containers
Loading...
Searching...
No Matches
example-map.cpp
Go to the documentation of this file.
2#include <iostream>
3#include <map>
4
5// Utility function to print contents of the map
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;
11 }
12}
13
14int main() {
15 // Create a configuration for the database
17 config.db_path = "example-map.db";
18
19 // Create a KeyValueDB instance
21 map_db.connect();
22
23 // Insert key-value pairs
24 map_db.insert(1, "value1");
25 map_db.insert(2, "value2");
26 map_db.insert(3, "value3");
27
28 // Find and print a value by key
29 std::string value;
30 if (map_db.find(2, value)) {
31 std::cout << "Found value for key 2: " << value << std::endl;
32 } else {
33 std::cout << "Key 2 not found." << std::endl;
34 }
35
36 // Load the database contents into a std::map using the overloaded () operator
37 std::map<int, std::string> my_map;
38 my_map = map_db(); // Use the overloaded () operator to load data from the database into the map
39 print_map(my_map, "Contents of my_map after using operator():");
40
41 // Load the database contents into another std::map using the load method
42 std::map<int, std::string> my_map2;
43 map_db.load(my_map2); // Use the load method to explicitly load data from the database
44 print_map(my_map2, "Contents of my_map2 after using load:");
45
46 // Retrieve and print all key-value pairs directly from the database
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:");
49
50 // Remove a key-value pair and print the result
51 map_db.remove(3);
52 all_entries = map_db.retrieve_all<std::map>();
53 print_map(all_entries, "Contents of database after removing key 3:");
54
55 // Insert a new key-value pair and print the result
56 map_db.insert(4, "value4");
57 all_entries = map_db.retrieve_all<std::map>();
58 print_map(all_entries, "Contents of database after inserting key 4:");
59
60 // Append the contents of the std::map to the database and print the result
61 my_map[5] = "value5";
62 map_db.append(my_map);
63 all_entries = map_db.retrieve_all<std::map>();
64 print_map(all_entries, "Contents of database after append:");
65
66 // Reconcile the contents of the std::map with the database and print the result
67 my_map.erase(5);
68 my_map[6] = "value6";
69 map_db.reconcile(my_map);
70 all_entries = map_db.retrieve_all<std::map>();
71 print_map(all_entries, "Contents of database after reconcile:");
72
73 // Use operator= to reconcile (equivalent to calling reconcile)
74 my_map.erase(6);
75 my_map[7] = "value7";
76 map_db = my_map; // Operator= to append or reconcile the contents of the map to the database
77 all_entries = map_db.retrieve_all<std::map>();
78 print_map(all_entries, "Contents of database after using operator= to reconcile:");
79
80 // Print count and empty status of the database
81 std::cout << "count: " << map_db.count() << std::endl;
82 std::cout << "empty: " << map_db.empty() << std::endl;
83
84 // Clear the database and print the result
85 map_db.clear();
86 all_entries = map_db.retrieve_all<std::map>();
87 if (all_entries.empty()) {
88 std::cout << "Database is empty after clear." << std::endl;
89 } else {
90 print_map(all_entries, "Contents of database after clear:");
91 }
92
93 // Print the final empty status
94 std::cout << "empty: " << map_db.empty() << std::endl;
95
96 return 0;
97}
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...
Definition BaseDB.hpp:53
Configuration class for SQLite database settings.
Definition Config.hpp:11
std::string db_path
Path to the SQLite database file.
Definition Config.hpp:13
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)
int main()