168{
169 const char * schema_file = "/tmp/tutorial.oks";
170 const char * data_file = "/tmp/tutorial.okd";
171
172 if(
173 argc > 1 &&
174 (
175 !strcmp(argv[1], "--help") ||
176 !strcmp(argv[1], "-help") ||
177 !strcmp(argv[1], "--h") ||
178 !strcmp(argv[1], "-h")
179 )
180 ) {
181 std::cout << "Usage: " << argv[0] << " [new_schema new_data]\n";
182 return 0;
183 }
184
185 if(argc == 3) {
186 schema_file = argv[1];
187 data_file = argv[2];
188 }
189
190
191
192
193 std::cout << "[OKS TUTORIAL]: Creating OKS kernel...\n";
194
195 OksKernel kernel(
false,
false,
false,
false);
196
197 std::cout << "[OKS TUTORIAL]: Done creating OKS kernel\n\n";
198
199
200 try {
201
202
203
204
205 std::cout << "[OKS TUTORIAL]: Creating new schema file...\n";
206
207 OksFile * schema_h = kernel.new_schema(schema_file);
208
209 std::cout << "[OKS TUTORIAL]: Done creating new schema file...\n\n"
210 "[OKS TUTORIAL]: Define database class schema...\n\n"
211 " ********** ************ 1..1 **************\n"
212 " * Person *<|------* Employee *--------<>* Department *\n"
213 " ********** ************ 0..N **************\n\n";
214
215
216
217
219 "Person",
220 "It is a class to describe a person",
221 false,
222 &kernel
223 );
224
227 "Name",
229 false,
230 "",
231 "Unknown",
232 "A string to describe person name",
233 true
234 )
235 );
236
238 "Birthday",
240 false,
241 "",
242 "2009/01/01",
243 "A date to describe person birthday",
244 true
245 );
246
247 Person->
add(PersonBirthday);
248
251 "Family_Situation",
253 false,
254 "Single,Married,Widow(er)",
255 "Single",
256 "A enumeration to describe a person family state",
257 true
258 )
259 );
260
261
262
263
265 "Employee",
266 "It is a class to describe an employee",
267 false,
268 &kernel
269 );
270
272 "Salary",
274 false,
275 "",
276 "1000",
277 "An integer to describe employee salary",
278 false
279 );
280
282 "Works at",
283 "Department",
286 false,
287 false,
288 false,
289 "A employee works at one and only one department"
290 );
291
293 Employee->
add(EmployeeSalary);
294 Employee->
add(WorksAt);
295
296
297
298
300 "Department",
301 "It is a class to describe a department",
302 false,
303 &kernel
304 );
305
307 "Name",
309 false,
310 "",
311 "Unknown",
312 "A string to describe department name",
313 true
314 );
315
317 "Staff",
318 "Employee",
321 true,
322 true,
323 true,
324 "A department has zero or many employess"
325 );
326
327 Department->
add(DepartmentName);
328 Department->
add(DepartmentStaff);
329
330
331
332
333 std::cout << "[OKS TUTORIAL]: Saves created OKS schema file...\n";
334
335 kernel.save_schema(schema_h);
336
337
338
339
340 std::cout << "[OKS TUTORIAL]: Creating new data file...\n";
341
342 OksFile * data_h = kernel.new_data(data_file,
"OKS TUTORIAL DATA FILE");
343
345
346
347
348
357
358
359
360
361 setPersonValues(person1,
"Peter", boost::gregorian::from_string(
"1960/02/01"),
"Married");
362 setPersonValues(person2,
"Mick", boost::gregorian::from_string(
"1956-09-01"),
"Single");
363 setPersonValues(person3,
"Julia", boost::gregorian::from_string(
"2000-May-25"),
"Single");
364
365
366
367
368 setEmployeeValues(employee1,
"Alexander", boost::gregorian::from_string(
"1972/05/12"),
"Single", 3540) ;
369 setEmployeeValues(employee2,
"Michel", boost::gregorian::from_string(
"1963/01/28"),
"Married", 4950) ;
370 setEmployeeValues(employee3,
"Maria", boost::gregorian::from_string(
"1951/08/18"),
"Widow(er)", 4020) ;
371
372
373
374
377
378
379
380
387
388
389
390
391 std::cout << "\n[OKS TUTORIAL]: Database contains the following data:\n";
392
401
402
403
404
405 std::cout << "\n[OKS TUTORIAL]: Saves created OKS data file...\n";
406
407 kernel.save_data(data_h);
408
409
410 std::cout << "[OKS TUTORIAL]: Done with saving created OKS data file\n";
411
412
413
414
415
416
417
418 std::cout << "\n[OKS TUTORIAL]: Start database querying tests\n\n";
419
420
421
422
423 {
424 std::cout << "[QUERY]: Start simple database querying...\n";
425
426
427
428 boost::gregorian::date aDate(boost::gregorian::from_string("1960/01/01"));
430
431
432
433
434 std::cout << "[QUERY]: Looking for persons were born after " << aDate << " ...\n\n";
435
437
438
439
440
441 if(queryResult) {
442 std::cout << "[QUERY]: Query \'" << query
443 << "\'\n founds the following objects in class \'"
444 << Person->
get_name() <<
"\' and subclasses:\n";
445
446 for(OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
449 std::cout <<
" - " << o <<
" was born " << *
d << std::endl;
450 }
451
452
453
454
455 delete queryResult;
456 }
457
458 std::cout << "[QUERY]: Done with simple database querying\n\n";
459 }
460
461 {
462 std::cout << "[QUERY]: Start database querying with logical function...\n";
463
464
465
466
467
468 boost::gregorian::date lowDate(boost::gregorian::from_string("1960/01/01"));
469 boost::gregorian::date highDate(boost::gregorian::from_string("1970/01/01"));
470
472
475
476 OksQuery query(
true, andExpression);
477
478
479
480
481 std::cout << "[QUERY]: Looking for persons were born between " << lowDate
482 << " and " << highDate << " ...\n\n";
483
485
486
487
488
489 if(queryResult) {
490 std::cout << "[QUERY]: Query \'" << query
491 << "\'\n founds the following objects in class \'"
492 << Person->
get_name() <<
"\' and subclasses:\n";
493
494 for(OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
497 std::cout <<
" - " << o <<
" was born " << *
d << std::endl;
498 }
499
500
501
502
503 delete queryResult;
504 }
505
506 std::cout << "[QUERY]: Done database querying with logical function\n\n";
507
508 }
509
510 {
511
512 std::cout << "[QUERY]: Start database querying with relationship expression...\n";
513
514
515
516
517
518 boost::gregorian::date aDate(boost::gregorian::from_string("1971/01/01"));
519 const char * departmentName = "IT Department";
520
522
525
526 OksQuery query(
true, andExpression);
527
528
529
530
531 std::cout << "[QUERY]: Looking for employee were born after " << aDate
532 << " and works at department " << departmentName << " ...\n\n";
533
535
536
537
538
539 if(queryResult) {
540 std::cout << "[QUERY]: Query \'" << query
541 << "\'\n founds the following objects in class \'"
542 << Employee->
get_name() <<
"\' and subclasses:\n";
543
544 for(OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
547 std::cout <<
" - " << o <<
" was born " << *
d << std::endl;
548 }
549
550
551
552
553 delete queryResult;
554 }
555
556 std::cout << "[QUERY]: Done database querying with relationship expression...\n";
557 }
558
559 std::cout << "\n[OKS TUTORIAL]: Done with database querying tests\n\n";
560
561
562
563
564
565 }
566 catch (const std::exception & ex) {
567 std::cerr << "Caught exception:\n" << ex.what() << std::endl;
568 }
569
570
571
572 return 0;
573}
OKS query logical AND expression class.
static const char * u32_int_type
static const char * string_type
static const char * enum_type
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
OksObject::List * execute_query(OksQuery *query) const
Execute query.
OKS query expression comparator class.
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.
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 *)
OKS query relationship expression class.
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)
the structure to pass common parameters to various read() methods of OksData and OksObject class