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