]> git.saurik.com Git - wxWidgets.git/blob - include/wx/db.h
Compilation fix for old GCC versions.
[wxWidgets.git] / include / wx / db.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/db.h
3 // Purpose: Header file wxDb class. The wxDb class represents a connection
4 // to an ODBC data source. The wxDb class allows operations on the data
5 // source such as opening and closing the data source.
6 // Author: Doug Card
7 // Modified by: George Tasker
8 // Bart Jourquin
9 // Mark Johnson, wxWindows@mj10777.de
10 // Mods: Dec, 1998:
11 // -Added support for SQL statement logging and database cataloging
12 // April, 1999
13 // -Added QUERY_ONLY mode support to reduce default number of cursors
14 // -Added additional SQL logging code
15 // -Added DEBUG-ONLY tracking of Ctable objects to detect orphaned DB connections
16 // -Set ODBC option to only read committed writes to the DB so all
17 // databases operate the same in that respect
18 //
19 // Created: 9.96
20 // RCS-ID: $Id$
21 // Copyright: (c) 1996 Remstar International, Inc.
22 // Licence: wxWindows licence
23 ///////////////////////////////////////////////////////////////////////////////
24
25 #ifndef _WX_DB_H_
26 #define _WX_DB_H_
27
28
29 // BJO 20000503: introduce new GetColumns members which are more database independent and
30 // return columns in the order they were created
31 #define OLD_GETCOLUMNS 1
32 #define EXPERIMENTAL_WXDB_FUNCTIONS 1
33
34 #include "wx/defs.h"
35 #include "wx/string.h"
36
37 #if defined(__VISUALC__)
38 // we need to include standard Windows headers but we can't include
39 // <windows.h> directly when using MFC because it includes it itself in a
40 // different manner
41 #if wxUSE_MFC
42 #include <afxwin.h>
43 #else // !wxUSE_MFC
44 #ifndef STRICT
45 #define STRICT 1
46 #endif
47
48 #include <windows.h>
49 #include "wx/msw/winundef.h"
50 #endif // wxUSE_MFC/!wxUSE_MFC
51
52 // If you use the wxDbCreateDataSource() function with MSW/VC6,
53 // you cannot use the iODBC headers, you must use the VC headers,
54 // plus the odbcinst.h header - gt Nov 2 2000
55 //
56 // Must add "odbccp32.lib" in \wx2\wxWidgets\src\makevc.env to the WINLIBS= line
57 //
58 #include "sql.h"
59 #include "sqlext.h"
60 //#if wxUSE_UNICODE
61 // #include <sqlucode.h>
62 //#endif
63 #include "odbcinst.h"
64 #else
65 #if defined(__WINDOWS__) && ( defined(HAVE_W32API_H) || defined(__BORLANDC__) )
66 #include <windows.h>
67 #include "wx/msw/winundef.h"
68 #endif
69 extern "C" {
70 #if defined(wxUSE_BUILTIN_IODBC) && wxUSE_BUILTIN_IODBC
71 // Use the ones from the library
72 #include "wx/isql.h"
73 #include "wx/isqlext.h"
74 // Not available in v2.x of iODBC
75 #ifndef __WXMSW__
76 #if wxUSE_UNICODE
77 typedef wxChar SQLTCHAR;
78 #else
79 typedef UCHAR SQLTCHAR;
80 #endif
81 #endif
82 #else
83 #if defined( __WXMOTIF__ ) && defined( __VMS )
84 // solves a type definition mismatch between IODBC and MOTIF on OpenVMS
85 #define BOOL int
86 #endif
87 #include <sql.h>
88 #include <sqlext.h>
89 //#if wxUSE_UNICODE
90 // #include <sqlucode.h>
91 //#endif
92 #if defined( __WXMOTIF__ ) && defined( __VMS )
93 #undef BOOL
94 #endif
95 #endif
96 }
97 #endif
98
99 #if wxUSE_UNICODE
100 #define SQL_C_WXCHAR SQL_C_WCHAR
101 #else
102 #define SQL_C_WXCHAR SQL_C_CHAR
103 #endif
104
105 #ifdef __DIGITALMARS__
106 #if wxUSE_UNICODE
107 typedef wxChar SQLTCHAR;
108 #else
109 typedef UCHAR SQLTCHAR;
110 #endif
111 #endif
112
113 typedef float SFLOAT;
114 typedef double SDOUBLE;
115 typedef unsigned int UINT;
116 #define ULONG UDWORD
117
118 #ifndef wxODBC_FWD_ONLY_CURSORS
119 #define wxODBC_FWD_ONLY_CURSORS 1
120 #endif
121
122 enum enumDummy {enumDum1};
123
124 #ifndef SQL_C_BOOLEAN
125 #define SQL_C_BOOLEAN(datatype) (sizeof(datatype) == 1 ? SQL_C_UTINYINT : (sizeof(datatype) == 2 ? SQL_C_USHORT : SQL_C_ULONG))
126 #endif
127
128 #ifndef SQL_C_ENUM
129 #define SQL_C_ENUM (sizeof(enumDummy) == 2 ? SQL_C_USHORT : SQL_C_ULONG)
130 #endif
131
132 // NOTE: If SQL_C_BLOB is defined, and it is not SQL_C_BINARY, iODBC 2.x
133 // may not function correctly. Likely best to use SQL_C_BINARY direct
134 #ifndef SQL_C_BLOB
135 #ifdef SQL_C_BINARY
136 #define SQL_C_BLOB SQL_C_BINARY
137 #endif
138 #endif
139
140 #ifndef _WIN64
141 #ifndef SQLLEN
142 #define SQLLEN SQLINTEGER
143 #endif
144 #ifndef SQLULEN
145 #define SQLULEN SQLUINTEGER
146 #endif
147 #endif
148
149 const int wxDB_PATH_MAX = 254;
150
151 extern WXDLLIMPEXP_DATA_ODBC(wxChar const *) SQL_LOG_FILENAME;
152 extern WXDLLIMPEXP_DATA_ODBC(wxChar const *) SQL_CATALOG_FILENAME;
153
154 // Database Globals
155 const int DB_TYPE_NAME_LEN = 40;
156 const int DB_MAX_STATEMENT_LEN = 4096;
157 const int DB_MAX_WHERE_CLAUSE_LEN = 2048;
158 const int DB_MAX_ERROR_MSG_LEN = 512;
159 const int DB_MAX_ERROR_HISTORY = 5;
160 const int DB_MAX_TABLE_NAME_LEN = 128;
161 const int DB_MAX_COLUMN_NAME_LEN = 128;
162
163 const int DB_DATA_TYPE_VARCHAR = 1;
164 const int DB_DATA_TYPE_INTEGER = 2;
165 const int DB_DATA_TYPE_FLOAT = 3;
166 const int DB_DATA_TYPE_DATE = 4;
167 const int DB_DATA_TYPE_BLOB = 5;
168 const int DB_DATA_TYPE_MEMO = 6;
169
170 const int DB_SELECT_KEYFIELDS = 1;
171 const int DB_SELECT_WHERE = 2;
172 const int DB_SELECT_MATCHING = 3;
173 const int DB_SELECT_STATEMENT = 4;
174
175 const int DB_UPD_KEYFIELDS = 1;
176 const int DB_UPD_WHERE = 2;
177
178 const int DB_DEL_KEYFIELDS = 1;
179 const int DB_DEL_WHERE = 2;
180 const int DB_DEL_MATCHING = 3;
181
182 const int DB_WHERE_KEYFIELDS = 1;
183 const int DB_WHERE_MATCHING = 2;
184
185 const int DB_GRANT_SELECT = 1;
186 const int DB_GRANT_INSERT = 2;
187 const int DB_GRANT_UPDATE = 4;
188 const int DB_GRANT_DELETE = 8;
189 const int DB_GRANT_ALL = DB_GRANT_SELECT | DB_GRANT_INSERT | DB_GRANT_UPDATE | DB_GRANT_DELETE;
190
191 // ODBC Error codes (derived from ODBC SqlState codes)
192 enum wxODBC_ERRORS
193 {
194 DB_FAILURE = 0,
195 DB_SUCCESS = 1,
196 DB_ERR_NOT_IN_USE,
197 DB_ERR_GENERAL_WARNING, // SqlState = '01000'
198 DB_ERR_DISCONNECT_ERROR, // SqlState = '01002'
199 DB_ERR_DATA_TRUNCATED, // SqlState = '01004'
200 DB_ERR_PRIV_NOT_REVOKED, // SqlState = '01006'
201 DB_ERR_INVALID_CONN_STR_ATTR, // SqlState = '01S00'
202 DB_ERR_ERROR_IN_ROW, // SqlState = '01S01'
203 DB_ERR_OPTION_VALUE_CHANGED, // SqlState = '01S02'
204 DB_ERR_NO_ROWS_UPD_OR_DEL, // SqlState = '01S03'
205 DB_ERR_MULTI_ROWS_UPD_OR_DEL, // SqlState = '01S04'
206 DB_ERR_WRONG_NO_OF_PARAMS, // SqlState = '07001'
207 DB_ERR_DATA_TYPE_ATTR_VIOL, // SqlState = '07006'
208 DB_ERR_UNABLE_TO_CONNECT, // SqlState = '08001'
209 DB_ERR_CONNECTION_IN_USE, // SqlState = '08002'
210 DB_ERR_CONNECTION_NOT_OPEN, // SqlState = '08003'
211 DB_ERR_REJECTED_CONNECTION, // SqlState = '08004'
212 DB_ERR_CONN_FAIL_IN_TRANS, // SqlState = '08007'
213 DB_ERR_COMM_LINK_FAILURE, // SqlState = '08S01'
214 DB_ERR_INSERT_VALUE_LIST_MISMATCH, // SqlState = '21S01'
215 DB_ERR_DERIVED_TABLE_MISMATCH, // SqlState = '21S02'
216 DB_ERR_STRING_RIGHT_TRUNC, // SqlState = '22001'
217 DB_ERR_NUMERIC_VALUE_OUT_OF_RNG, // SqlState = '22003'
218 DB_ERR_ERROR_IN_ASSIGNMENT, // SqlState = '22005'
219 DB_ERR_DATETIME_FLD_OVERFLOW, // SqlState = '22008'
220 DB_ERR_DIVIDE_BY_ZERO, // SqlState = '22012'
221 DB_ERR_STR_DATA_LENGTH_MISMATCH, // SqlState = '22026'
222 DB_ERR_INTEGRITY_CONSTRAINT_VIOL, // SqlState = '23000'
223 DB_ERR_INVALID_CURSOR_STATE, // SqlState = '24000'
224 DB_ERR_INVALID_TRANS_STATE, // SqlState = '25000'
225 DB_ERR_INVALID_AUTH_SPEC, // SqlState = '28000'
226 DB_ERR_INVALID_CURSOR_NAME, // SqlState = '34000'
227 DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL, // SqlState = '37000'
228 DB_ERR_DUPLICATE_CURSOR_NAME, // SqlState = '3C000'
229 DB_ERR_SERIALIZATION_FAILURE, // SqlState = '40001'
230 DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2, // SqlState = '42000'
231 DB_ERR_OPERATION_ABORTED, // SqlState = '70100'
232 DB_ERR_UNSUPPORTED_FUNCTION, // SqlState = 'IM001'
233 DB_ERR_NO_DATA_SOURCE, // SqlState = 'IM002'
234 DB_ERR_DRIVER_LOAD_ERROR, // SqlState = 'IM003'
235 DB_ERR_SQLALLOCENV_FAILED, // SqlState = 'IM004'
236 DB_ERR_SQLALLOCCONNECT_FAILED, // SqlState = 'IM005'
237 DB_ERR_SQLSETCONNECTOPTION_FAILED, // SqlState = 'IM006'
238 DB_ERR_NO_DATA_SOURCE_DLG_PROHIB, // SqlState = 'IM007'
239 DB_ERR_DIALOG_FAILED, // SqlState = 'IM008'
240 DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL, // SqlState = 'IM009'
241 DB_ERR_DATA_SOURCE_NAME_TOO_LONG, // SqlState = 'IM010'
242 DB_ERR_DRIVER_NAME_TOO_LONG, // SqlState = 'IM011'
243 DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR, // SqlState = 'IM012'
244 DB_ERR_TRACE_FILE_ERROR, // SqlState = 'IM013'
245 DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS, // SqlState = 'S0001'
246 DB_ERR_TABLE_NOT_FOUND, // SqlState = 'S0002'
247 DB_ERR_INDEX_ALREADY_EXISTS, // SqlState = 'S0011'
248 DB_ERR_INDEX_NOT_FOUND, // SqlState = 'S0012'
249 DB_ERR_COLUMN_ALREADY_EXISTS, // SqlState = 'S0021'
250 DB_ERR_COLUMN_NOT_FOUND, // SqlState = 'S0022'
251 DB_ERR_NO_DEFAULT_FOR_COLUMN, // SqlState = 'S0023'
252 DB_ERR_GENERAL_ERROR, // SqlState = 'S1000'
253 DB_ERR_MEMORY_ALLOCATION_FAILURE, // SqlState = 'S1001'
254 DB_ERR_INVALID_COLUMN_NUMBER, // SqlState = 'S1002'
255 DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE, // SqlState = 'S1003'
256 DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE, // SqlState = 'S1004'
257 DB_ERR_OPERATION_CANCELLED, // SqlState = 'S1008'
258 DB_ERR_INVALID_ARGUMENT_VALUE, // SqlState = 'S1009'
259 DB_ERR_FUNCTION_SEQUENCE_ERROR, // SqlState = 'S1010'
260 DB_ERR_OPERATION_INVALID_AT_THIS_TIME, // SqlState = 'S1011'
261 DB_ERR_INVALID_TRANS_OPERATION_CODE, // SqlState = 'S1012'
262 DB_ERR_NO_CURSOR_NAME_AVAIL, // SqlState = 'S1015'
263 DB_ERR_INVALID_STR_OR_BUF_LEN, // SqlState = 'S1090'
264 DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE, // SqlState = 'S1091'
265 DB_ERR_OPTION_TYPE_OUT_OF_RANGE, // SqlState = 'S1092'
266 DB_ERR_INVALID_PARAM_NO, // SqlState = 'S1093'
267 DB_ERR_INVALID_SCALE_VALUE, // SqlState = 'S1094'
268 DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE, // SqlState = 'S1095'
269 DB_ERR_INF_TYPE_OUT_OF_RANGE, // SqlState = 'S1096'
270 DB_ERR_COLUMN_TYPE_OUT_OF_RANGE, // SqlState = 'S1097'
271 DB_ERR_SCOPE_TYPE_OUT_OF_RANGE, // SqlState = 'S1098'
272 DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE, // SqlState = 'S1099'
273 DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE, // SqlState = 'S1100'
274 DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE, // SqlState = 'S1101'
275 DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE, // SqlState = 'S1103'
276 DB_ERR_INVALID_PRECISION_VALUE, // SqlState = 'S1104'
277 DB_ERR_INVALID_PARAM_TYPE, // SqlState = 'S1105'
278 DB_ERR_FETCH_TYPE_OUT_OF_RANGE, // SqlState = 'S1106'
279 DB_ERR_ROW_VALUE_OUT_OF_RANGE, // SqlState = 'S1107'
280 DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE, // SqlState = 'S1108'
281 DB_ERR_INVALID_CURSOR_POSITION, // SqlState = 'S1109'
282 DB_ERR_INVALID_DRIVER_COMPLETION, // SqlState = 'S1110'
283 DB_ERR_INVALID_BOOKMARK_VALUE, // SqlState = 'S1111'
284 DB_ERR_DRIVER_NOT_CAPABLE, // SqlState = 'S1C00'
285 DB_ERR_TIMEOUT_EXPIRED // SqlState = 'S1T00'
286 };
287
288 #ifndef MAXNAME
289 #define MAXNAME 31
290 #endif
291
292 #ifndef SQL_MAX_AUTHSTR_LEN
293 // There does not seem to be a standard for this, so I am
294 // defaulting to the value that MS uses
295 #define SQL_MAX_AUTHSTR_LEN MAXNAME
296 #endif
297
298 #ifndef SQL_MAX_CONNECTSTR_LEN
299 // There does not seem to be a standard for this, so I am
300 // defaulting to the value that MS recommends
301 #define SQL_MAX_CONNECTSTR_LEN 1024
302 #endif
303
304
305 class WXDLLIMPEXP_ODBC wxDbConnectInf
306 {
307 private:
308 bool freeHenvOnDestroy;
309 bool useConnectionStr;
310
311 public:
312 HENV Henv;
313 wxChar Dsn[SQL_MAX_DSN_LENGTH+1]; // Data Source Name
314 wxChar Uid[SQL_MAX_USER_NAME_LEN+1]; // User ID
315 wxChar AuthStr[SQL_MAX_AUTHSTR_LEN+1]; // Authorization string (password)
316 wxChar ConnectionStr[SQL_MAX_CONNECTSTR_LEN+1]; // Connection string (password)
317
318 wxString Description; // Not sure what the max length is
319 wxString FileType; // Not sure what the max length is
320
321 // Optionals needed for some databases like dBase
322 wxString DefaultDir; // Directory that db file resides in
323
324 public:
325
326 wxDbConnectInf();
327 wxDbConnectInf(HENV henv, const wxString &dsn, const wxString &userID=wxEmptyString,
328 const wxString &password=wxEmptyString, const wxString &defaultDir=wxEmptyString,
329 const wxString &description=wxEmptyString, const wxString &fileType=wxEmptyString);
330
331 ~wxDbConnectInf();
332
333 bool Initialize();
334
335 bool AllocHenv();
336 void FreeHenv();
337
338 // Accessors
339 const HENV &GetHenv() { return Henv; };
340
341 const wxChar *GetDsn() { return Dsn; };
342
343 const wxChar *GetUid() { return Uid; };
344 const wxChar *GetUserID() { return Uid; };
345
346 const wxChar *GetAuthStr() { return AuthStr; };
347 const wxChar *GetPassword() { return AuthStr; };
348
349 const wxChar *GetConnectionStr() { return ConnectionStr; };
350 bool UseConnectionStr() { return useConnectionStr; };
351
352 const wxChar *GetDescription() { return Description; };
353 const wxChar *GetFileType() { return FileType; };
354 const wxChar *GetDefaultDir() { return DefaultDir; };
355
356 void SetHenv(const HENV henv) { Henv = henv; };
357
358 void SetDsn(const wxString &dsn);
359
360 void SetUserID(const wxString &userID);
361 void SetUid(const wxString &uid) { SetUserID(uid); };
362
363 void SetPassword(const wxString &password);
364 void SetAuthStr(const wxString &authstr) { SetPassword(authstr); };
365
366 void SetConnectionStr(const wxString &connectStr);
367
368 void SetDescription(const wxString &desc) { Description = desc; };
369 void SetFileType(const wxString &fileType) { FileType = fileType; };
370 void SetDefaultDir(const wxString &defDir) { DefaultDir = defDir; };
371 }; // class wxDbConnectInf
372
373
374 struct WXDLLIMPEXP_ODBC wxDbSqlTypeInfo
375 {
376 wxString TypeName;
377 SWORD FsqlType;
378 long Precision;
379 short CaseSensitive;
380 short MaximumScale;
381 };
382
383
384 class WXDLLIMPEXP_ODBC wxDbColFor
385 {
386 public:
387 wxString s_Field; // Formatted String for Output
388 wxString s_Format[7]; // Formatted Objects - TIMESTAMP has the biggest (7)
389 wxString s_Amount[7]; // Formatted Objects - amount of things that can be formatted
390 int i_Amount[7]; // Formatted Objects - TT MM YYYY HH MM SS m
391 int i_Nation; // 0 = timestamp , 1=EU, 2=UK, 3=International, 4=US
392 int i_dbDataType; // conversion of the 'sqlDataType' to the generic data type used by these classes
393 SWORD i_sqlDataType;
394
395 wxDbColFor();
396 ~wxDbColFor(){}
397
398 void Initialize();
399 int Format(int Nation, int dbDataType, SWORD sqlDataType, short columnLength, short decimalDigits);
400 };
401
402
403 class WXDLLIMPEXP_ODBC wxDbColInf
404 {
405 public:
406 wxChar catalog[128+1];
407 wxChar schema[128+1];
408 wxChar tableName[DB_MAX_TABLE_NAME_LEN+1];
409 wxChar colName[DB_MAX_COLUMN_NAME_LEN+1];
410 SWORD sqlDataType;
411 wxChar typeName[128+1];
412 SWORD columnLength;
413 SWORD bufferSize;
414 short decimalDigits;
415 short numPrecRadix;
416 short nullable;
417 wxChar remarks[254+1];
418 int dbDataType; // conversion of the 'sqlDataType' to the generic data type used by these classes
419 // mj10777.19991224 : new
420 int PkCol; // Primary key column 0=No; 1= First Key, 2 = Second Key etc.
421 wxChar PkTableName[DB_MAX_TABLE_NAME_LEN+1]; // Tables that use this PKey as a FKey
422 int FkCol; // Foreign key column 0=No; 1= First Key, 2 = Second Key etc.
423 wxChar FkTableName[DB_MAX_TABLE_NAME_LEN+1]; // Foreign key table name
424 wxDbColFor *pColFor; // How should this columns be formatted
425
426 wxDbColInf();
427 ~wxDbColInf();
428
429 bool Initialize();
430 };
431
432
433 class WXDLLIMPEXP_ODBC wxDbTableInf // Description of a Table
434 {
435 public:
436 wxChar tableName[DB_MAX_TABLE_NAME_LEN+1];
437 wxChar tableType[254+1]; // "TABLE" or "SYSTEM TABLE" etc.
438 wxChar tableRemarks[254+1];
439 UWORD numCols; // How many Columns does this Table have: GetColumnCount(..);
440 wxDbColInf *pColInf; // pColInf = NULL ; User can later call GetColumns(..);
441
442 wxDbTableInf();
443 ~wxDbTableInf();
444
445 bool Initialize();
446 };
447
448
449 class WXDLLIMPEXP_ODBC wxDbInf // Description of a Database
450 {
451 public:
452 wxChar catalog[128+1];
453 wxChar schema[128+1];
454 int numTables; // How many tables does this database have
455 wxDbTableInf *pTableInf; // pTableInf = new wxDbTableInf[numTables];
456
457 wxDbInf();
458 ~wxDbInf();
459
460 bool Initialize();
461 };
462
463
464 enum wxDbSqlLogState
465 {
466 sqlLogOFF,
467 sqlLogON
468 };
469
470 // These are the databases currently tested and working with these classes
471 // See the comments in wxDb::Dbms() for exceptions/issues with
472 // each of these database engines
473 enum wxDBMS
474 {
475 dbmsUNIDENTIFIED,
476 dbmsORACLE,
477 dbmsSYBASE_ASA, // Adaptive Server Anywhere
478 dbmsSYBASE_ASE, // Adaptive Server Enterprise
479 dbmsMS_SQL_SERVER,
480 dbmsMY_SQL,
481 dbmsPOSTGRES,
482 dbmsACCESS,
483 dbmsDBASE,
484 dbmsINFORMIX,
485 dbmsVIRTUOSO,
486 dbmsDB2,
487 dbmsINTERBASE,
488 dbmsPERVASIVE_SQL,
489 dbmsXBASE_SEQUITER,
490 dbmsFIREBIRD,
491 dbmsMAXDB,
492 dbmsFuture1,
493 dbmsFuture2,
494 dbmsFuture3,
495 dbmsFuture4,
496 dbmsFuture5,
497 dbmsFuture6,
498 dbmsFuture7,
499 dbmsFuture8,
500 dbmsFuture9,
501 dbmsFuture10
502 };
503
504
505 // The wxDb::errorList is copied to this variable when the wxDb object
506 // is closed. This way, the error list is still available after the
507 // database object is closed. This is necessary if the database
508 // connection fails so the calling application can show the operator
509 // why the connection failed. Note: as each wxDb object is closed, it
510 // will overwrite the errors of the previously destroyed wxDb object in
511 // this variable.
512
513 extern WXDLLIMPEXP_DATA_ODBC(wxChar)
514 DBerrorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN+1];
515
516
517 class WXDLLIMPEXP_ODBC wxDb
518 {
519 private:
520 bool dbIsOpen;
521 bool dbIsCached; // Was connection created by caching functions
522 bool dbOpenedWithConnectionString; // Was the database connection opened with a connection string
523 wxString dsn; // Data source name
524 wxString uid; // User ID
525 wxString authStr; // Authorization string (password)
526 wxString inConnectionStr; // Connection string used to connect to the database
527 wxString outConnectionStr;// Connection string returned by the database when a connection is successfully opened
528 FILE *fpSqlLog; // Sql Log file pointer
529 wxDbSqlLogState sqlLogState; // On or Off
530 bool fwdOnlyCursors;
531 wxDBMS dbmsType; // Type of datasource - i.e. Oracle, dBase, SQLServer, etc
532
533 // Private member functions
534 bool getDbInfo(bool failOnDataTypeUnsupported=true);
535 bool getDataTypeInfo(SWORD fSqlType, wxDbSqlTypeInfo &structSQLTypeInfo);
536 bool setConnectionOptions(void);
537 void logError(const wxString &errMsg, const wxString &SQLState);
538 const wxChar *convertUserID(const wxChar *userID, wxString &UserID);
539 bool determineDataTypes(bool failOnDataTypeUnsupported);
540 void initialize();
541 bool open(bool failOnDataTypeUnsupported=true);
542
543 #if !wxODBC_BACKWARD_COMPATABILITY
544 // ODBC handles
545 HENV henv; // ODBC Environment handle
546 HDBC hdbc; // ODBC DB Connection handle
547 HSTMT hstmt; // ODBC Statement handle
548
549 //Error reporting mode
550 bool silent;
551
552 // Number of Ctable objects connected to this db object. FOR INTERNAL USE ONLY!!!
553 unsigned int nTables;
554
555 // Information about logical data types VARCHAR, INTEGER, FLOAT and DATE.
556 //
557 // This information is obtained from the ODBC driver by use of the
558 // SQLGetTypeInfo() function. The key piece of information is the
559 // type name the data source uses for each logical data type.
560 // e.g. VARCHAR; Oracle calls it VARCHAR2.
561 wxDbSqlTypeInfo typeInfVarchar;
562 wxDbSqlTypeInfo typeInfInteger;
563 wxDbSqlTypeInfo typeInfFloat;
564 wxDbSqlTypeInfo typeInfDate;
565 wxDbSqlTypeInfo typeInfBlob;
566 wxDbSqlTypeInfo typeInfMemo;
567 #endif
568
569 public:
570
571 void setCached(bool cached) { dbIsCached = cached; }; // This function must only be called by wxDbGetConnection() and wxDbCloseConnections!!!
572 bool IsCached() { return dbIsCached; };
573
574 bool GetDataTypeInfo(SWORD fSqlType, wxDbSqlTypeInfo &structSQLTypeInfo)
575 { return getDataTypeInfo(fSqlType, structSQLTypeInfo); }
576
577 #if wxODBC_BACKWARD_COMPATABILITY
578 // ODBC handles
579 HENV henv; // ODBC Environment handle
580 HDBC hdbc; // ODBC DB Connection handle
581 HSTMT hstmt; // ODBC Statement handle
582
583 //Error reporting mode
584 bool silent;
585
586 // Number of Ctable objects connected to this db object. FOR INTERNAL USE ONLY!!!
587 unsigned int nTables;
588 #endif
589
590 // The following structure contains database information gathered from the
591 // datasource when the datasource is first opened.
592 struct
593 {
594 wxChar dbmsName[40]; // Name of the dbms product
595 wxChar dbmsVer[64]; // Version # of the dbms product
596 wxChar driverName[40]; // Driver name
597 wxChar odbcVer[60]; // ODBC version of the driver
598 wxChar drvMgrOdbcVer[60]; // ODBC version of the driver manager
599 wxChar driverVer[60]; // Driver version
600 wxChar serverName[80]; // Server Name, typically a connect string
601 wxChar databaseName[128]; // Database filename
602 wxChar outerJoins[2]; // Indicates whether the data source supports outer joins
603 wxChar procedureSupport[2]; // Indicates whether the data source supports stored procedures
604 wxChar accessibleTables[2]; // Indicates whether the data source only reports accessible tables in SQLTables.
605 UWORD maxConnections; // Maximum # of connections the data source supports
606 UWORD maxStmts; // Maximum # of HSTMTs per HDBC
607 UWORD apiConfLvl; // ODBC API conformance level
608 UWORD cliConfLvl; // Indicates whether the data source is SAG compliant
609 UWORD sqlConfLvl; // SQL conformance level
610 UWORD cursorCommitBehavior; // Indicates how cursors are affected by a db commit
611 UWORD cursorRollbackBehavior; // Indicates how cursors are affected by a db rollback
612 UWORD supportNotNullClause; // Indicates if data source supports NOT NULL clause
613 wxChar supportIEF[2]; // Integrity Enhancement Facility (Referential Integrity)
614 UDWORD txnIsolation; // Default transaction isolation level supported by the driver
615 UDWORD txnIsolationOptions; // Transaction isolation level options available
616 UDWORD fetchDirections; // Fetch directions supported
617 UDWORD lockTypes; // Lock types supported in SQLSetPos
618 UDWORD posOperations; // Position operations supported in SQLSetPos
619 UDWORD posStmts; // Position statements supported
620 UDWORD scrollConcurrency; // Concurrency control options supported for scrollable cursors
621 UDWORD scrollOptions; // Scroll Options supported for scrollable cursors
622 UDWORD staticSensitivity; // Indicates if additions, deletions and updates can be detected
623 UWORD txnCapable; // Indicates if the data source supports transactions
624 UDWORD loginTimeout; // Number seconds to wait for a login request
625 } dbInf;
626
627 // ODBC Error Inf.
628 SWORD cbErrorMsg;
629 int DB_STATUS;
630 #ifdef __VMS
631 // The DECC compiler chokes when in db.cpp the array is accessed outside
632 // its bounds. Maybe this change should also applied for other platforms.
633 wxChar errorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN+1];
634 #else
635 wxChar errorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN];
636 #endif
637 wxChar errorMsg[SQL_MAX_MESSAGE_LENGTH];
638 SQLINTEGER nativeError;
639 wxChar sqlState[20];
640
641 #if wxODBC_BACKWARD_COMPATABILITY
642 // Information about logical data types VARCHAR, INTEGER, FLOAT and DATE.
643 //
644 // This information is obtained from the ODBC driver by use of the
645 // SQLGetTypeInfo() function. The key piece of information is the
646 // type name the data source uses for each logical data type.
647 // e.g. VARCHAR; Oracle calls it VARCHAR2.
648 wxDbSqlTypeInfo typeInfVarchar;
649 wxDbSqlTypeInfo typeInfInteger;
650 wxDbSqlTypeInfo typeInfFloat;
651 wxDbSqlTypeInfo typeInfDate;
652 wxDbSqlTypeInfo typeInfBlob;
653 #endif
654
655 // Public member functions
656 wxDb(const HENV &aHenv, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS);
657 ~wxDb();
658
659 // Data Source Name, User ID, Password and whether open should fail on data type not supported
660 bool Open(const wxString& inConnectStr, bool failOnDataTypeUnsupported=true);
661 ///This version of Open will open the odbc source selection dialog. Cast a wxWindow::GetHandle() to SQLHWND to use.
662 bool Open(const wxString& inConnectStr, SQLHWND parentWnd, bool failOnDataTypeUnsupported=true);
663 bool Open(const wxString &Dsn, const wxString &Uid, const wxString &AuthStr, bool failOnDataTypeUnsupported=true);
664 bool Open(wxDbConnectInf *dbConnectInf, bool failOnDataTypeUnsupported=true);
665 bool Open(wxDb *copyDb); // pointer to a wxDb whose connection info should be copied rather than re-queried
666 void Close(void);
667 bool CommitTrans(void);
668 bool RollbackTrans(void);
669 bool DispAllErrors(HENV aHenv, HDBC aHdbc = SQL_NULL_HDBC, HSTMT aHstmt = SQL_NULL_HSTMT);
670 bool GetNextError(HENV aHenv, HDBC aHdbc = SQL_NULL_HDBC, HSTMT aHstmt = SQL_NULL_HSTMT);
671 void DispNextError(void);
672 bool CreateView(const wxString &viewName, const wxString &colList, const wxString &pSqlStmt, bool attemptDrop=true);
673 bool DropView(const wxString &viewName);
674 bool ExecSql(const wxString &pSqlStmt);
675 bool ExecSql(const wxString &pSqlStmt, wxDbColInf** columns, short& numcols);
676 bool GetNext(void);
677 bool GetData(UWORD colNo, SWORD cType, PTR pData, SDWORD maxLen, SQLLEN FAR *cbReturned);
678 bool Grant(int privileges, const wxString &tableName, const wxString &userList = wxT("PUBLIC"));
679 int TranslateSqlState(const wxString &SQLState);
680 wxDbInf *GetCatalog(const wxChar *userID=NULL);
681 bool Catalog(const wxChar *userID=NULL, const wxString &fileName=SQL_CATALOG_FILENAME);
682 int GetKeyFields(const wxString &tableName, wxDbColInf* colInf, UWORD noCols);
683
684 wxDbColInf *GetColumns(wxChar *tableName[], const wxChar *userID=NULL);
685 wxDbColInf *GetColumns(const wxString &tableName, UWORD *numCols, const wxChar *userID=NULL);
686
687 int GetColumnCount(const wxString &tableName, const wxChar *userID=NULL);
688 const wxChar *GetDatabaseName(void) {return dbInf.dbmsName;}
689 const wxString &GetDataSource(void) {return dsn;}
690 const wxString &GetDatasourceName(void){return dsn;}
691 const wxString &GetUsername(void) {return uid;}
692 const wxString &GetPassword(void) {return authStr;}
693 const wxString &GetConnectionInStr(void) {return inConnectionStr;}
694 const wxString &GetConnectionOutStr(void) {return outConnectionStr;}
695 bool IsOpen(void) {return dbIsOpen;}
696 bool OpenedWithConnectionString(void) {return dbOpenedWithConnectionString;}
697 HENV GetHENV(void) {return henv;}
698 HDBC GetHDBC(void) {return hdbc;}
699 HSTMT GetHSTMT(void) {return hstmt;}
700 int GetTableCount() {return nTables;} // number of tables using this connection
701 wxDbSqlTypeInfo GetTypeInfVarchar() {return typeInfVarchar;}
702 wxDbSqlTypeInfo GetTypeInfInteger() {return typeInfInteger;}
703 wxDbSqlTypeInfo GetTypeInfFloat() {return typeInfFloat;}
704 wxDbSqlTypeInfo GetTypeInfDate() {return typeInfDate;}
705 wxDbSqlTypeInfo GetTypeInfBlob() {return typeInfBlob;}
706 wxDbSqlTypeInfo GetTypeInfMemo() {return typeInfMemo;}
707
708 // tableName can refer to a table, view, alias or synonym
709 bool TableExists(const wxString &tableName, const wxChar *userID=NULL,
710 const wxString &tablePath=wxEmptyString);
711 bool TablePrivileges(const wxString &tableName, const wxString &priv,
712 const wxChar *userID=NULL, const wxChar *schema=NULL,
713 const wxString &path=wxEmptyString);
714
715 // These two functions return the table name or column name in a form ready
716 // for use in SQL statements. For example, if the datasource allows spaces
717 // in the table name or column name, the returned string will have the
718 // correct enclosing marks around the name to allow it to be properly
719 // included in a SQL statement
720 const wxString SQLTableName(const wxChar *tableName);
721 const wxString SQLColumnName(const wxChar *colName);
722
723 void LogError(const wxString &errMsg, const wxString &SQLState = wxEmptyString)
724 { logError(errMsg, SQLState); }
725 void SetDebugErrorMessages(bool state) { silent = !state; }
726 bool SetSqlLogging(wxDbSqlLogState state, const wxString &filename = SQL_LOG_FILENAME,
727 bool append = false);
728 bool WriteSqlLog(const wxString &logMsg);
729
730 wxDBMS Dbms(void);
731 bool ModifyColumn(const wxString &tableName, const wxString &columnName,
732 int dataType, ULONG columnLength=0,
733 const wxString &optionalParam=wxEmptyString);
734
735 bool FwdOnlyCursors(void) {return fwdOnlyCursors;}
736
737 // return the string with all special SQL characters escaped
738 wxString EscapeSqlChars(const wxString& value);
739
740 // These two functions are provided strictly for use by wxDbTable.
741 // DO NOT USE THESE FUNCTIONS, OR MEMORY LEAKS MAY OCCUR
742 void incrementTableCount() { nTables++; return; }
743 void decrementTableCount() { nTables--; return; }
744
745 }; // wxDb
746
747
748 // This structure forms a node in a linked list. The linked list of "DbList" objects
749 // keeps track of allocated database connections. This allows the application to
750 // open more than one database connection through ODBC for multiple transaction support
751 // or for multiple database support.
752 struct wxDbList
753 {
754 wxDbList *PtrPrev; // Pointer to previous item in the list
755 wxString Dsn; // Data Source Name
756 wxString Uid; // User ID
757 wxString AuthStr; // Authorization string (password)
758 wxString ConnectionStr; // Connection string used instead of DSN
759 wxDb *PtrDb; // Pointer to the wxDb object
760 bool Free; // Is item free or in use?
761 wxDbList *PtrNext; // Pointer to next item in the list
762 };
763
764
765 #ifdef __WXDEBUG__
766 #include "wx/object.h"
767 class wxTablesInUse : public wxObject
768 {
769 public:
770 const wxChar *tableName;
771 ULONG tableID;
772 class wxDb *pDb;
773 }; // wxTablesInUse
774 #endif
775
776
777 // The following routines allow a user to get new database connections, free them
778 // for other code segments to use, or close all of them when the application has
779 // completed.
780 wxDb WXDLLIMPEXP_ODBC *wxDbGetConnection(wxDbConnectInf *pDbConfig, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS);
781 bool WXDLLIMPEXP_ODBC wxDbFreeConnection(wxDb *pDb);
782 void WXDLLIMPEXP_ODBC wxDbCloseConnections(void);
783 int WXDLLIMPEXP_ODBC wxDbConnectionsInUse(void);
784
785
786 // Writes a message to the wxLog window (stdout usually) when an internal error
787 // situation occurs. This function only works in DEBUG builds
788 const wxChar WXDLLIMPEXP_ODBC *
789 wxDbLogExtendedErrorMsg(const wxChar *userText,
790 wxDb *pDb,
791 const wxChar *ErrFile,
792 int ErrLine);
793
794
795 // This function sets the sql log state for all open wxDb objects
796 bool WXDLLIMPEXP_ODBC
797 wxDbSqlLog(wxDbSqlLogState state, const wxString &filename = SQL_LOG_FILENAME);
798
799
800 #if 0
801 // MSW/VC6 ONLY!!! Experimental
802 int WXDLLEXPORT wxDbCreateDataSource(const wxString &driverName, const wxString &dsn, const wxString &description=wxEmptyString,
803 bool sysDSN=false, const wxString &defDir=wxEmptyString, wxWindow *parent=NULL);
804 #endif
805
806 // This routine allows you to query a driver manager
807 // for a list of available datasources. Call this routine
808 // the first time using SQL_FETCH_FIRST. Continue to call it
809 // using SQL_FETCH_NEXT until you've exhausted the list.
810 bool WXDLLIMPEXP_ODBC
811 wxDbGetDataSource(HENV henv, wxChar *Dsn, SWORD DsnMaxLength, wxChar *DsDesc,
812 SWORD DsDescMaxLength, UWORD direction = SQL_FETCH_NEXT);
813
814
815 // Change this to 0 to remove use of all deprecated functions
816 #if wxODBC_BACKWARD_COMPATABILITY
817 //#################################################################################
818 //############### DEPRECATED functions for backward compatibility #################
819 //#################################################################################
820
821 // Backward compability structures/classes. This will eventually go away
822 const int DB_PATH_MAX = wxDB_PATH_MAX;
823
824 typedef wxDb wxDB;
825 typedef wxDbTableInf wxTableInf;
826 typedef wxDbColInf wxColInf;
827 typedef wxDbColInf CcolInf;
828 typedef wxDbColFor wxColFor;
829 typedef wxDbSqlTypeInfo SqlTypeInfo;
830 typedef wxDbSqlTypeInfo wxSqlTypeInfo;
831 typedef enum wxDbSqlLogState sqlLog;
832 typedef enum wxDbSqlLogState wxSqlLogState;
833 typedef enum wxDBMS dbms;
834 typedef enum wxDBMS DBMS;
835 typedef wxODBC_ERRORS ODBC_ERRORS;
836 typedef wxDbConnectInf DbStuff;
837 typedef wxDbList DbList;
838 #ifdef __WXDEBUG__
839 typedef wxTablesInUse CstructTablesInUse;
840 #endif
841
842 // Deprecated function names that are replaced by the function names listed above
843 wxDB WXDLLIMPEXP_ODBC
844 *GetDbConnection(DbStuff *pDbStuff, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS);
845 bool WXDLLIMPEXP_ODBC FreeDbConnection(wxDB *pDb);
846 void WXDLLIMPEXP_ODBC CloseDbConnections(void);
847 int WXDLLIMPEXP_ODBC NumberDbConnectionsInUse(void);
848
849 bool SqlLog(sqlLog state, const wxChar *filename = SQL_LOG_FILENAME);
850
851 bool WXDLLIMPEXP_ODBC
852 GetDataSource(HENV henv, char *Dsn, SWORD DsnMaxLength, char *DsDesc, SWORD DsDescMaxLength,
853 UWORD direction = SQL_FETCH_NEXT);
854
855 #endif // Deprecated structures/classes/functions
856
857 #endif // _WX_DB_H_
858