Stroika Library 3.0d20
 
Loading...
Searching...
No Matches
Samples/DocumentDB/Sources/Main.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Frameworks/StroikaPreComp.h"
5
6#include <cstdlib>
7#include <iostream>
8
9#include "Stroika/Foundation/Database/Document/MongoDBClient.h"
13#include "Stroika/Foundation/Execution/CommandLine.h"
14#include "Stroika/Foundation/Execution/Module.h"
17
18#include "ComputerNetwork.h"
19#include "EmployeesDB.h"
20
21using namespace std;
22
23using namespace Stroika::Foundation;
25using namespace Stroika::Foundation::Database;
26using namespace Stroika::Foundation::Execution;
27
28int main ([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[])
29{
30 using namespace Database::Document;
31
32 using namespace Stroika::Samples::Document;
33
34#if qStroika_HasComponent_mongocxxdriver
35 optional<String> mongoConnectionString;
36#endif
37
38 // handle command-line arguments
39 {
40 using namespace StandardCommandLineOptions;
41 const CommandLine::Option kMongoConnectionStringOption_{
42 .fLongName = "mongoConnectionString"sv,
43 .fSupportsArgument = true,
44 .fHelpOptionText = "Connect to this mongo database for testing, eg: mongodb://admin:pass@localhost:27017; OR ENV VAR MONGO_CONNECTION_STRING"sv};
45
46 const initializer_list<CommandLine::Option> kAllOptions_{kHelp,
47#if qStroika_HasComponent_mongocxxdriver
48 kMongoConnectionStringOption_
49#endif
50 };
51
52 CommandLine cmdLine{argc, argv};
53 if (cmdLine.Has (kHelp)) {
54 cerr << CommandLine::GenerateUsage ("documentDB", kAllOptions_);
55 return EXIT_SUCCESS;
56 }
57 if (auto error = cmdLine.ValidateQuietly (kAllOptions_)) {
58 cerr << "{}"_f(*error) << endl;
59 cerr << CommandLine::GenerateUsage ("myApp", kAllOptions_) << endl;
60 }
61#if qStroika_HasComponent_mongocxxdriver
62 mongoConnectionString = Memory::Or_Else (cmdLine.GetArgument (kMongoConnectionStringOption_),
63 [] () { return kEnvironment->Lookup ("MONGO_CONNECTION_STRING"sv); });
64#endif
65 }
66
67#if qStroika_HasComponent_mongocxxdriver
68 Database::Document::MongoDBClient::Activator activator{}; // must exist while using mongocxxclient library
69#endif
70
71#if qStroika_HasComponent_mongocxxdriver
72 if (not mongoConnectionString) {
73 cerr << "Warning: skipping mongodb test because no connection string specified on command-line nor environment variable" << endl;
74 }
75 if (mongoConnectionString) {
76 using namespace Stroika::Foundation::Database::Document::MongoDBClient;
77 const String kTestDBName_ = "DocumentDB-Sample-Networks"sv;
78 try {
79 static const Activity kMongoCXXActivity_{"performing MongoDBClient test on {}"_f(kTestDBName_)};
80 DeclareActivity da{&kMongoCXXActivity_};
81 auto adminDB = AdminConnection::New (AdminConnection::Options{.fConnectionTarget = *mongoConnectionString});
82 IgnoreExceptionsForCall (adminDB.DropDatabase (kTestDBName_));
83 adminDB.CreateDatabase (kTestDBName_);
84 Database::Document::Connection::Ptr p = MongoDBClient::Connection::New (
85 MongoDBClient::Connection::Options{.fConnectionTarget = *mongoConnectionString, .fDatabase = kTestDBName_});
86 cerr << "Starting MongoDBClient networks sample:" << endl;
87 ComputerNetworksModel ([=] () {
88 cerr << "\tConnecting to {} database {}"_f(*mongoConnectionString, kTestDBName_) << endl;
89 return MongoDBClient::Connection::New (
90 MongoDBClient::Connection::Options{.fConnectionTarget = *mongoConnectionString, .fDatabase = kTestDBName_});
91 });
92 cerr << "done." << endl;
93 }
94 catch (...) {
95 cerr << "\t{}"_f(current_exception ()) << endl;
96 }
97 }
98 if (mongoConnectionString) {
99 using namespace Stroika::Foundation::Database::Document::MongoDBClient;
100 const String kTestDBName_ = "DocumentDB-Sample-Employees"sv;
101 try {
102 static const Activity kMongoCXXActivity_{"performing MongoDBClient test on {}"_f(kTestDBName_)};
103 DeclareActivity da{&kMongoCXXActivity_};
104 auto adminDB = AdminConnection::New (AdminConnection::Options{.fConnectionTarget = *mongoConnectionString});
105 IgnoreExceptionsForCall (adminDB.DropDatabase (kTestDBName_));
106 adminDB.CreateDatabase (kTestDBName_);
107 Database::Document::Connection::Ptr p = MongoDBClient::Connection::New (
108 MongoDBClient::Connection::Options{.fConnectionTarget = *mongoConnectionString, .fDatabase = kTestDBName_});
109 cerr << "Starting mongodb employees sample:" << endl;
110 EmployeesDB ([=] () {
111 cerr << "\tConnecting to {} database {}"_f(*mongoConnectionString, kTestDBName_) << endl;
112 return MongoDBClient::Connection::New (
113 MongoDBClient::Connection::Options{.fConnectionTarget = *mongoConnectionString, .fDatabase = kTestDBName_});
114 });
115 cerr << "done." << endl;
116 }
117 catch (...) {
118 cerr << "\t{}"_f(current_exception ()) << endl;
119 }
120 }
121#endif
122
123#if qStroika_HasComponent_sqlite
124 // quick tests with in-memory DB
125 try {
126 const String kTestDBName_ = "DocumentDB-Sample-Networks"sv;
127 static const Activity kMongoCXXActivity_{"performing sqlite document db networks sample on {}"_f(kTestDBName_)};
128 DeclareActivity da{&kMongoCXXActivity_};
129 cerr << "Starting sqlite document db networks sample:" << endl;
130 ComputerNetworksModel ([=] () {
131 cerr << "\tConnecting to sqlite memory db: {}"_f(kTestDBName_) << endl;
132 return SQLite::Connection::New (SQLite::Connection::Options{.fInMemoryDB = kTestDBName_});
133 });
134 cerr << "done." << endl;
135 }
136 catch (...) {
137 cerr << "\t{}"_f(current_exception ()) << endl;
138 }
139 try {
140 const String kTestDBName_ = "DocumentDB-Sample-Employees"sv;
141 static const Activity kMongoCXXActivity_{"performing sqlite document db employees sample on {}"_f(kTestDBName_)};
142 DeclareActivity da{&kMongoCXXActivity_};
143 cerr << "Starting sqlite document db employees sample on memory db {}"_f(kTestDBName_) << endl;
144 EmployeesDB ([=] () {
145 cerr << "\tConnecting to sqlite memory db: {}"_f(kTestDBName_) << endl;
146 return SQLite::Connection::New (SQLite::Connection::Options{.fInMemoryDB = kTestDBName_});
147 });
148 cerr << "done." << endl;
149 }
150 catch (...) {
151 cerr << "\t{}"_f(current_exception ()) << endl;
152 }
153 // or run same test on filesystem
154 try {
155 auto dbPath = IO::FileSystem::WellKnownLocations::GetTemporary () / "networks-test.db";
156 static const Activity kMongoCXXActivity_{"performing sqlite document db networks sample on {}"_f(dbPath)};
157 DeclareActivity da{&kMongoCXXActivity_};
158 remove (dbPath); // test assumes empty db
159 cerr << "Starting sqlite document db networks sample:" << endl;
160 ComputerNetworksModel ([=] () {
161 cerr << "\tConnecting to sqlite db: {}"_f(dbPath) << endl;
162 return SQLite::Connection::New (SQLite::Connection::Options{.fDBPath = dbPath});
163 });
164 cerr << "done." << endl;
165 }
166 catch (...) {
167 cerr << "\t{}"_f(current_exception ()) << endl;
168 }
169 try {
170 auto dbPath = IO::FileSystem::WellKnownLocations::GetTemporary () / "employees-test.db";
171 static const Activity kMongoCXXActivity_{"performing Starting sqlite document db employees sample on {}"_f(dbPath)};
172 DeclareActivity da{&kMongoCXXActivity_};
173 remove (dbPath); // test assumes empty db
174 cerr << "Starting sqlite document db employees sample on {}"_f(dbPath) << endl;
175 EmployeesDB ([=] () {
176 cerr << "\tConnecting to sqlite db: {}"_f(dbPath) << endl;
177 auto c = SQLite::Connection::New (SQLite::Connection::Options{.fDBPath = dbPath});
178 return c;
179 });
180 cerr << "done." << endl;
181 }
182 catch (...) {
183 cerr << "\t{}"_f(current_exception ()) << endl;
184 }
185#endif
186 try {
187 static const Activity kMongoCXXActivity_{"performing trivial document db networks sample"sv};
188 DeclareActivity da{&kMongoCXXActivity_};
189 cerr << "Starting trivial document db networks sample:" << endl;
190 auto internallySynchronizedDBConnection =
191 TrivialDocumentDB::New (TrivialDocumentDB::Options{.fStorage = TrivialDocumentDB::Options::MemoryStorage{}});
192 ComputerNetworksModel ([=] () {
193 cerr << "\tConnecting to trivial document db: memory" << endl;
194 return internallySynchronizedDBConnection;
195 });
196 cerr << "done." << endl;
197 }
198 catch (...) {
199 cerr << "\t{}"_f(current_exception ()) << endl;
200 }
201 try {
202 static const Activity kMongoCXXActivity_{"performing trivial document db employees sample"sv};
203 DeclareActivity da{&kMongoCXXActivity_};
204 cerr << "Starting trivial document db employees sample:" << endl;
205 auto internallySynchronizedDBConnection =
206 TrivialDocumentDB::New (TrivialDocumentDB::Options{.fStorage = TrivialDocumentDB::Options::MemoryStorage{}});
207 EmployeesDB ([=] () {
208 cerr << "\tConnecting to trivial document db: memory" << endl;
209 return internallySynchronizedDBConnection;
210 });
211 cerr << "done." << endl;
212 }
213 catch (...) {
214 cerr << "\t{}"_f(current_exception ()) << endl;
215 }
216 return EXIT_SUCCESS;
217}
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
STL namespace.