13 os << ms.
a <<
" " << ms.
b;
24template <
typename MultimapType>
25void print_multimap(
const MultimapType& multimap,
const std::string& header) {
26 std::cout << header << std::endl;
27 for (
const auto& pair : multimap) {
28 std::cout <<
"Key: " << pair.first <<
" -> Struct: {" << pair.second.a <<
", " << pair.second.b <<
"}" << std::endl;
33template <
typename MapType>
35 std::cout << header << std::endl;
36 for (
const auto& pair : map) {
37 std::cout <<
"Key: " << pair.first <<
" -> ";
38 for (
const auto& item : pair.second) {
39 std::cout <<
"Struct: {" << item.a <<
", " << item.b <<
"} ";
41 std::cout << std::endl;
49 config.
db_path =
"example-multimap-struct.db";
59 std::multimap<int, MyStruct> multimap_pairs = {
70 std::map<int, std::list<MyStruct>> map_with_list_pairs = {
76 key_value_db.
append(multimap_pairs);
79 key_value_db.
append(map_with_list_pairs);
82 auto retrieved_key_value_pairs = key_value_db.
retrieve_all<std::multimap>();
83 print_multimap(retrieved_key_value_pairs,
"Key-value pairs in database after append:");
86 key_value_db.
insert(4, {50, 7.7});
87 key_value_db.
insert(4, {50, 7.7});
90 using map_with_list_t = std::map<int, std::list<MyStruct>>;
91 map_with_list_t retrieved_map_with_list_pairs = key_value_db.
retrieve_all<std::map, std::list>();
92 print_map_with_list(retrieved_map_with_list_pairs,
"Key-value pairs in database after insert:");
95 std::list<MyStruct> values;
96 if (key_value_db.
find(4, values)) {
97 std::cout <<
"Key 4 found in the database with values:" << std::endl;
98 for (
const auto& value : values) {
99 std::cout <<
"Struct: {" << value.a <<
", " << value.b <<
"}" << std::endl;
102 std::cout <<
"Key 4 not found in the database." << std::endl;
107 if (key_value_db.
find(10, values)) {
108 std::cout <<
"Key 10 found in the database with values:" << std::endl;
109 for (
const auto& value : values) {
110 std::cout <<
"Struct: {" << value.a <<
", " << value.b <<
"}" << std::endl;
113 std::cout <<
"Key 10 not found in the database." << std::endl;
117 key_value_db.
remove(2, {20, 2.2});
123 retrieved_key_value_pairs = key_value_db.
retrieve_all<std::multimap>();
124 print_multimap(retrieved_key_value_pairs,
"Key-value pairs in database after removals:");
130 retrieved_key_value_pairs = key_value_db.
retrieve_all<std::multimap>();
131 print_multimap(retrieved_key_value_pairs,
"Key-value pairs in database after reconcile:");
134 std::cerr <<
"SQLite error: " << e.what() << std::endl;
135 }
catch (
const std::exception &e) {
136 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.
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)
friend std::istream & operator>>(std::istream &is, MyStruct &ms)
friend std::ostream & operator<<(std::ostream &os, const MyStruct &ms)