home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / kdbf / example / demo.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  11.3 KB  |  376 lines

  1. /****************************************************************************
  2. *                                                                           
  3. * FILE: DEMO.H
  4. *
  5. * DESCRIPTION:  This file contains the class definitions for the Restaurant
  6. *               sample program.  
  7. *
  8. *  NOTES:    The sample program provides basic functions that are
  9. *            relevant to most applications. The program provides 
  10. *            functions for searching, retrieving and copying data. It 
  11. *            also provides basic database operations such as opening,   
  12. *            and closing database tables, and using cursors.
  13. *            The Restaurant application uses a single database 
  14. *            consisting of two tables. The first table, EATS, contains 
  15. *            data on individual restaurants; name, address, how they 
  16. *            are rated, a review, and a menu for each restaurant. The 
  17. *            EATS table acts as a glossary for all restaurants. The
  18. *            second table, FAVEATS, provides a list of favorite 
  19. *            restaurants that have been copied from the EATS table and 
  20. *            used as a reference for those restaurants that the user
  21. *            has patronized. 
  22. *
  23. *            The database is initialized by user-defined types provided
  24. *            by C++ classes. A list of these types are BEngine, BDatabase,
  25. *            Main Restaurant, and FavoriteRestaurant. Each table in the 
  26. *            database is opened and closed by member functions provided 
  27. *            by the MainRestaurant and FavoriteRestaurant instances. These
  28. *            two types of instances are the two objects in the application
  29. *            corresponding to the two tables in the database. The user 
  30. *            manipulates the tables in the database through member
  31. *            functions (for example, open and close) provided by each 
  32. *            object.
  33. *
  34. *            Movement between records in the tables are provide by
  35. *            member functions Home, End, Up and Down. The cursor acts as
  36. *            a pointer to records in each table. Through the cursor, a
  37. *            user can access data in a particular record. Home sets 
  38. *            the cursor to the first record in the table and End sets 
  39. *            the cursor to the last record in the table. Up sets the
  40. *            cursor to the previous record and Down sets the cursor to
  41. *            the next record in the table.
  42. *
  43. *            Access of data in a record is provided by member functions
  44. *            getRecord, getQuickReview, getReview, and getMenu. Except
  45. *            for getRecord, the other data access functions deal with
  46. *            with retrieving data from BLOB fields. getRecord accesses
  47. *            the current record and retrieve all field values except 
  48. *            BLOBs, and assign variables within the object for the
  49. *            field values retrieved. The user can then reference 
  50. *            these variables. getQuickReview, getReview, and getMenu 
  51. *            retrieve the current records individual BLOB fields. 
  52. *            The EATS table contains two BLOB fields , Review and
  53. *            Menu. The FAVEATS tables contains one BLOB field named
  54. *            Menu. sizeOfMenuBlob and sizeOfReviewBlob are additional 
  55. *            functions provide for accessing these BLOB fields. 
  56. *
  57. *            Two member functions provide searching methods for the
  58. *            EATS table.  findRatingType and findCityType define
  59. *            two searches to locate a restaurant, the first searching 
  60. *            restaurants by rating and type. The second function searches 
  61. *            based on city and type. Each search function can declare its
  62. *            search method to search for the first occurrence of specified
  63. *            field values or search for the next occurrence after a
  64. *            successful search has occurred.
  65. *
  66. *            To copy all relevant data from the EATS table to the
  67. *            FAVREST table, a user can call the copyToFavorite member
  68. *            function. This function provides all record locking, handles
  69. *            concurrency issues and access to both tables to perform a 
  70. *            copy of the current record from the EATS table to the 
  71. *            FAVREST table.
  72. *
  73. *
  74. ****************************************************************************/
  75. //
  76. // include files
  77. //
  78. #include <bengine.h>
  79. #include <bdatabas.h>
  80. #include <bcursor.h>
  81. #include <brecord.h>
  82.  
  83. #ifdef _MSC_VER
  84. #include <memory.h>
  85. #else
  86. #include <mem.h>
  87. #endif
  88.  
  89. //
  90. // Defines for field lengths.
  91. //
  92. #define MAX_NAME_LEN      50
  93. #define MAX_TYPE_LEN      15
  94. #define MAX_STREET_LEN    50
  95. #define MAX_CITY_LEN      50
  96. #define MAX_STATE_LEN     20
  97. #define MAX_ZIP_LEN       10
  98. #define MAX_TELEPHONE_LEN 14
  99.  
  100. //
  101. // Define a buffer size to copy BLOBs.
  102. //
  103. #define BLOB_COPY_LEN 255
  104.  
  105. //
  106. // Defines for field handles to the main table.
  107. //
  108. #define MAIN_NAME_FIELD       1
  109. #define MAIN_STREET_FIELD     2
  110. #define MAIN_CITY_FIELD       3
  111. #define MAIN_STATE_FIELD      4
  112. #define MAIN_ZIP_FIELD        5
  113. #define MAIN_TELEPHONE_FIELD  6
  114. #define MAIN_TYPE_FIELD       7
  115. #define MAIN_RATING_FIELD     8
  116. #define MAIN_REVIEW_FIELD     9
  117. #define MAIN_MENU_FIELD       10
  118.  
  119. //
  120. // Defines for field handles to the Favorite table.
  121. //
  122. #define FAV_NAME_FIELD    1
  123. #define FAV_TYPE_FIELD    2
  124. #define FAV_MENU_FIELD    3
  125.  
  126. //
  127. // Quick review data size.
  128. //
  129. #define MAX_REVIEW_SIZE 80
  130.  
  131. //
  132. // Some special errors returned by the restaurant classes.
  133. //
  134. #define ERROR_TABLENOTOPEN   -1
  135.  
  136. //
  137. // Define the name of the composite indexes for the main table.
  138. //
  139. #define RATING_TYPE_INDEX "rating/type"
  140. #define CITY_TYPE_INDEX   "city/type"
  141.  
  142. //
  143. // Define the number of fields in the secondary indexes.
  144. //
  145. #define RATING_TYPE_FLDCOUNT  2
  146. #define CITY_TYPE_FLDCOUNT    2
  147.  
  148.  
  149. /****************************************************************************
  150. *
  151. * This is the base class for the different restaurant tables that will be
  152. * used in the program.
  153. *
  154. ****************************************************************************/
  155.  
  156. class AbstractRestaurant {
  157.  
  158. protected:
  159.  
  160.   //
  161.   // Flag to identify if the table is open.
  162.   // 
  163.   int isOpen;
  164.   //
  165.   // Pointer to the table's BCursor class instance.
  166.   //
  167.   BCursor *cursorPtr;
  168.  
  169.   //
  170.   // Pointer to the active database.
  171.   //
  172.   BDatabase *dataBase;
  173.  
  174.   //
  175.   // Fix the length to read from the memo field (so user does not pass EOF).
  176.   //
  177.   virtual Retcode fixReadLength(FIELDNUMBER blobField, 
  178.     unsigned int sizeOfBuffer, long offset, unsigned int& readLength);
  179.  
  180.   //
  181.   // Get the BLOB field but also null terminate the string
  182.   // and adjust the size so the user will not read past the EOF.
  183.   //
  184.   virtual Retcode getBlobField(FIELDNUMBER blobField,char *buffer,
  185.     unsigned int sizeOfBuffer, long offset, unsigned int &bytesRead); 
  186.  
  187.   //
  188.   // Get only the BLOB header.
  189.   //
  190.   virtual Retcode getQuickBlob(FIELDNUMBER blobField, char *buffer, 
  191.     int length, int &bytesRead);
  192.   //
  193.   // Find the length of a BLOB field.
  194.   //
  195.   virtual long getBlobSize(FIELDNUMBER blobField);
  196.  
  197. public:
  198.   //
  199.   // Last error that member functions returned.
  200.   //
  201.   Retcode lastError;
  202.  
  203.   //
  204.   // Constructor and destructor.
  205.   //
  206.   AbstractRestaurant(BDatabase *db);
  207.   virtual ~AbstractRestaurant();
  208.  
  209.   //
  210.   // Opens the table and allocates cursorPtr if successful.
  211.   //
  212.   virtual Retcode open(char *tableName);
  213.   virtual Retcode close();
  214.  
  215.   //
  216.   // Some basic browse functions.
  217.   //
  218.   virtual Retcode up();
  219.   virtual Retcode down();
  220.   virtual Retcode home();
  221.   virtual Retcode end();
  222.  
  223.   //
  224.   // Retrieves parts of the MEMO field menu.
  225.   //
  226.   virtual Retcode getMenu(char *buffer, unsigned length, long offset, 
  227.     unsigned int &bytesRead) = 0;
  228.   //
  229.   // Get the size of the BLOB fields.
  230.   //
  231.   virtual long sizeOfMenuBlob() = 0;
  232.   //
  233.   // This function retrieves the current record from the database and 
  234.   // assigns the field values to BCursor's generic record buffer.
  235.   //
  236.   virtual Retcode getRecord();
  237.  
  238. };
  239.  
  240. /****************************************************************************
  241. *
  242. * This class encapsulates the master restaurant database.
  243. *
  244. ****************************************************************************/
  245.  
  246. class MainRestaurant : public AbstractRestaurant {
  247.  
  248.   friend class FavoriteRestaurant;
  249.  
  250.   //
  251.   // Since the Database Framework does not provide a function like 
  252.   // PXSrchFld(), you must open up secondary indexes for the particular 
  253.   // fields you want to search. Use the member function setToCursor() to 
  254.   // sync the match found in a secondary index with the primary index view. 
  255.  
  256. private:
  257.  
  258.   BCursor *ratingTypeIndex;
  259.   BCursor *cityTypeIndex;
  260.  
  261. public:
  262.  
  263.   //
  264.   // Specific variables used to store column values for this table.
  265.   //
  266.   char  name[MAX_NAME_LEN + 1];
  267.   char  type[MAX_TYPE_LEN + 1];
  268.   char  street[MAX_STREET_LEN + 1];
  269.   char  city[MAX_CITY_LEN + 1];
  270.   char  state[MAX_STATE_LEN + 1];
  271.   char  zip[MAX_ZIP_LEN + 1];
  272.   char  telephone[MAX_TELEPHONE_LEN + 1];
  273.   INT16 rating;
  274.  
  275.   //
  276.   // Constructor.
  277.   //
  278.   MainRestaurant(BDatabase *db);
  279.   //
  280.   // Destructor.
  281.   //
  282.   virtual ~MainRestaurant();
  283.   //
  284.   // Redefine the open and close functions for this class.
  285.   //
  286.   virtual Retcode open(char *tableName);
  287.   virtual Retcode close();
  288.  
  289.   //
  290.   // BLOB retrieval functions.
  291.   //
  292.   Retcode getQuickReview(char *buffer, int sizeOfBuffer, 
  293.     int &bytesRead);
  294.  
  295.   Retcode getReview(char *buffer, unsigned int length, long offset, 
  296.     unsigned int &bytesRead);
  297.  
  298.   virtual Retcode getMenu(char *buffer, unsigned int length, long offset, 
  299.     unsigned int &bytesRead);
  300.   //
  301.   // BLOB size functions.
  302.   //
  303.   long sizeOfReviewBlob();
  304.   virtual long sizeOfMenuBlob();
  305.   //
  306.   // This function copies the current record in the main database to the
  307.   // favorite restuarant database.
  308.   //
  309.   Retcode copyToFavorite(FavoriteRestaurant &placeHere);
  310.  
  311.   //
  312.   // This function locates a restaurant by rating and type using a composite
  313.   // index.
  314.   //
  315.   Retcode findRatingType(int ratingToFind, char *typeToFind, 
  316.     PXSearchMode searchMode);
  317.   //
  318.   // This function uses a composite index to locate a restaurant by city 
  319.   // and type.
  320.   //
  321.   Retcode findCityType(char *cityToFind, char *typeToFind, 
  322.     PXSearchMode searchMode);
  323.   //
  324.   // Get the current record from the table.
  325.   //
  326.   virtual Retcode getRecord();
  327.  
  328. };
  329.  
  330. /****************************************************************************
  331. *
  332. * This class encapsulates the favorite restaurant database.
  333. *
  334. ****************************************************************************/
  335.  
  336. class FavoriteRestaurant : public AbstractRestaurant {
  337.  
  338.   friend class MainRestaurant;
  339.  
  340. public:
  341.   //
  342.   // Specific variables used to store column values for this table.
  343.   //
  344.   char name[MAX_NAME_LEN + 1];
  345.   char type[MAX_TYPE_LEN + 1];
  346.   //
  347.   // Constructor.
  348.   //
  349.   FavoriteRestaurant(BDatabase *db) : AbstractRestaurant(db)
  350.   {
  351.           memset(name,0, MAX_NAME_LEN + 1);
  352.           memset(type,0, MAX_TYPE_LEN + 1);
  353.   }
  354.  
  355.   //
  356.   // BLOB retrieval functions.
  357.   //
  358.   virtual Retcode getMenu(char *buffer, unsigned int length, long offset, 
  359.     unsigned int &bytesRead);
  360.   //
  361.   // BLOB size functions.
  362.   //
  363.   virtual long sizeOfMenuBlob();
  364.   //
  365.   // Get the current record from the table.
  366.   //
  367.   virtual Retcode getRecord();
  368.      
  369. };
  370.  
  371.  
  372.  
  373.  
  374.  
  375.