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