SQLite Containers
Loading...
Searching...
No Matches
example-set-struct.cpp
Go to the documentation of this file.
2#include <iostream>
3#include <set>
4#include <vector>
5#include <list>
6
7// Structure MyStruct with support for serialization and deserialization
8struct MyStruct {
9 int64_t a;
10 double b;
11
12 // Serialization of MyStruct to an output stream
13 friend std::ostream& operator<<(std::ostream& os, const MyStruct& ms) {
14 os << ms.a << " " << ms.b;
15 return os;
16 }
17
18 // Deserialization of MyStruct from an input stream
19 friend std::istream& operator>>(std::istream& is, MyStruct& ms) {
20 is >> ms.a >> ms.b;
21 return is;
22 }
23
24 // Comparison operator for std::set and other sorted containers
25 bool operator<(const MyStruct& other) const {
26 if (a != other.a) {
27 return a < other.a;
28 }
29 return b < other.b;
30 }
31};
32
33// Utility function to print contents of a set
34template <typename SetType>
35void print_set(const SetType& set, const std::string& header) {
36 std::cout << header << std::endl;
37 for (const auto& key : set) {
38 std::cout << key << std::endl;
39 }
40 std::cout << std::endl;
41}
42
43// Utility function to print contents of a list or vector
44template <typename ListType>
45void print_list(const ListType& list, const std::string& header) {
46 std::cout << header << std::endl;
47 for (const auto& key : list) {
48 std::cout << key << std::endl;
49 }
50 std::cout << std::endl;
51}
52
53int main() {
54 try {
55 // Create the database configuration
57 config.db_path = "example-set-struct.db";
58
59 // Create KeyDB instance for working with MyStruct keys
61 key_db.connect();
62
63 // Clear the table for a fresh start
64 key_db.clear();
65
66 // Create a std::set of keys
67 std::set<MyStruct> keys = {
68 {10, 1.0},
69 {20, 3.0},
70 {30, 4.0},
71 {40, 5.0},
72 {50, 6.0}
73 };
74
75 // Append the contents of the std::set to the database
76 key_db.append(keys);
77
78 // Retrieve all keys from the database and print them
79 std::set<MyStruct> retrieved_keys_set = key_db.retrieve_all<std::set>();
80 print_set(retrieved_keys_set, "Keys in database after append:");
81
82 // Insert a new key
83 key_db.insert({60, 1.0});
84 std::list<MyStruct> retrieved_keys_list = key_db.retrieve_all<std::list>();
85 print_list(retrieved_keys_list, "Keys in database after insert:");
86
87 // Check if the key exists in the database
88 if (key_db.find({60, 1.0})) {
89 std::cout << "Key {60, 1.0} found in the database." << std::endl;
90 } else {
91 std::cout << "Key {60, 1.0} not found in the database." << std::endl;
92 }
93
94 // Check if a non-existing key is found
95 if (key_db.find({100, 8.0})) {
96 std::cout << "Key {100, 8.0} found in the database." << std::endl;
97 } else {
98 std::cout << "Key {100, 8.0} not found in the database." << std::endl;
99 }
100
101 // Remove a key
102 key_db.remove({30, 4.0});
103
104 // Retrieve all keys from the database after removal and print them
105 std::vector<MyStruct> retrieved_keys_vector = key_db.retrieve_all<std::vector>();
106 print_list(retrieved_keys_vector, "Keys in database after removing key {30, 4.0}:");
107
108 // Print the number of keys and check if the database is empty
109 std::cout << "Number of keys in the database: " << key_db.count() << std::endl;
110 std::cout << "Is the database empty? " << (key_db.empty() ? "Yes" : "No") << std::endl;
111
112 } catch (const sqlite_containers::sqlite_exception &e) {
113 std::cerr << "SQLite error: " << e.what() << std::endl;
114 } catch (const std::exception &e) {
115 std::cerr << "Error: " << e.what() << std::endl;
116 }
117 return 0;
118}
Declaration of the KeyDB class for managing keys 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 keys in a SQLite database.
Definition KeyDB.hpp:17
std::size_t count() const
Returns the number of keys in the database.
Definition KeyDB.hpp:225
bool empty() const
Checks if the database is empty (no keys present).
Definition KeyDB.hpp:233
bool find(const KeyT &key)
Finds if a key exists in the database.
Definition KeyDB.hpp:217
ContainerT< KeyT > retrieve_all()
Retrieves all keys from the database.
Definition KeyDB.hpp:111
void append(const ContainerT< KeyT > &container)
Appends the content of the container to the database.
Definition KeyDB.hpp:152
void insert(const KeyT &key)
Inserts a key into the database.
Definition KeyDB.hpp:208
void clear()
Clears all keys from the database.
Definition KeyDB.hpp:247
void remove(const KeyT &key)
Removes a key from the database.
Definition KeyDB.hpp:240
Exception class for SQLite errors.
Definition Utils.hpp:27
void print_set(const SetType &set, const std::string &header)
void print_list(const ListType &list, const std::string &header)
int main()
friend std::istream & operator>>(std::istream &is, MyStruct &ms)
bool operator<(const MyStruct &other) const
friend std::ostream & operator<<(std::ostream &os, const MyStruct &ms)