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;
17template <
typename MapType>
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;
31 config.
db_path =
"example-multimap.db";
41 std::multimap<int, std::string> multimap_pairs = {
51 std::map<int, std::set<std::string>> map_with_set_pairs = {
58 key_value_db.
append(multimap_pairs);
61 key_value_db.
append(map_with_set_pairs);
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:");
68 key_value_db = multimap_pairs;
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:");
75 key_value_db = map_with_set_pairs;
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:");
82 key_value_db.
insert(4,
"date");
83 key_value_db.
insert(4,
"date");
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:");
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;
98 std::cout <<
"Key 4 not found in the database." << std::endl;
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 <<
" ";
108 std::cout << std::endl;
110 std::cout <<
"Key 10 not found in the database." << std::endl;
114 key_value_db.
remove(2,
"banana");
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:");
127 retrieved_key_value_pairs = key_value_db();
128 print_multimap(retrieved_key_value_pairs,
"Key-value pairs in database after reconcile:");
131 key_value_db = map_with_set_pairs;
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:");
138 std::cout <<
"Number of keys in the database: " << key_value_db.
count() << std::endl;
141 std::cout <<
"Is the database empty? " << (key_value_db.
empty() ?
"Yes" :
"No") << std::endl;
144 std::cerr <<
"SQLite error: " << e.what() << std::endl;
145 }
catch (
const std::exception &e) {
146 std::cerr <<
"Error: " << e.what() << std::endl;
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...
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, 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.
void print_multimap(const MultimapType &multimap, const std::string &header)
void print_map_with_list(const MapType &map, const std::string &header)