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