4#include "Stroika/Frameworks/StroikaPreComp.h"
10#include "Stroika/Foundation/DataExchange/ObjectVariantMapper.h"
17#include "EmployeesDB.h"
25using namespace Stroika::Foundation::Database;
27using namespace Stroika::Foundation::Debug;
30using namespace Database::Document;
38 optional<IDType> ID{};
43 bool fStillEmployed{};
54 {
"id"sv, &Employee::ID},
55 {
"Name"sv, &Employee::fName},
56 {
"Age"sv, &Employee::fAge},
57 {
"Address"sv, &Employee::fAddress},
58 {
"Salary"sv, &Employee::fSalary},
59 {
"Still-Employed"sv, &Employee::fStillEmployed},
68 optional<IDType> ID{};
82 {
"id"sv, &Paycheck::ID},
83 {
"Employee-Ref"sv, &Paycheck::fEmployeeRef},
84 {
"Amount"sv, &Paycheck::fAmount},
85 {
"Date"sv, &Paycheck::fDate},
97 mapper += Employee::kMapper;
98 mapper += Paycheck::kMapper;
113 employeeCollection.Add (Employee{.fName =
"Paul", .fAge = 32, .fAddress =
"California", .fSalary = 20000.00, .fStillEmployed =
true});
114 employeeCollection.Add (Employee{.fName =
"Allen", .fAge = 25, .fAddress =
"Texas", .fSalary = 15000.00, .fStillEmployed =
true});
115 employeeCollection.Add (Employee{.fName =
"Teddy", .fAge = 23, .fAddress =
"Norway", .fSalary = 20000.00, .fStillEmployed =
true});
116 employeeCollection.Add (Employee{.fName =
"Mark", .fAge = 25, .fAddress =
"Rich-Mond", .fSalary = 65000.00, .fStillEmployed =
true});
117 employeeCollection.Add (Employee{.fName =
"David", .fAge = 27, .fAddress =
"Texas", .fSalary = 85000.00, .fStillEmployed =
true});
118 employeeCollection.Add (Employee{.fName =
"Kim", .fAge = 22, .fAddress =
"South-Hall", .fSalary = 45000.00, .fStillEmployed =
true});
119 employeeCollection.Add (Employee{.fName =
"James", .fAge = 24, .fAddress =
"Houston", .fSalary = 10000.00, .fStillEmployed =
true});
122 cerr <<
"\tException adding initial employees to DB - this should generally not happen: {}"_f(current_exception ()) << endl;
125 default_random_engine generator;
126 uniform_int_distribution<int> distribution{1, 6};
131 static const Sequence<String> kNames_{
"Joe",
"Phred",
"Barny",
"Sue",
"Anne"};
132 uniform_int_distribution<int> namesDistr{0,
static_cast<int> (kNames_.size () - 1)};
133 uniform_int_distribution<int> ageDistr{25, 50};
134 static const Sequence<String> kAddresses{
"Houston",
"Pittsburg",
"New York",
"Paris",
"California"};
135 uniform_int_distribution<int> addressesDistr{0,
static_cast<int> (kAddresses.size () - 1)};
136 uniform_real_distribution<float> salaryDistr{10000.00, 50000.00};
139 uniform_int_distribution<int> whatTodoDistr{0, 3};
140 switch (whatTodoDistr (generator)) {
143 String name = kNames_[namesDistr (generator)];
144 cout <<
"\tAdding employee {}"_f(name) << endl;
145 employeeCollection.Add (Employee{nullopt, name, ageDistr (generator), kAddresses[addressesDistr (generator)],
146 salaryDistr (generator),
true});
150 auto activeEmps = employeeCollection.GetAll ();
151 if (not activeEmps.empty ()) {
152 uniform_int_distribution<int> empDistr{0,
static_cast<int> (activeEmps.size () - 1)};
153 Employee killMe = activeEmps[empDistr (generator)];
154 Assert (killMe.ID.has_value ());
155 cout <<
"\tFiring employee: {}, {}"_f(*killMe.ID, killMe.fName) << endl;
156 killMe.fStillEmployed =
false;
157 employeeCollection.Replace (*killMe.ID, killMe);
165 cerr <<
"\tException updating database: this should generally not happen: {}"_f(current_exception ()) << endl;
183 for (
const auto& employee : employeeCollection.GetAll ()) {
184 Assert (employee.ID != nullopt);
185 cout <<
"\tWriting paycheck for employee #{} ({}) amount {}"_f(*employee.ID, employee.fName, employee.fSalary) << endl;
186 paycheckCollection.Add (Paycheck{nullopt, *employee.ID, employee.fSalary / 12, DateTime::Now ().GetDate ()});
191 cout <<
"\tException processing paychecks - this should generally not happen: {}"_f(current_exception ()) << endl;
198void Stroika::Samples::Document::EmployeesDB (
const function<
Connection::Ptr ()>& connectionFactory)
210 Thread::New ([=] () { PeriodicallyUpdateEmployeesTable_ (conn1); }, Thread::eAutoStart,
"Update Employee Table"sv)};
212 Thread::New ([=] () { PeriodicallyWriteChecksForEmployeesTable_ (conn2); }, Thread::eAutoStart,
"Write Checks"sv)};
String is like std::u32string, except it is much easier to use, often much more space efficient,...
A generalization of a vector: a container whose elements are keyed by the natural numbers.
ObjectVariantMapper can be used to map C++ types to and from variant-union types, which can be transp...
nonvirtual void AddClass(const Traversal::Iterable< StructFieldInfo > &fieldDescriptions, const ClassMapperOptions< CLASS > &mapperOptions={})
nonvirtual void AddCommonType(ARGS &&... args)
nonvirtual Collection::Ptr CreateCollection(const String &name) const
nonvirtual WaitStatus WaitQuietly(Time::DurationSeconds timeout=Time::kInfinity)
Ptr New(const function< void()> &fun2CallOnce, const optional< Characters::String > &name, const optional< Configuration > &configuration)
void Sleep(Time::Duration seconds2Wait)