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;
114 employeeCollection.Add (Employee{.fName =
"Paul", .fAge = 32, .fAddress =
"California", .fSalary = 20000.00, .fStillEmployed =
true});
115 employeeCollection.Add (Employee{.fName =
"Allen", .fAge = 25, .fAddress =
"Texas", .fSalary = 15000.00, .fStillEmployed =
true});
116 employeeCollection.Add (Employee{.fName =
"Teddy", .fAge = 23, .fAddress =
"Norway", .fSalary = 20000.00, .fStillEmployed =
true});
117 employeeCollection.Add (Employee{.fName =
"Mark", .fAge = 25, .fAddress =
"Rich-Mond", .fSalary = 65000.00, .fStillEmployed =
true});
118 employeeCollection.Add (Employee{.fName =
"David", .fAge = 27, .fAddress =
"Texas", .fSalary = 85000.00, .fStillEmployed =
true});
119 employeeCollection.Add (Employee{.fName =
"Kim", .fAge = 22, .fAddress =
"South-Hall", .fSalary = 45000.00, .fStillEmployed =
true});
120 employeeCollection.Add (Employee{.fName =
"James", .fAge = 24, .fAddress =
"Houston", .fSalary = 10000.00, .fStillEmployed =
true});
123 cerr <<
"\tException adding initial employees to DB - this should generally not happen: {}"_f(current_exception ()) << endl;
126 default_random_engine generator;
127 uniform_int_distribution<int> distribution{1, 6};
132 static const Sequence<String> kNames_{
"Joe",
"Phred",
"Barny",
"Sue",
"Anne"};
133 uniform_int_distribution<int> namesDistr{0,
static_cast<int> (kNames_.size () - 1)};
134 uniform_int_distribution<int> ageDistr{25, 50};
135 static const Sequence<String> kAddresses{
"Houston",
"Pittsburg",
"New York",
"Paris",
"California"};
136 uniform_int_distribution<int> addressesDistr{0,
static_cast<int> (kAddresses.size () - 1)};
137 uniform_real_distribution<float> salaryDistr{10000.00, 50000.00};
140 uniform_int_distribution<int> whatTodoDistr{0, 3};
141 switch (whatTodoDistr (generator)) {
144 String name = kNames_[namesDistr (generator)];
145 cout <<
"\tAdding employee {}"_f(name) << endl;
146 employeeCollection.Add (Employee{nullopt, name, ageDistr (generator), kAddresses[addressesDistr (generator)],
147 salaryDistr (generator),
true});
151 auto activeEmps = employeeCollection.GetAll ();
152 if (not activeEmps.empty ()) {
153 uniform_int_distribution<int> empDistr{0,
static_cast<int> (activeEmps.size () - 1)};
154 Employee killMe = activeEmps[empDistr (generator)];
155 Assert (killMe.ID.has_value ());
156 cout <<
"\tFiring employee: {}, {}"_f(*killMe.ID, killMe.fName) << endl;
157 killMe.fStillEmployed =
false;
158 employeeCollection.Replace (*killMe.ID, killMe);
166 cerr <<
"\tException updating database: this should generally not happen: {}"_f(current_exception ()) << endl;
186 for (
const auto& employee : employeeCollection.GetAll ()) {
187 Assert (employee.ID != nullopt);
188 cout <<
"\tWriting paycheck for employee #{} ({}) amount {}"_f(*employee.ID, employee.fName, employee.fSalary) << endl;
189 paycheckCollection.Add (Paycheck{nullopt, *employee.ID, employee.fSalary / 12, DateTime::Now ().GetDate ()});
194 cout <<
"\tException processing paychecks - this should generally not happen: {}"_f(current_exception ()) << endl;
201void Stroika::Samples::Document::EmployeesDB (
const function<
Connection::Ptr ()>& connectionFactory)
213 Thread::New ([=] () { PeriodicallyUpdateEmployeesTable_ (conn1); }, Thread::eAutoStart,
"Update Employee Table"sv)};
215 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 void CreateCollection(const String &name)
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)