home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / OOFILE / Buildable, limited OOFILE / samples / ooftst12.cpp < prev   
Encoding:
C/C++ Source or Header  |  1995-09-27  |  3.4 KB  |  107 lines  |  [TEXT/CWIE]

  1. // Copyright 1995 A.D. Software. All Rights Reserved
  2.  
  3. // OOFTEST12
  4.  
  5. // This sample tests the compound indexes by creating a single table
  6. // and storing and retrieving indexed data.
  7.  
  8. // Simple stream I/O is used to interact with the user.
  9.  
  10. #include "oofile.hpp"
  11.  
  12. CLASS_TABLE(dbPeople)
  13.     dbChar        LastName, OtherNames, DepartmentCode, ProjectCode;
  14.     dbLong        Salary;
  15.     dbCompoundField    CombinedNames, NameAndSalary, JobCode;
  16.     dbUshort    StartedYear;
  17.     
  18.  
  19.     dbPeople() :
  20.                 dbTable("People"),
  21.                 LastName(39, "Last Name"),
  22.                 OtherNames(79, "Other Names", kIndexCompress),
  23.                 DepartmentCode(3, "Dept. Code"), 
  24.                 ProjectCode(4, "Proj. Code"),
  25.                 Salary("Salary")
  26.                 
  27. {
  28.     CombinedNames << LastName << OtherNames;
  29.     CombinedNames.index(kIndexCompress);  // order of these two lines is not significant
  30.     
  31.     NameAndSalary << LastName << Salary;
  32.     NameAndSalary.index(kIndexCompress);
  33.     
  34.     JobCode << DepartmentCode << ProjectCode << StartedYear;
  35.     JobCode.index(kIndexed);
  36. };
  37.     
  38. // my own data entry procedures
  39.     void Add(const char *lname, const char *oname, const long salary, const char *dept, const char *proj, const unsigned short year);
  40. };
  41.  
  42. void dbPeople::Add(const char *lname, const char *oname, const long salary, const char *dept, const char *proj, const unsigned short year)
  43. {
  44.     newRecord();
  45.     LastName = lname;
  46.     OtherNames = oname;
  47.     Salary = salary;
  48.     DepartmentCode = dept;
  49.     ProjectCode = proj;
  50.     StartedYear = year;
  51.     saveRecord();
  52. }
  53.  
  54.  
  55.  
  56. int main()
  57. {
  58.     cout << "OOFILE Validation Suite - Test 12\n"
  59.          << "Simple test to build compound indexes\n"
  60.          << "and demonstrate sorting and searching by them and their components\n";
  61.     
  62.     dbConnect_ctree    theDB;
  63.     dbPeople     People;
  64.     
  65.     if (dbConnect::fileExists("ooftst12.db"))
  66.         theDB.openConnection("ooftst12.db");
  67.     else {
  68.         theDB.newConnection("ooftst12.db");
  69.         People.Add("Smith", "John", 0, "Acc", "XXX", 1970);  // specifically to test zero searches
  70.         People.Add("DENT", "Trissa", 5000, "Acc", "Rec", 1985);
  71.         People.Add("Dent", "Andy", 25000, "Acc", "XXX", 1992);
  72.         People.Add("Dent", "Andrew", 40000, "MIS", "Boss", 1982);  // should sort before Andy on CombinedNames but after on Salary
  73.         People.Add("Denton", "Andrew", 39000, "Pur", "XXX", 1994);  
  74.         People.Add("Taylor", "Ken", 75000, "MIS", "Cntr", 1994);
  75.     }
  76.     People.sortBy(People.CombinedNames);
  77.     cout << "Listing records by combined names\n" << People << endl;
  78.  
  79.     People.sortBy(People.OtherNames);
  80.     cout << "Listing records in OtherNames order\n" << People << endl;
  81.  
  82.     People.search(People.LastName=="Dent");  // uses compound index
  83.     cout << "Listing three Dent records: " << endl << People << endl;
  84.  
  85.     People.search(People.JobCode.startsWith("AccXXX"));  // uses compound index in direct query
  86.     cout << "Listing two AccXXX records: " << endl << People << endl;
  87.  
  88.     People[People.LastName=="Dent" && People.Salary==25000];
  89.     cout << "Listing Andy Dent by compound key search: " << endl << People << endl;
  90.  
  91.     People[People.LastName=="Dent" && People.OtherNames=="Andrew"];
  92.     cout << "Listing Andrew Dent by compound key search: " << endl << People << endl;
  93.  
  94. // combinatorial search
  95.     People[ (People.LastName=="Dent" && People.OtherNames=="Andrew") | People.OtherNames=="Andrew"];
  96.     cout << "Listing Andrew Dent & Denton: " << endl << People << endl;
  97.  
  98.     cout << "Test Getting copy from read-only compound field" << endl;
  99.     People.start();
  100.     char* str = People.JobCode.copyAsChars();
  101.     cout << "JobCode is: " << str << endl;
  102.     delete[] str;
  103.     
  104.     cout << endl << "Test Completed" << endl;
  105.     
  106.     return EXIT_SUCCESS;