home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / dbsets.h < prev    next >
C/C++ Source or Header  |  1998-04-25  |  7KB  |  241 lines

  1. /***********************************************************************
  2. **      D B S E T S . H                                                *
  3. **                                                                     *
  4. ************************************************************************
  5. ** Copyright (C) 1996 by Microsoft Corporation                         *
  6. **                 All Rights Reserved                                 *
  7. ************************************************************************/
  8. /*
  9.  
  10.         DBSETS.H
  11.  
  12.         Database recordset class definitions for Microsoft Guide
  13.  
  14.  
  15.  
  16. */
  17.  
  18.  
  19.  
  20. #ifndef _DBSETS_H_
  21. #define _DBSETS_H_
  22.  
  23. #include "mstv.h"
  24.  
  25. #ifdef DBSETS_STATIC
  26. #undef COMMMSTVEXPORT
  27. #define COMMMSTVEXPORT
  28. #endif
  29.  
  30. /*
  31.  
  32. The following classes provide an encapsulation of the dbDAO classes that
  33. offers a simplified interface to the database tables.
  34.  
  35. CDatabaseConnection and CDatabaseRecordset are the root classes from
  36. which individual Recordset classes are derived.
  37.  
  38. A Recordset class provides a means to open a recordset, fetch, update,
  39. insert, and delete records, and close the recordset.  The records
  40. produced are of the object type associated with the table.  The
  41. Recordset object itself may be repeatedly opened and closed without
  42. the need for deleting and re-instantiating a new object.
  43.  
  44. For example, the Episode table is accessed using the CEpisodeRecordset class.
  45. It is opened with a where clause parameter that specifies the set of records.
  46. Getting a record produces a CEpisode object, on a one-to-one basis.  The user
  47. is responsible for deleting the object, and appropriate manipulation of the
  48. object is done using the CEpisode methods.
  49.  
  50. Only the current record of an open recordset may be updated or deleted.
  51.  
  52. It is important to note that recordsets are opened by the Jet database engine
  53. as either a Dynaset or Snapshot.  A Dynaset is essentially a list of pointers
  54. to the records in the table.  Data is read from the database only when the
  55. record is actually fetched.  A Snapshot reads all records in the set into a
  56. RAM cache.  Large sets will cause caching to disk and subsequent poorer
  57. performance.  Recordsets will be opened as Dynasets unless the optional lType
  58. parameter specifies otherwise.
  59.  
  60. The record count is not defined on opening a recordset.  Calling
  61. GetRecordCount requires "moving to the last record", which may be slow for
  62. larger recordsets.  Recordsets generated with an ORDER BY clause appear to
  63. correctly set the initial record count.
  64.  
  65.  
  66. */
  67.  
  68. COMMMSTVEXPORT void __stdcall SetJet ( CdbDBEngine* pEngine, CdbWorkspace* pWorkspace, CdbDatabase* pDatabase ); 
  69. COMMMSTVEXPORT CdbDatabase * __stdcall GetDatabase(VOID);
  70. COMMMSTVEXPORT BOOL __stdcall GetDatabaseConnected(VOID);
  71.  
  72.  
  73. class COMMMSTVEXPORT CDatabaseConnection
  74. {
  75.  
  76. //  This class provides the root encapsulation of the Jet engine and
  77. //  the EPG database.
  78.  
  79.  
  80. public:
  81.  
  82.  
  83.     BOOL    StartEngine( const CString &csViewer, const CString &csPassword,
  84.                                              const CString &systemFile);
  85.     BOOL    ConnectToDatabase( const CString &csViewer, const CString &csPassword,
  86.                              const CString &systemFile, const CString &databaseFile, CString csWorkspaceName = "");
  87.  
  88.     BOOL    DisconnectFromDatabase( VOID);
  89.  
  90.     BOOL    CompactDatabase( VOID);
  91.  
  92.     CString  GetWorkspaceName( VOID);
  93.  
  94.     LPUNKNOWN GetWorkspaceUnknown( VOID);
  95.  
  96. protected:
  97.  
  98.     VOID    HandleDBException( CdbException* pException, LPCTSTR SourceOfException);
  99.  
  100.     BOOL     RepairDB( const CString &databaseFile );
  101.  
  102.     BOOL    DataBaseInNeedOfRepair();
  103. };
  104.  
  105.  
  106.  
  107.  
  108.  
  109.     enum // FindType is one of
  110.     {   dbFindFirst = 1,
  111.         dbFindLast,
  112.         dbFindNext,
  113.         dbFindPrevious
  114.     };
  115.  
  116.  
  117.  
  118. class COMMMSTVEXPORT CDatabaseRecordset : public CDatabaseConnection
  119. {
  120.  
  121. // This class provides the root encapsulation for recordset manipulation
  122.  
  123.  
  124. // lType is one of: dbOpenSnapshot or default dbOpenDynaset
  125.                 
  126.  
  127. public:
  128.                 CDatabaseRecordset(VOID);
  129.                ~CDatabaseRecordset(VOID);
  130.  
  131.         virtual BOOL    OpenRecordset( LPCTSTR WhereBy, LONG lType=-1);
  132.  
  133.         BOOL    OpenRecordsetQueryDef( LPCTSTR QueryName, LONG lType=-1);
  134.  
  135.         virtual VOID    CloseRecordset( VOID);
  136.  
  137.         CString GetQueryPrefix( VOID);
  138.  
  139.         LONG    GetRecordCount( VOID);
  140.  
  141.         VOID*   GetRecord( LONG RecordNumber);
  142.  
  143.         BOOL    GotoRecord( LONG RecordNumber);
  144.  
  145.         LONG    FindRecord( LONG FindType, LPCTSTR Criteria);
  146.  
  147.         BOOL    InsertRecord( VOID* cRecordsetObject);
  148.  
  149.         BOOL    UpdateRecord( VOID* cRecordsetObject);
  150.  
  151.         BOOL    DeleteRecord( VOID);
  152.  
  153.         void OpenIndexed(int iKeyID, int nOpenType,
  154.                                         LPCTSTR lpszSQL, int nOptions);
  155.         void SeekAddRS(CObject &coo);
  156.         void UpdateRS(CObject &coo);
  157.         void StartUpdateRS(CObject &coo);
  158.         void EndUpdateRS();
  159.  
  160.         void Edit(void);
  161.         void Update(void);
  162.         void MoveFirst(void);
  163.         void MovePrevious(void);
  164.         void MoveNext(void);
  165.         void MoveLast(void);
  166.         BOOL GetEOF(void);
  167.  
  168.         COleVariant GetField(LPCTSTR pstrIndex);
  169.         LONG GetLongField(LPCTSTR pstrIndex);
  170.         COleVariant GetField(LONG lIndex);
  171.         VOID SetField(LPCTSTR pstrIndex, COleVariant cov);
  172.         ULONG GetFieldSize(LPCTSTR pstrIndex);
  173.  
  174.                 // implemented by child recordset
  175.  
  176. virtual CString GetTableName( VOID) = 0;
  177.  
  178.  
  179.  
  180. protected:      // implemented by child recordset
  181.  
  182. virtual BOOLEAN Seek(LPCTSTR lpszComparison, CObject &coo);
  183.  
  184. virtual VOID*   GetRecordsetObject( VOID) = 0;
  185.  
  186. virtual BOOL    SetRecordsetObject( VOID* cRecordsetObject) = 0;
  187.  
  188.  
  189.  
  190. protected:          // accessed by child recordset
  191.  
  192.     CdbRecordset    m_cRecordset;
  193.  
  194.  
  195. protected:
  196.  
  197.     time_t          m_ConnectTime;
  198.  
  199.     LONG            m_lRecordCount;
  200.     LONG            m_lRecordNumber;
  201.     BOOL            m_bRecordsetOpen;
  202.  
  203. };
  204.  
  205.  
  206. #define AFX_RFX_LONG_PSEUDO_NULL (0x4a4d4120L)    // from afxdb.h
  207.  
  208.  
  209. // Conversion macros - Variant
  210.  
  211. // These macros are used by the GetRecordsetObject routines to convert the
  212. // Variant returned by dbdao to the local data type.
  213. // Note there is no checking for nulls or incorrect data type,
  214. // therefore use these macros at your own risk!
  215.  
  216. #define VAR2BOOL(v)           (v).bVal
  217. #define VAR2BYTE(v)           (v).bVal
  218. #define VAR2SHORT(v)          (v).iVal
  219. #define VAR2LONG(v)           (v).lVal
  220. #define VAR2FLOAT(v)          (v).fltVal
  221. #define VAR2DOUBLE(v)         (v).dblVal
  222. #define VAR2DATE(v)           (v)
  223. #define VAR2CURRENCY(v)       (v)
  224. #define VAR2CSTR(v)  (LPCTSTR)(v).bstrVal
  225.  
  226. // These macros are used in SetRecordsetObject routines to convert the local
  227. // data type to COleVariant for dbdao
  228.  
  229. #define BOOL2OLEVAR(a)      COleVariant((BYTE)(a))
  230. #define BYTE2OLEVAR(a)      COleVariant((a))
  231. #define SHORT2OLEVAR(a)     COleVariant((a))
  232. #define LONG2OLEVAR(a)      COleVariant((a))
  233. #define FLOAT2OLEVAR(a)     COleVariant((a))
  234. #define DOUBLE2OLEVAR(a)    COleVariant((a))
  235. #define DATE2OLEVAR(a)      COleVariant((a))
  236. #define CURRENCY2OLEVAR(a)  COleVariant((a))
  237. #define CSTR2OLEVAR(a)      COleVariant((LPCTSTR)(a),VT_BSTRT)
  238.  
  239. #endif
  240.  
  241.