| 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 __WXGTK__ |
| 29 | extern "C" { |
| 30 | #include <../iodbc/isql.h> |
| 31 | #include <../iodbc/isqlext.h> |
| 32 | #include <../iodbc/odbc_funcs.h> |
| 33 | #include <../iodbc/odbc_types.h> |
| 34 | typedef float SFLOAT; |
| 35 | typedef double SDOUBLE; |
| 36 | #define ULONG UDWORD |
| 37 | } |
| 38 | #else |
| 39 | #include <sqlext.h> |
| 40 | #endif |
| 41 | |
| 42 | #include "wx/defs.h" |
| 43 | #include "wx/list.h" |
| 44 | #include "wx/string.h" |
| 45 | |
| 46 | typedef RETCODE wxRETCODE; |
| 47 | |
| 48 | // Recordset open types |
| 49 | #define wxOPEN_TYPE_DYNASET 1 |
| 50 | #define wxOPEN_TYPE_SNAPSHOT 2 |
| 51 | #define wxOPEN_TYPE_FORWARD_ONLY 3 |
| 52 | |
| 53 | // Recordset open options |
| 54 | #define wxOPTION_DEFAULT 1 |
| 55 | #define wxOPTION_APPEND_ONLY 2 |
| 56 | #define wxOPTION_READ_ONLY 3 |
| 57 | |
| 58 | // Data types |
| 59 | class WXDLLEXPORT wxRecordSet; |
| 60 | |
| 61 | class WXDLLEXPORT wxDatabase: public wxObject |
| 62 | { |
| 63 | // JACS |
| 64 | DECLARE_DYNAMIC_CLASS(wxDatabase) |
| 65 | private: |
| 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(void); |
| 89 | ~wxDatabase(void); |
| 90 | |
| 91 | bool Open(char *, bool exclusive =FALSE, bool readOnly =TRUE, char *username ="ODBC", char *password =""); |
| 92 | bool Close(void); |
| 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(void); |
| 113 | bool CanUpdate(void); |
| 114 | bool CanTransact(void); |
| 115 | bool InWaitForDataSource(void); |
| 116 | void SetLoginTimeout(long seconds); |
| 117 | void SetQueryTimeout(long seconds); |
| 118 | void SetSynchronousMode(bool synchronous); |
| 119 | |
| 120 | // Database operations |
| 121 | bool BeginTrans(void); |
| 122 | bool CommitTrans(void); |
| 123 | bool RollbackTrans(void); |
| 124 | void Cancel(void); |
| 125 | |
| 126 | // Error handling |
| 127 | bool ErrorOccured(void); |
| 128 | char* GetErrorMessage(void); |
| 129 | long GetErrorNumber(void); |
| 130 | char* GetErrorClass(void); |
| 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(void); |
| 158 | |
| 159 | public: |
| 160 | wxQueryField(void); |
| 161 | ~wxQueryField(void); |
| 162 | |
| 163 | bool SetData(void*, long); |
| 164 | void SetDirty(bool =TRUE); |
| 165 | void ClearData(void); |
| 166 | void SetType(short); |
| 167 | void SetSize(long); |
| 168 | |
| 169 | void* GetData(void); |
| 170 | short GetType(void); |
| 171 | long GetSize(void); |
| 172 | |
| 173 | bool IsDirty(void); |
| 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(void); |
| 192 | ~wxQueryCol(void); |
| 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(void); |
| 204 | short GetType(void); |
| 205 | bool IsNullable(void); |
| 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, int =wxOPEN_TYPE_DYNASET, int =wxOPTION_DEFAULT); |
| 241 | ~wxRecordSet(void); |
| 242 | |
| 243 | // My own, lower-level functions. |
| 244 | bool BeginQuery(int openType, char *sql = NULL, int options = wxOPTION_DEFAULT); |
| 245 | bool EndQuery(void); |
| 246 | bool Query(char* columns, char* table =NULL, char *filter =NULL); |
| 247 | |
| 248 | // Attributes |
| 249 | inline int GetNumberFields(void) { return nFields; } |
| 250 | inline int GetNumberParams(void) { return nParams; } |
| 251 | long GetNumberRecords(void); |
| 252 | long GetNumberCols(void); |
| 253 | inline char *GetFilter(void) { return recordFilter; } |
| 254 | inline char *GetSortString(void) { return sortString; } |
| 255 | inline wxDatabase *GetDatabase(void) { return parentdb; } |
| 256 | inline wxRETCODE GetErrorCode(void) { return retcode; } |
| 257 | bool CanAppend(void); |
| 258 | bool CanRestart(void); |
| 259 | bool CanScroll(void); |
| 260 | bool CanTransact(void); |
| 261 | bool CanUpdate(void); |
| 262 | long GetCurrentRecord(void); |
| 263 | bool RecordCountFinal(void); |
| 264 | bool GetResultSet(void); |
| 265 | bool ExecuteSQL(char*); |
| 266 | bool GetTables(void); |
| 267 | bool GetColumns(char* =NULL); |
| 268 | bool GetPrimaryKeys(char* =NULL); |
| 269 | bool GetForeignKeys(char* , char * ); |
| 270 | char *GetTableName(void); |
| 271 | void SetTableName(char*); |
| 272 | char *GetSQL(void); |
| 273 | bool IsOpen(void); |
| 274 | bool IsBOF(void); |
| 275 | bool IsEOF(void); |
| 276 | bool IsDeleted(void); |
| 277 | |
| 278 | bool GetFieldData(int colPos, int dataType, void *dataPtr); |
| 279 | bool GetFieldData(const char*, int dataType, void *dataPtr); |
| 280 | void* GetFieldDataPtr(int, int); |
| 281 | void* GetFieldDataPtr(const char*, int); |
| 282 | char* GetColName(int); |
| 283 | short GetColType(int); |
| 284 | short GetColType(const char*); |
| 285 | void* BindVar(int, void*, long); |
| 286 | void* BindVar(const char*, void*, long); |
| 287 | |
| 288 | void SetType(int); |
| 289 | int GetType(void); |
| 290 | void SetOptions(int); |
| 291 | int GetOptions(void); |
| 292 | |
| 293 | // Update operations |
| 294 | void AddNew(void); |
| 295 | bool Delete(void); |
| 296 | void Edit(void); |
| 297 | bool Update(void); |
| 298 | |
| 299 | // Record navigation |
| 300 | virtual bool Move(long rows); |
| 301 | virtual bool MoveFirst(void); |
| 302 | virtual bool MoveLast(void); |
| 303 | virtual bool MoveNext(void); |
| 304 | virtual bool MovePrev(void); |
| 305 | virtual bool GoTo(long); |
| 306 | |
| 307 | // Others |
| 308 | bool GetDataSources(void); |
| 309 | |
| 310 | // Associate a column name/position with a data location |
| 311 | // bool BindColumn(int colPos, int dataType, void *dataPtr); |
| 312 | |
| 313 | void Cancel(void); |
| 314 | bool IsFieldDirty(int); |
| 315 | bool IsFieldDirty(const char*); |
| 316 | bool IsFieldNull(int); |
| 317 | bool IsFieldNull(const char*); |
| 318 | bool IsColNullable(int); |
| 319 | bool IsColNullable(const char*); |
| 320 | virtual bool Requery(void); |
| 321 | virtual void SetFieldDirty(int, bool dirty = TRUE); |
| 322 | virtual void SetFieldDirty(const char*, bool dirty = TRUE); |
| 323 | void SetFieldNull(void *p, bool isNull = TRUE); |
| 324 | |
| 325 | // Overridables |
| 326 | virtual char *GetDefaultConnect(void); |
| 327 | virtual char *GetDefaultSQL(void); |
| 328 | |
| 329 | // Internal |
| 330 | |
| 331 | // Build SQL query from column specification |
| 332 | bool ConstructDefaultSQL(void); |
| 333 | void SetDefaultSQL(char *s); |
| 334 | bool ReleaseHandle(void); // Added JACS |
| 335 | }; |
| 336 | |
| 337 | #endif |
| 338 | |
| 339 | #endif // wxUSE_ODBC |