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

  1. // Copyright 1994 A.D. Software. All Rights Reserved
  2.  
  3. // OOFTEST7
  4.  
  5. // this sample demonstrates searching and sorting by 
  6. // different types of fields, and how to parameterize
  7. // calls, passing in table and field references
  8.  
  9. // Simple stream I/O is used to interact with the user.
  10. #include "oofile.hpp"
  11.  
  12.  
  13. CLASS_TABLE(dbPeople)
  14.     dbChar        LastName, Othernames;
  15.     dbLong        Salary, SalaryNoDup;
  16.     dbShort        SalaryShort, SalaryShortNoDup;
  17.     dbReal        SalaryReal, SalaryRealNoDup;
  18.     dbDate        LastVisit, LastVisitNoDup;
  19.     dbUshort    ShortUnsigned;
  20.     dbUlong        LongUnsigned;
  21.  
  22.     dbPeople() :
  23.                 dbTable("People"),
  24.                 LastName(40, "Last Name", kIndexed),
  25.                 Othernames(80, "Other names", kIndexNoDups),
  26.                 Salary("Salary", kIndexed),
  27.                 SalaryNoDup("Salary NoDup", kIndexNoDups),
  28.                 SalaryShort("SalaryShort", kIndexed),
  29.                 SalaryShortNoDup("SalaryShortNoDup", kIndexNoDups),
  30.                 SalaryReal("SalaryReal", kIndexed),
  31.                 SalaryRealNoDup("SalaryRealNoDup", kIndexNoDups),
  32.                 LastVisit("LastVisit", kIndexed),
  33.                 LastVisitNoDup("LastVisitNoDup", kIndexNoDups),
  34.                 ShortUnsigned("ShortUnsigned", kIndexed),
  35.                 LongUnsigned("LongUnsigned", kIndexed)                
  36.     {};
  37.     
  38. // my own data entry procedure
  39.     void Add(const char *lname, const char *oname, const long salary, const char* visitDate);
  40. };
  41.     
  42.  
  43. void dbPeople::Add(const char *lname, const char *oname, const long salary, const char* visitDate)
  44. {
  45.     newRecord();
  46.     LastName = lname;
  47.     Othernames = oname;
  48.     Salary = salary;
  49.     SalaryNoDup = salary;
  50.     SalaryReal = salary;
  51.     SalaryRealNoDup = salary;
  52.     SalaryShort = salary;
  53.     SalaryShortNoDup = salary;
  54.     LastVisit = visitDate;
  55.     LastVisitNoDup = visitDate;
  56.     
  57.     ShortUnsigned = salary*5;  // numbers 10,000 to 49,999
  58.     LongUnsigned = ULONG_MAX - salary;  
  59.     
  60.     saveRecord();
  61. }
  62.  
  63. void testField(dbPeople& searchTable, dbNumericField& searchBy);
  64. void testField(dbPeople& searchTable, dbChar& searchBy, const char* searchAgainst, const char* searchUpto, const char* searchAgainstPartial);
  65. void testField(dbPeople& searchTable, dbDate& searchBy, const char* searchAgainst, const char* searchUpto);
  66.  
  67.  
  68. int main()
  69. {
  70.     cout << "OOFILE Validation Suite - Test 7\n"
  71.          << "Simple test to demonstrate comparative searches" << endl
  72.          << "using a database similar to ooftest2 but with lots of indexed fields" << endl;
  73.     
  74.     dbConnect_ctree    theDB;
  75.     dbPeople     People;
  76.  
  77.     if (dbConnect::fileExists("ooftst07.db"))
  78.         theDB.openConnection("ooftst07.db");
  79.     else {
  80.         theDB.newConnection("ooftst07.db");
  81.         People.Add("Smith", "John", 2000, "23/11/94");
  82.         People.Add("DENT", "Trissa", 9999, "22/11/94");
  83.         People.Add("Denton", "Andy", 5000, "23-1-95");
  84.         People.Add("Taylor", "Ken", 7500, "16,11,95");
  85.     }
  86. // simple numeric test first, then use the parameterized versions
  87.     People.selectAll();
  88.  
  89.     People.sortBy(People.LongUnsigned);
  90.     cout << "Dumping database in key order by LongUnsigned (reverse of Salary)" << endl 
  91.          << People << endl << endl;
  92.  
  93.     cout << "Retrieving Andy Dent by ShortUnsigned == 25,000: " << endl;
  94.     People[People.ShortUnsigned==25000];
  95.     cout << People << endl << endl;
  96.  
  97.     cout << "Finding LongUnsigned between " << ULONG_MAX-5001 << " and " << ULONG_MAX << endl;
  98.     People[People.LongUnsigned.between(ULONG_MAX-5001, ULONG_MAX)];
  99.     cout << People << endl << endl;
  100.  
  101.  
  102. // test char searches
  103.     testField(People, People.LastName, "Dent", "Denton", "DE");
  104.     testField(People, People.Othernames, "Andy", "Kell", "J");
  105.  
  106. // test numeric searches
  107.     testField(People, People.SalaryShort);
  108.     testField(People, People.SalaryShortNoDup);
  109.     testField(People, People.Salary);
  110.     testField(People, People.SalaryNoDup);
  111.     testField(People, People.SalaryReal);
  112.     testField(People, People.SalaryRealNoDup);
  113.     
  114. // test date searches
  115.     testField(People, People.LastVisit, "23/11/94", "23-1-95");
  116.     testField(People, People.LastVisitNoDup, "23/11/94", "22-1-95");
  117.     
  118.     cout << "done" << endl;
  119.  
  120.     return EXIT_SUCCESS;
  121. }       
  122.  
  123. void testField(dbPeople& searchTable, dbNumericField& searchBy)
  124. {    
  125.     searchTable.selectAll();
  126.     
  127.     searchTable.sortBy(searchBy);
  128.     cout << "Dumping database in key order by " << searchBy.fieldName() << endl 
  129.          << searchTable << endl << endl;
  130.     
  131.     cout << "Retrieving Andy Dent by " << searchBy.fieldName() << " = 5000: " << endl;
  132.     searchTable[searchBy==5000];
  133.     cout << searchTable << endl << endl;
  134.  
  135.     cout << "Retrieving John Smith by " << searchBy.fieldName() << " < 5000: " << endl;
  136.     searchTable[searchBy<5000];
  137.     cout << searchTable << endl << endl;
  138.  
  139.     cout << "Retrieving Smith & Andy Dent by " << searchBy.fieldName() << " <= 5000: " << endl;
  140.     searchTable[searchBy<=5000];
  141.     cout << searchTable << endl << endl;
  142.  
  143.     cout << "Retrieving Taylor & Trissa Dent by " << searchBy.fieldName() << " > 5000: " << endl;
  144.     searchTable[searchBy>5000];
  145.     cout << searchTable << endl << endl;
  146.  
  147.     cout << "Same search with >=5000 (should also get Andy Dent): " << endl;
  148.     searchTable[searchBy>=5000];
  149.     cout << searchTable << endl << endl;
  150.  
  151.     cout << "Finding " << searchBy.fieldName() << " between 5000 and 7500 " << endl;
  152.     searchTable[searchBy.between(5000,7500)];
  153.     cout << searchTable << endl << endl;
  154.  
  155.     cout << "Finding " << searchBy.fieldName() << " outside 5000 to 7500 " << endl;
  156.     searchTable[sea