Joedb 10.2.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Service.h
Go to the documentation of this file.
1#ifndef tutorial_rpc_Service_declared
2#define tutorial_rpc_Service_declared
3
4#include "tutorial/Client.h"
7
8namespace tutorial::rpc
9{
10 /// A collection of procedures that will be executed in the rpc server
11 ///
12 /// joedbc uses a regular expression to find procedures in the file.
13 /// The constructor is not called from compiled code, so it can have any
14 /// signature. In particular, a Service does not necessarily have to
15 /// access a database at all.
16 class Service
17 {
18 private:
19 Client &client;
20
21 public:
22 Service(Client &client): client(client)
23 {
24 }
25
26 /// Insert a city from a name string
28 {
29 client.transaction
30 (
31 [&city](Writable_Database &db)
32 {
33 const auto city_id = db.find_city_by_name(city.get_name());
34 if (city_id.is_null())
35 db.new_city(city.get_name());
36 else
37 throw joedb::Exception("city already exists");
38 }
39 );
40 }
41
42 /// Delete a city from a name string
44 {
45 client.transaction
46 (
47 [&city](Writable_Database &db)
48 {
49 const auto city_id = db.find_city_by_name(city.get_name());
50 if (city_id.is_not_null())
51 db.delete_city(city_id);
52 else
53 throw joedb::Exception("city does not exist");
54 }
55 );
56 }
57
58 /// A procedure can return values by writing them to the message database
60 {
61 const auto &db = client.get_database();
62
63 for (const auto data: population.get_data_table())
64 {
65 const std::string &city_name = population.get_city_name(data);
66 const id_of_city city = db.find_city_by_name(city_name);
67
68 int64_t N = 0;
69
70 if (city.is_not_null())
71 for (const auto person: db.get_person_table())
72 if (db.get_home(person) == city)
73 N++;
74
75 population.set_city(data, city);
76 population.set_population(data, N);
77 }
78 }
79
80 /// A message can have the same schema as the main database
82 {
83 const auto message_city = message.get_city_table().first();
84
85 if (message_city.is_not_null())
86 {
87 const auto &db = client.get_database();
88
89 const auto city = db.find_city_by_name(message.get_name(message_city));
90 if (city.is_not_null())
91 {
92 for (const auto person: db.get_person_table())
93 {
94 if (db.get_home(person) == city)
95 {
96 message.new_person
97 (
98 db.get_first_name(person),
99 db.get_last_name(person),
100 db.get_home(person)
101 );
102 }
103 }
104 }
105 }
106 }
107 };
108}
109
110#endif
id_of_city find_city_by_name(const std::string &field_value_of_name) const
Definition Database.h:435
const std::string & get_name(id_of_city record) const
Definition Database.h:356
container_of_city get_city_table() const
Definition Database.h:489
A Database that contains a joedb::Writable_Journal and keeps them in sync.
void delete_city(id_of_city record)
id_of_city first() const
Definition Database.h:484
Strongly-typed wrapper around an integer representing a row of the city table.
Definition ids.h:25
constexpr bool is_not_null() const
Definition ids.h:34
Specialization of joedb::rpc::Client.
Definition Client.h:22
A collection of procedures that will be executed in the rpc server.
Definition Service.h:17
void get_inhabitants(tutorial::Writable_Database &message)
A message can have the same schema as the main database.
Definition Service.h:81
Service(Client &client)
Definition Service.h:22
void get_population(population::Writable_Database &population)
A procedure can return values by writing them to the message database.
Definition Service.h:59
void delete_city(city::Writable_Database &city)
Delete a city from a name string.
Definition Service.h:43
void insert_city(city::Writable_Database &city)
Insert a city from a name string.
Definition Service.h:27
const std::string & get_name(id_of_city record=id_of_city{0}) const
Definition Database.h:149
A Database that contains a joedb::Writable_Journal and keeps them in sync.
container_of_data get_data_table() const
Definition Database.h:379
const std::string & get_city_name(id_of_data record) const
Definition Database.h:259
A Database that contains a joedb::Writable_Journal and keeps them in sync.
void set_city(id_of_data record, id_of_city field_value_of_city)
void set_population(id_of_data record, int64_t field_value_of_population)