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