You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.8 KiB

  1. /***********************************************************************/
  2. /* ODBConn.h : header file for the ODBC connection classes. */
  3. /***********************************************************************/
  4. //nclude <windows.h> /* Windows include file */
  5. //nclude <windowsx.h> /* Message crackers */
  6. /***********************************************************************/
  7. /* Included C-definition files required by the interface. */
  8. /***********************************************************************/
  9. #include "block.h"
  10. /***********************************************************************/
  11. /* ODBC interface. */
  12. /***********************************************************************/
  13. #include <sql.h>
  14. #include <sqlext.h>
  15. /***********************************************************************/
  16. /* Constants and defines. */
  17. /***********************************************************************/
  18. // Miscellaneous sizing info
  19. #define MAX_NUM_OF_MSG 10 // Max number of error messages
  20. //efine MAX_CURRENCY 30 // Max size of Currency($) string
  21. #define MAX_TNAME_LEN 32 // Max size of table names
  22. //efine MAX_FNAME_LEN 256 // Max size of field names
  23. #define MAX_STRING_INFO 256 // Max size of string from SQLGetInfo
  24. //efine MAX_DNAME_LEN 256 // Max size of Recordset names
  25. #define MAX_CONNECT_LEN 512 // Max size of Connect string
  26. //efine MAX_CURSOR_NAME 18 // Max size of a cursor name
  27. #define DEFAULT_FIELD_TYPE SQL_TYPE_NULL // pick "C" data type to match SQL data type
  28. #if !defined(WIN32)
  29. typedef unsigned char *PUCHAR;
  30. #endif // !WIN32
  31. // Timeout and net wait defaults
  32. #define DEFAULT_LOGIN_TIMEOUT 15 // seconds to before fail on connect
  33. #define DEFAULT_QUERY_TIMEOUT 15 // seconds to before fail waiting for results
  34. // Field Flags, used to indicate status of fields
  35. //efine SQL_FIELD_FLAG_DIRTY 0x1
  36. //efine SQL_FIELD_FLAG_NULL 0x2
  37. // Update options flags
  38. #define SQL_SETPOSUPDATES 0x0001
  39. #define SQL_POSITIONEDSQL 0x0002
  40. //efine SQL_GDBOUND 0x0004
  41. enum CATINFO {CAT_TAB = 1, /* SQLTables */
  42. CAT_COL = 2, /* SQLColumns */
  43. CAT_KEY = 3, /* SQLPrimaryKeys */
  44. CAT_STAT = 4, /* SQLStatistics */
  45. CAT_SPC = 5}; /* SQLSpecialColumns */
  46. /***********************************************************************/
  47. /* This structure is used to control the catalog functions. */
  48. /***********************************************************************/
  49. typedef struct tagCATPARM {
  50. CATINFO Id; // Id to indicate function
  51. PQRYRES Qrp; // Result set pointer
  52. PUCHAR Tab; // Table name or pattern
  53. PUCHAR Pat; // Table type or column pattern
  54. SQLLEN* *Vlen; // To array of indicator values
  55. UWORD *Status; // To status block
  56. // For SQLStatistics
  57. UWORD Unique; // Index type
  58. UWORD Accuracy; // For Cardinality and Pages
  59. // For SQLSpecialColumns
  60. UWORD ColType;
  61. UWORD Scope;
  62. UWORD Nullable;
  63. } CATPARM;
  64. // ODBC connection to a data source
  65. class TDBODBC;
  66. class ODBCCOL;
  67. class ODBConn;
  68. /***********************************************************************/
  69. /* Class DBX (ODBC exception). */
  70. /***********************************************************************/
  71. class DBX : public BLOCK {
  72. friend class ODBConn;
  73. // Construction (by ThrowDBX only) -- destruction
  74. protected:
  75. DBX(RETCODE rc, PSZ msg = NULL);
  76. public:
  77. //virtual ~DBX() {}
  78. //void operator delete(void*, PGLOBAL, void*) {};
  79. // Implementation (use ThrowDBX to create)
  80. RETCODE GetRC(void) {return m_RC;}
  81. PSZ GetMsg(void) {return m_Msg;}
  82. const char *GetErrorMessage(int i)
  83. {return (i >=0 && i < MAX_NUM_OF_MSG) ? m_ErrMsg[i] : "No ODBC error";}
  84. protected:
  85. void BuildErrorMessage(ODBConn* pdb, HSTMT hstmt = SQL_NULL_HSTMT);
  86. // Attributes
  87. RETCODE m_RC;
  88. PSZ m_Msg;
  89. PSZ m_ErrMsg[MAX_NUM_OF_MSG];
  90. }; // end of DBX class definition
  91. /***********************************************************************/
  92. /* ODBConn class. */
  93. /***********************************************************************/
  94. class ODBConn : public BLOCK {
  95. friend class DBX;
  96. friend PQRYRES GetColumnInfo(PGLOBAL, char*&, char *, int, PVBLK&);
  97. private:
  98. ODBConn(); // Standard (unused) constructor
  99. public:
  100. ODBConn(PGLOBAL g, TDBODBC *tdbp);
  101. enum DOP { // Db Open oPtions
  102. traceSQL = 0x0001, // Trace SQL calls
  103. openReadOnly = 0x0002, // Open database read only
  104. useCursorLib = 0x0004, // Use ODBC cursor lib
  105. noOdbcDialog = 0x0008, // Don't display ODBC Connect dialog
  106. forceOdbcDialog = 0x0010}; // Always display ODBC connect dialog
  107. int Open(PSZ ConnectString, DWORD Options = 0);
  108. void Close(void);
  109. // Attributes
  110. public:
  111. char GetQuoteChar(void) {return m_IDQuoteChar;}
  112. // Database successfully opened?
  113. bool IsOpen(void) {return m_hdbc != SQL_NULL_HDBC;}
  114. PSZ GetStringInfo(ushort infotype);
  115. int GetMaxValue(ushort infotype);
  116. PSZ GetConnect(void) {return m_Connect;}
  117. public:
  118. // Operations
  119. void SetLoginTimeout(DWORD sec) {m_LoginTimeout = sec;}
  120. void SetQueryTimeout(DWORD sec) {m_QueryTimeout = sec;}
  121. int GetResultSize(char *sql, ODBCCOL *colp);
  122. int ExecDirectSQL(char *sql, ODBCCOL *tocols);
  123. int Fetch(void);
  124. int PrepareSQL(char *sql);
  125. bool ExecuteSQL(void);
  126. bool BindParam(ODBCCOL *colp);
  127. int GetCatInfo(CATPARM *cap);
  128. bool GetDataSources(PQRYRES qrp);
  129. bool GetDrivers(PQRYRES qrp);
  130. public:
  131. // Set special options
  132. void OnSetOptions(HSTMT hstmt);
  133. // Implementation
  134. public:
  135. // virtual ~ODBConn();
  136. // ODBC operations
  137. protected:
  138. bool Check(RETCODE rc);
  139. void ThrowDBX(RETCODE rc, PSZ msg, HSTMT hstmt = SQL_NULL_HSTMT);
  140. void ThrowDBX(PSZ msg);
  141. void AllocConnect(DWORD dwOptions);
  142. bool Connect(DWORD Options);
  143. void VerifyConnect(void);
  144. void GetConnectInfo(void);
  145. void Free(void);
  146. protected:
  147. // Static members
  148. //static HENV m_henv;
  149. //static int m_nAlloc; // per-Appl reference to HENV above
  150. // Members
  151. PGLOBAL m_G;
  152. TDBODBC *m_Tdb;
  153. HENV m_henv;
  154. HDBC m_hdbc;
  155. HSTMT m_hstmt;
  156. DWORD m_LoginTimeout;
  157. DWORD m_QueryTimeout;
  158. DWORD m_UpdateOptions;
  159. DWORD m_RowsetSize;
  160. int m_Catver;
  161. PSZ m_Connect;
  162. bool m_Updatable;
  163. char m_IDQuoteChar;
  164. }; // end of ODBConn class definition