]> git.saurik.com Git - wxWidgets.git/blame - include/wx/db.h
temporarily reverted binary backwards incompatible DoCreate to XmDoCreateTLW renaming
[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
12028905 34#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20987580 35 #pragma interface "db.h"
108106cf 36#endif
16b52fa1 37
d4af841d
GT
38#include "wx/defs.h"
39#include "wx/string.h"
40
68379eaf 41#if defined(__VISUALC__)
2b5f62a0
VZ
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 //
77ffb593 60 // Must add "odbccp32.lib" in \wx2\wxWidgets\src\makevc.env to the WINLIBS= line
2b5f62a0 61 //
e2f4e132
GT
62 #include "sql.h"
63 #include "sqlext.h"
2889677e
GT
64 //#if wxUSE_UNICODE
65 // #include <sqlucode.h>
66 //#endif
e2f4e132 67 #include "odbcinst.h"
f7556ff0 68#else
bfe5de86 69 #if defined(__WINDOWS__) && ( defined(HAVE_W32API_H) || defined(__BORLANDC__) )
190869b2
VS
70 #include <windows.h>
71 #include "wx/msw/winundef.h"
72 #endif
2b5f62a0 73 extern "C" {
0bdb08e7
VS
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"
2889677e
GT
78 // Not available in v2.x of iODBC
79 #ifndef __WXMSW__
cf0cf17b
JS
80 #if wxUSE_UNICODE
81 typedef wxChar SQLTCHAR;
82 #else
83 typedef UCHAR SQLTCHAR;
84 #endif
2889677e 85 #endif
0bdb08e7 86 #else
f7556ff0
JS
87 #include <sql.h>
88 #include <sqlext.h>
2889677e
GT
89 //#if wxUSE_UNICODE
90 // #include <sqlucode.h>
91 //#endif
0bdb08e7 92 #endif
2b5f62a0 93 }
aecca2c8
MB
94#endif
95
e4e45573
GT
96#if wxUSE_UNICODE
97#define SQL_C_WXCHAR SQL_C_WCHAR
98#else
99#define SQL_C_WXCHAR SQL_C_CHAR
100#endif
30a6d2d2 101
e716b9be
JS
102#ifdef __DIGITALMARS__
103#if wxUSE_UNICODE
104typedef wxChar SQLTCHAR;
105#else
106typedef UCHAR SQLTCHAR;
107#endif
108#endif
109
595b5c4e
MJ
110typedef float SFLOAT;
111typedef double SDOUBLE;
95ca6a20
GT
112typedef unsigned int UINT;
113#define ULONG UDWORD
30a6d2d2
GT
114
115#ifndef wxODBC_FWD_ONLY_CURSORS
116#define wxODBC_FWD_ONLY_CURSORS 1
117#endif
118
95ca6a20 119enum enumDummy {enumDum1};
7e616b10 120
b742792c 121#ifndef SQL_C_BOOLEAN
f3df29e3 122 #define SQL_C_BOOLEAN(datatype) (sizeof(datatype) == 1 ? SQL_C_UTINYINT : (sizeof(datatype) == 2 ? SQL_C_USHORT : SQL_C_ULONG))
b742792c 123#endif
20987580 124
b742792c 125#ifndef SQL_C_ENUM
f3df29e3 126 #define SQL_C_ENUM (sizeof(enumDummy) == 2 ? SQL_C_USHORT : SQL_C_ULONG)
b742792c 127#endif
20987580 128
f3df29e3
GT
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
b742792c 131#ifndef SQL_C_BLOB
f3df29e3
GT
132 #ifdef SQL_C_BINARY
133 #define SQL_C_BLOB SQL_C_BINARY
b742792c
GT
134 #endif
135#endif
7e616b10 136
e716b9be
JS
137#ifndef _WIN64
138#ifndef SQLLEN
139#define SQLLEN SQLINTEGER
140#endif
141#ifndef SQLULEN
142#define SQLULEN SQLUINTEGER
143#endif
144#endif
145
f6bcfd97 146const int wxDB_PATH_MAX = 254;
95ca6a20 147
16cba29d
WS
148extern WXDLLIMPEXP_DATA_ODBC(wxChar const *) SQL_LOG_FILENAME;
149extern WXDLLIMPEXP_DATA_ODBC(wxChar const *) SQL_CATALOG_FILENAME;
108106cf 150
108106cf 151// Database Globals
95ca6a20 152const int DB_TYPE_NAME_LEN = 40;
eb3b9dda
GT
153const int DB_MAX_STATEMENT_LEN = 4096;
154const int DB_MAX_WHERE_CLAUSE_LEN = 2048;
95ca6a20
GT
155const int DB_MAX_ERROR_MSG_LEN = 512;
156const int DB_MAX_ERROR_HISTORY = 5;
157const int DB_MAX_TABLE_NAME_LEN = 128;
158const int DB_MAX_COLUMN_NAME_LEN = 128;
159
160const int DB_DATA_TYPE_VARCHAR = 1;
161const int DB_DATA_TYPE_INTEGER = 2;
162const int DB_DATA_TYPE_FLOAT = 3;
163const int DB_DATA_TYPE_DATE = 4;
b742792c 164const int DB_DATA_TYPE_BLOB = 5;
95ca6a20
GT
165
166const int DB_SELECT_KEYFIELDS = 1;
167const int DB_SELECT_WHERE = 2;
168const int DB_SELECT_MATCHING = 3;
169const int DB_SELECT_STATEMENT = 4;
170
171const int DB_UPD_KEYFIELDS = 1;
172const int DB_UPD_WHERE = 2;
173
174const int DB_DEL_KEYFIELDS = 1;
175const int DB_DEL_WHERE = 2;
176const int DB_DEL_MATCHING = 3;
177
178const int DB_WHERE_KEYFIELDS = 1;
179const int DB_WHERE_MATCHING = 2;
180
181const int DB_GRANT_SELECT = 1;
182const int DB_GRANT_INSERT = 2;
183const int DB_GRANT_UPDATE = 4;
184const int DB_GRANT_DELETE = 8;
185const int DB_GRANT_ALL = DB_GRANT_SELECT | DB_GRANT_INSERT | DB_GRANT_UPDATE | DB_GRANT_DELETE;
108106cf
JS
186
187// ODBC Error codes (derived from ODBC SqlState codes)
fdc03678 188enum wxODBC_ERRORS
108106cf 189{
95ca6a20
GT
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'
108106cf
JS
282};
283
5cf34cf0 284#ifndef MAXNAME
f3df29e3 285 #define MAXNAME 31
5cf34cf0
GT
286#endif
287
288#ifndef SQL_MAX_AUTHSTR_LEN
f3df29e3
GT
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
5cf34cf0 292#endif
95ca6a20 293
0907ea9c
GT
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
bb41dcbe 301class WXDLLIMPEXP_ODBC wxDbConnectInf
108106cf 302{
5cf34cf0
GT
303 private:
304 bool freeHenvOnDestroy;
0907ea9c 305 bool useConnectionStr;
a2115c88 306
5cf34cf0 307 public:
2f971924
GT
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)
0907ea9c 312 wxChar ConnectionStr[SQL_MAX_CONNECTSTR_LEN+1]; // Connection string (password)
5cf34cf0
GT
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();
68379eaf
WS
323 wxDbConnectInf(HENV henv, const wxString &dsn, const wxString &userID=wxEmptyString,
324 const wxString &password=wxEmptyString, const wxString &defaultDir=wxEmptyString,
da99271d 325 const wxString &description=wxEmptyString, const wxString &fileType=wxEmptyString);
5cf34cf0
GT
326
327 ~wxDbConnectInf();
328
329 bool Initialize();
330
331 bool AllocHenv();
332 void FreeHenv();
333
334 // Accessors
6a71ee86 335 const HENV &GetHenv() { return Henv; };
5cf34cf0
GT
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
0907ea9c
GT
345 const wxChar *GetConnectionStr() { return ConnectionStr; };
346 bool UseConnectionStr() { return useConnectionStr; };
347
5cf34cf0
GT
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
0907ea9c
GT
362 void SetConnectionStr(const wxString &connectStr);
363
5cf34cf0
GT
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
a2115c88 368
108106cf 369
bb41dcbe 370struct WXDLLIMPEXP_ODBC wxDbSqlTypeInfo
108106cf 371{
4fdae997 372 wxString TypeName;
02cf6fdd 373 SWORD FsqlType;
4fdae997
GT
374 long Precision;
375 short CaseSensitive;
4fdae997 376 short MaximumScale;
fdc03678 377};
108106cf 378
95ca6a20 379
bb41dcbe 380class WXDLLIMPEXP_ODBC wxDbColFor
30a6d2d2 381{
95ca6a20 382public:
5cf34cf0
GT
383 wxString s_Field; // Formated String for Output
384 wxString s_Format[7]; // Formated Objects - TIMESTAMP has the biggest (7)
3ca6a5f0
BP
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
5cf34cf0
GT
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
95ca6a20
GT
389 SWORD i_sqlDataType;
390
f6bcfd97 391 wxDbColFor();
1b941f2d 392 ~wxDbColFor(){}
595b5c4e 393
6a71ee86 394 void Initialize();
e4e45573 395 int Format(int Nation, int dbDataType, SWORD sqlDataType, short columnLength, short decimalDigits);
30a6d2d2
GT
396};
397
95ca6a20 398
bb41dcbe 399class WXDLLIMPEXP_ODBC wxDbColInf
108106cf
JS
400{
401public:
4fdae997
GT
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];
95ca6a20 406 SWORD sqlDataType;
4fdae997 407 wxChar typeName[128+1];
e4e45573
GT
408 SWORD columnLength;
409 SWORD bufferSize;
95ca6a20
GT
410 short decimalDigits;
411 short numPrecRadix;
412 short nullable;
4fdae997 413 wxChar remarks[254+1];
95ca6a20 414 int dbDataType; // conversion of the 'sqlDataType' to the generic data type used by these classes
30a6d2d2
GT
415 // mj10777.19991224 : new
416 int PkCol; // Primary key column 0=No; 1= First Key, 2 = Second Key etc.
4fdae997 417 wxChar PkTableName[DB_MAX_TABLE_NAME_LEN+1]; // Tables that use this PKey as a FKey
30a6d2d2 418 int FkCol; // Foreign key column 0=No; 1= First Key, 2 = Second Key etc.
4fdae997 419 wxChar FkTableName[DB_MAX_TABLE_NAME_LEN+1]; // Foreign key table name
f6bcfd97 420 wxDbColFor *pColFor; // How should this columns be formatted
3ca6a5f0
BP
421
422 wxDbColInf();
423 ~wxDbColInf();
da99271d
GT
424
425 bool Initialize();
30a6d2d2
GT
426};
427
95ca6a20 428
bb41dcbe 429class WXDLLIMPEXP_ODBC wxDbTableInf // Description of a Table
95ca6a20 430{
30a6d2d2 431public:
4fdae997
GT
432 wxChar tableName[DB_MAX_TABLE_NAME_LEN+1];
433 wxChar tableType[254+1]; // "TABLE" or "SYSTEM TABLE" etc.
434 wxChar tableRemarks[254+1];
02cf6fdd 435 UWORD numCols; // How many Columns does this Table have: GetColumnCount(..);
f6bcfd97 436 wxDbColInf *pColInf; // pColInf = NULL ; User can later call GetColumns(..);
da99271d 437
3ca6a5f0
BP
438 wxDbTableInf();
439 ~wxDbTableInf();
da99271d
GT
440
441 bool Initialize();
30a6d2d2
GT
442};
443
95ca6a20 444
bb41dcbe 445class WXDLLIMPEXP_ODBC wxDbInf // Description of a Database
95ca6a20 446{
30a6d2d2 447public:
4fdae997
GT
448 wxChar catalog[128+1];
449 wxChar schema[128+1];
f6bcfd97
BP
450 int numTables; // How many tables does this database have
451 wxDbTableInf *pTableInf; // pTableInf = new wxDbTableInf[numTables];
3ca6a5f0
BP
452
453 wxDbInf();
454 ~wxDbInf();
6a71ee86 455
da99271d 456 bool Initialize();
108106cf
JS
457};
458
95ca6a20 459
f6bcfd97 460enum wxDbSqlLogState
1fc5dd6f 461{
95ca6a20
GT
462 sqlLogOFF,
463 sqlLogON
1fc5dd6f
JS
464};
465
95ca6a20 466// These are the databases currently tested and working with these classes
f6bcfd97 467// See the comments in wxDb::Dbms() for exceptions/issues with
95ca6a20 468// each of these database engines
fdc03678 469enum wxDBMS
a2115c88 470{
95ca6a20
GT
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,
f6bcfd97 480 dbmsINFORMIX,
e2f4e132 481 dbmsVIRTUOSO,
d8a0a1c9 482 dbmsDB2,
1b12ab16 483 dbmsINTERBASE,
9f4de2dc 484 dbmsPERVASIVE_SQL,
215a0070 485 dbmsXBASE_SEQUITER,
2889677e
GT
486 dbmsFIREBIRD,
487 dbmsMAXDB,
488 dbmsFuture1,
489 dbmsFuture2,
490 dbmsFuture3,
491 dbmsFuture4,
492 dbmsFuture5,
493 dbmsFuture6,
494 dbmsFuture7,
495 dbmsFuture8,
496 dbmsFuture9,
497 dbmsFuture10
a2115c88
GT
498};
499
95ca6a20 500
f6bcfd97 501// The wxDb::errorList is copied to this variable when the wxDb object
a2115c88 502// is closed. This way, the error list is still available after the
595b5c4e 503// database object is closed. This is necessary if the database
a2115c88 504// connection fails so the calling application can show the operator
f6bcfd97
BP
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
a2115c88 507// this variable.
8a77c0d7 508
16cba29d 509extern WXDLLIMPEXP_DATA_ODBC(wxChar)
580820e3 510 DBerrorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN+1];
a2115c88 511
95ca6a20 512
bb41dcbe 513class WXDLLIMPEXP_ODBC wxDb
108106cf
JS
514{
515private:
95ca6a20 516 bool dbIsOpen;
20987580 517 bool dbIsCached; // Was connection created by caching functions
0907ea9c 518 bool dbOpenedWithConnectionString; // Was the database connection opened with a connection string
4fdae997
GT
519 wxString dsn; // Data source name
520 wxString uid; // User ID
521 wxString authStr; // Authorization string (password)
0907ea9c
GT
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
95ca6a20 524 FILE *fpSqlLog; // Sql Log file pointer
f6bcfd97 525 wxDbSqlLogState sqlLogState; // On or Off
95ca6a20 526 bool fwdOnlyCursors;
e2f4e132 527 wxDBMS dbmsType; // Type of datasource - i.e. Oracle, dBase, SQLServer, etc
95ca6a20
GT
528
529 // Private member functions
68379eaf 530 bool getDbInfo(bool failOnDataTypeUnsupported=true);
f6bcfd97 531 bool getDataTypeInfo(SWORD fSqlType, wxDbSqlTypeInfo &structSQLTypeInfo);
95ca6a20 532 bool setConnectionOptions(void);
4fdae997 533 void logError(const wxString &errMsg, const wxString &SQLState);
4fdae997 534 const wxChar *convertUserID(const wxChar *userID, wxString &UserID);
5f72522e 535 bool determineDataTypes(bool failOnDataTypeUnsupported);
da99271d 536 void initialize();
68379eaf 537 bool open(bool failOnDataTypeUnsupported=true);
4fdae997 538
f6bcfd97
BP
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.
3ca6a5f0 552 //
f6bcfd97
BP
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;
b742792c 561 wxDbSqlTypeInfo typeInfBlob;
f6bcfd97 562#endif
a3439c7d 563
108106cf 564public:
b742792c 565
20987580
GT
566 void setCached(bool cached) { dbIsCached = cached; }; // This function must only be called by wxDbGetConnection() and wxDbCloseConnections!!!
567 bool IsCached() { return dbIsCached; };
568
b742792c
GT
569 bool GetDataTypeInfo(SWORD fSqlType, wxDbSqlTypeInfo &structSQLTypeInfo)
570 { return getDataTypeInfo(fSqlType, structSQLTypeInfo); }
571
f6bcfd97
BP
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
95ca6a20
GT
585 // The following structure contains database information gathered from the
586 // datasource when the datasource is first opened.
587 struct
588 {
4fdae997
GT
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.
95ca6a20
GT
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
4fdae997 608 wxChar supportIEF[2]; // Integrity Enhancement Facility (Referential Integrity)
95ca6a20
GT
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
95ca6a20 622 // ODBC Error Inf.
95ca6a20 623 SWORD cbErrorMsg;
95ca6a20 624 int DB_STATUS;
13973396
JJ
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
f6bcfd97 630 wxChar errorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN];
13973396 631#endif
f6bcfd97 632 wxChar errorMsg[SQL_MAX_MESSAGE_LENGTH];
c497d0b2 633 SQLINTEGER nativeError;
f6bcfd97 634 wxChar sqlState[20];
95ca6a20 635
f6bcfd97
BP
636#if wxODBC_BACKWARD_COMPATABILITY
637 // Information about logical data types VARCHAR, INTEGER, FLOAT and DATE.
3ca6a5f0 638 //
f6bcfd97
BP
639 // This information is obtained from the ODBC driver by use of the
640 // SQLGetTypeInfo() function. The key piece of information is the
95ca6a20
GT
641 // type name the data source uses for each logical data type.
642 // e.g. VARCHAR; Oracle calls it VARCHAR2.
f6bcfd97
BP
643 wxDbSqlTypeInfo typeInfVarchar;
644 wxDbSqlTypeInfo typeInfInteger;
645 wxDbSqlTypeInfo typeInfFloat;
646 wxDbSqlTypeInfo typeInfDate;
b742792c 647 wxDbSqlTypeInfo typeInfBlob;
f6bcfd97 648#endif
595b5c4e 649
95ca6a20 650 // Public member functions
6a71ee86 651 wxDb(const HENV &aHenv, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS);
20987580 652 ~wxDb();
5cf34cf0 653
e25cdb86 654 // Data Source Name, User ID, Password and whether open should fail on data type not supported
68379eaf 655 bool Open(const wxString& inConnectStr, bool failOnDataTypeUnsupported=true);
11bfe4bf
VZ
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);
68379eaf
WS
658 bool Open(const wxString &Dsn, const wxString &Uid, const wxString &AuthStr, bool failOnDataTypeUnsupported=true);
659 bool Open(wxDbConnectInf *dbConnectInf, bool failOnDataTypeUnsupported=true);
e2f4e132 660 bool Open(wxDb *copyDb); // pointer to a wxDb whose connection info should be copied rather than re-queried
95ca6a20
GT
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);
68379eaf 667 bool CreateView(const wxString &viewName, const wxString &colList, const wxString &pSqlStmt, bool attemptDrop=true);
4fdae997
GT
668 bool DropView(const wxString &viewName);
669 bool ExecSql(const wxString &pSqlStmt);
f110f395 670 bool ExecSql(const wxString &pSqlStmt, wxDbColInf** columns, short& numcols);
95ca6a20 671 bool GetNext(void);
e716b9be 672 bool GetData(UWORD colNo, SWORD cType, PTR pData, SDWORD maxLen, SQLLEN FAR *cbReturned);
4fdae997
GT
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);
02cf6fdd 677 int GetKeyFields(const wxString &tableName, wxDbColInf* colInf, UWORD noCols);
4fdae997
GT
678
679 wxDbColInf *GetColumns(wxChar *tableName[], const wxChar *userID=NULL);
68379eaf 680 wxDbColInf *GetColumns(const wxString &tableName, UWORD *numCols, const wxChar *userID=NULL);
4fdae997 681
dba2120c 682 int GetColumnCount(const wxString &tableName, const wxChar *userID=NULL);
b742792c 683 const wxChar *GetDatabaseName(void) {return dbInf.dbmsName;}
4fdae997
GT
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;}
0907ea9c
GT
688 const wxString &GetConnectionInStr(void) {return inConnectionStr;}
689 const wxString &GetConnectionOutStr(void) {return outConnectionStr;}
b742792c 690 bool IsOpen(void) {return dbIsOpen;}
0907ea9c 691 bool OpenedWithConnectionString(void) {return dbOpenedWithConnectionString;}
b742792c
GT
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
68379eaf 703 bool TableExists(const wxString &tableName, const wxChar *userID=NULL,
b742792c 704 const wxString &tablePath=wxEmptyString);
68379eaf
WS
705 bool TablePrivileges(const wxString &tableName, const wxString &priv,
706 const wxChar *userID=NULL, const wxChar *schema=NULL,
b742792c
GT
707 const wxString &path=wxEmptyString);
708
243d4b36
GT
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
68379eaf 712 // correct enclosing marks around the name to allow it to be properly
243d4b36 713 // included in a SQL statement
5bdad317
GT
714 const wxString SQLTableName(const wxChar *tableName);
715 const wxString SQLColumnName(const wxChar *colName);
243d4b36 716
68379eaf 717 void LogError(const wxString &errMsg, const wxString &SQLState = wxEmptyString)
b742792c 718 { logError(errMsg, SQLState); }
f6bcfd97 719 void SetDebugErrorMessages(bool state) { silent = !state; }
68379eaf
WS
720 bool SetSqlLogging(wxDbSqlLogState state, const wxString &filename = SQL_LOG_FILENAME,
721 bool append = false);
4fdae997 722 bool WriteSqlLog(const wxString &logMsg);
b742792c 723
fdc03678 724 wxDBMS Dbms(void);
4fdae997
GT
725 bool ModifyColumn(const wxString &tableName, const wxString &columnName,
726 int dataType, ULONG columnLength=0,
da99271d 727 const wxString &optionalParam=wxEmptyString);
4fdae997 728
f6bcfd97
BP
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
108106cf 737
fdc03678 738
108106cf
JS
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.
fdc03678 743struct wxDbList
108106cf 744{
4fdae997
GT
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)
0907ea9c 749 wxString ConnectionStr; // Connection string used instead of DSN
4fdae997
GT
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
108106cf
JS
753};
754
5cf34cf0 755
154f22b3 756#ifdef __WXDEBUG__
e115e771 757#include "wx/object.h"
fdc03678 758class wxTablesInUse : public wxObject
a2115c88 759{
95ca6a20 760 public:
4fdae997 761 const wxChar *tableName;
95ca6a20 762 ULONG tableID;
f6bcfd97 763 class wxDb *pDb;
fdc03678 764}; // wxTablesInUse
a2115c88
GT
765#endif
766
fdc03678 767
108106cf
JS
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.
bb41dcbe
VS
771wxDb WXDLLIMPEXP_ODBC *wxDbGetConnection(wxDbConnectInf *pDbConfig, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS);
772bool WXDLLIMPEXP_ODBC wxDbFreeConnection(wxDb *pDb);
773void WXDLLIMPEXP_ODBC wxDbCloseConnections(void);
774int WXDLLIMPEXP_ODBC wxDbConnectionsInUse(void);
95ca6a20 775
108106cf 776
20987580
GT
777// Writes a message to the wxLog window (stdout usually) when an internal error
778// situation occurs. This function only works in DEBUG builds
f157ad08 779const wxChar WXDLLIMPEXP_ODBC *
886dd7d2
VZ
780wxDbLogExtendedErrorMsg(const wxChar *userText,
781 wxDb *pDb,
782 const wxChar *ErrFile,
783 int ErrLine);
5bdad317 784
20987580 785
f6bcfd97 786// This function sets the sql log state for all open wxDb objects
bb41dcbe 787bool WXDLLIMPEXP_ODBC
886dd7d2 788wxDbSqlLog(wxDbSqlLogState state, const wxString &filename = SQL_LOG_FILENAME);
fdc03678 789
95ca6a20 790
fdc03678
GT
791#if 0
792// MSW/VC6 ONLY!!! Experimental
da99271d 793int WXDLLEXPORT wxDbCreateDataSource(const wxString &driverName, const wxString &dsn, const wxString &description=wxEmptyString,
68379eaf 794 bool sysDSN=false, const wxString &defDir=wxEmptyString, wxWindow *parent=NULL);
fdc03678 795#endif
a2115c88 796
108106cf
JS
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.
bb41dcbe 801bool WXDLLIMPEXP_ODBC
e4e45573
GT
802wxDbGetDataSource(HENV henv, wxChar *Dsn, SWORD DsnMaxLength, wxChar *DsDesc,
803 SWORD DsDescMaxLength, UWORD direction = SQL_FETCH_NEXT);
fdc03678
GT
804
805
806// Change this to 0 to remove use of all deprecated functions
f6bcfd97 807#if wxODBC_BACKWARD_COMPATABILITY
fdc03678 808//#################################################################################
3103e8a9 809//############### DEPRECATED functions for backward compatibility #################
fdc03678
GT
810//#################################################################################
811
812// Backward compability structures/classes. This will eventually go away
f6bcfd97
BP
813const int DB_PATH_MAX = wxDB_PATH_MAX;
814
815typedef wxDb wxDB;
816typedef wxDbTableInf wxTableInf;
817typedef wxDbColInf wxColInf;
818typedef wxDbColInf CcolInf;
819typedef wxDbColFor wxColFor;
820typedef wxDbSqlTypeInfo SqlTypeInfo;
821typedef wxDbSqlTypeInfo wxSqlTypeInfo;
822typedef enum wxDbSqlLogState sqlLog;
823typedef enum wxDbSqlLogState wxSqlLogState;
824typedef enum wxDBMS dbms;
825typedef enum wxDBMS DBMS;
826typedef wxODBC_ERRORS ODBC_ERRORS;
827typedef wxDbConnectInf DbStuff;
828typedef wxDbList DbList;
27508856 829#ifdef __WXDEBUG__
f6bcfd97 830typedef wxTablesInUse CstructTablesInUse;
27508856 831#endif
fdc03678
GT
832
833// Deprecated function names that are replaced by the function names listed above
bb41dcbe 834wxDB WXDLLIMPEXP_ODBC
886dd7d2 835*GetDbConnection(DbStuff *pDbStuff, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS);
bb41dcbe
VS
836bool WXDLLIMPEXP_ODBC FreeDbConnection(wxDB *pDb);
837void WXDLLIMPEXP_ODBC CloseDbConnections(void);
838int WXDLLIMPEXP_ODBC NumberDbConnectionsInUse(void);
fdc03678 839
2b5f62a0 840bool SqlLog(sqlLog state, const wxChar *filename = SQL_LOG_FILENAME);
fdc03678 841
bb41dcbe 842bool WXDLLIMPEXP_ODBC
e4e45573 843GetDataSource(HENV henv, char *Dsn, SWORD DsnMaxLength, char *DsDesc, SWORD DsDescMaxLength,
886dd7d2
VZ
844 UWORD direction = SQL_FETCH_NEXT);
845
fdc03678 846#endif // Deprecated structures/classes/functions
108106cf 847
2b5f62a0
VZ
848#endif // _WX_DB_H_
849