| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: odbc.h |
| 3 | // Purpose: ODBC classes |
| 4 | // Author: Olaf Klein, Patrick Halke, Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 01/02/97 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart and Markus Holzem |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #include "wx/setup.h" |
| 13 | |
| 14 | #if wxUSE_ODBC |
| 15 | |
| 16 | #ifdef __GNUG__ |
| 17 | #pragma interface "odbc.h" |
| 18 | #endif |
| 19 | |
| 20 | #ifndef _WX_ODBCH__ |
| 21 | #define _WX_ODBCH__ |
| 22 | |
| 23 | //#ifdef __WXMSW__ |
| 24 | //#include <windows.h> |
| 25 | //#endif |
| 26 | |
| 27 | |
| 28 | #ifdef __UNIX__ |
| 29 | extern "C" |
| 30 | { |
| 31 | #include "wx/isql.h" |
| 32 | #include "wx/isqlext.h" |
| 33 | |
| 34 | typedef float SFLOAT; |
| 35 | typedef double SDOUBLE; |
| 36 | |
| 37 | #define ULONG UDWORD |
| 38 | #define SQL_SQLSTATE_SIZE 5 |
| 39 | } |
| 40 | #else // !Unix |
| 41 | #include <sqlext.h> |
| 42 | #endif // Unix/!Unix |
| 43 | |
| 44 | #include "wx/defs.h" |
| 45 | #include "wx/list.h" |
| 46 | #include "wx/string.h" |
| 47 | |
| 48 | typedef RETCODE wxRETCODE; |
| 49 | |
| 50 | // Recordset open types |
| 51 | #define wxOPEN_TYPE_DYNASET 1 |
| 52 | #define wxOPEN_TYPE_SNAPSHOT 2 |
| 53 | #define wxOPEN_TYPE_FORWARD_ONLY 3 |
| 54 | |
| 55 | // Recordset open options |
| 56 | #define wxOPTION_DEFAULT 1 |
| 57 | #define wxOPTION_APPEND_ONLY 2 |
| 58 | #define wxOPTION_READ_ONLY 3 |
| 59 | |
| 60 | // Data types |
| 61 | class WXDLLEXPORT wxRecordSet; |
| 62 | |
| 63 | class WXDLLEXPORT wxDatabase: public wxObject |
| 64 | { |
| 65 | DECLARE_DYNAMIC_CLASS(wxDatabase) |
| 66 | protected: |
| 67 | static HENV hEnv; |
| 68 | static int refCount; |
| 69 | |
| 70 | HDBC hDBC; |
| 71 | char* username; |
| 72 | char* password; |
| 73 | char* datasource; |
| 74 | char* dbname; |
| 75 | char* connectstring; |
| 76 | bool isOpen; |
| 77 | |
| 78 | // error-handling variables |
| 79 | wxRETCODE retcode; |
| 80 | char sqlstate[SQL_SQLSTATE_SIZE+1]; // error class and subclass |
| 81 | char errmsg[SQL_MAX_MESSAGE_LENGTH]; // error message |
| 82 | long nat_err; // error number by ODBC driver |
| 83 | bool err_occured; |
| 84 | |
| 85 | wxList recordSets; // Record sets: Added by JACS |
| 86 | |
| 87 | public: |
| 88 | wxDatabase(); |
| 89 | ~wxDatabase(); |
| 90 | |
| 91 | bool Open(char *, bool exclusive =FALSE, bool readOnly =TRUE, char *username ="ODBC", char *password =""); |
| 92 | bool Close(); |
| 93 | |
| 94 | // Cleanup operations, added by JACS |
| 95 | void DeleteRecordSets(void); // Called when the database is deleted |
| 96 | void ResetRecordSets(void); // Required if the database is closed |
| 97 | inline wxList& GetRecordSets(void) { return recordSets; } |
| 98 | |
| 99 | inline char *GetUsername(void) { return username; } |
| 100 | inline char *GetPassword(void) { return password; } |
| 101 | inline char *GetDataSource(void) { return datasource; } |
| 102 | inline bool IsOpen(void) { return isOpen; } |
| 103 | inline wxRETCODE GetErrorCode(void) { return retcode; } |
| 104 | inline HDBC GetHDBC(void) { return hDBC; } |
| 105 | inline HENV GetHENV(void) { return hEnv; } |
| 106 | |
| 107 | void SetPassword(char *s); |
| 108 | void SetUsername(char *s); |
| 109 | void SetDataSource(char *s); |
| 110 | |
| 111 | // Database attributes |
| 112 | char *GetDatabaseName(); |
| 113 | bool CanUpdate(); |
| 114 | bool CanTransact(); |
| 115 | bool InWaitForDataSource(); |
| 116 | void SetLoginTimeout(long seconds); |
| 117 | void SetQueryTimeout(long seconds); |
| 118 | void SetSynchronousMode(bool synchronous); |
| 119 | |
| 120 | // Database operations |
| 121 | bool BeginTrans(); |
| 122 | bool CommitTrans(); |
| 123 | bool RollbackTrans(); |
| 124 | void Cancel(); |
| 125 | |
| 126 | // Error handling |
| 127 | bool ErrorOccured(); |
| 128 | char* GetErrorMessage(); |
| 129 | long GetErrorNumber(); |
| 130 | char* GetErrorClass(); |
| 131 | inline void ErrorSnapshot(HSTMT =SQL_NULL_HSTMT); |
| 132 | |
| 133 | // Overridables |
| 134 | virtual void OnSetOptions(wxRecordSet *recordSet); |
| 135 | virtual void OnWaitForDataSource(bool stillExecuting); |
| 136 | |
| 137 | bool GetInfo(long infoType, long *buf); |
| 138 | bool GetInfo(long infoType, char *buf, int bufSize = -1); |
| 139 | |
| 140 | // implementation = TRUE means get the DLL version. |
| 141 | // Otherwise, returns header file version. |
| 142 | wxString GetODBCVersionString(bool implementation = TRUE); |
| 143 | float GetODBCVersionFloat(bool implementation = TRUE); |
| 144 | }; |
| 145 | |
| 146 | // Represents a data row |
| 147 | class WXDLLEXPORT wxQueryField: public wxObject |
| 148 | { |
| 149 | // JACS |
| 150 | DECLARE_DYNAMIC_CLASS(wxQueryField) |
| 151 | private: |
| 152 | void *data; |
| 153 | short type; |
| 154 | long size; |
| 155 | bool dirty; |
| 156 | |
| 157 | bool AllocData(); |
| 158 | |
| 159 | public: |
| 160 | wxQueryField(); |
| 161 | ~wxQueryField(); |
| 162 | |
| 163 | bool SetData(void*, long); |
| 164 | void SetDirty(bool =TRUE); |
| 165 | void ClearData(); |
| 166 | void SetType(short); |
| 167 | void SetSize(long); |
| 168 | |
| 169 | void* GetData(); |
| 170 | short GetType(); |
| 171 | long GetSize(); |
| 172 | |
| 173 | bool IsDirty(); |
| 174 | }; |
| 175 | |
| 176 | // Represents a column description |
| 177 | class WXDLLEXPORT wxQueryCol: public wxObject |
| 178 | { |
| 179 | // JACS |
| 180 | DECLARE_DYNAMIC_CLASS(wxQueryCol) |
| 181 | private: |
| 182 | short type; |
| 183 | char *name; |
| 184 | bool nullable; |
| 185 | long varsize; |
| 186 | void* var; |
| 187 | |
| 188 | public: |
| 189 | wxList fields; |
| 190 | |
| 191 | wxQueryCol(); |
| 192 | ~wxQueryCol(); |
| 193 | |
| 194 | void* BindVar(void*, long); |
| 195 | void FillVar(int); |
| 196 | void AppendField(void*, long); |
| 197 | bool SetData(int, void*, long); |
| 198 | void SetName(char*); |
| 199 | void SetNullable(bool); |
| 200 | void SetFieldDirty(int, bool =TRUE); |
| 201 | void SetType(short); |
| 202 | |
| 203 | char* GetName(); |
| 204 | short GetType(); |
| 205 | bool IsNullable(); |
| 206 | void* GetData(int); |
| 207 | long GetSize(int); |
| 208 | |
| 209 | bool IsFieldDirty(int); |
| 210 | }; |
| 211 | |
| 212 | class WXDLLEXPORT wxRecordSet: public wxObject |
| 213 | { |
| 214 | // JACS |
| 215 | DECLARE_DYNAMIC_CLASS(wxRecordSet) |
| 216 | private: |
| 217 | int cursor; |
| 218 | int type; |
| 219 | int options; |
| 220 | |
| 221 | protected: |
| 222 | HSTMT hStmt; |
| 223 | int nFields; |
| 224 | int nParams; |
| 225 | int nRecords; |
| 226 | short nCols; |
| 227 | char *recordFilter; |
| 228 | char *sortString; |
| 229 | char *defaultSQL; |
| 230 | char* tablename; |
| 231 | wxDatabase *parentdb; |
| 232 | wxRETCODE retcode; |
| 233 | wxList cols; |
| 234 | wxList fetchbuf; |
| 235 | |
| 236 | void FillVars(int); |
| 237 | |
| 238 | public: |
| 239 | // JACS gave parent a default value for benefit of IMPLEMENT_DYNAMIC_CLASS |
| 240 | wxRecordSet(wxDatabase *parent = NULL, |
| 241 | int = wxOPEN_TYPE_DYNASET, |
| 242 | int = wxOPTION_DEFAULT); |
| 243 | ~wxRecordSet(); |
| 244 | |
| 245 | // My own, lower-level functions. |
| 246 | bool BeginQuery(int openType, char *sql = NULL, int options = wxOPTION_DEFAULT); |
| 247 | bool EndQuery(); |
| 248 | bool Query(char* columns, char* table =NULL, char *filter =NULL); |
| 249 | |
| 250 | // Attributes |
| 251 | inline int GetNumberFields(void) { return nFields; } |
| 252 | inline int GetNumberParams(void) { return nParams; } |
| 253 | long GetNumberRecords(); |
| 254 | long GetNumberCols(); |
| 255 | inline char *GetFilter(void) { return recordFilter; } |
| 256 | inline char *GetSortString(void) { return sortString; } |
| 257 | inline wxDatabase *GetDatabase(void) { return parentdb; } |
| 258 | inline wxRETCODE GetErrorCode(void) { return retcode; } |
| 259 | bool CanAppend(); |
| 260 | bool CanRestart(); |
| 261 | bool CanScroll(); |
| 262 | bool CanTransact(); |
| 263 | bool CanUpdate(); |
| 264 | long GetCurrentRecord(); |
| 265 | bool RecordCountFinal(); |
| 266 | bool GetResultSet(); |
| 267 | bool ExecuteSQL(char*); |
| 268 | bool GetTables(); |
| 269 | bool GetColumns(char* =NULL); |
| 270 | bool GetPrimaryKeys(char* =NULL); |
| 271 | bool GetForeignKeys(char* , char * ); |
| 272 | char *GetTableName(); |
| 273 | void SetTableName(char*); |
| 274 | char *GetSQL(); |
| 275 | bool IsOpen(); |
| 276 | bool IsBOF(); |
| 277 | bool IsEOF(); |
| 278 | bool IsDeleted(); |
| 279 | |
| 280 | bool GetFieldData(int colPos, int dataType, void *dataPtr); |
| 281 | bool GetFieldData(const char*, int dataType, void *dataPtr); |
| 282 | void* GetFieldDataPtr(int, int); |
| 283 | void* GetFieldDataPtr(const char*, int); |
| 284 | char* GetColName(int); |
| 285 | short GetColType(int); |
| 286 | short GetColType(const char*); |
| 287 | void* BindVar(int, void*, long); |
| 288 | void* BindVar(const char*, void*, long); |
| 289 | |
| 290 | void SetType(int); |
| 291 | int GetType(); |
| 292 | void SetOptions(int); |
| 293 | int GetOptions(); |
| 294 | |
| 295 | // Update operations |
| 296 | void AddNew(); |
| 297 | bool Delete(); |
| 298 | void Edit(); |
| 299 | bool Update(); |
| 300 | |
| 301 | // Record navigation |
| 302 | virtual bool Move(long rows); |
| 303 | virtual bool MoveFirst(); |
| 304 | virtual bool MoveLast(); |
| 305 | virtual bool MoveNext(); |
| 306 | virtual bool MovePrev(); |
| 307 | virtual bool GoTo(long); |
| 308 | |
| 309 | // Others |
| 310 | bool GetDataSources(); |
| 311 | |
| 312 | // Associate a column name/position with a data location |
| 313 | // bool BindColumn(int colPos, int dataType, void *dataPtr); |
| 314 | |
| 315 | void Cancel(); |
| 316 | bool IsFieldDirty(int); |
| 317 | bool IsFieldDirty(const char*); |
| 318 | bool IsFieldNull(int); |
| 319 | bool IsFieldNull(const char*); |
| 320 | bool IsColNullable(int); |
| 321 | bool IsColNullable(const char*); |
| 322 | virtual bool Requery(); |
| 323 | virtual void SetFieldDirty(int, bool dirty = TRUE); |
| 324 | virtual void SetFieldDirty(const char*, bool dirty = TRUE); |
| 325 | void SetFieldNull(void *p, bool isNull = TRUE); |
| 326 | |
| 327 | // Overridables |
| 328 | virtual char *GetDefaultConnect(); |
| 329 | virtual char *GetDefaultSQL(); |
| 330 | |
| 331 | // Internal |
| 332 | |
| 333 | // Build SQL query from column specification |
| 334 | bool ConstructDefaultSQL(); |
| 335 | void SetDefaultSQL(char *s); |
| 336 | bool ReleaseHandle(void); // Added JACS |
| 337 | }; |
| 338 | |
| 339 | #endif |
| 340 | |
| 341 | #endif // wxUSE_ODBC |