]> git.saurik.com Git - wxWidgets.git/blame - include/wx/db.h
SetTextColour is gone
[wxWidgets.git] / include / wx / db.h
CommitLineData
108106cf 1///////////////////////////////////////////////////////////////////////////////
2b5f62a0 2// Name: wx/db.h
f6bcfd97
BP
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
108106cf
JS
5// source such as opening and closing the data source.
6// Author: Doug Card
30a6d2d2 7// Modified by: George Tasker
3ca6a5f0 8// Bart Jourquin
a9f4558f 9// Mark Johnson, wxWindows@mj10777.de
ad2522ad 10// Mods: Dec, 1998:
a2115c88 11// -Added support for SQL statement logging and database cataloging
95ca6a20
GT
12// April, 1999
13// -Added QUERY_ONLY mode support to reduce default number of cursors
14// -Added additional SQL logging code
a2115c88 15// -Added DEBUG-ONLY tracking of Ctable objects to detect orphaned DB connections
95ca6a20
GT
16// -Set ODBC option to only read committed writes to the DB so all
17// databases operate the same in that respect
a2115c88 18//
108106cf
JS
19// Created: 9.96
20// RCS-ID: $Id$
21// Copyright: (c) 1996 Remstar International, Inc.
7e559cd8 22// Licence: wxWindows licence
108106cf
JS
23///////////////////////////////////////////////////////////////////////////////
24
2b5f62a0
VZ
25#ifndef _WX_DB_H_
26#define _WX_DB_H_
a2115c88 27
f6bcfd97 28
43e8916f 29// BJO 20000503: introduce new GetColumns members which are more database independent and
f6bcfd97
BP
30// return columns in the order they were created
31#define OLD_GETCOLUMNS 1
e2f4e132 32#define EXPERIMENTAL_WXDB_FUNCTIONS 1
f6bcfd97 33
d4af841d
GT
34#include "wx/defs.h"
35#include "wx/string.h"
36
68379eaf 37#if defined(__VISUALC__)
2b5f62a0
VZ
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 //
77ffb593 56 // Must add "odbccp32.lib" in \wx2\wxWidgets\src\makevc.env to the WINLIBS= line
2b5f62a0 57 //
e2f4e132
GT
58 #include "sql.h"
59 #include "sqlext.h"
2889677e
GT
60 //#if wxUSE_UNICODE
61 // #include <sqlucode.h>
62 //#endif
e2f4e132 63 #include "odbcinst.h"
f7556ff0 64#else
bfe5de86 65 #if defined(__WINDOWS__) && ( defined(HAVE_W32API_H) || defined(__BORLANDC__) )
190869b2
VS
66 #include <windows.h>
67 #include "wx/msw/winundef.h"
68 #endif
2b5f62a0 69 extern "C" {
0bdb08e7
VS
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"
2889677e
GT
74 // Not available in v2.x of iODBC
75 #ifndef __WXMSW__
cf0cf17b
JS
76 #if wxUSE_UNICODE
77 typedef wxChar SQLTCHAR;
78 #else
79 typedef UCHAR SQLTCHAR;
80 #endif
2889677e 81 #endif
0bdb08e7 82 #else
6c1465ef
VZ
83 #if defined( __WXMOTIF__ ) && defined( __VMS )
84 // solves a type definition mismatch between IODBC and MOTIF on OpenVMS
85 #define BOOL int
86 #endif
f7556ff0
JS
87 #include <sql.h>
88 #include <sqlext.h>
2889677e
GT
89 //#if wxUSE_UNICODE
90 // #include <sqlucode.h>
91 //#endif
6c1465ef
VZ
92 #if defined( __WXMOTIF__ ) && defined( __VMS )
93 #undef BOOL
94 #endif
0bdb08e7 95 #endif
2b5f62a0 96 }
aecca2c8
MB
97#endif
98
e4e45573
GT
99#if wxUSE_UNICODE
100#define SQL_C_WXCHAR SQL_C_WCHAR
101#else
102#define SQL_C_WXCHAR SQL_C_CHAR
103#endif
30a6d2d2 104
e716b9be
JS
105#ifdef __DIGITALMARS__
106#if wxUSE_UNICODE
107typedef wxChar SQLTCHAR;
108#else
109typedef UCHAR SQLTCHAR;
110#endif
111#endif
112
595b5c4e
MJ
113typedef float SFLOAT;
114typedef double SDOUBLE;
95ca6a20
GT
115typedef unsigned int UINT;
116#define ULONG UDWORD
30a6d2d2
GT
117
118#ifndef wxODBC_FWD_ONLY_CURSORS
119#define wxODBC_FWD_ONLY_CURSORS 1
120#endif
121
95ca6a20 122enum enumDummy {enumDum1};
7e616b10 123
b742792c 124#ifndef SQL_C_BOOLEAN
f3df29e3 125 #define SQL_C_BOOLEAN(datatype) (sizeof(datatype) == 1 ? SQL_C_UTINYINT : (sizeof(datatype) == 2 ? SQL_C_USHORT : SQL_C_ULONG))
b742792c 126#endif
20987580 127
b742792c 128#ifndef SQL_C_ENUM
f3df29e3 129 #define SQL_C_ENUM (sizeof(enumDummy) == 2 ? SQL_C_USHORT : SQL_C_ULONG)
b742792c 130#endif
20987580 131
f3df29e3
GT
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
b742792c 134#ifndef SQL_C_BLOB
f3df29e3
GT
135 #ifdef SQL_C_BINARY
136 #define SQL_C_BLOB SQL_C_BINARY
b742792c
GT
137 #endif
138#endif
7e616b10 139
e716b9be
JS
140#ifndef _WIN64
141#ifndef SQLLEN
142#define SQLLEN SQLINTEGER
143#endif
144#ifndef SQLULEN
145#define SQLULEN SQLUINTEGER
146#endif
147#endif
148
f6bcfd97 149const int wxDB_PATH_MAX = 254;
95ca6a20 150
16cba29d
WS
151extern WXDLLIMPEXP_DATA_ODBC(wxChar const *) SQL_LOG_FILENAME;
152extern WXDLLIMPEXP_DATA_ODBC(wxChar const *) SQL_CATALOG_FILENAME;
108106cf 153
108106cf 154// Database Globals
95ca6a20 155const int DB_TYPE_NAME_LEN = 40;
eb3b9dda
GT
156const int DB_MAX_STATEMENT_LEN = 4096;
157const int DB_MAX_WHERE_CLAUSE_LEN = 2048;
95ca6a20
GT
158const int DB_MAX_ERROR_MSG_LEN = 512;
159const int DB_MAX_ERROR_HISTORY = 5;
160const int DB_MAX_TABLE_NAME_LEN = 128;
161const int DB_MAX_COLUMN_NAME_LEN = 128;
162
163const int DB_DATA_TYPE_VARCHAR = 1;
164const int DB_DATA_TYPE_INTEGER = 2;
165const int DB_DATA_TYPE_FLOAT = 3;
166const int DB_DATA_TYPE_DATE = 4;
b742792c 167const int DB_DATA_TYPE_BLOB = 5;
7eeba511 168const int DB_DATA_TYPE_MEMO = 6;
95ca6a20
GT
169
170const int DB_SELECT_KEYFIELDS = 1;
171const int DB_SELECT_WHERE = 2;
172const int DB_SELECT_MATCHING = 3;
173const int DB_SELECT_STATEMENT = 4;
174
175const int DB_UPD_KEYFIELDS = 1;
176const int DB_UPD_WHERE = 2;
177
178const int DB_DEL_KEYFIELDS = 1;
179const int DB_DEL_WHERE = 2;
180const int DB_DEL_MATCHING = 3;
181
182const int DB_WHERE_KEYFIELDS = 1;
183const int DB_WHERE_MATCHING = 2;
184
185const int DB_GRANT_SELECT = 1;
186const int DB_GRANT_INSERT = 2;
187const int DB_GRANT_UPDATE = 4;
188const int DB_GRANT_DELETE = 8;
189const int DB_GRANT_ALL = DB_GRANT_SELECT | DB_GRANT_INSERT | DB_GRANT_UPDATE | DB_GRANT_DELETE;
108106cf
JS
190
191// ODBC Error codes (derived from ODBC SqlState codes)
fdc03678 192enum wxODBC_ERRORS
108106cf 193{
95ca6a20
GT
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'
108106cf
JS
286};
287
5cf34cf0 288#ifndef MAXNAME
f3df29e3 289 #define MAXNAME 31
5cf34cf0
GT
290#endif
291
292#ifndef SQL_MAX_AUTHSTR_LEN
f3df29e3
GT
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
5cf34cf0 296#endif
95ca6a20 297
0907ea9c
GT
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
bb41dcbe 305class WXDLLIMPEXP_ODBC wxDbConnectInf
108106cf 306{
5cf34cf0
GT
307 private:
308 bool freeHenvOnDestroy;
0907ea9c 309 bool useConnectionStr;
a2115c88 310
5cf34cf0 311 public:
2f971924
GT
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)
0907ea9c 316 wxChar ConnectionStr[SQL_MAX_CONNECTSTR_LEN+1]; // Connection string (password)
5cf34cf0
GT
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();
68379eaf
WS
327 wxDbConnectInf(HENV henv, const wxString &dsn, const wxString &userID=wxEmptyString,
328 const wxString &password=wxEmptyString, const wxString &defaultDir=wxEmptyString,
da99271d 329 const wxString &description=wxEmptyString, const wxString &fileType=wxEmptyString);
5cf34cf0
GT
330
331 ~wxDbConnectInf();
332
333 bool Initialize();
334
335 bool AllocHenv();
336 void FreeHenv();
337
338 // Accessors
6a71ee86 339 const HENV &GetHenv() { return Henv; };
5cf34cf0
GT
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
0907ea9c
GT
349 const wxChar *GetConnectionStr() { return ConnectionStr; };
350 bool UseConnectionStr() { return useConnectionStr; };
351
5cf34cf0
GT
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
0907ea9c
GT
366 void SetConnectionStr(const wxString &connectStr);
367
5cf34cf0
GT
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
a2115c88 372
108106cf 373
bb41dcbe 374struct WXDLLIMPEXP_ODBC wxDbSqlTypeInfo
108106cf 375{
4fdae997 376 wxString TypeName;
02cf6fdd 377 SWORD FsqlType;
4fdae997
GT
378 long Precision;
379 short CaseSensitive;
4fdae997 380 short MaximumScale;
fdc03678 381};
108106cf 382
95ca6a20 383
bb41dcbe 384class WXDLLIMPEXP_ODBC wxDbColFor
30a6d2d2 385{
95ca6a20 386public:
90e572f1
MR
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
5cf34cf0
GT
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
95ca6a20
GT
393 SWORD i_sqlDataType;
394
f6bcfd97 395 wxDbColFor();
1b941f2d 396 ~wxDbColFor(){}
595b5c4e 397
6a71ee86 398 void Initialize();
e4e45573 399 int Format(int Nation, int dbDataType, SWORD sqlDataType, short columnLength, short decimalDigits);
30a6d2d2
GT
400};
401
95ca6a20 402
bb41dcbe 403class WXDLLIMPEXP_ODBC wxDbColInf
108106cf
JS
404{
405public:
4fdae997
GT
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];
95ca6a20 410 SWORD sqlDataType;
4fdae997 411 wxChar typeName[128+1];
e4e45573
GT
412 SWORD columnLength;
413 SWORD bufferSize;
95ca6a20
GT
414 short decimalDigits;
415 short numPrecRadix;
416 short nullable;
4fdae997 417 wxChar remarks[254+1];
95ca6a20 418 int dbDataType; // conversion of the 'sqlDataType' to the generic data type used by these classes
30a6d2d2
GT
419 // mj10777.19991224 : new
420 int PkCol; // Primary key column 0=No; 1= First Key, 2 = Second Key etc.
4fdae997 421 wxChar PkTableName[DB_MAX_TABLE_NAME_LEN+1]; // Tables that use this PKey as a FKey
30a6d2d2 422 int FkCol; // Foreign key column 0=No; 1= First Key, 2 = Second Key etc.
4fdae997 423 wxChar FkTableName[DB_MAX_TABLE_NAME_LEN+1]; // Foreign key table name
f6bcfd97 424 wxDbColFor *pColFor; // How should this columns be formatted
3ca6a5f0
BP
425
426 wxDbColInf();
427 ~wxDbColInf();
da99271d
GT
428
429 bool Initialize();
30a6d2d2
GT
430};
431
95ca6a20 432
bb41dcbe 433class WXDLLIMPEXP_ODBC wxDbTableInf // Description of a Table
95ca6a20 434{
30a6d2d2 435public:
4fdae997
GT
436 wxChar tableName[DB_MAX_TABLE_NAME_LEN+1];
437 wxChar tableType[254+1]; // "TABLE" or "SYSTEM TABLE" etc.
438 wxChar tableRemarks[254+1];
02cf6fdd 439 UWORD numCols; // How many Columns does this Table have: GetColumnCount(..);
f6bcfd97 440 wxDbColInf *pColInf; // pColInf = NULL ; User can later call GetColumns(..);
da99271d 441
3ca6a5f0
BP
442 wxDbTableInf();
443 ~wxDbTableInf();
da99271d
GT
444
445 bool Initialize();
30a6d2d2
GT
446};
447
95ca6a20 448
bb41dcbe 449class WXDLLIMPEXP_ODBC wxDbInf // Description of a Database
95ca6a20 450{
30a6d2d2 451public:
4fdae997
GT
452 wxChar catalog[128+1];
453 wxChar schema[128+1];
f6bcfd97
BP
454 int numTables; // How many tables does this database have
455 wxDbTableInf *pTableInf; // pTableInf = new wxDbTableInf[numTables];
3ca6a5f0
BP
456
457 wxDbInf();
458 ~wxDbInf();
6a71ee86 459
da99271d 460 bool Initialize();
108106cf
JS
461};
462
95ca6a20 463
f6bcfd97 464enum wxDbSqlLogState
1fc5dd6f 465{
95ca6a20
GT
466 sqlLogOFF,
467 sqlLogON
1fc5dd6f
JS
468};
469
95ca6a20 470// These are the databases currently tested and working with these classes
f6bcfd97 471// See the comments in wxDb::Dbms() for exceptions/issues with
95ca6a20 472// each of these database engines
fdc03678 473enum wxDBMS
a2115c88 474{
95ca6a20
GT
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,
f6bcfd97 484 dbmsINFORMIX,
e2f4e132 485 dbmsVIRTUOSO,
d8a0a1c9 486 dbmsDB2,
1b12ab16 487 dbmsINTERBASE,
9f4de2dc 488 dbmsPERVASIVE_SQL,
215a0070 489 dbmsXBASE_SEQUITER,
2889677e
GT
490 dbmsFIREBIRD,
491 dbmsMAXDB,
492 dbmsFuture1,
493 dbmsFuture2,
494 dbmsFuture3,
495 dbmsFuture4,
496 dbmsFuture5,
497 dbmsFuture6,
498 dbmsFuture7,
499 dbmsFuture8,
500 dbmsFuture9,
501 dbmsFuture10
a2115c88
GT
502};
503
95ca6a20 504
f6bcfd97 505// The wxDb::errorList is copied to this variable when the wxDb object
a2115c88 506// is closed. This way, the error list is still available after the
595b5c4e 507// database object is closed. This is necessary if the database
a2115c88 508// connection fails so the calling application can show the operator
f6bcfd97
BP
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
a2115c88 511// this variable.
8a77c0d7 512
16cba29d 513extern WXDLLIMPEXP_DATA_ODBC(wxChar)
580820e3 514 DBerrorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN+1];
a2115c88 515
95ca6a20 516
bb41dcbe 517class WXDLLIMPEXP_ODBC wxDb
108106cf
JS
518{
519private:
95ca6a20 520 bool dbIsOpen;
20987580 521 bool dbIsCached; // Was connection created by caching functions
0907ea9c 522 bool dbOpenedWithConnectionString; // Was the database connection opened with a connection string
4fdae997
GT
523 wxString dsn; // Data source name
524 wxString uid; // User ID
525 wxString authStr; // Authorization string (password)
0907ea9c
GT
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
95ca6a20 528 FILE *fpSqlLog; // Sql Log file pointer
f6bcfd97 529 wxDbSqlLogState sqlLogState; // On or Off
95ca6a20 530 bool fwdOnlyCursors;
e2f4e132 531 wxDBMS dbmsType; // Type of datasource - i.e. Oracle, dBase, SQLServer, etc
95ca6a20
GT
532
533 // Private member functions
68379eaf 534 bool getDbInfo(bool failOnDataTypeUnsupported=true);
f6bcfd97 535 bool getDataTypeInfo(SWORD fSqlType, wxDbSqlTypeInfo &structSQLTypeInfo);
95ca6a20 536 bool setConnectionOptions(void);
4fdae997 537 void logError(const wxString &errMsg, const wxString &SQLState);
4fdae997 538 const wxChar *convertUserID(const wxChar *userID, wxString &UserID);
5f72522e 539 bool determineDataTypes(bool failOnDataTypeUnsupported);
da99271d 540 void initialize();
68379eaf 541 bool open(bool failOnDataTypeUnsupported=true);
4fdae997 542
f6bcfd97
BP
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.
3ca6a5f0 556 //
f6bcfd97
BP
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;
b742792c 565 wxDbSqlTypeInfo typeInfBlob;
7eeba511 566 wxDbSqlTypeInfo typeInfMemo;
f6bcfd97 567#endif
a3439c7d 568
108106cf 569public:
b742792c 570
20987580
GT
571 void setCached(bool cached) { dbIsCached = cached; }; // This function must only be called by wxDbGetConnection() and wxDbCloseConnections!!!
572 bool IsCached() { return dbIsCached; };
573
b742792c
GT
574 bool GetDataTypeInfo(SWORD fSqlType, wxDbSqlTypeInfo &structSQLTypeInfo)
575 { return getDataTypeInfo(fSqlType, structSQLTypeInfo); }
576
f6bcfd97
BP
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
95ca6a20
GT
590 // The following structure contains database information gathered from the
591 // datasource when the datasource is first opened.
592 struct
593 {
4fdae997
GT
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.
95ca6a20
GT
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
4fdae997 613 wxChar supportIEF[2]; // Integrity Enhancement Facility (Referential Integrity)
95ca6a20
GT
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
95ca6a20 627 // ODBC Error Inf.
95ca6a20 628 SWORD cbErrorMsg;
95ca6a20 629 int DB_STATUS;
13973396
JJ
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
f6bcfd97 635 wxChar errorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN];
13973396 636#endif
f6bcfd97 637 wxChar errorMsg[SQL_MAX_MESSAGE_LENGTH];
c497d0b2 638 SQLINTEGER nativeError;
f6bcfd97 639 wxChar sqlState[20];
95ca6a20 640
f6bcfd97
BP
641#if wxODBC_BACKWARD_COMPATABILITY
642 // Information about logical data types VARCHAR, INTEGER, FLOAT and DATE.
3ca6a5f0 643 //
f6bcfd97
BP
644 // This information is obtained from the ODBC driver by use of the
645 // SQLGetTypeInfo() function. The key piece of information is the
95ca6a20
GT
646 // type name the data source uses for each logical data type.
647 // e.g. VARCHAR; Oracle calls it VARCHAR2.
f6bcfd97
BP
648 wxDbSqlTypeInfo typeInfVarchar;
649 wxDbSqlTypeInfo typeInfInteger;
650 wxDbSqlTypeInfo typeInfFloat;
651 wxDbSqlTypeInfo typeInfDate;
b742792c 652 wxDbSqlTypeInfo typeInfBlob;
f6bcfd97 653#endif
595b5c4e 654
95ca6a20 655 // Public member functions
6a71ee86 656 wxDb(const HENV &aHenv, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS);
20987580 657 ~wxDb();
5cf34cf0 658
e25cdb86 659 // Data Source Name, User ID, Password and whether open should fail on data type not supported
68379eaf 660 bool Open(const wxString& inConnectStr, bool failOnDataTypeUnsupported=true);
11bfe4bf
VZ
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);
68379eaf
WS
663 bool Open(const wxString &Dsn, const wxString &Uid, const wxString &AuthStr, bool failOnDataTypeUnsupported=true);
664 bool Open(wxDbConnectInf *dbConnectInf, bool failOnDataTypeUnsupported=true);
e2f4e132 665 bool Open(wxDb *copyDb); // pointer to a wxDb whose connection info should be copied rather than re-queried
95ca6a20
GT
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);
68379eaf 672 bool CreateView(const wxString &viewName, const wxString &colList, const wxString &pSqlStmt, bool attemptDrop=true);
4fdae997
GT
673 bool DropView(const wxString &viewName);
674 bool ExecSql(const wxString &pSqlStmt);
f110f395 675 bool ExecSql(const wxString &pSqlStmt, wxDbColInf** columns, short& numcols);
95ca6a20 676 bool GetNext(void);
e716b9be 677 bool GetData(UWORD colNo, SWORD cType, PTR pData, SDWORD maxLen, SQLLEN FAR *cbReturned);
4fdae997
GT
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);
02cf6fdd 682 int GetKeyFields(const wxString &tableName, wxDbColInf* colInf, UWORD noCols);
4fdae997
GT
683
684 wxDbColInf *GetColumns(wxChar *tableName[], const wxChar *userID=NULL);
68379eaf 685 wxDbColInf *GetColumns(const wxString &tableName, UWORD *numCols, const wxChar *userID=NULL);
4fdae997 686
dba2120c 687 int GetColumnCount(const wxString &tableName, const wxChar *userID=NULL);
b742792c 688 const wxChar *GetDatabaseName(void) {return dbInf.dbmsName;}
4fdae997
GT
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;}
0907ea9c
GT
693 const wxString &GetConnectionInStr(void) {return inConnectionStr;}
694 const wxString &GetConnectionOutStr(void) {return outConnectionStr;}
b742792c 695 bool IsOpen(void) {return dbIsOpen;}
0907ea9c 696 bool OpenedWithConnectionString(void) {return dbOpenedWithConnectionString;}
b742792c
GT
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;}
7eeba511 706 wxDbSqlTypeInfo GetTypeInfMemo() {return typeInfMemo;}
b742792c
GT
707
708 // tableName can refer to a table, view, alias or synonym
68379eaf 709 bool TableExists(const wxString &tableName, const wxChar *userID=NULL,
b742792c 710 const wxString &tablePath=wxEmptyString);
68379eaf
WS
711 bool TablePrivileges(const wxString &tableName, const wxString &priv,
712 const wxChar *userID=NULL, const wxChar *schema=NULL,
b742792c
GT
713 const wxString &path=wxEmptyString);
714
243d4b36
GT
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
68379eaf 718 // correct enclosing marks around the name to allow it to be properly
243d4b36 719 // included in a SQL statement
5bdad317
GT
720 const wxString SQLTableName(const wxChar *tableName);
721 const wxString SQLColumnName(const wxChar *colName);
243d4b36 722
68379eaf 723 void LogError(const wxString &errMsg, const wxString &SQLState = wxEmptyString)
b742792c 724 { logError(errMsg, SQLState); }
f6bcfd97 725 void SetDebugErrorMessages(bool state) { silent = !state; }
68379eaf
WS
726 bool SetSqlLogging(wxDbSqlLogState state, const wxString &filename = SQL_LOG_FILENAME,
727 bool append = false);
4fdae997 728 bool WriteSqlLog(const wxString &logMsg);
b742792c 729
fdc03678 730 wxDBMS Dbms(void);
4fdae997
GT
731 bool ModifyColumn(const wxString &tableName, const wxString &columnName,
732 int dataType, ULONG columnLength=0,
da99271d 733 const wxString &optionalParam=wxEmptyString);
4fdae997 734
f6bcfd97
BP
735 bool FwdOnlyCursors(void) {return fwdOnlyCursors;}
736
737 // These two functions are provided strictly for use by wxDbTable.
738 // DO NOT USE THESE FUNCTIONS, OR MEMORY LEAKS MAY OCCUR
739 void incrementTableCount() { nTables++; return; }
740 void decrementTableCount() { nTables--; return; }
741
742}; // wxDb
108106cf 743
fdc03678 744
108106cf
JS
745// This structure forms a node in a linked list. The linked list of "DbList" objects
746// keeps track of allocated database connections. This allows the application to
747// open more than one database connection through ODBC for multiple transaction support
748// or for multiple database support.
fdc03678 749struct wxDbList
108106cf 750{
4fdae997
GT
751 wxDbList *PtrPrev; // Pointer to previous item in the list
752 wxString Dsn; // Data Source Name
753 wxString Uid; // User ID
754 wxString AuthStr; // Authorization string (password)
0907ea9c 755 wxString ConnectionStr; // Connection string used instead of DSN
4fdae997
GT
756 wxDb *PtrDb; // Pointer to the wxDb object
757 bool Free; // Is item free or in use?
758 wxDbList *PtrNext; // Pointer to next item in the list
108106cf
JS
759};
760
5cf34cf0 761
154f22b3 762#ifdef __WXDEBUG__
e115e771 763#include "wx/object.h"
fdc03678 764class wxTablesInUse : public wxObject
a2115c88 765{
95ca6a20 766 public:
4fdae997 767 const wxChar *tableName;
95ca6a20 768 ULONG tableID;
f6bcfd97 769 class wxDb *pDb;
fdc03678 770}; // wxTablesInUse
a2115c88
GT
771#endif
772
fdc03678 773
108106cf
JS
774// The following routines allow a user to get new database connections, free them
775// for other code segments to use, or close all of them when the application has
776// completed.
bb41dcbe
VS
777wxDb WXDLLIMPEXP_ODBC *wxDbGetConnection(wxDbConnectInf *pDbConfig, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS);
778bool WXDLLIMPEXP_ODBC wxDbFreeConnection(wxDb *pDb);
779void WXDLLIMPEXP_ODBC wxDbCloseConnections(void);
780int WXDLLIMPEXP_ODBC wxDbConnectionsInUse(void);
95ca6a20 781
108106cf 782
20987580
GT
783// Writes a message to the wxLog window (stdout usually) when an internal error
784// situation occurs. This function only works in DEBUG builds
f157ad08 785const wxChar WXDLLIMPEXP_ODBC *
886dd7d2
VZ
786wxDbLogExtendedErrorMsg(const wxChar *userText,
787 wxDb *pDb,
788 const wxChar *ErrFile,
789 int ErrLine);
5bdad317 790
20987580 791
f6bcfd97 792// This function sets the sql log state for all open wxDb objects
bb41dcbe 793bool WXDLLIMPEXP_ODBC
886dd7d2 794wxDbSqlLog(wxDbSqlLogState state, const wxString &filename = SQL_LOG_FILENAME);
fdc03678 795
95ca6a20 796
fdc03678
GT
797#if 0
798// MSW/VC6 ONLY!!! Experimental
da99271d 799int WXDLLEXPORT wxDbCreateDataSource(const wxString &driverName, const wxString &dsn, const wxString &description=wxEmptyString,
68379eaf 800 bool sysDSN=false, const wxString &defDir=wxEmptyString, wxWindow *parent=NULL);
fdc03678 801#endif
a2115c88 802
108106cf
JS
803// This routine allows you to query a driver manager
804// for a list of available datasources. Call this routine
805// the first time using SQL_FETCH_FIRST. Continue to call it
806// using SQL_FETCH_NEXT until you've exhausted the list.
bb41dcbe 807bool WXDLLIMPEXP_ODBC
e4e45573
GT
808wxDbGetDataSource(HENV henv, wxChar *Dsn, SWORD DsnMaxLength, wxChar *DsDesc,
809 SWORD DsDescMaxLength, UWORD direction = SQL_FETCH_NEXT);
fdc03678
GT
810
811
812// Change this to 0 to remove use of all deprecated functions
f6bcfd97 813#if wxODBC_BACKWARD_COMPATABILITY
fdc03678 814//#################################################################################
3103e8a9 815//############### DEPRECATED functions for backward compatibility #################
fdc03678
GT
816//#################################################################################
817
818// Backward compability structures/classes. This will eventually go away
f6bcfd97
BP
819const int DB_PATH_MAX = wxDB_PATH_MAX;
820
821typedef wxDb wxDB;
822typedef wxDbTableInf wxTableInf;
823typedef wxDbColInf wxColInf;
824typedef wxDbColInf CcolInf;
825typedef wxDbColFor wxColFor;
826typedef wxDbSqlTypeInfo SqlTypeInfo;
827typedef wxDbSqlTypeInfo wxSqlTypeInfo;
828typedef enum wxDbSqlLogState sqlLog;
829typedef enum wxDbSqlLogState wxSqlLogState;
830typedef enum wxDBMS dbms;
831typedef enum wxDBMS DBMS;
832typedef wxODBC_ERRORS ODBC_ERRORS;
833typedef wxDbConnectInf DbStuff;
834typedef wxDbList DbList;
27508856 835#ifdef __WXDEBUG__
f6bcfd97 836typedef wxTablesInUse CstructTablesInUse;
27508856 837#endif
fdc03678
GT
838
839// Deprecated function names that are replaced by the function names listed above
bb41dcbe 840wxDB WXDLLIMPEXP_ODBC
886dd7d2 841*GetDbConnection(DbStuff *pDbStuff, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS);
bb41dcbe
VS
842bool WXDLLIMPEXP_ODBC FreeDbConnection(wxDB *pDb);
843void WXDLLIMPEXP_ODBC CloseDbConnections(void);
844int WXDLLIMPEXP_ODBC NumberDbConnectionsInUse(void);
fdc03678 845
2b5f62a0 846bool SqlLog(sqlLog state, const wxChar *filename = SQL_LOG_FILENAME);
fdc03678 847
bb41dcbe 848bool WXDLLIMPEXP_ODBC
e4e45573 849GetDataSource(HENV henv, char *Dsn, SWORD DsnMaxLength, char *DsDesc, SWORD DsDescMaxLength,
886dd7d2
VZ
850 UWORD direction = SQL_FETCH_NEXT);
851
fdc03678 852#endif // Deprecated structures/classes/functions
108106cf 853
2b5f62a0
VZ
854#endif // _WX_DB_H_
855