SQLite Containers
Loading...
Searching...
No Matches
example-multimap.cpp
Go to the documentation of this file.
2#include <iostream>
3#include <map>
4#include <set>
5#include <list>
6
7// Utility function to print contents of a multimap
8template <typename MultimapType>
9void print_multimap(const MultimapType& multimap, const std::string& header) {
10 std::cout << header << std::endl;
11 for (const auto& pair : multimap) {
12 std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
13 }
14}
15
16// Utility function to print contents of a map with list as values
17template <typename MapType>
18void print_map_with_list(const MapType& map, const std::string& header) {
19 std::cout << header << std::endl;
20 for (const auto& pair : map) {
21 for (const auto& item : pair.second) {
22 std::cout << "Key: " << pair.first << ", Value: " << item << std::endl;
23 }
24 }
25}
26
27int main() {
28 try {
29 // Create a configuration for the database
31 config.db_path = "example-multimap.db";
32
33 // Create a KeyMultiValueDB instance
35 key_value_db.connect();
36
37 // Clear the table for a fresh start
38 key_value_db.clear();
39
40 // Create a std::multimap with key-value pairs
41 std::multimap<int, std::string> multimap_pairs = {
42 {1, "apple"},
43 {2, "banana"},
44 {2, "banana"}, // Duplicate key-value pair
45 {1, "apricot"},
46 {3, "cherry"},
47 {2, "blueberry"}
48 };
49
50 // Create a std::map with std::set as values
51 std::map<int, std::set<std::string>> map_with_set_pairs = {
52 {3, {"cherry"}}, // Key 3 maps to a set with "cherry"
53 {1, {"banana"}}, // Key 1 maps to a set with "banana"
54 {4, {}}, // Key 4 with an empty set, will be ignored during insertion
55 };
56
57 // Append the contents of the std::multimap to the database
58 key_value_db.append(multimap_pairs);
59
60 // Append the contents of the std::map with std::set values to the database
61 key_value_db.append(map_with_set_pairs);
62
63 // Retrieve all key-value pairs from the database and print them
64 auto retrieved_key_value_pairs = key_value_db.retrieve_all<std::multimap>();
65 print_multimap(retrieved_key_value_pairs, "Key-value pairs in database after append:");
66
67 // Use operator= to assign the std::multimap to the database
68 key_value_db = multimap_pairs;
69
70 // Retrieve all key-value pairs from the database and print them
71 retrieved_key_value_pairs = key_value_db.retrieve_all<std::multimap>();
72 print_multimap(retrieved_key_value_pairs, "Key-value pairs in database after operator= assignment:");
73
74 // Use operator= to assign the std::map with std::set values to the database
75 key_value_db = map_with_set_pairs;
76
77 // Retrieve all key-value pairs from the database and print them
78 retrieved_key_value_pairs = key_value_db.retrieve_all<std::multimap>();
79 print_multimap(retrieved_key_value_pairs, "Key-value pairs in database after operator= assignment with map:");
80
81 // Insert a new key-value pair
82 key_value_db.insert(4, "date");
83 key_value_db.insert(4, "date");
84
85 // Retrieve all key-value pairs from the database into a std::map with std::list values and print them
86 using map_with_list_t = std::map<int, std::list<std::string>>;
87 map_with_list_t retrieved_map_with_list_pairs = key_value_db.retrieve_all<std::map, std::list>();
88 print_map_with_list(retrieved_map_with_list_pairs, "Key-value pairs in database after insert:");
89
90 // Check if the key exists in the database
91 std::list<std::string> values;
92 if (key_value_db.find(4, values)) {
93 std::cout << "Key 4 found in the database with values:" << std::endl;
94 for (const auto& value : values) {
95 std::cout << value << std::endl;
96 }
97 } else {
98 std::cout << "Key 4 not found in the database." << std::endl;
99 }
100
101 // Check if a non-existing key is found
102 values.clear();
103 if (key_value_db.find(10, values)) {
104 std::cout << "Key 10 found in the database with values:" << std::endl;
105 for (const auto& value : values) {
106 std::cout << value << " ";
107 }
108 std::cout << std::endl;
109 } else {
110 std::cout << "Key 10 not found in the database." << std::endl;
111 }
112
113 // Remove a specific key-value pair
114 key_value_db.remove(2, "banana");
115
116 // Remove all values associated with a key
117 key_value_db.remove(1);
118
119 // Retrieve all key-value pairs from the database after removal and print them
120 retrieved_key_value_pairs = key_value_db.retrieve_all<std::multimap>();
121 print_multimap(retrieved_key_value_pairs, "Key-value pairs in database after removals:");
122
123 // Reconcile the database with the contents of the std::multimap
124 key_value_db.reconcile(multimap_pairs);
125
126 // Retrieve all key-value pairs from the database after reconciliation and print them
127 retrieved_key_value_pairs = key_value_db();
128 print_multimap(retrieved_key_value_pairs, "Key-value pairs in database after reconcile:");
129
130 // Reconcile the database with the contents of the std::map with sets using operator=
131 key_value_db = map_with_set_pairs;
132
133 // Retrieve all key-value pairs from the database after reconciliation and print them
134 retrieved_key_value_pairs = key_value_db.retrieve_all<std::multimap>();
135 print_multimap(retrieved_key_value_pairs, "Key-value pairs in database after operator= reconciliation:");
136
137 // Check the number of keys in the database
138 std::cout << "Number of keys in the database: " << key_value_db.count() << std::endl;
139
140 // Check if the database is empty
141 std::cout << "Is the database empty? " << (key_value_db.empty() ? "Yes" : "No") << std::endl;
142
143 } catch (const sqlite_containers::sqlite_exception &e) {
144 std::cerr << "SQLite error: " << e.what() << std::endl;
145 } catch (const std::exception &e) {
146 std::cerr << "Error: " << e.what() << std::endl;
147 }
148
149 return 0;
150}
151
Template 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, where each key can map to multiple ...
void append(const ContainerT< KeyT, ValueT > &container, const TransactionMode &mode)
Appends the content of the container to the database with a transaction.
ContainerT< KeyT, ValueT > retrieve_all(const TransactionMode &mode)
Retrieves all key-value pairs from the database with a transaction.
void insert(const KeyT &key, const ValueT &value, const TransactionMode &mode)
Inserts a key-value pair into the database with a transaction.
void clear(const TransactionMode &mode)
Clears all key-value pairs from the database with a transaction.
void reconcile(const ContainerT< KeyT, ValueT > &container, const TransactionMode &mode)
Reconciles the content of the container with the database with a transaction.
std::size_t count(const KeyT &key, const ValueT &value) const
Alias for get_value_count(). This method is an alias for get_value_count() and performs the same oper...
bool empty() const
Checks if the database is empty. This method checks if there are any keys stored in the database.
bool find(const KeyT &key, ContainerT< ValueT > &values)
Finds values by key in the database.
void remove(const KeyT &key, const ValueT &value)
Removes a specific key-value pair from the database.
Exception class for SQLite errors.
Definition Utils.hpp:27
void print_multimap(const MultimapType &multimap, const std::string &header)
void print_map_with_list(const MapType &map, const std::string &header)
int main()