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

  1. // Copyright 1994 A.D. Software. All Rights Reserved
  2.  
  3. // OOFTEST1
  4.  
  5. // This sample tests the database backend 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. // NOTE the odd sizes in the fields below are to help trap alignment issues
  11.  
  12. #include "oofile.hpp"
  13.  
  14. CLASS_TABLE(dbPeople)
  15.     dbChar        LastName, OtherNames;
  16.     dbLong        Salary;
  17.     dbText        Description;
  18.  
  19.     dbPeople() :
  20.                 dbTable("People"),
  21.                 LastName(39, "Last Name", kIndexed),
  22.                 OtherNames(79, "Other Names", kIndexed),
  23.                 Salary("Salary", kIndexed),
  24.                 Description("Description")
  25.                 {};
  26.     
  27. // my own data entry procedures
  28.     void Add(const char *lname, const char *oname, const long salary);
  29. };
  30.  
  31. void dbPeople::Add(const char *lname, const char *oname, const long salary)
  32. {
  33.     newRecord();
  34.     LastName = lname;
  35.     OtherNames = oname;
  36.     Salary = salary;
  37.     saveRecord();
  38. }
  39.  
  40.  
  41.  
  42. int main()
  43. {
  44.     cout << "OOFILE Validation Suite - Test 1\n"
  45.          << "Simple test to store some data and retrieve it\n"
  46.          << "in a single-table database, with no relations\n";
  47.     
  48.     dbConnect_ctree    theDB;
  49.     dbPeople     People;
  50.  
  51.     theDB.useSeparateFiles();  // note the blank connection names!
  52. // this test creates People.dat, People.idx & Blobs
  53.     
  54.     if (dbConnect::fileExists("People.dat"))
  55.         theDB.openConnection("");
  56.     else {
  57.         theDB.newConnection("");
  58.         People.Add("Smith", "John", 0);  // specifically to test zero searches
  59.         People.Description = "John is a plain sort of bloke, not the kind to stand-out in a crowd\n";
  60.         People.Description += "and in fact you'd probably say he's the classic Mr Average. However ";
  61.         People.Description += "he harbours secret dreams of being a brain surgeon and a bloke 'wot ";
  62.         People.Description += "goes down the sewers in big rubber boots'\n";
  63.         People.saveRecord();
  64.  
  65.         People.Add("DENT", "Trissa", 5000);
  66.         People.Description = "Trissa is married to Andy and mother of Tanith and Ryan";
  67.         People.saveRecord();
  68.  
  69.         People.Add("Dent", "Andy", 25000);
  70.         People.Description = "Andy has a Don Quixote complex but is trying to strengthen his Sancho ";
  71.         People.Description += "Panza side to stop him before he gets into trouble,\n rather than taking ";
  72.         People.Description += "Herculean efforts to get him out of the overcommitted messes he creates.\n";
  73.         People.saveRecord();
  74.  
  75.         People.Add("Taylor", "Ken", 75000);
  76.     }
  77.     People.sortBy(People.LastName);
  78.     cout << "Listing records\n" << People << endl;
  79.  
  80.     People.sortBy(People.OtherNames);
  81.     cout << "Listing records in OtherNames order\n" << People << endl;
  82.  
  83.     People.sortBy(People.LastName);
  84.     cout << "Now retrieving by index\n";
  85.     cout << "Retrieving Taylor: " << People["Taylor"].LastName << endl;
  86.  
  87.     cout << "Retrieving Smith by Salary: ";
  88.     People[People.Salary==0];
  89.     cout << People.LastName << endl << endl;
  90. // save their record number so we can come back to them    
  91.     long smithNo = People.recordNumber();
  92.  
  93.     People.search(People.LastName=="Dent");
  94.     cout << "Listing two Dent records: " << endl << People << endl;
  95.  
  96.     cout << endl << "now producing a subset of Taylor & Dent" << endl;
  97.     dbPeople entrepeneurs = People;
  98.     entrepeneurs.search(entrepeneurs.LastName=="Taylor");
  99.     entrepeneurs += People;
  100.     cout << entrepeneurs;
  101.  
  102.     dbPeople savedEnts = entrepeneurs;
  103.     cout << endl << "now reducing via Intersection to Dent" << endl;
  104.     entrepeneurs &= People;      // Dent, Taylor & Dent
  105.     cout << entrepeneurs;
  106.  
  107.     cout << endl << "now Inverting to Smith & Taylor" << endl;
  108.     ~entrepeneurs;
  109.     cout << entrepeneurs;   // Dent inverted
  110.  
  111.     cout << endl << "now reducing via Difference to Dent" << endl;
  112.     savedEnts -= entrepeneurs;  // Dent, Taylor - Smith, Taylor
  113.     cout << savedEnts << endl;
  114.     
  115.     People.selectAll();
  116.  
  117. // demonstrate going to a specific relative record in a selection
  118. // then going to the previous record, as would be common in a GUI
  119. // by double-clicking a line in a browser, then pressing Prev Record
  120.     cout << "Retrieving Taylor by relative record number: ";
  121.     People.gotoRelativeRecord(3);
  122.     cout << People.LastName << endl << endl;
  123.     cout << "Retrieving previous record Smith: ";
  124.     People.prev();
  125.     cout << People.LastName << endl << endl;
  126.  
  127. // demonstrate going to a specific record in a selection
  128.     cout << "Retrieving Smith by record number: ";
  129.     People.gotoRecord(smithNo);
  130.     cout << People.LastName << endl << endl;
  131.         
  132.     cout << "Test Completed" << endl;
  133.     
  134.     return EXIT_SUCCESS;