Joedb 9.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
tutorial_main.cpp
Go to the documentation of this file.
3
4#include <iostream>
5
6/////////////////////////////////////////////////////////////////////////////
7static int tutorial_main(int argc, char **argv)
8/////////////////////////////////////////////////////////////////////////////
9{
10 //
11 // Open the database
12 //
14
15 //
16 // Simple data manipulation
17 //
18 db.new_city("Tokyo");
19 db.new_city("New York");
20 db.new_city("Paris");
21
22 const auto Lille = db.new_city("Lille");
23 const auto Amsterdam = db.new_city("Amsterdam");
24
25 db.new_person("Rémi", "Coulom", Lille);
26 db.new_person("Bertrand", "Picard", db.null_city());
27
28 const auto Aristide = db.new_person("Aristide", "Martines", Amsterdam);
29
30 db.set_last_name(Aristide, "Martinez");
31
32 //
33 // Use the index to display cities in alphabetical order
34 //
35 std::cout << "List of cities in alphabetical order:\n";
36 for (const auto &[name, city]: db.get_index_of_city_by_name())
37 std::cout << " " << name << '\n';
38
39 //
40 // Referring to another table
41 //
42 std::cout << "\nList of persons with their cities:\n";
43 for (const auto person: db.get_person_table())
44 {
45 std::cout << " " << db.get_first_name(person) << ' ';
46 std::cout << db.get_last_name(person) << ' ';
47 const auto city = db.get_home(person);
48 if (city.is_null())
49 std::cout << "is homeless\n";
50 else
51 std::cout << "lives in " << db.get_name(city) << '\n';
52 }
53
54 //
55 // Deleting a record
56 //
57 db.delete_city(db.find_city_by_name("New York"));
58
59 //
60 // Time stamp and comment
61 //
62 db.write_timestamp();
63 db.write_comment("The End");
64
65 //
66 // Writes to the database must be confirmed by an explicit checkpoint
67 //
68 db.soft_checkpoint();
69
70 return 0;
71}
72
73/////////////////////////////////////////////////////////////////////////////
74int main(int argc, char **argv)
75/////////////////////////////////////////////////////////////////////////////
76{
77 joedb::main_exception_catcher(tutorial_main, argc, argv);
78}
Shortcut to directly build a Writable_Database from a file name.
int main()
@ create_new
fails if already exists, locks the file for writing
int main_exception_catcher(int(*main)(int, char **), int argc, char **argv)
Catch exception from main.