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

  1. /****************************************************************************
  2. *                                                                           
  3. * FILE: FAVREST.CPP
  4. *
  5. * DESCRIPTION:  This file contains the member functions for the class
  6. *               FavoriteRestaurant used in the sample program for the   
  7. *               C++ Database Framework.
  8. *               
  9. *
  10. ****************************************************************************/
  11.  
  12. #ifdef _MSC_VER
  13. #include <memory.h>
  14. #else
  15. #include <mem.h>
  16. #endif
  17. #include <string.h>
  18. #include "demo.h"
  19.  
  20. /****************************************************************************
  21. *          
  22. * CLASS:  FavoriteRestaurant
  23. *                                                                
  24. * MEMBER FUNCTION: FavoriteRestaurant (constructor)
  25. *
  26. * DESCRIPTION:  This function creates an instance of the FavoriteRestaurant
  27. *               class.
  28. *               
  29. * RETURNS: NONE
  30. *
  31. ****************************************************************************/
  32.  
  33. // the code for the constructor is inline and may be found in DEMO.H
  34.  
  35. /****************************************************************************
  36. *          
  37. * CLASS:  FavoriteRestaurant
  38. *                                                                
  39. * MEMBER FUNCTION: getRecord
  40. *
  41. * DESCRIPTION:  This function gets the current record from the database
  42. *               and assigns the field values to the data members in the
  43. *               class.
  44. *    NOTE:
  45. *
  46. *    Since BLOB fields are not stored in the table's .DB file, getting the
  47. *    record does not get the BLOB field's contents. Two functions are needed
  48. *    to get a record and a BLOB field. First, one must get the record and
  49. *    then the BLOB fields must be retrieved. Between the call to getRecord()
  50. *    and openBlobRead() the BLOBs record could be deleted, or the BLOB could
  51. *    be changed. If this happens, openBlobRead() will return 
  52. *    PXERR_BLOBMODIFIED.  To prevent updates to BLOBs while the record is
  53. *    being read, you can lock the record prior to calling getRecord() 
  54. *    and release the lock after getting the BLOB fields.  
  55. *
  56. *    RETURNS: PXERR_, if one occurs.
  57. *
  58. ****************************************************************************/
  59. Retcode FavoriteRestaurant::getRecord()
  60. {
  61.   BRecord     *currentRecord;
  62.   LOCKHANDLE  recordLockHandle;
  63.   BOOL        isBlank;      
  64.  
  65.   //
  66.   // See if the table was opened.
  67.   //
  68.   if(!isOpen)
  69.   {
  70.      return(lastError = ERROR_TABLENOTOPEN);
  71.   }
  72.   //
  73.   // If the application is running in a multiuser environment, check to 
  74.   // see if another user has changed the table.
  75.   //
  76.   if(cursorPtr->hasChanged() == TRUE)
  77.   {
  78.     //
  79.     // Refresh the table's buffers.
  80.     //
  81.     cursorPtr->refresh();
  82.   }
  83.   //
  84.   // Reduce the amount of pointer dereference.
  85.   //
  86.   currentRecord = cursorPtr->genericRec;
  87.   //
  88.   // Close any BLOB fields that might be open in this table.
  89.   //
  90.   currentRecord->closeBlob(FAV_MENU_FIELD, FALSE);  
  91.   // 
  92.   // Lock the record.
  93.   //
  94.   recordLockHandle = cursorPtr->lockRecord();
  95.  
  96.   //
  97.   // Check if the record is locked and the network is initialized. 
  98.   //
  99.   if((cursorPtr->lastError != PXSUCCESS) &&
  100.     (cursorPtr->lastError != PXERR_NONETINIT))
  101.   {
  102.     //
  103.     // Return any errors that occurred. 
  104.     //
  105.     return(lastError = cursorPtr->lastError);
  106.   }
  107.  
  108.   lastError = AbstractRestaurant::getRecord();
  109.   //
  110.   // Check if successful. 
  111.   //
  112.   if (lastError == PXSUCCESS)
  113.   {
  114.     //
  115.     // Use the BCursor gereric record to extract fields from the record.
  116.     //
  117.     currentRecord->getField(FAV_NAME_FIELD, name, MAX_NAME_LEN + 1, isBlank); 
  118.     currentRecord->getField(FAV_TYPE_FIELD, type, MAX_TYPE_LEN + 1, isBlank); 
  119.  
  120.     //
  121.     // Create PRIVATE BLOBs for the memo field.  This will prevent
  122.     // other users from changing the BLOB or deleting the 
  123.     // record with which the BLOB is associated.
  124.     //
  125.     currentRecord->openBlobRead(FAV_MENU_FIELD, TRUE);
  126.  
  127.   }
  128.   //
  129.   // Unlock the record.
  130.   //
  131.   cursorPtr->unlockRecord(recordLockHandle);
  132.  
  133.   return(lastError);
  134. }
  135.  
  136. /****************************************************************************
  137. *          
  138. * CLASS:  FavoriteRestaurant
  139. *                                                                
  140. * MEMBER FUNCTION: getMenu
  141. *
  142. * DESCRIPTION:  This function retrieves the menu field from the table.
  143. *               
  144. * RETURNS: PXERR_, if one occurs.
  145. *
  146. ****************************************************************************/
  147.  
  148. Retcode FavoriteRestaurant::getMenu(char *buffer, unsigned int sizeOfBuffer, 
  149.   long offset, unsigned int &bytesRead) 
  150. {
  151.   //
  152.   // Specify a particular field and use the generic getBlobField function.
  153.   //
  154.   lastError = getBlobField(FAV_MENU_FIELD, buffer, sizeOfBuffer, offset,
  155.     bytesRead);
  156.  
  157.   return(lastError);
  158. }
  159.  
  160. /****************************************************************************
  161. *          
  162. * CLASS: FavoriteRestaurant
  163. *                                                                
  164. * MEMBER FUNCTION: sizeOfMenuBlob
  165. *
  166. * DESCRIPTION: This function returns the size of the Menu memo field.
  167. *               
  168. * RETURNS: Number of bytes in the Menu memo field.
  169. *
  170. ****************************************************************************/
  171.  
  172. long FavoriteRestaurant::sizeOfMenuBlob()
  173. {
  174.   //
  175.   // Return the BLOB size. Error checking is done in base function.
  176.   //
  177.   return(getBlobSize(FAV_MENU_FIELD));
  178.  
  179. }
  180.  
  181.