161{
162 const char * schema_file = "/tmp/tutorial.oks";
163 const char * data_file = "/tmp/tutorial.okd";
164
165 if(
166 argc > 1 &&
167 (
168 !strcmp(argv[1], "--help") ||
169 !strcmp(argv[1], "-help") ||
170 !strcmp(argv[1], "--h") ||
171 !strcmp(argv[1], "-h")
172 )
173 ) {
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
186 std::cout << "[OKS TUTORIAL]: Creating OKS kernel...\n";
187
188 OksKernel kernel(
false,
false,
false,
false);
189
190 std::cout << "[OKS TUTORIAL]: Done creating OKS kernel\n\n";
191
192
193 try {
194
195
196
197
198 std::cout << "[OKS TUTORIAL]: Creating new schema file...\n";
199
200 OksFile * schema_h = kernel.new_schema(schema_file);
201
202 std::cout << "[OKS TUTORIAL]: Done creating new schema file...\n\n"
203 "[OKS TUTORIAL]: Define database class schema...\n\n"
204 " ********** ************ 1..1 **************\n"
205 " * Person *<|------* Employee *--------<>* Department *\n"
206 " ********** ************ 0..N **************\n\n";
207
208
209
210
212 "Person",
213 "It is a class to describe a person",
214 false,
215 &kernel
216 );
217
220 "Name",
222 false,
223 "",
224 "Unknown",
225 "A string to describe person name",
226 true
227 )
228 );
229
231 "Birthday",
233 false,
234 "",
235 "2009/01/01",
236 "A date to describe person birthday",
237 true
238 );
239
240 Person->
add(PersonBirthday);
241
244 "Family_Situation",
246 false,
247 "Single,Married,Widow(er)",
248 "Single",
249 "A enumeration to describe a person family state",
250 true
251 )
252 );
253
254
255
256
258 "Employee",
259 "It is a class to describe an employee",
260 false,
261 &kernel
262 );
263
265 "Salary",
267 false,
268 "",
269 "1000",
270 "An integer to describe employee salary",
271 false
272 );
273
275 "Works at",
276 "Department",
279 false,
280 false,
281 false,
282 "A employee works at one and only one department"
283 );
284
286 Employee->
add(EmployeeSalary);
287 Employee->
add(WorksAt);
288
289
290
291
293 "Department",
294 "It is a class to describe a department",
295 false,
296 &kernel
297 );
298
300 "Name",
302 false,
303 "",
304 "Unknown",
305 "A string to describe department name",
306 true
307 );
308
310 "Staff",
311 "Employee",
314 true,
315 true,
316 true,
317 "A department has zero or many employess"
318 );
319
320 Department->
add(DepartmentName);
321 Department->
add(DepartmentStaff);
322
323
324
325
326 std::cout << "[OKS TUTORIAL]: Saves created OKS schema file...\n";
327
328 kernel.save_schema(schema_h);
329
330
331
332
333 std::cout << "[OKS TUTORIAL]: Creating new data file...\n";
334
335 OksFile * data_h = kernel.new_data(data_file,
"OKS TUTORIAL DATA FILE");
336
338
339
340
341
350
351
352
353
354 setPersonValues(person1,
"Peter", boost::gregorian::from_string(
"1960/02/01"),
"Married");
355 setPersonValues(person2,
"Mick", boost::gregorian::from_string(
"1956-09-01"),
"Single");
356 setPersonValues(person3,
"Julia", boost::gregorian::from_string(
"2000-May-25"),
"Single");
357
358
359
360
361 setEmployeeValues(employee1,
"Alexander", boost::gregorian::from_string(
"1972/05/12"),
"Single", 3540) ;
362 setEmployeeValues(employee2,
"Michel", boost::gregorian::from_string(
"1963/01/28"),
"Married", 4950) ;
363 setEmployeeValues(employee3,
"Maria", boost::gregorian::from_string(
"1951/08/18"),
"Widow(er)", 4020) ;
364
365
366
367
370
371
372
373
380
381
382
383
384 std::cout << "\n[OKS TUTORIAL]: Database contains the following data:\n";
385
394
395
396
397
398 std::cout << "\n[OKS TUTORIAL]: Saves created OKS data file...\n";
399
400 kernel.save_data(data_h);
401
402
403 std::cout << "[OKS TUTORIAL]: Done with saving created OKS data file\n";
404
405
406
407
408
409
410
411 std::cout << "\n[OKS TUTORIAL]: Start database querying tests\n\n";
412
413
414
415
416 {
417 std::cout << "[QUERY]: Start simple database querying...\n";
418
419
420
421 boost::gregorian::date aDate(boost::gregorian::from_string("1960/01/01"));
423
424
425
426
427 std::cout << "[QUERY]: Looking for persons were born after " << aDate << " ...\n\n";
428
430
431
432
433
434 if(queryResult) {
435 std::cout << "[QUERY]: Query \'" << query
436 << "\'\n founds the following objects in class \'"
437 << Person->
get_name() <<
"\' and subclasses:\n";
438
439 for(OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
442 std::cout <<
" - " << o <<
" was born " << *
d << std::endl;
443 }
444
445
446
447
448 delete queryResult;
449 }
450
451 std::cout << "[QUERY]: Done with simple database querying\n\n";
452 }
453
454 {
455 std::cout << "[QUERY]: Start database querying with logical function...\n";
456
457
458
459
460
461 boost::gregorian::date lowDate(boost::gregorian::from_string("1960/01/01"));
462 boost::gregorian::date highDate(boost::gregorian::from_string("1970/01/01"));
463
465
468
469 OksQuery query(
true, andExpression);
470
471
472
473
474 std::cout << "[QUERY]: Looking for persons were born between " << lowDate
475 << " and " << highDate << " ...\n\n";
476
478
479
480
481
482 if(queryResult) {
483 std::cout << "[QUERY]: Query \'" << query
484 << "\'\n founds the following objects in class \'"
485 << Person->
get_name() <<
"\' and subclasses:\n";
486
487 for(OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
490 std::cout <<
" - " << o <<
" was born " << *
d << std::endl;
491 }
492
493
494
495
496 delete queryResult;
497 }
498
499 std::cout << "[QUERY]: Done database querying with logical function\n\n";
500
501 }
502
503 {
504
505 std::cout << "[QUERY]: Start database querying with relationship expression...\n";
506
507
508
509
510
511 boost::gregorian::date aDate(boost::gregorian::from_string("1971/01/01"));
512 const char * departmentName = "IT Department";
513
515
518
519 OksQuery query(
true, andExpression);
520
521
522
523
524 std::cout << "[QUERY]: Looking for employee were born after " << aDate
525 << " and works at department " << departmentName << " ...\n\n";
526
528
529
530
531
532 if(queryResult) {
533 std::cout << "[QUERY]: Query \'" << query
534 << "\'\n founds the following objects in class \'"
535 << Employee->
get_name() <<
"\' and subclasses:\n";
536
537 for(OksObject::List::iterator i = queryResult->begin(); i != queryResult->end(); ++i) {
540 std::cout <<
" - " << o <<
" was born " << *
d << std::endl;
541 }
542
543
544
545
546 delete queryResult;
547 }
548
549 std::cout << "[QUERY]: Done database querying with relationship expression...\n";
550 }
551
552 std::cout << "\n[OKS TUTORIAL]: Done with database querying tests\n\n";
553
554
555
556
557
558 }
559 catch (const std::exception & ex) {
560 std::cerr << "Caught exception:\n" << ex.what() << std::endl;
561 }
562
563
564
565 return 0;
566}
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