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

  1. /*********************************************************************
  2. **
  3. **                    BLISTCUR.H
  4. **
  5. ** Implement functions of the BEngine class.
  6. **
  7. *********************************************************************/
  8.  
  9. // DBF - (C) Copyright 1994 by Borland International
  10.  
  11. #ifndef __BLISTCUR_H
  12. #define __BLISTCUR_H
  13.  
  14. #include "envdef.h"
  15. #include "bdbobjec.h"
  16. #include "intstrct.h"
  17. #include "bcursor.h"
  18. #include <string.h>
  19.  
  20. template<class T> class BListCursor : public BDbObject {
  21.  
  22. public:
  23.     Retcode     lastError;
  24.     BOOL        isOpen;
  25.  
  26. public:
  27.  
  28.     BListCursor();
  29.     ~BListCursor();
  30.  
  31.     // Attach the BListCursor class to an existing TABLEHANDLE
  32.     
  33.     Retcode attach(TABLEHANDLE tableHandle);
  34.  
  35.     // Go to the beginning of the cursor
  36.  
  37.     Retcode gotoTop();
  38.  
  39.     // Go to the bottom of the cursor
  40.  
  41.     Retcode gotoBottom();
  42.  
  43.     // Get the next record
  44.  
  45.     Retcode getNextRecord(T &data);
  46.  
  47.     // Get the current record
  48.     
  49.     Retcode getRecord(T &data);
  50.  
  51.     // Close the table handle
  52.     
  53.     Retcode close();
  54.  
  55.     // Return the handle to the cursor. WARNING: be carefull when using
  56.     // the handle outside of the class
  57.  
  58.     TABLEHANDLE getHandle();
  59.  
  60.     // Redefine pure virtuals from the BDbObject class.
  61.  
  62.     virtual char *nameOf() const { return "BListCursor"; }
  63.     virtual void printOn(ostream& os) { os << nameOf()
  64.                                            << " Open Status = "
  65.                                            << (isOpen ? "TRUE" : "FALSE")
  66.                                            << endl; }
  67.  
  68. protected:
  69.     TABLEHANDLE tabH;
  70. };
  71.  
  72. template<class T>
  73. BListCursor<T>::BListCursor()
  74. {
  75.     tabH = NULL;
  76.     isOpen = FALSE;
  77. }
  78.  
  79. template<class T>
  80. BListCursor<T>::~BListCursor()
  81. {
  82.     close();
  83.     tabH = NULL;
  84.     isOpen = FALSE;
  85. }
  86.  
  87. template<class T>
  88. Retcode BListCursor<T>::close()
  89. {
  90.     if (!isOpen)
  91.     {
  92.         return (lastError = PXERR_CURSORNOTOPEN);
  93.     }
  94.  
  95.     lastError = DbiCloseCursor(&tabH);
  96.  
  97.     isOpen = FALSE;
  98.     tabH = NULL;
  99.  
  100.     return lastError;
  101. }
  102.  
  103. template<class T>
  104. Retcode BListCursor<T>::attach(TABLEHANDLE tableHandle)
  105. {
  106.     if (isOpen)
  107.     {
  108.         return (lastError = PXERR_CURSORALREADYOPEN);
  109.     }
  110.  
  111.     if (tableHandle == NULL)
  112.     {
  113.         return (lastError = DBIERR_INVALIDHNDL);
  114.     }
  115.  
  116.     tabH = tableHandle;
  117.     isOpen = TRUE;
  118.     lastError = DBIERR_NONE;
  119.  
  120.     return lastError;
  121. }
  122.  
  123.  
  124. template<class T>
  125. Retcode BListCursor<T>::gotoTop()
  126. {
  127.     if (!isOpen)
  128.     {
  129.         return (lastError = PXERR_CURSORNOTOPEN);
  130.     }
  131.  
  132.     lastError = DbiSetToBegin(tabH);
  133.  
  134.     return lastError;
  135. }
  136.  
  137. template<class T>
  138. Retcode BListCursor<T>::gotoBottom()
  139. {
  140.     if (!isOpen)
  141.     {
  142.         return (lastError = PXERR_CURSORNOTOPEN);
  143.     }
  144.  
  145.     lastError = DbiSetToEnd(tabH);
  146.  
  147.     return lastError;
  148. }
  149.   
  150. template<class T>
  151. Retcode BListCursor<T>::getNextRecord(T &data)
  152. {
  153.     if (!isOpen)
  154.     {
  155.         return (lastError = PXERR_CURSORNOTOPEN);
  156.     }
  157.  
  158.     lastError = DbiGetNextRecord(tabH, dbiNOLOCK, (pBYTE)&data, NULL);
  159.  
  160.     return lastError;
  161. }
  162.  
  163. template<class T>
  164. Retcode BListCursor<T>::getRecord(T &data)
  165. {
  166.     if (!isOpen)
  167.     {
  168.         return (lastError = PXERR_CURSORNOTOPEN);
  169.     }
  170.  
  171.     lastError = DbiGetRecord(tabH, dbiNOLOCK, (pBYTE)&data, NULL);
  172.  
  173.     return lastError;
  174. }
  175.  
  176. template<class T>
  177. TABLEHANDLE BListCursor<T>::getHandle()
  178. {
  179.     if (!isOpen)
  180.     {
  181.         lastError = PXERR_CURSORNOTOPEN;
  182.         return 0;
  183.     }
  184.  
  185.     lastError = DBIERR_NONE;
  186.     
  187.     return tabH;
  188. }
  189.  
  190.  
  191. #endif
  192.