168{
169 const char* schema_file = "/tmp/tutorial.oks";
170 const char* data_file = "/tmp/tutorial.okd";
171
172 if (argc > 1 &&
173 (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-help") || !strcmp(argv[1], "--h") || !strcmp(argv[1], "-h"))) {
174 std::cout << "Usage: " << argv[0] << " [new_schema new_data]\n";
175 return 0;
176 }
177
178 if (argc == 3) {
179 schema_file = argv[1];
180 data_file = argv[2];
181 }
182
183
184
185 std::cout << "[OKS TUTORIAL]: Creating OKS kernel...\n";
186
187 OksKernel kernel(
false,
false,
false,
false);
188
189 std::cout << "[OKS TUTORIAL]: Done creating OKS kernel\n\n";
190
191 try {
192
193
194
195 std::cout << "[OKS TUTORIAL]: Creating new schema file...\n";
196
197 OksFile* schema_h = kernel.new_schema(schema_file);
198
199 std::cout << "[OKS TUTORIAL]: Done creating new schema file...\n\n"
200 "[OKS TUTORIAL]: Define database class schema...\n\n"
201 " ********** ************ 1..1 **************\n"
202 " * Person *<|------* Employee *--------<>* Department *\n"
203 " ********** ************ 0..N **************\n\n";
204
205
206
207 OksClass* Person =
new OksClass(
"Person",
"It is a class to describe a person",
false, &kernel);
208
211
214
215 Person->
add(PersonBirthday);
216
219 false,
220 "Single,Married,Widow(er)",
221 "Single",
222 "A enumeration to describe a person family state",
223 true));
224
225
226
227 OksClass* Employee =
new OksClass(
"Employee",
"It is a class to describe an employee",
false, &kernel);
228
231
233 "Department",
236 false,
237 false,
238 false,
239 "A employee works at one and only one department");
240
242 Employee->
add(EmployeeSalary);
243 Employee->
add(WorksAt);
244
245
246
247 OksClass* Department =
new OksClass(
"Department",
"It is a class to describe a department",
false, &kernel);
248
251
253 "Employee",
256 true,
257 true,
258 true,
259 "A department has zero or many employess");
260
261 Department->
add(DepartmentName);
262 Department->
add(DepartmentStaff);
263
264
265
266 std::cout << "[OKS TUTORIAL]: Saves created OKS schema file...\n";
267
268 kernel.save_schema(schema_h);
269
270
271
272 std::cout << "[OKS TUTORIAL]: Creating new data file...\n";
273
274 OksFile* data_h = kernel.new_data(data_file,
"OKS TUTORIAL DATA FILE");
275
277
278
279
288
289
290
291 setPersonValues(person1,
"Peter", boost::gregorian::from_string(
"1960/02/01"),
"Married");
292 setPersonValues(person2,
"Mick", boost::gregorian::from_string(
"1956-09-01"),
"Single");
293 setPersonValues(person3,
"Julia", boost::gregorian::from_string(
"2000-May-25"),
"Single");
294
295
296
297 setEmployeeValues(employee1,
"Alexander", boost::gregorian::from_string(
"1972/05/12"),
"Single", 3540);
298 setEmployeeValues(employee2,
"Michel", boost::gregorian::from_string(
"1963/01/28"),
"Married", 4950);
299 setEmployeeValues(employee3,
"Maria", boost::gregorian::from_string(
"1951/08/18"),
"Widow(er)", 4020);
300
301
302
305
306
307
314
315
316
317 std::cout << "\n[OKS TUTORIAL]: Database contains the following data:\n";
318
327
328
329
330 std::cout << "\n[OKS TUTORIAL]: Saves created OKS data file...\n";
331
332 kernel.save_data(data_h);
333
334 std::cout << "[OKS TUTORIAL]: Done with saving created OKS data file\n";
335
336
337
338
339
340 std::cout << "\n[OKS TUTORIAL]: Start database querying tests\n\n";
341
342
343
344 {
345 std::cout << "[QUERY]: Start simple database querying...\n";
346
347
348
349 boost::gregorian::date aDate(boost::gregorian::from_string("1960/01/01"));
351
352
353
354 std::cout << "[QUERY]: Looking for persons were born after " << aDate << " ...\n\n";
355
357
358
359
360 if (queryResult) {
361 std::cout << "[QUERY]: Query \'" << query << "\'\n founds the following objects in class \'"
362 << Person->
get_name() <<
"\' and subclasses:\n";
363
364 for (OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
367 std::cout <<
" - " << o <<
" was born " << *
d << std::endl;
368 }
369
370
371
372 delete queryResult;
373 }
374
375 std::cout << "[QUERY]: Done with simple database querying\n\n";
376 }
377
378 {
379 std::cout << "[QUERY]: Start database querying with logical function...\n";
380
381
382
383
384 boost::gregorian::date lowDate(boost::gregorian::from_string("1960/01/01"));
385 boost::gregorian::date highDate(boost::gregorian::from_string("1970/01/01"));
386
388
391
392 OksQuery query(
true, andExpression);
393
394
395
396 std::cout << "[QUERY]: Looking for persons were born between " << lowDate << " and " << highDate << " ...\n\n";
397
399
400
401
402 if (queryResult) {
403 std::cout << "[QUERY]: Query \'" << query << "\'\n founds the following objects in class \'"
404 << Person->
get_name() <<
"\' and subclasses:\n";
405
406 for (OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
409 std::cout <<
" - " << o <<
" was born " << *
d << std::endl;
410 }
411
412
413
414 delete queryResult;
415 }
416
417 std::cout << "[QUERY]: Done database querying with logical function\n\n";
418 }
419
420 {
421
422 std::cout << "[QUERY]: Start database querying with relationship expression...\n";
423
424
425
426
427 boost::gregorian::date aDate(boost::gregorian::from_string("1971/01/01"));
428 const char* departmentName = "IT Department";
429
431
435
436 OksQuery query(
true, andExpression);
437
438
439
440 std::cout << "[QUERY]: Looking for employee were born after " << aDate << " and works at department "
441 << departmentName << " ...\n\n";
442
444
445
446
447 if (queryResult) {
448 std::cout << "[QUERY]: Query \'" << query << "\'\n founds the following objects in class \'"
449 << Employee->
get_name() <<
"\' and subclasses:\n";
450
451 for (OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
454 std::cout <<
" - " << o <<
" was born " << *
d << std::endl;
455 }
456
457
458
459 delete queryResult;
460 }
461
462 std::cout << "[QUERY]: Done database querying with relationship expression...\n";
463 }
464
465 std::cout << "\n[OKS TUTORIAL]: Done with database querying tests\n\n";
466
467
468
469
470
471 } catch (const std::exception& ex) {
472 std::cerr << "Caught exception:\n" << ex.what() << std::endl;
473 }
474
475
476
477 return 0;
478}
OKS query logical AND expression class.
static const char * u32_int_type
static const char * string_type
static const char * enum_type
OksAttribute(const std::string &name, OksClass *p=nullptr)
OKS attribute simple constructor.
static const char * date_type
void add_super_class(const std::string &name)
void add(OksAttribute *a)
Add attribute.
const std::string & get_name() const noexcept
OksClass(const std::string &name, OksKernel *kernel, bool transient=false)
Create OKS class.
OksObject::List * execute_query(OksQuery *query) const
Execute query.
OksComparator(const OksAttribute *a, OksData *v, OksQuery::Comparator f)
Provides interface to the OKS XML schema and data files.
void add_include_file(const std::string &name)
Add include file.
Provides interface to the OKS kernel.
void add(OksQueryExpression *q)
OksObject describes instance of OksClass.
void AddRelationshipValue(const std::string &name, OksObject *object)
Add object value to multi-value relationship by name.
std::list< OksObject * > List
void SetRelationshipValue(const std::string &name, OksData *data, bool skip_non_null_check=false)
Set value of relationship by name.
OksObject(const OksClass *oks_class, const char *object_id=0, bool skip_init=false)
OKS object constructor.
OksData * GetAttributeValue(const std::string &name) const
Get value of attribute by name.
static bool equal_cmp(const OksData *, const OksData *)
static bool greater_cmp(const OksData *, const OksData *)
static bool less_cmp(const OksData *, const OksData *)
OksRelationshipExpression(const OksRelationship *r, OksQueryExpression *q, bool b=false)
OksRelationship(const std::string &name, OksClass *p=nullptr)
OKS relationship simple constructor.
void printEmployee(const OksObject *o)
void setPersonValues(OksObject *o, const char *name, boost::gregorian::date birthday, const char *familySituation)
void setEmployeeValues(OksObject *o, const char *name, boost::gregorian::date birthday, const char *familySituation, uint32_t salary)
void setDepartmentValues(OksObject *o, const char *name)
void printPerson(const OksObject *o)
void printDepartment(const OksObject *o)