]> git.saurik.com Git - wxWidgets.git/blame - src/common/db.cpp
New colour, font, theme and size code..
[wxWidgets.git] / src / common / db.cpp
CommitLineData
108106cf
JS
1///////////////////////////////////////////////////////////////////////////////
2// Name: db.cpp
3// Purpose: Implementation of the wxDB class. The wxDB class represents a connection
4// to an ODBC data source. The wxDB class allows operations on the data
5// source such as opening and closing the data source.
6// Author: Doug Card
7// Modified by:
a2115c88
GT
8// Mods: Dec, 1998:
9// -Added support for SQL statement logging and database cataloging
10// Mods: April, 1999
11// -Added QUERY_ONLY mode support to reduce default number of cursors
12// -Added additional SQL logging code
13// -Added DEBUG-ONLY tracking of wxTable objects to detect orphaned DB connections
14// -Set ODBC option to only read committed writes to the DB so all
15// databases operate the same in that respect
108106cf
JS
16// Created: 9.96
17// RCS-ID: $Id$
18// Copyright: (c) 1996 Remstar International, Inc.
19// Licence: wxWindows licence, plus:
a2115c88 20// Notice: This class library and its intellectual design are free of charge for use,
108106cf
JS
21// modification, enhancement, debugging under the following conditions:
22// 1) These classes may only be used as part of the implementation of a
23// wxWindows-based application
24// 2) All enhancements and bug fixes are to be submitted back to the wxWindows
25// user groups free of all charges for use with the wxWindows library.
26// 3) These classes may not be distributed as part of any other class library,
27// DLL, text (written or electronic), other than a complete distribution of
28// the wxWindows GUI development toolkit.
29///////////////////////////////////////////////////////////////////////////////
30
31/*
32// SYNOPSIS START
33// SYNOPSIS STOP
34*/
35
01dba85a
JS
36#include "wx/wxprec.h"
37
a2115c88
GT
38// Use this line for wxWindows v1.x
39//#include "wx_ver.h"
40// Use this line for wxWindows v2.x
41#include "wx/version.h"
a2115c88
GT
42
43#if wxMAJOR_VERSION == 2
44 #ifdef __GNUG__
45 #pragma implementation "db.h"
46 #endif
47#endif
48
1fc5dd6f 49#ifdef DBDEBUG_CONSOLE
108106cf
JS
50 #include <iostream.h>
51#endif
108106cf
JS
52
53#ifdef __BORLANDC__
a2115c88 54 #pragma hdrstop
108106cf
JS
55#endif //__BORLANDC__
56
a2115c88 57#if wxMAJOR_VERSION == 2
9199e66f
RR
58 #ifndef WX_PRECOMP
59 #include "wx/string.h"
60 #include "wx/object.h"
61 #include "wx/list.h"
62 #include "wx/utils.h"
63 #include "wx/msgdlg.h"
64 #endif
3351164d 65 #include "wx/filefn.h"
aa33452c 66 #include "wx/wxchar.h"
a2115c88
GT
67#endif
68
69#if wxMAJOR_VERSION == 1
70# if defined(wx_msw) || defined(wx_x)
71# ifdef WX_PRECOMP
72# include "wx_prec.h"
73# else
74# include "wx.h"
75# endif
76# endif
77# define wxUSE_ODBC 1
78#endif
108106cf 79
47d67540 80#if wxUSE_ODBC
108106cf 81
108106cf
JS
82#include <stdio.h>
83#include <string.h>
84#include <assert.h>
7e616b10
RR
85#include <stdlib.h>
86#include <ctype.h>
a2115c88
GT
87#if wxMAJOR_VERSION == 1
88 #include "db.h"
89#elif wxMAJOR_VERSION == 2
90 #include "wx/db.h"
91#endif
108106cf 92
a497618a 93DbList WXDLLEXPORT *PtrBegDbList = 0;
108106cf 94
e041ce57 95#ifdef __WXDEBUG__
a2115c88
GT
96 extern wxList TablesInUse;
97#endif
98
99// SQL Log defaults to be used by GetDbConnection
100enum sqlLog SQLLOGstate = sqlLogOFF;
101
102char SQLLOGfn[DB_PATH_MAX+1] = "sqllog.txt";
103
104// The wxDB::errorList is copied to this variable when the wxDB object
105// is closed. This way, the error list is still available after the
106// database object is closed. This is necessary if the database
107// connection fails so the calling application can show the operator
108// why the connection failed. Note: as each wxDB object is closed, it
109// will overwrite the errors of the previously destroyed wxDB object in
110// this variable.
111char DBerrorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN];
112
108106cf 113/********** wxDB Constructor **********/
a3439c7d 114wxDB::wxDB(HENV &aHenv, bool FwdOnlyCursors)
108106cf
JS
115{
116 int i;
1fc5dd6f
JS
117
118 fpSqlLog = 0; // Sql Log file pointer
119 sqlLogState = sqlLogOFF; // By default, logging is turned off
a2115c88 120 nTables = 0;
108106cf 121
e8af3814
GT
122 wxStrcpy(sqlState,"");
123 wxStrcpy(errorMsg,"");
108106cf
JS
124 nativeError = cbErrorMsg = 0;
125 for (i = 0; i < DB_MAX_ERROR_HISTORY; i++)
e8af3814 126 wxStrcpy(errorList[i], "");
108106cf
JS
127
128 // Init typeInf structures
e8af3814 129 wxStrcpy(typeInfVarchar.TypeName,"");
108106cf
JS
130 typeInfVarchar.FsqlType = 0;
131 typeInfVarchar.Precision = 0;
132 typeInfVarchar.CaseSensitive = 0;
133 typeInfVarchar.MaximumScale = 0;
134
e8af3814 135 wxStrcpy(typeInfInteger.TypeName,"");
108106cf
JS
136 typeInfInteger.FsqlType = 0;
137 typeInfInteger.Precision = 0;
138 typeInfInteger.CaseSensitive = 0;
139 typeInfInteger.MaximumScale = 0;
140
e8af3814 141 wxStrcpy(typeInfFloat.TypeName,"");
108106cf
JS
142 typeInfFloat.FsqlType = 0;
143 typeInfFloat.Precision = 0;
144 typeInfFloat.CaseSensitive = 0;
145 typeInfFloat.MaximumScale = 0;
146
e8af3814 147 wxStrcpy(typeInfDate.TypeName,"");
108106cf
JS
148 typeInfDate.FsqlType = 0;
149 typeInfDate.Precision = 0;
150 typeInfDate.CaseSensitive = 0;
151 typeInfDate.MaximumScale = 0;
152
153 // Error reporting is turned OFF by default
154 silent = TRUE;
155
156 // Copy the HENV into the db class
157 henv = aHenv;
a3439c7d 158 fwdOnlyCursors = FwdOnlyCursors;
108106cf
JS
159
160 // Allocate a data source connection handle
161 if (SQLAllocConnect(henv, &hdbc) != SQL_SUCCESS)
162 DispAllErrors(henv);
163
164 // Initialize the db status flag
165 DB_STATUS = 0;
166
167 // Mark database as not open as of yet
168 dbIsOpen = FALSE;
169
170} // wxDB::wxDB()
171
172/********** wxDB::Open() **********/
173bool wxDB::Open(char *Dsn, char *Uid, char *AuthStr)
174{
e8af3814 175 assert(Dsn && wxStrlen(Dsn));
108106cf
JS
176 dsn = Dsn;
177 uid = Uid;
178 authStr = AuthStr;
179
108106cf
JS
180 RETCODE retcode;
181
a3439c7d
GT
182 if (!FwdOnlyCursors())
183 {
184 // Specify that the ODBC cursor library be used, if needed. This must be
185 // specified before the connection is made.
186 retcode = SQLSetConnectOption(hdbc, SQL_ODBC_CURSORS, SQL_CUR_USE_IF_NEEDED);
108106cf 187
6919c53f 188#ifdef DBDEBUG_CONSOLE
108106cf
JS
189 if (retcode == SQL_SUCCESS)
190 cout << "SQLSetConnectOption(CURSOR_LIB) successful" << endl;
191 else
192 cout << "SQLSetConnectOption(CURSOR_LIB) failed" << endl;
6919c53f 193#endif
a3439c7d 194 }
108106cf
JS
195
196 // Connect to the data source
a2115c88
GT
197 retcode = SQLConnect(hdbc, (UCHAR FAR *) Dsn, SQL_NTS,
198 (UCHAR FAR *) Uid, SQL_NTS,
199 (UCHAR FAR *) AuthStr,SQL_NTS);
200 if (retcode == SQL_SUCCESS_WITH_INFO)
201 DispAllErrors(henv, hdbc);
202 else if (retcode != SQL_SUCCESS)
108106cf
JS
203 return(DispAllErrors(henv, hdbc));
204
a2115c88
GT
205/*
206 If using Intersolv branded ODBC drivers, this is the place where you would substitute
207 your branded driver license information
208
209 SQLSetConnectOption(hdbc, 1041, (UDWORD) "");
210 SQLSetConnectOption(hdbc, 1042, (UDWORD) "");
211*/
108106cf
JS
212 // Mark database as open
213 dbIsOpen = TRUE;
214
215 // Allocate a statement handle for the database connection
216 if (SQLAllocStmt(hdbc, &hstmt) != SQL_SUCCESS)
217 return(DispAllErrors(henv, hdbc));
218
219 // Set Connection Options
220 if (! setConnectionOptions())
221 return(FALSE);
222
223 // Query the data source for inf. about itself
224 if (! getDbInfo())
225 return(FALSE);
226
227 // Query the data source regarding data type information
228
229 //
230 // The way I determined which SQL data types to use was by calling SQLGetInfo
231 // for all of the possible SQL data types to see which ones were supported. If
232 // a type is not supported, the SQLFetch() that's called from getDataTypeInfo()
233 // fails with SQL_NO_DATA_FOUND. This is ugly because I'm sure the three SQL data
234 // types I've selected below will not alway's be what we want. These are just
235 // what happened to work against an Oracle 7/Intersolv combination. The following is
236 // a complete list of the results I got back against the Oracle 7 database:
237 //
238 // SQL_BIGINT SQL_NO_DATA_FOUND
239 // SQL_BINARY SQL_NO_DATA_FOUND
240 // SQL_BIT SQL_NO_DATA_FOUND
241 // SQL_CHAR type name = 'CHAR', Precision = 255
242 // SQL_DATE SQL_NO_DATA_FOUND
243 // SQL_DECIMAL type name = 'NUMBER', Precision = 38
244 // SQL_DOUBLE type name = 'NUMBER', Precision = 15
245 // SQL_FLOAT SQL_NO_DATA_FOUND
246 // SQL_INTEGER SQL_NO_DATA_FOUND
247 // SQL_LONGVARBINARY type name = 'LONG RAW', Precision = 2 billion
248 // SQL_LONGVARCHAR type name = 'LONG', Precision = 2 billion
249 // SQL_NUMERIC SQL_NO_DATA_FOUND
250 // SQL_REAL SQL_NO_DATA_FOUND
251 // SQL_SMALLINT SQL_NO_DATA_FOUND
252 // SQL_TIME SQL_NO_DATA_FOUND
253 // SQL_TIMESTAMP type name = 'DATE', Precision = 19
254 // SQL_VARBINARY type name = 'RAW', Precision = 255
255 // SQL_VARCHAR type name = 'VARCHAR2', Precision = 2000
256 // =====================================================================
257 // Results from a Microsoft Access 7.0 db, using a driver from Microsoft
258 //
a2115c88 259 // SQL_VARCHAR type name = 'TEXT', Precision = 255
108106cf
JS
260 // SQL_TIMESTAMP type name = 'DATETIME'
261 // SQL_DECIMAL SQL_NO_DATA_FOUND
262 // SQL_NUMERIC type name = 'CURRENCY', Precision = 19
263 // SQL_FLOAT SQL_NO_DATA_FOUND
264 // SQL_REAL type name = 'SINGLE', Precision = 7
265 // SQL_DOUBLE type name = 'DOUBLE', Precision = 15
266 // SQL_INTEGER type name = 'LONG', Precision = 10
267
268 // VARCHAR = Variable length character string
269 if (! getDataTypeInfo(SQL_VARCHAR, typeInfVarchar))
270 if (! getDataTypeInfo(SQL_CHAR, typeInfVarchar))
271 return(FALSE);
272 else
273 typeInfVarchar.FsqlType = SQL_CHAR;
274 else
275 typeInfVarchar.FsqlType = SQL_VARCHAR;
276
277 // Float
278 if (! getDataTypeInfo(SQL_DOUBLE, typeInfFloat))
279 if (! getDataTypeInfo(SQL_REAL, typeInfFloat))
280 if (! getDataTypeInfo(SQL_FLOAT, typeInfFloat))
281 if (! getDataTypeInfo(SQL_DECIMAL, typeInfFloat))
282 if (! getDataTypeInfo(SQL_NUMERIC, typeInfFloat))
283 return(FALSE);
284 else
285 typeInfFloat.FsqlType = SQL_NUMERIC;
286 else
287 typeInfFloat.FsqlType = SQL_DECIMAL;
288 else
289 typeInfFloat.FsqlType = SQL_FLOAT;
290 else
291 typeInfFloat.FsqlType = SQL_REAL;
292 else
293 typeInfFloat.FsqlType = SQL_DOUBLE;
294
295 // Integer
296 if (! getDataTypeInfo(SQL_INTEGER, typeInfInteger))
297 // If SQL_INTEGER is not supported, use the floating point
298 // data type to store integers as well as floats
299 if (! getDataTypeInfo(typeInfFloat.FsqlType, typeInfInteger))
300 return(FALSE);
301 else
302 typeInfInteger.FsqlType = typeInfFloat.FsqlType;
303 else
304 typeInfInteger.FsqlType = SQL_INTEGER;
305
306 // Date/Time
a2115c88
GT
307 if (Dbms() != dbmsDBASE)
308 {
309 if (! getDataTypeInfo(SQL_TIMESTAMP, typeInfDate))
310 return(FALSE);
311 else
312 typeInfDate.FsqlType = SQL_TIMESTAMP;
313 }
108106cf 314 else
a2115c88
GT
315 {
316 if (! getDataTypeInfo(SQL_DATE, typeInfDate))
317 return(FALSE);
318 else
319 typeInfDate.FsqlType = SQL_DATE;
320 }
108106cf 321
1fc5dd6f 322#ifdef DBDEBUG_CONSOLE
108106cf
JS
323 cout << "VARCHAR DATA TYPE: " << typeInfVarchar.TypeName << endl;
324 cout << "INTEGER DATA TYPE: " << typeInfInteger.TypeName << endl;
325 cout << "FLOAT DATA TYPE: " << typeInfFloat.TypeName << endl;
326 cout << "DATE DATA TYPE: " << typeInfDate.TypeName << endl;
327 cout << endl;
328#endif
329
330 // Completed Successfully
331 return(TRUE);
332
333} // wxDB::Open()
334
335// The Intersolv/Oracle 7 driver was "Not Capable" of setting the login timeout.
336
337/********** wxDB::setConnectionOptions() **********/
338bool wxDB::setConnectionOptions(void)
339{
340 SQLSetConnectOption(hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
341 SQLSetConnectOption(hdbc, SQL_OPT_TRACE, SQL_OPT_TRACE_OFF);
342
343 // Display the connection options to verify them
1fc5dd6f 344#ifdef DBDEBUG_CONSOLE
108106cf
JS
345 long l;
346 cout << ">>>>> CONNECTION OPTIONS <<<<<<" << endl;
347
348 if (SQLGetConnectOption(hdbc, SQL_AUTOCOMMIT, &l) != SQL_SUCCESS)
349 return(DispAllErrors(henv, hdbc));
350 cout << "AUTOCOMMIT: " << (l == SQL_AUTOCOMMIT_OFF ? "OFF" : "ON") << endl;
351
352 if (SQLGetConnectOption(hdbc, SQL_ODBC_CURSORS, &l) != SQL_SUCCESS)
353 return(DispAllErrors(henv, hdbc));
354 cout << "ODBC CURSORS: ";
355 switch(l)
356 {
357 case(SQL_CUR_USE_IF_NEEDED):
358 cout << "SQL_CUR_USE_IF_NEEDED";
359 break;
360 case(SQL_CUR_USE_ODBC):
361 cout << "SQL_CUR_USE_ODBC";
362 break;
363 case(SQL_CUR_USE_DRIVER):
364 cout << "SQL_CUR_USE_DRIVER";
365 break;
366 }
367 cout << endl;
368
369 if (SQLGetConnectOption(hdbc, SQL_OPT_TRACE, &l) != SQL_SUCCESS)
370 return(DispAllErrors(henv, hdbc));
371 cout << "TRACING: " << (l == SQL_OPT_TRACE_OFF ? "OFF" : "ON") << endl;
372
373 cout << endl;
374#endif
375
376 // Completed Successfully
377 return(TRUE);
378
379} // wxDB::setConnectionOptions()
380
381/********** wxDB::getDbInfo() **********/
382bool wxDB::getDbInfo(void)
383{
384 SWORD cb;
a2115c88 385 RETCODE retcode;
108106cf 386
a2115c88 387 if (SQLGetInfo(hdbc, SQL_SERVER_NAME, (UCHAR*) dbInf.serverName, 80, &cb) != SQL_SUCCESS)
108106cf
JS
388 return(DispAllErrors(henv, hdbc));
389
7e616b10 390 if (SQLGetInfo(hdbc, SQL_DATABASE_NAME, (UCHAR*) dbInf.databaseName, 128, &cb) != SQL_SUCCESS)
108106cf
JS
391 return(DispAllErrors(henv, hdbc));
392
7e616b10 393 if (SQLGetInfo(hdbc, SQL_DBMS_NAME, (UCHAR*) dbInf.dbmsName, 40, &cb) != SQL_SUCCESS)
108106cf
JS
394 return(DispAllErrors(henv, hdbc));
395
a2115c88
GT
396 // 16-Mar-1999
397 // After upgrading to MSVC6, the original 20 char buffer below was insufficient,
398 // causing database connectivity to fail in some cases.
399 retcode = SQLGetInfo(hdbc, SQL_DBMS_VER, (UCHAR*) dbInf.dbmsVer, 64, &cb);
400 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO )
108106cf
JS
401 return(DispAllErrors(henv, hdbc));
402
7e616b10 403 if (SQLGetInfo(hdbc, SQL_ACTIVE_CONNECTIONS, (UCHAR*) &dbInf.maxConnections, sizeof(dbInf.maxConnections), &cb) != SQL_SUCCESS)
108106cf
JS
404 return(DispAllErrors(henv, hdbc));
405
7e616b10 406 if (SQLGetInfo(hdbc, SQL_ACTIVE_STATEMENTS, (UCHAR*) &dbInf.maxStmts, sizeof(dbInf.maxStmts), &cb) != SQL_SUCCESS)
108106cf
JS
407 return(DispAllErrors(henv, hdbc));
408
7e616b10 409 if (SQLGetInfo(hdbc, SQL_DRIVER_NAME, (UCHAR*) dbInf.driverName, 40, &cb) != SQL_SUCCESS)
108106cf
JS
410 return(DispAllErrors(henv, hdbc));
411
1acd7ba6 412 if (SQLGetInfo(hdbc, SQL_DRIVER_ODBC_VER, (UCHAR*) dbInf.odbcVer, 60, &cb) == SQL_ERROR)
108106cf
JS
413 return(DispAllErrors(henv, hdbc));
414
a2115c88
GT
415 retcode = SQLGetInfo(hdbc, SQL_ODBC_VER, (UCHAR*) dbInf.drvMgrOdbcVer, 60, &cb);
416 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
108106cf
JS
417 return(DispAllErrors(henv, hdbc));
418
1acd7ba6 419 if (SQLGetInfo(hdbc, SQL_DRIVER_VER, (UCHAR*) dbInf.driverVer, 60, &cb) == SQL_ERROR)
108106cf
JS
420 return(DispAllErrors(henv, hdbc));
421
7e616b10 422 if (SQLGetInfo(hdbc, SQL_ODBC_API_CONFORMANCE, (UCHAR*) &dbInf.apiConfLvl, sizeof(dbInf.apiConfLvl), &cb) != SQL_SUCCESS)
108106cf
JS
423 return(DispAllErrors(henv, hdbc));
424
7e616b10 425 if (SQLGetInfo(hdbc, SQL_ODBC_SAG_CLI_CONFORMANCE, (UCHAR*) &dbInf.cliConfLvl, sizeof(dbInf.cliConfLvl), &cb) != SQL_SUCCESS)
108106cf
JS
426 return(DispAllErrors(henv, hdbc));
427
7e616b10 428 if (SQLGetInfo(hdbc, SQL_ODBC_SQL_CONFORMANCE, (UCHAR*) &dbInf.sqlConfLvl, sizeof(dbInf.sqlConfLvl), &cb) != SQL_SUCCESS)
108106cf
JS
429 return(DispAllErrors(henv, hdbc));
430
7e616b10 431 if (SQLGetInfo(hdbc, SQL_OUTER_JOINS, (UCHAR*) dbInf.outerJoins, 2, &cb) != SQL_SUCCESS)
108106cf
JS
432 return(DispAllErrors(henv, hdbc));
433
7e616b10 434 if (SQLGetInfo(hdbc, SQL_PROCEDURES, (UCHAR*) dbInf.procedureSupport, 2, &cb) != SQL_SUCCESS)
108106cf
JS
435 return(DispAllErrors(henv, hdbc));
436
7e616b10 437 if (SQLGetInfo(hdbc, SQL_CURSOR_COMMIT_BEHAVIOR, (UCHAR*) &dbInf.cursorCommitBehavior, sizeof(dbInf.cursorCommitBehavior), &cb) != SQL_SUCCESS)
108106cf
JS
438 return(DispAllErrors(henv, hdbc));
439
7e616b10 440 if (SQLGetInfo(hdbc, SQL_CURSOR_ROLLBACK_BEHAVIOR, (UCHAR*) &dbInf.cursorRollbackBehavior, sizeof(dbInf.cursorRollbackBehavior), &cb) != SQL_SUCCESS)
108106cf
JS
441 return(DispAllErrors(henv, hdbc));
442
7e616b10 443 if (SQLGetInfo(hdbc, SQL_NON_NULLABLE_COLUMNS, (UCHAR*) &dbInf.supportNotNullClause, sizeof(dbInf.supportNotNullClause), &cb) != SQL_SUCCESS)
108106cf
JS
444 return(DispAllErrors(henv, hdbc));
445
7e616b10 446 if (SQLGetInfo(hdbc, SQL_ODBC_SQL_OPT_IEF, (UCHAR*) dbInf.supportIEF, 2, &cb) != SQL_SUCCESS)
108106cf
JS
447 return(DispAllErrors(henv, hdbc));
448
7e616b10 449 if (SQLGetInfo(hdbc, SQL_DEFAULT_TXN_ISOLATION, (UCHAR*) &dbInf.txnIsolation, sizeof(dbInf.txnIsolation), &cb) != SQL_SUCCESS)
108106cf
JS
450 return(DispAllErrors(henv, hdbc));
451
7e616b10 452 if (SQLGetInfo(hdbc, SQL_TXN_ISOLATION_OPTION, (UCHAR*) &dbInf.txnIsolationOptions, sizeof(dbInf.txnIsolationOptions), &cb) != SQL_SUCCESS)
108106cf
JS
453 return(DispAllErrors(henv, hdbc));
454
7e616b10 455 if (SQLGetInfo(hdbc, SQL_FETCH_DIRECTION, (UCHAR*) &dbInf.fetchDirections, sizeof(dbInf.fetchDirections), &cb) != SQL_SUCCESS)
108106cf
JS
456 return(DispAllErrors(henv, hdbc));
457
7e616b10 458 if (SQLGetInfo(hdbc, SQL_LOCK_TYPES, (UCHAR*) &dbInf.lockTypes, sizeof(dbInf.lockTypes), &cb) != SQL_SUCCESS)
108106cf
JS
459 return(DispAllErrors(henv, hdbc));
460
7e616b10 461 if (SQLGetInfo(hdbc, SQL_POS_OPERATIONS, (UCHAR*) &dbInf.posOperations, sizeof(dbInf.posOperations), &cb) != SQL_SUCCESS)
108106cf
JS
462 return(DispAllErrors(henv, hdbc));
463
7e616b10 464 if (SQLGetInfo(hdbc, SQL_POSITIONED_STATEMENTS, (UCHAR*) &dbInf.posStmts, sizeof(dbInf.posStmts), &cb) != SQL_SUCCESS)
108106cf
JS
465 return(DispAllErrors(henv, hdbc));
466
7e616b10 467 if (SQLGetInfo(hdbc, SQL_SCROLL_CONCURRENCY, (UCHAR*) &dbInf.scrollConcurrency, sizeof(dbInf.scrollConcurrency), &cb) != SQL_SUCCESS)
108106cf
JS
468 return(DispAllErrors(henv, hdbc));
469
7e616b10 470 if (SQLGetInfo(hdbc, SQL_SCROLL_OPTIONS, (UCHAR*) &dbInf.scrollOptions, sizeof(dbInf.scrollOptions), &cb) != SQL_SUCCESS)
108106cf
JS
471 return(DispAllErrors(henv, hdbc));
472
7e616b10 473 if (SQLGetInfo(hdbc, SQL_STATIC_SENSITIVITY, (UCHAR*) &dbInf.staticSensitivity, sizeof(dbInf.staticSensitivity), &cb) != SQL_SUCCESS)
108106cf
JS
474 return(DispAllErrors(henv, hdbc));
475
7e616b10 476 if (SQLGetInfo(hdbc, SQL_TXN_CAPABLE, (UCHAR*) &dbInf.txnCapable, sizeof(dbInf.txnCapable), &cb) != SQL_SUCCESS)
108106cf
JS
477 return(DispAllErrors(henv, hdbc));
478
7e616b10 479 if (SQLGetInfo(hdbc, SQL_LOGIN_TIMEOUT, (UCHAR*) &dbInf.loginTimeout, sizeof(dbInf.loginTimeout), &cb) != SQL_SUCCESS)
108106cf
JS
480 return(DispAllErrors(henv, hdbc));
481
1fc5dd6f 482#ifdef DBDEBUG_CONSOLE
108106cf
JS
483 cout << ">>>>> DATA SOURCE INFORMATION <<<<<" << endl;
484 cout << "SERVER Name: " << dbInf.serverName << endl;
485 cout << "DBMS Name: " << dbInf.dbmsName << "; DBMS Version: " << dbInf.dbmsVer << endl;
486 cout << "ODBC Version: " << dbInf.odbcVer << "; Driver Version: " << dbInf.driverVer << endl;
487
488 cout << "API Conf. Level: ";
489 switch(dbInf.apiConfLvl)
490 {
491 case SQL_OAC_NONE: cout << "None"; break;
492 case SQL_OAC_LEVEL1: cout << "Level 1"; break;
493 case SQL_OAC_LEVEL2: cout << "Level 2"; break;
494 }
495 cout << endl;
496
497 cout << "SAG CLI Conf. Level: ";
498 switch(dbInf.cliConfLvl)
499 {
500 case SQL_OSCC_NOT_COMPLIANT: cout << "Not Compliant"; break;
501 case SQL_OSCC_COMPLIANT: cout << "Compliant"; break;
502 }
503 cout << endl;
504
505 cout << "SQL Conf. Level: ";
506 switch(dbInf.sqlConfLvl)
507 {
508 case SQL_OSC_MINIMUM: cout << "Minimum Grammer"; break;
509 case SQL_OSC_CORE: cout << "Core Grammer"; break;
510 case SQL_OSC_EXTENDED: cout << "Extended Grammer"; break;
511 }
512 cout << endl;
513
514 cout << "Max. Connections: " << dbInf.maxConnections << endl;
515 cout << "Outer Joins: " << dbInf.outerJoins << endl;
516 cout << "Support for Procedures: " << dbInf.procedureSupport << endl;
517
518 cout << "Cursor COMMIT Behavior: ";
519 switch(dbInf.cursorCommitBehavior)
520 {
521 case SQL_CB_DELETE: cout << "Delete cursors"; break;
522 case SQL_CB_CLOSE: cout << "Close cursors"; break;
523 case SQL_CB_PRESERVE: cout << "Preserve cursors"; break;
524 }
525 cout << endl;
526
527 cout << "Cursor ROLLBACK Behavior: ";
528 switch(dbInf.cursorRollbackBehavior)
529 {
530 case SQL_CB_DELETE: cout << "Delete cursors"; break;
531 case SQL_CB_CLOSE: cout << "Close cursors"; break;
532 case SQL_CB_PRESERVE: cout << "Preserve cursors"; break;
533 }
534 cout << endl;
535
536 cout << "Support NOT NULL clause: ";
537 switch(dbInf.supportNotNullClause)
538 {
539 case SQL_NNC_NULL: cout << "No"; break;
540 case SQL_NNC_NON_NULL: cout << "Yes"; break;
541 }
542 cout << endl;
543
544 cout << "Support IEF (Ref. Integrity): " << dbInf.supportIEF << endl;
545 cout << "Login Timeout: " << dbInf.loginTimeout << endl;
546
547 cout << endl << endl << "more ..." << endl;
548 getchar();
549
550 cout << "Default Transaction Isolation: ";
551 switch(dbInf.txnIsolation)
552 {
553 case SQL_TXN_READ_UNCOMMITTED: cout << "Read Uncommitted"; break;
554 case SQL_TXN_READ_COMMITTED: cout << "Read Committed"; break;
555 case SQL_TXN_REPEATABLE_READ: cout << "Repeatable Read"; break;
556 case SQL_TXN_SERIALIZABLE: cout << "Serializable"; break;
557#ifdef ODBC_V20
558 case SQL_TXN_VERSIONING: cout << "Versioning"; break;
559#endif
560 }
561 cout << endl;
562
563 cout << "Transaction Isolation Options: ";
564 if (dbInf.txnIsolationOptions & SQL_TXN_READ_UNCOMMITTED)
565 cout << "Read Uncommitted, ";
566 if (dbInf.txnIsolationOptions & SQL_TXN_READ_COMMITTED)
567 cout << "Read Committed, ";
568 if (dbInf.txnIsolationOptions & SQL_TXN_REPEATABLE_READ)
569 cout << "Repeatable Read, ";
570 if (dbInf.txnIsolationOptions & SQL_TXN_SERIALIZABLE)
571 cout << "Serializable, ";
572#ifdef ODBC_V20
573 if (dbInf.txnIsolationOptions & SQL_TXN_VERSIONING)
574 cout << "Versioning";
575#endif
576 cout << endl;
577
578 cout << "Fetch Directions Supported:" << endl << " ";
579 if (dbInf.fetchDirections & SQL_FD_FETCH_NEXT)
580 cout << "Next, ";
581 if (dbInf.fetchDirections & SQL_FD_FETCH_PRIOR)
582 cout << "Prev, ";
583 if (dbInf.fetchDirections & SQL_FD_FETCH_FIRST)
584 cout << "First, ";
585 if (dbInf.fetchDirections & SQL_FD_FETCH_LAST)
586 cout << "Last, ";
587 if (dbInf.fetchDirections & SQL_FD_FETCH_ABSOLUTE)
588 cout << "Absolute, ";
589 if (dbInf.fetchDirections & SQL_FD_FETCH_RELATIVE)
590 cout << "Relative, ";
591#ifdef ODBC_V20
592 if (dbInf.fetchDirections & SQL_FD_FETCH_RESUME)
593 cout << "Resume, ";
594#endif
595 if (dbInf.fetchDirections & SQL_FD_FETCH_BOOKMARK)
596 cout << "Bookmark";
597 cout << endl;
598
599 cout << "Lock Types Supported (SQLSetPos): ";
600 if (dbInf.lockTypes & SQL_LCK_NO_CHANGE)
601 cout << "No Change, ";
602 if (dbInf.lockTypes & SQL_LCK_EXCLUSIVE)
603 cout << "Exclusive, ";
604 if (dbInf.lockTypes & SQL_LCK_UNLOCK)
605 cout << "UnLock";
606 cout << endl;
607
608 cout << "Position Operations Supported (SQLSetPos): ";
609 if (dbInf.posOperations & SQL_POS_POSITION)
610 cout << "Position, ";
611 if (dbInf.posOperations & SQL_POS_REFRESH)
612 cout << "Refresh, ";
613 if (dbInf.posOperations & SQL_POS_UPDATE)
614 cout << "Upd, ";
615 if (dbInf.posOperations & SQL_POS_DELETE)
616 cout << "Del, ";
617 if (dbInf.posOperations & SQL_POS_ADD)
618 cout << "Add";
619 cout << endl;
620
621 cout << "Positioned Statements Supported: ";
622 if (dbInf.posStmts & SQL_PS_POSITIONED_DELETE)
623 cout << "Pos delete, ";
624 if (dbInf.posStmts & SQL_PS_POSITIONED_UPDATE)
625 cout << "Pos update, ";
626 if (dbInf.posStmts & SQL_PS_SELECT_FOR_UPDATE)
627 cout << "Select for update";
628 cout << endl;
629
630 cout << "Scroll Concurrency: ";
631 if (dbInf.scrollConcurrency & SQL_SCCO_READ_ONLY)
632 cout << "Read Only, ";
633 if (dbInf.scrollConcurrency & SQL_SCCO_LOCK)
634 cout << "Lock, ";
635 if (dbInf.scrollConcurrency & SQL_SCCO_OPT_ROWVER)
636 cout << "Opt. Rowver, ";
637 if (dbInf.scrollConcurrency & SQL_SCCO_OPT_VALUES)
638 cout << "Opt. Values";
639 cout << endl;
640
641 cout << "Scroll Options: ";
642 if (dbInf.scrollOptions & SQL_SO_FORWARD_ONLY)
643 cout << "Fwd Only, ";
644 if (dbInf.scrollOptions & SQL_SO_STATIC)
645 cout << "Static, ";
646 if (dbInf.scrollOptions & SQL_SO_KEYSET_DRIVEN)
647 cout << "Keyset Driven, ";
648 if (dbInf.scrollOptions & SQL_SO_DYNAMIC)
649 cout << "Dynamic, ";
650 if (dbInf.scrollOptions & SQL_SO_MIXED)
651 cout << "Mixed";
652 cout << endl;
653
654 cout << "Static Sensitivity: ";
655 if (dbInf.staticSensitivity & SQL_SS_ADDITIONS)
656 cout << "Additions, ";
657 if (dbInf.staticSensitivity & SQL_SS_DELETIONS)
658 cout << "Deletions, ";
659 if (dbInf.staticSensitivity & SQL_SS_UPDATES)
660 cout << "Updates";
661 cout << endl;
662
663 cout << "Transaction Capable?: ";
664 switch(dbInf.txnCapable)
665 {
666 case SQL_TC_NONE: cout << "No"; break;
667 case SQL_TC_DML: cout << "DML Only"; break;
668 case SQL_TC_DDL_COMMIT: cout << "DDL Commit"; break;
669 case SQL_TC_DDL_IGNORE: cout << "DDL Ignore"; break;
670 case SQL_TC_ALL: cout << "DDL & DML"; break;
671 }
672 cout << endl;
673
674 cout << endl;
675
676#endif
677
678 // Completed Successfully
679 return(TRUE);
680
681} // wxDB::getDbInfo()
682
683/********** wxDB::getDataTypeInfo() **********/
684bool wxDB::getDataTypeInfo(SWORD fSqlType, SqlTypeInfo &structSQLTypeInfo)
685{
686 // fSqlType will be something like SQL_VARCHAR. This parameter determines
687 // the data type inf. is gathered for.
688 //
689 // SqlTypeInfo is a structure that is filled in with data type information,
690
691 RETCODE retcode;
692 SDWORD cbRet;
693
694 // Get information about the data type specified
695 if (SQLGetTypeInfo(hstmt, fSqlType) != SQL_SUCCESS)
696 return(DispAllErrors(henv, hdbc, hstmt));
697 // Fetch the record
698 if ((retcode = SQLFetch(hstmt)) != SQL_SUCCESS)
699 {
1fc5dd6f 700#ifdef DBDEBUG_CONSOLE
108106cf
JS
701 if (retcode == SQL_NO_DATA_FOUND)
702 cout << "SQL_NO_DATA_FOUND fetching inf. about data type." << endl;
703#endif
704 DispAllErrors(henv, hdbc, hstmt);
705 SQLFreeStmt(hstmt, SQL_CLOSE);
706 return(FALSE);
707 }
708 // Obtain columns from the record
7e616b10 709 if (SQLGetData(hstmt, 1, SQL_C_CHAR, (UCHAR*) structSQLTypeInfo.TypeName, DB_TYPE_NAME_LEN, &cbRet) != SQL_SUCCESS)
108106cf 710 return(DispAllErrors(henv, hdbc, hstmt));
7e616b10 711 if (SQLGetData(hstmt, 3, SQL_C_LONG, (UCHAR*) &structSQLTypeInfo.Precision, 0, &cbRet) != SQL_SUCCESS)
108106cf 712 return(DispAllErrors(henv, hdbc, hstmt));
7e616b10 713 if (SQLGetData(hstmt, 8, SQL_C_SHORT, (UCHAR*) &structSQLTypeInfo.CaseSensitive, 0, &cbRet) != SQL_SUCCESS)
108106cf 714 return(DispAllErrors(henv, hdbc, hstmt));
7e616b10 715// if (SQLGetData(hstmt, 14, SQL_C_SHORT, (UCHAR*) &structSQLTypeInfo.MinimumScale, 0, &cbRet) != SQL_SUCCESS)
108106cf 716// return(DispAllErrors(henv, hdbc, hstmt));
a2115c88
GT
717
718//#ifdef __UNIX__ // BJO : IODBC knows about 5, not 15...
719// if (SQLGetData(hstmt, 5, SQL_C_SHORT,(UCHAR*) &structSQLTypeInfo.MaximumScale, 0, &cbRet) != SQL_SUCCESS)
720// return(DispAllErrors(henv, hdbc, hstmt));
721//#else
722 if (SQLGetData(hstmt, 15, SQL_C_SHORT,(UCHAR*) &structSQLTypeInfo.MaximumScale, 0, &cbRet) != SQL_SUCCESS)
108106cf 723 return(DispAllErrors(henv, hdbc, hstmt));
a2115c88 724//#endif
108106cf
JS
725
726 if (structSQLTypeInfo.MaximumScale < 0)
727 structSQLTypeInfo.MaximumScale = 0;
728
729 // Close the statement handle which closes open cursors
730 if (SQLFreeStmt(hstmt, SQL_CLOSE) != SQL_SUCCESS)
731 return(DispAllErrors(henv, hdbc, hstmt));
732
733 // Completed Successfully
734 return(TRUE);
735
736} // wxDB::getDataTypeInfo()
737
738/********** wxDB::Close() **********/
739void wxDB::Close(void)
740{
1fc5dd6f
JS
741 // Close the Sql Log file
742 if (fpSqlLog)
743 {
744 fclose(fpSqlLog);
a2115c88 745 fpSqlLog = 0;
1fc5dd6f
JS
746 }
747
108106cf
JS
748 // Free statement handle
749 if (dbIsOpen)
750 {
751 if (SQLFreeStmt(hstmt, SQL_DROP) != SQL_SUCCESS)
752 DispAllErrors(henv, hdbc);
753 }
754
755 // Disconnect from the datasource
756 if (SQLDisconnect(hdbc) != SQL_SUCCESS)
757 DispAllErrors(henv, hdbc);
758
759 // Free the connection to the datasource
760 if (SQLFreeConnect(hdbc) != SQL_SUCCESS)
761 DispAllErrors(henv, hdbc);
762
a2115c88
GT
763 // There should be zero Ctable objects still connected to this db object
764 assert(nTables == 0);
765
e041ce57 766#ifdef __WXDEBUG__
a2115c88
GT
767 CstructTablesInUse *tiu;
768 wxNode *pNode;
769 pNode = TablesInUse.First();
770 char s[80];
771 char s2[80];
772 while (pNode)
773 {
774 tiu = (CstructTablesInUse *)pNode->Data();
775 if (tiu->pDb == this)
776 {
12234741
VZ
777 sprintf(s, "(%-20s) tableID:[%6lu] pDb:[%p]", tiu->tableName,tiu->tableID,tiu->pDb);
778 sprintf(s2,"Orphaned found using pDb:[%p]",this);
a2115c88
GT
779 wxMessageBox (s,s2);
780 }
781 pNode = pNode->Next();
782 }
783#endif
784
785 // Copy the error messages to a global variable
9c40a88d
GT
786 int i;
787 for (i = 0; i < DB_MAX_ERROR_HISTORY; i++)
e8af3814 788 wxStrcpy(DBerrorList[i],errorList[i]);
a2115c88 789
108106cf
JS
790} // wxDB::Close()
791
792/********** wxDB::CommitTrans() **********/
793bool wxDB::CommitTrans(void)
794{
a2115c88
GT
795 if (this)
796 {
797 // Commit the transaction
798 if (SQLTransact(henv, hdbc, SQL_COMMIT) != SQL_SUCCESS)
799 return(DispAllErrors(henv, hdbc));
800 }
108106cf
JS
801
802 // Completed successfully
803 return(TRUE);
804
805} // wxDB::CommitTrans()
806
807/********** wxDB::RollbackTrans() **********/
808bool wxDB::RollbackTrans(void)
809{
810 // Rollback the transaction
811 if (SQLTransact(henv, hdbc, SQL_ROLLBACK) != SQL_SUCCESS)
812 return(DispAllErrors(henv, hdbc));
813
814 // Completed successfully
815 return(TRUE);
816
817} // wxDB::RollbackTrans()
818
819/********** wxDB::DispAllErrors() **********/
820bool wxDB::DispAllErrors(HENV aHenv, HDBC aHdbc, HSTMT aHstmt)
821{
822 char odbcErrMsg[DB_MAX_ERROR_MSG_LEN];
823
824 while (SQLError(aHenv, aHdbc, aHstmt, (UCHAR FAR *) sqlState, &nativeError, (UCHAR FAR *) errorMsg, SQL_MAX_MESSAGE_LENGTH - 1, &cbErrorMsg) == SQL_SUCCESS)
825 {
826 sprintf(odbcErrMsg, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState, nativeError, errorMsg);
827 logError(odbcErrMsg, sqlState);
828 if (!silent)
829 {
1fc5dd6f 830#ifdef DBDEBUG_CONSOLE
108106cf
JS
831 // When run in console mode, use standard out to display errors.
832 cout << odbcErrMsg << endl;
833 cout << "Press any key to continue..." << endl;
834 getchar();
835#endif
836 }
a2115c88
GT
837
838#ifdef __WXDEBUG__
839 wxMessageBox(odbcErrMsg);
840#endif
108106cf
JS
841 }
842
a2115c88 843 return(FALSE); // This function always returns false.
108106cf
JS
844
845} // wxDB::DispAllErrors()
846
847/********** wxDB::GetNextError() **********/
848bool wxDB::GetNextError(HENV aHenv, HDBC aHdbc, HSTMT aHstmt)
849{
850 if (SQLError(aHenv, aHdbc, aHstmt, (UCHAR FAR *) sqlState, &nativeError, (UCHAR FAR *) errorMsg, SQL_MAX_MESSAGE_LENGTH - 1, &cbErrorMsg) == SQL_SUCCESS)
851 return(TRUE);
852 else
853 return(FALSE);
854
855} // wxDB::GetNextError()
856
857/********** wxDB::DispNextError() **********/
858void wxDB::DispNextError(void)
859{
860 char odbcErrMsg[DB_MAX_ERROR_MSG_LEN];
861
862 sprintf(odbcErrMsg, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState, nativeError, errorMsg);
863 logError(odbcErrMsg, sqlState);
864
865 if (silent)
866 return;
867
1fc5dd6f 868#ifdef DBDEBUG_CONSOLE
108106cf
JS
869 // When run in console mode, use standard out to display errors.
870 cout << odbcErrMsg << endl;
871 cout << "Press any key to continue..." << endl;
872 getchar();
873#endif
874
875} // wxDB::DispNextError()
876
877/********** wxDB::logError() **********/
6919c53f 878void wxDB::logError(const char *errMsg, const char *SQLState)
108106cf 879{
e8af3814 880 assert(errMsg && wxStrlen(errMsg));
108106cf
JS
881
882 static int pLast = -1;
883 int dbStatus;
884
885 if (++pLast == DB_MAX_ERROR_HISTORY)
886 {
a2115c88
GT
887 int i;
888 for (i = 0; i < DB_MAX_ERROR_HISTORY; i++)
e8af3814 889 wxStrcpy(errorList[i], errorList[i+1]);
108106cf
JS
890 pLast--;
891 }
892
e8af3814 893 wxStrcpy(errorList[pLast], errMsg);
108106cf 894
e8af3814 895 if (SQLState && wxStrlen(SQLState))
108106cf
JS
896 if ((dbStatus = TranslateSqlState(SQLState)) != DB_ERR_FUNCTION_SEQUENCE_ERROR)
897 DB_STATUS = dbStatus;
898
a2115c88
GT
899 // Add the errmsg to the sql log
900 WriteSqlLog(errMsg);
901
108106cf
JS
902} // wxDB::logError()
903
904/**********wxDB::TranslateSqlState() **********/
6919c53f 905int wxDB::TranslateSqlState(const char *SQLState)
108106cf 906{
9199e66f 907 if (!wxStrcmp(SQLState, "01000"))
108106cf 908 return(DB_ERR_GENERAL_WARNING);
9199e66f 909 if (!wxStrcmp(SQLState, "01002"))
108106cf 910 return(DB_ERR_DISCONNECT_ERROR);
9199e66f 911 if (!wxStrcmp(SQLState, "01004"))
108106cf 912 return(DB_ERR_DATA_TRUNCATED);
9199e66f 913 if (!wxStrcmp(SQLState, "01006"))
108106cf 914 return(DB_ERR_PRIV_NOT_REVOKED);
9199e66f 915 if (!wxStrcmp(SQLState, "01S00"))
108106cf 916 return(DB_ERR_INVALID_CONN_STR_ATTR);
9199e66f 917 if (!wxStrcmp(SQLState, "01S01"))
108106cf 918 return(DB_ERR_ERROR_IN_ROW);
9199e66f 919 if (!wxStrcmp(SQLState, "01S02"))
108106cf 920 return(DB_ERR_OPTION_VALUE_CHANGED);
9199e66f 921 if (!wxStrcmp(SQLState, "01S03"))
108106cf 922 return(DB_ERR_NO_ROWS_UPD_OR_DEL);
9199e66f 923 if (!wxStrcmp(SQLState, "01S04"))
108106cf 924 return(DB_ERR_MULTI_ROWS_UPD_OR_DEL);
9199e66f 925 if (!wxStrcmp(SQLState, "07001"))
108106cf 926 return(DB_ERR_WRONG_NO_OF_PARAMS);
9199e66f 927 if (!wxStrcmp(SQLState, "07006"))
108106cf 928 return(DB_ERR_DATA_TYPE_ATTR_VIOL);
9199e66f 929 if (!wxStrcmp(SQLState, "08001"))
108106cf 930 return(DB_ERR_UNABLE_TO_CONNECT);
9199e66f 931 if (!wxStrcmp(SQLState, "08002"))
108106cf 932 return(DB_ERR_CONNECTION_IN_USE);
9199e66f 933 if (!wxStrcmp(SQLState, "08003"))
108106cf 934 return(DB_ERR_CONNECTION_NOT_OPEN);
9199e66f 935 if (!wxStrcmp(SQLState, "08004"))
108106cf 936 return(DB_ERR_REJECTED_CONNECTION);
9199e66f 937 if (!wxStrcmp(SQLState, "08007"))
108106cf 938 return(DB_ERR_CONN_FAIL_IN_TRANS);
9199e66f 939 if (!wxStrcmp(SQLState, "08S01"))
108106cf 940 return(DB_ERR_COMM_LINK_FAILURE);
9199e66f 941 if (!wxStrcmp(SQLState, "21S01"))
108106cf 942 return(DB_ERR_INSERT_VALUE_LIST_MISMATCH);
9199e66f 943 if (!wxStrcmp(SQLState, "21S02"))
108106cf 944 return(DB_ERR_DERIVED_TABLE_MISMATCH);
9199e66f 945 if (!wxStrcmp(SQLState, "22001"))
108106cf 946 return(DB_ERR_STRING_RIGHT_TRUNC);
9199e66f 947 if (!wxStrcmp(SQLState, "22003"))
108106cf 948 return(DB_ERR_NUMERIC_VALUE_OUT_OF_RNG);
9199e66f 949 if (!wxStrcmp(SQLState, "22005"))
108106cf 950 return(DB_ERR_ERROR_IN_ASSIGNMENT);
9199e66f 951 if (!wxStrcmp(SQLState, "22008"))
108106cf 952 return(DB_ERR_DATETIME_FLD_OVERFLOW);
9199e66f 953 if (!wxStrcmp(SQLState, "22012"))
108106cf 954 return(DB_ERR_DIVIDE_BY_ZERO);
9199e66f 955 if (!wxStrcmp(SQLState, "22026"))
108106cf 956 return(DB_ERR_STR_DATA_LENGTH_MISMATCH);
9199e66f 957 if (!wxStrcmp(SQLState, "23000"))
108106cf 958 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL);
9199e66f 959 if (!wxStrcmp(SQLState, "24000"))
108106cf 960 return(DB_ERR_INVALID_CURSOR_STATE);
9199e66f 961 if (!wxStrcmp(SQLState, "25000"))
108106cf 962 return(DB_ERR_INVALID_TRANS_STATE);
9199e66f 963 if (!wxStrcmp(SQLState, "28000"))
108106cf 964 return(DB_ERR_INVALID_AUTH_SPEC);
9199e66f 965 if (!wxStrcmp(SQLState, "34000"))
108106cf 966 return(DB_ERR_INVALID_CURSOR_NAME);
9199e66f 967 if (!wxStrcmp(SQLState, "37000"))
108106cf 968 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL);
9199e66f 969 if (!wxStrcmp(SQLState, "3C000"))
108106cf 970 return(DB_ERR_DUPLICATE_CURSOR_NAME);
9199e66f 971 if (!wxStrcmp(SQLState, "40001"))
108106cf 972 return(DB_ERR_SERIALIZATION_FAILURE);
9199e66f 973 if (!wxStrcmp(SQLState, "42000"))
108106cf 974 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2);
9199e66f 975 if (!wxStrcmp(SQLState, "70100"))
108106cf 976 return(DB_ERR_OPERATION_ABORTED);
9199e66f 977 if (!wxStrcmp(SQLState, "IM001"))
108106cf 978 return(DB_ERR_UNSUPPORTED_FUNCTION);
9199e66f 979 if (!wxStrcmp(SQLState, "IM002"))
108106cf 980 return(DB_ERR_NO_DATA_SOURCE);
9199e66f 981 if (!wxStrcmp(SQLState, "IM003"))
108106cf 982 return(DB_ERR_DRIVER_LOAD_ERROR);
9199e66f 983 if (!wxStrcmp(SQLState, "IM004"))
108106cf 984 return(DB_ERR_SQLALLOCENV_FAILED);
9199e66f 985 if (!wxStrcmp(SQLState, "IM005"))
108106cf 986 return(DB_ERR_SQLALLOCCONNECT_FAILED);
9199e66f 987 if (!wxStrcmp(SQLState, "IM006"))
108106cf 988 return(DB_ERR_SQLSETCONNECTOPTION_FAILED);
9199e66f 989 if (!wxStrcmp(SQLState, "IM007"))
108106cf 990 return(DB_ERR_NO_DATA_SOURCE_DLG_PROHIB);
9199e66f 991 if (!wxStrcmp(SQLState, "IM008"))
108106cf 992 return(DB_ERR_DIALOG_FAILED);
9199e66f 993 if (!wxStrcmp(SQLState, "IM009"))
108106cf 994 return(DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL);
9199e66f 995 if (!wxStrcmp(SQLState, "IM010"))
108106cf 996 return(DB_ERR_DATA_SOURCE_NAME_TOO_LONG);
9199e66f 997 if (!wxStrcmp(SQLState, "IM011"))
108106cf 998 return(DB_ERR_DRIVER_NAME_TOO_LONG);
9199e66f 999 if (!wxStrcmp(SQLState, "IM012"))
108106cf 1000 return(DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR);
9199e66f 1001 if (!wxStrcmp(SQLState, "IM013"))
108106cf 1002 return(DB_ERR_TRACE_FILE_ERROR);
9199e66f 1003 if (!wxStrcmp(SQLState, "S0001"))
108106cf 1004 return(DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS);
9199e66f 1005 if (!wxStrcmp(SQLState, "S0002"))
108106cf 1006 return(DB_ERR_TABLE_NOT_FOUND);
9199e66f 1007 if (!wxStrcmp(SQLState, "S0011"))
108106cf 1008 return(DB_ERR_INDEX_ALREADY_EXISTS);
9199e66f 1009 if (!wxStrcmp(SQLState, "S0012"))
108106cf 1010 return(DB_ERR_INDEX_NOT_FOUND);
9199e66f 1011 if (!wxStrcmp(SQLState, "S0021"))
108106cf 1012 return(DB_ERR_COLUMN_ALREADY_EXISTS);
9199e66f 1013 if (!wxStrcmp(SQLState, "S0022"))
108106cf 1014 return(DB_ERR_COLUMN_NOT_FOUND);
9199e66f 1015 if (!wxStrcmp(SQLState, "S0023"))
108106cf 1016 return(DB_ERR_NO_DEFAULT_FOR_COLUMN);
9199e66f 1017 if (!wxStrcmp(SQLState, "S1000"))
108106cf 1018 return(DB_ERR_GENERAL_ERROR);
9199e66f 1019 if (!wxStrcmp(SQLState, "S1001"))
108106cf 1020 return(DB_ERR_MEMORY_ALLOCATION_FAILURE);
9199e66f 1021 if (!wxStrcmp(SQLState, "S1002"))
108106cf 1022 return(DB_ERR_INVALID_COLUMN_NUMBER);
9199e66f 1023 if (!wxStrcmp(SQLState, "S1003"))
108106cf 1024 return(DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE);
9199e66f 1025 if (!wxStrcmp(SQLState, "S1004"))
108106cf 1026 return(DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE);
9199e66f 1027 if (!wxStrcmp(SQLState, "S1008"))
108106cf 1028 return(DB_ERR_OPERATION_CANCELLED);
9199e66f 1029 if (!wxStrcmp(SQLState, "S1009"))
108106cf 1030 return(DB_ERR_INVALID_ARGUMENT_VALUE);
9199e66f 1031 if (!wxStrcmp(SQLState, "S1010"))
108106cf 1032 return(DB_ERR_FUNCTION_SEQUENCE_ERROR);
9199e66f 1033 if (!wxStrcmp(SQLState, "S1011"))
108106cf 1034 return(DB_ERR_OPERATION_INVALID_AT_THIS_TIME);
9199e66f 1035 if (!wxStrcmp(SQLState, "S1012"))
108106cf 1036 return(DB_ERR_INVALID_TRANS_OPERATION_CODE);
9199e66f 1037 if (!wxStrcmp(SQLState, "S1015"))
108106cf 1038 return(DB_ERR_NO_CURSOR_NAME_AVAIL);
9199e66f 1039 if (!wxStrcmp(SQLState, "S1090"))
108106cf 1040 return(DB_ERR_INVALID_STR_OR_BUF_LEN);
9199e66f 1041 if (!wxStrcmp(SQLState, "S1091"))
108106cf 1042 return(DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE);
9199e66f 1043 if (!wxStrcmp(SQLState, "S1092"))
108106cf 1044 return(DB_ERR_OPTION_TYPE_OUT_OF_RANGE);
9199e66f 1045 if (!wxStrcmp(SQLState, "S1093"))
108106cf 1046 return(DB_ERR_INVALID_PARAM_NO);
9199e66f 1047 if (!wxStrcmp(SQLState, "S1094"))
108106cf 1048 return(DB_ERR_INVALID_SCALE_VALUE);
9199e66f 1049 if (!wxStrcmp(SQLState, "S1095"))
108106cf 1050 return(DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE);
9199e66f 1051 if (!wxStrcmp(SQLState, "S1096"))
108106cf 1052 return(DB_ERR_INF_TYPE_OUT_OF_RANGE);
9199e66f 1053 if (!wxStrcmp(SQLState, "S1097"))
108106cf 1054 return(DB_ERR_COLUMN_TYPE_OUT_OF_RANGE);
9199e66f 1055 if (!wxStrcmp(SQLState, "S1098"))
108106cf 1056 return(DB_ERR_SCOPE_TYPE_OUT_OF_RANGE);
9199e66f 1057 if (!wxStrcmp(SQLState, "S1099"))
108106cf 1058 return(DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE);
9199e66f 1059 if (!wxStrcmp(SQLState, "S1100"))
108106cf 1060 return(DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE);
9199e66f 1061 if (!wxStrcmp(SQLState, "S1101"))
108106cf 1062 return(DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE);
9199e66f 1063 if (!wxStrcmp(SQLState, "S1103"))
108106cf 1064 return(DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE);
9199e66f 1065 if (!wxStrcmp(SQLState, "S1104"))
108106cf 1066 return(DB_ERR_INVALID_PRECISION_VALUE);
9199e66f 1067 if (!wxStrcmp(SQLState, "S1105"))
108106cf 1068 return(DB_ERR_INVALID_PARAM_TYPE);
9199e66f 1069 if (!wxStrcmp(SQLState, "S1106"))
108106cf 1070 return(DB_ERR_FETCH_TYPE_OUT_OF_RANGE);
9199e66f 1071 if (!wxStrcmp(SQLState, "S1107"))
108106cf 1072 return(DB_ERR_ROW_VALUE_OUT_OF_RANGE);
9199e66f 1073 if (!wxStrcmp(SQLState, "S1108"))
108106cf 1074 return(DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE);
9199e66f 1075 if (!wxStrcmp(SQLState, "S1109"))
108106cf 1076 return(DB_ERR_INVALID_CURSOR_POSITION);
9199e66f 1077 if (!wxStrcmp(SQLState, "S1110"))
108106cf 1078 return(DB_ERR_INVALID_DRIVER_COMPLETION);
9199e66f 1079 if (!wxStrcmp(SQLState, "S1111"))
108106cf 1080 return(DB_ERR_INVALID_BOOKMARK_VALUE);
9199e66f 1081 if (!wxStrcmp(SQLState, "S1C00"))
108106cf 1082 return(DB_ERR_DRIVER_NOT_CAPABLE);
9199e66f 1083 if (!wxStrcmp(SQLState, "S1T00"))
108106cf
JS
1084 return(DB_ERR_TIMEOUT_EXPIRED);
1085
1086 // No match
1087 return(0);
1088
1089} // wxDB::TranslateSqlState()
1090
1091/********** wxDB::Grant() **********/
6919c53f 1092bool wxDB::Grant(int privileges, const char *tableName, const char *userList)
108106cf
JS
1093{
1094 char sqlStmt[DB_MAX_STATEMENT_LEN];
1095
1096 // Build the grant statement
e8af3814 1097 wxStrcpy(sqlStmt, "GRANT ");
108106cf 1098 if (privileges == DB_GRANT_ALL)
e8af3814 1099 wxStrcat(sqlStmt, "ALL");
108106cf
JS
1100 else
1101 {
1102 int c = 0;
1103 if (privileges & DB_GRANT_SELECT)
1104 {
e8af3814 1105 wxStrcat(sqlStmt, "SELECT");
108106cf
JS
1106 c++;
1107 }
1108 if (privileges & DB_GRANT_INSERT)
1109 {
1110 if (c++)
e8af3814
GT
1111 wxStrcat(sqlStmt, ", ");
1112 wxStrcat(sqlStmt, "INSERT");
108106cf
JS
1113 }
1114 if (privileges & DB_GRANT_UPDATE)
1115 {
1116 if (c++)
e8af3814
GT
1117 wxStrcat(sqlStmt, ", ");
1118 wxStrcat(sqlStmt, "UPDATE");
108106cf
JS
1119 }
1120 if (privileges & DB_GRANT_DELETE)
1121 {
1122 if (c++)
e8af3814
GT
1123 wxStrcat(sqlStmt, ", ");
1124 wxStrcat(sqlStmt, "DELETE");
108106cf
JS
1125 }
1126 }
1127
e8af3814
GT
1128 wxStrcat(sqlStmt, " ON ");
1129 wxStrcat(sqlStmt, tableName);
1130 wxStrcat(sqlStmt, " TO ");
1131 wxStrcat(sqlStmt, userList);
108106cf 1132
1fc5dd6f 1133#ifdef DBDEBUG_CONSOLE
108106cf
JS
1134 cout << endl << sqlStmt << endl;
1135#endif
1136
1fc5dd6f
JS
1137 WriteSqlLog(sqlStmt);
1138
108106cf
JS
1139 return(ExecSql(sqlStmt));
1140
1141} // wxDB::Grant()
1142
1143/********** wxDB::CreateView() **********/
6919c53f 1144bool wxDB::CreateView(const char *viewName, const char *colList, const char *pSqlStmt, bool attemptDrop)
108106cf
JS
1145{
1146 char sqlStmt[DB_MAX_STATEMENT_LEN];
1147
1148 // Drop the view first
a2115c88
GT
1149 if (attemptDrop && !DropView(viewName))
1150 return FALSE;
108106cf
JS
1151
1152 // Build the create view statement
e8af3814
GT
1153 wxStrcpy(sqlStmt, "CREATE VIEW ");
1154 wxStrcat(sqlStmt, viewName);
108106cf 1155
e8af3814 1156 if (wxStrlen(colList))
108106cf 1157 {
e8af3814
GT
1158 wxStrcat(sqlStmt, " (");
1159 wxStrcat(sqlStmt, colList);
1160 wxStrcat(sqlStmt, ")");
108106cf
JS
1161 }
1162
e8af3814
GT
1163 wxStrcat(sqlStmt, " AS ");
1164 wxStrcat(sqlStmt, pSqlStmt);
108106cf 1165
1fc5dd6f
JS
1166 WriteSqlLog(sqlStmt);
1167
1168#ifdef DBDEBUG_CONSOLE
108106cf
JS
1169 cout << sqlStmt << endl;
1170#endif
1171
1172 return(ExecSql(sqlStmt));
1173
1174} // wxDB::CreateView()
1175
a2115c88 1176/********** wxDB::DropView() **********/
6919c53f 1177bool wxDB::DropView(const char *viewName)
a2115c88
GT
1178{
1179 // NOTE: This function returns TRUE if the View does not exist, but
1180 // only for identified databases. Code will need to be added
1181 // below for any other databases when those databases are defined
1182 // to handle this situation consistently
1183
1184 char sqlStmt[DB_MAX_STATEMENT_LEN];
1185
1186 sprintf(sqlStmt, "DROP VIEW %s", viewName);
1187
1188 WriteSqlLog(sqlStmt);
1189
1190#ifdef DBDEBUG_CONSOLE
1191 cout << endl << sqlStmt << endl;
1192#endif
1193
1194 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
1195 {
1196 // Check for "Base table not found" error and ignore
1197 GetNextError(henv, hdbc, hstmt);
9199e66f 1198 if (wxStrcmp(sqlState,"S0002")) // "Base table not found"
a2115c88
GT
1199 {
1200 // Check for product specific error codes
9199e66f 1201 if (!((Dbms() == dbmsSYBASE_ASA && !wxStrcmp(sqlState,"42000")))) // 5.x (and lower?)
a2115c88
GT
1202 {
1203 DispNextError();
1204 DispAllErrors(henv, hdbc, hstmt);
1205 RollbackTrans();
1206 return(FALSE);
1207 }
1208 }
1209 }
1210
1211 // Commit the transaction
1212 if (! CommitTrans())
1213 return(FALSE);
1214
1215 return TRUE;
1216
1217} // wxDB::DropView()
1218
1219
108106cf 1220/********** wxDB::ExecSql() **********/
6919c53f 1221bool wxDB::ExecSql(const char *pSqlStmt)
108106cf 1222{
a2115c88 1223 SQLFreeStmt(hstmt, SQL_CLOSE);
108106cf
JS
1224 if (SQLExecDirect(hstmt, (UCHAR FAR *) pSqlStmt, SQL_NTS) == SQL_SUCCESS)
1225 return(TRUE);
1226 else
1227 {
1228 DispAllErrors(henv, hdbc, hstmt);
1229 return(FALSE);
1230 }
1231
1232} // wxDB::ExecSql()
1233
a2115c88
GT
1234/********** wxDB::GetNext() **********/
1235bool wxDB::GetNext(void)
1236{
1237 if (SQLFetch(hstmt) == SQL_SUCCESS)
1238 return(TRUE);
1239 else
1240 {
1241 DispAllErrors(henv, hdbc, hstmt);
1242 return(FALSE);
1243 }
1244
1245} // wxDB::GetNext()
1246
1247/********** wxDB::GetData() **********/
1248bool wxDB::GetData(UWORD colNo, SWORD cType, PTR pData, SDWORD maxLen, SDWORD FAR *cbReturned)
1249{
1250 assert(pData);
1251 assert(cbReturned);
1252
1253 if (SQLGetData(hstmt, colNo, cType, pData, maxLen, cbReturned) == SQL_SUCCESS)
1254 return(TRUE);
1255 else
1256 {
1257 DispAllErrors(henv, hdbc, hstmt);
1258 return(FALSE);
1259 }
1260
1261} // wxDB::GetData()
1262
108106cf
JS
1263/********** wxDB::GetColumns() **********/
1264/*
1265 * 1) The last array element of the tableName[] argument must be zero (null).
1266 * This is how the end of the array is detected.
1267 * 2) This function returns an array of CcolInf structures. If no columns
1268 * were found, or an error occured, this pointer will be zero (null). THE
1269 * CALLING FUNCTION IS RESPONSIBLE FOR DELETING THE MEMORY RETURNED WHEN IT
1270 * IS FINISHED WITH IT. i.e.
1271 *
a2115c88 1272 * CcolInf *colInf = pDb->GetColumns(tableList, userID);
108106cf
JS
1273 * if (colInf)
1274 * {
1275 * // Use the column inf
1276 * .......
1277 * // Destroy the memory
1278 * delete [] colInf;
1279 * }
1280 */
6919c53f 1281CcolInf *wxDB::GetColumns(char *tableName[], const char *userID)
108106cf 1282{
6919c53f
GT
1283 UINT noCols = 0;
1284 UINT colNo = 0;
108106cf 1285 CcolInf *colInf = 0;
6919c53f
GT
1286
1287 RETCODE retcode;
1288 SDWORD cb;
1289 char tblName[DB_MAX_TABLE_NAME_LEN+1];
1290 char colName[DB_MAX_COLUMN_NAME_LEN+1];
1291 SWORD sqlDataType;
1292
1293 wxString UserID;
1294 wxString TableName;
a2115c88 1295
e8af3814 1296 if (!userID || !wxStrlen(userID))
6919c53f
GT
1297 UserID = uid;
1298 else
1299 UserID = userID;
a2115c88
GT
1300
1301 // dBase does not use user names, and some drivers fail if you try to pass one
1302 if (Dbms() == dbmsDBASE)
6919c53f 1303 UserID = "";
a2115c88
GT
1304
1305 // Oracle user names may only be in uppercase, so force
1306 // the name to uppercase
1307 if (Dbms() == dbmsORACLE)
6919c53f 1308 UserID = UserID.Upper();
108106cf
JS
1309
1310 // Pass 1 - Determine how many columns there are.
1311 // Pass 2 - Allocate the CcolInf array and fill in
1312 // the array with the column information.
a2115c88
GT
1313 int pass;
1314 for (pass = 1; pass <= 2; pass++)
108106cf
JS
1315 {
1316 if (pass == 2)
1317 {
1318 if (noCols == 0) // Probably a bogus table name(s)
1319 break;
1320 // Allocate n CcolInf objects to hold the column information
1321 colInf = new CcolInf[noCols+1];
1322 if (!colInf)
1323 break;
1324 // Mark the end of the array
e8af3814
GT
1325 wxStrcpy(colInf[noCols].tableName, "");
1326 wxStrcpy(colInf[noCols].colName, "");
108106cf
JS
1327 colInf[noCols].sqlDataType = 0;
1328 }
1329 // Loop through each table name
a2115c88
GT
1330 int tbl;
1331 for (tbl = 0; tableName[tbl]; tbl++)
108106cf 1332 {
6919c53f 1333 TableName = tableName[tbl];
a2115c88
GT
1334 // Oracle table names are uppercase only, so force
1335 // the name to uppercase just in case programmer forgot to do this
1336 if (Dbms() == dbmsORACLE)
6919c53f 1337 TableName = TableName.Upper();
a2115c88 1338
108106cf 1339 SQLFreeStmt(hstmt, SQL_CLOSE);
a2115c88
GT
1340
1341 // MySQL and Access cannot accept a user name when looking up column names, so we
1342 // use the call below that leaves out the user name
6919c53f 1343 if (wxStrcmp(UserID.GetData(),"") &&
a2115c88
GT
1344 Dbms() != dbmsMY_SQL &&
1345 Dbms() != dbmsACCESS)
1346 {
1347 retcode = SQLColumns(hstmt,
1348 NULL, 0, // All qualifiers
6919c53f
GT
1349 (UCHAR *) UserID.GetData(), SQL_NTS, // Owner
1350 (UCHAR *) TableName.GetData(), SQL_NTS,
a2115c88
GT
1351 NULL, 0); // All columns
1352 }
1353 else
1354 {
1355 retcode = SQLColumns(hstmt,
1356 NULL, 0, // All qualifiers
1357 NULL, 0, // Owner
6919c53f 1358 (UCHAR *) TableName.GetData(), SQL_NTS,
a2115c88
GT
1359 NULL, 0); // All columns
1360 }
108106cf
JS
1361 if (retcode != SQL_SUCCESS)
1362 { // Error occured, abort
1363 DispAllErrors(henv, hdbc, hstmt);
1364 if (colInf)
1365 delete [] colInf;
a2115c88
GT
1366 SQLFreeStmt(hstmt, SQL_UNBIND);
1367 SQLFreeStmt(hstmt, SQL_CLOSE);
108106cf
JS
1368 return(0);
1369 }
6919c53f
GT
1370 SQLBindCol(hstmt, 3, SQL_C_CHAR, (UCHAR*) tblName, DB_MAX_TABLE_NAME_LEN+1, &cb);
1371 SQLBindCol(hstmt, 4, SQL_C_CHAR, (UCHAR*) colName, DB_MAX_COLUMN_NAME_LEN+1, &cb);
7e616b10 1372 SQLBindCol(hstmt, 5, SQL_C_SSHORT, (UCHAR*) &sqlDataType, 0, &cb);
108106cf
JS
1373 while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS)
1374 {
1375 if (pass == 1) // First pass, just add up the number of columns
1376 noCols++;
1377 else // Pass 2; Fill in the array of structures
1378 {
1379 if (colNo < noCols) // Some extra error checking to prevent memory overwrites
1380 {
e8af3814
GT
1381 wxStrcpy(colInf[colNo].tableName, tblName);
1382 wxStrcpy(colInf[colNo].colName, colName);
108106cf
JS
1383 colInf[colNo].sqlDataType = sqlDataType;
1384 colNo++;
1385 }
1386 }
1387 }
1388 if (retcode != SQL_NO_DATA_FOUND)
1389 { // Error occured, abort
1390 DispAllErrors(henv, hdbc, hstmt);
1391 if (colInf)
1392 delete [] colInf;
a2115c88
GT
1393 SQLFreeStmt(hstmt, SQL_UNBIND);
1394 SQLFreeStmt(hstmt, SQL_CLOSE);
108106cf
JS
1395 return(0);
1396 }
1397 }
1398 }
1399
a2115c88 1400 SQLFreeStmt(hstmt, SQL_UNBIND);
108106cf
JS
1401 SQLFreeStmt(hstmt, SQL_CLOSE);
1402 return colInf;
1403
1404} // wxDB::GetColumns()
1405
1406
1fc5dd6f 1407/********** wxDB::Catalog() **********/
6919c53f 1408bool wxDB::Catalog(const char *userID, const char *fileName)
1fc5dd6f 1409{
e8af3814 1410 assert(fileName && wxStrlen(fileName));
1fc5dd6f
JS
1411
1412 RETCODE retcode;
1413 SDWORD cb;
1414 char tblName[DB_MAX_TABLE_NAME_LEN+1];
1415 char tblNameSave[DB_MAX_TABLE_NAME_LEN+1];
1416 char colName[DB_MAX_COLUMN_NAME_LEN+1];
1417 SWORD sqlDataType;
a2115c88 1418 char typeName[30+1];
1fc5dd6f
JS
1419 SWORD precision, length;
1420
6919c53f
GT
1421 wxString UserID;
1422
1fc5dd6f
JS
1423 FILE *fp = fopen(fileName,"wt");
1424 if (fp == NULL)
1425 return(FALSE);
1426
1427 SQLFreeStmt(hstmt, SQL_CLOSE);
1428
e8af3814 1429 if (!userID || !wxStrlen(userID))
6919c53f
GT
1430 UserID = uid;
1431 else
1432 UserID = userID;
a2115c88 1433
a2115c88
GT
1434 // Oracle user names may only be in uppercase, so force
1435 // the name to uppercase
1436 if (Dbms() == dbmsORACLE)
6919c53f 1437 UserID = UserID.Upper();
a2115c88 1438
6919c53f 1439 if (wxStrcmp(UserID.GetData(),""))
a2115c88
GT
1440 {
1441 retcode = SQLColumns(hstmt,
1442 NULL, 0, // All qualifiers
6919c53f 1443 (UCHAR *) UserID.GetData(), SQL_NTS, // User specified
a2115c88
GT
1444 NULL, 0, // All tables
1445 NULL, 0); // All columns
1446 }
1447 else
1448 {
1449 retcode = SQLColumns(hstmt,
1450 NULL, 0, // All qualifiers
1451 NULL, 0, // User specified
1452 NULL, 0, // All tables
1453 NULL, 0); // All columns
1454 }
1fc5dd6f
JS
1455 if (retcode != SQL_SUCCESS)
1456 {
1457 DispAllErrors(henv, hdbc, hstmt);
1458 fclose(fp);
1459 return(FALSE);
1460 }
1461
a2115c88
GT
1462 SQLBindCol(hstmt, 3, SQL_C_CHAR, (UCHAR*) tblName, DB_MAX_TABLE_NAME_LEN+1, &cb);
1463 SQLBindCol(hstmt, 4, SQL_C_CHAR, (UCHAR*) colName, DB_MAX_COLUMN_NAME_LEN+1, &cb);
7e616b10 1464 SQLBindCol(hstmt, 5, SQL_C_SSHORT, (UCHAR*) &sqlDataType, 0, &cb);
a2115c88 1465 SQLBindCol(hstmt, 6, SQL_C_CHAR, (UCHAR*) typeName, sizeof(typeName), &cb);
7e616b10
RR
1466 SQLBindCol(hstmt, 7, SQL_C_SSHORT, (UCHAR*) &precision, 0, &cb);
1467 SQLBindCol(hstmt, 8, SQL_C_SSHORT, (UCHAR*) &length, 0, &cb);
1fc5dd6f
JS
1468
1469 char outStr[256];
e8af3814 1470 wxStrcpy(tblNameSave,"");
1fc5dd6f
JS
1471 int cnt = 0;
1472
1473 while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS)
1474 {
9199e66f 1475 if (wxStrcmp(tblName,tblNameSave))
1fc5dd6f
JS
1476 {
1477 if (cnt)
1478 fputs("\n", fp);
1479 fputs("================================ ", fp);
1480 fputs("================================ ", fp);
1481 fputs("===================== ", fp);
1482 fputs("========= ", fp);
1483 fputs("=========\n", fp);
1484 sprintf(outStr, "%-32s %-32s %-21s %9s %9s\n",
1485 "TABLE NAME", "COLUMN NAME", "DATA TYPE", "PRECISION", "LENGTH");
1486 fputs(outStr, fp);
1487 fputs("================================ ", fp);
1488 fputs("================================ ", fp);
1489 fputs("===================== ", fp);
1490 fputs("========= ", fp);
1491 fputs("=========\n", fp);
e8af3814 1492 wxStrcpy(tblNameSave,tblName);
1fc5dd6f
JS
1493 }
1494 sprintf(outStr, "%-32s %-32s (%04d)%-15s %9d %9d\n",
1495 tblName, colName, sqlDataType, typeName, precision, length);
1496 if (fputs(outStr, fp) == EOF)
1497 {
a2115c88
GT
1498 SQLFreeStmt(hstmt, SQL_UNBIND);
1499 SQLFreeStmt(hstmt, SQL_CLOSE);
1fc5dd6f
JS
1500 fclose(fp);
1501 return(FALSE);
1502 }
1503 cnt++;
1504 }
1505
1506 if (retcode != SQL_NO_DATA_FOUND)
1fc5dd6f 1507 DispAllErrors(henv, hdbc, hstmt);
1fc5dd6f 1508
a2115c88 1509 SQLFreeStmt(hstmt, SQL_UNBIND);
1fc5dd6f 1510 SQLFreeStmt(hstmt, SQL_CLOSE);
a2115c88 1511
1fc5dd6f 1512 fclose(fp);
a2115c88 1513 return(retcode == SQL_NO_DATA_FOUND);
1fc5dd6f
JS
1514
1515} // wxDB::Catalog()
1516
1517
108106cf
JS
1518// Table name can refer to a table, view, alias or synonym. Returns true
1519// if the object exists in the database. This function does not indicate
1520// whether or not the user has privleges to query or perform other functions
1521// on the table.
6919c53f 1522bool wxDB::TableExists(const char *tableName, const char *userID, const char *tablePath)
108106cf 1523{
6919c53f
GT
1524 wxString UserID;
1525 wxString TableName;
1526
e8af3814 1527 assert(tableName && wxStrlen(tableName));
108106cf 1528
a2115c88
GT
1529 if (Dbms() == dbmsDBASE)
1530 {
1531 wxString dbName;
e8af3814 1532 if (tablePath && wxStrlen(tablePath))
a2115c88
GT
1533 dbName.sprintf("%s/%s.dbf",tablePath,tableName);
1534 else
1535 dbName.sprintf("%s.dbf",tableName);
6919c53f
GT
1536
1537 bool exists;
1538 exists = wxFileExists(dbName.GetData());
1539 return exists;
a2115c88
GT
1540 }
1541
e8af3814 1542 if (!userID || !wxStrlen(userID))
6919c53f
GT
1543 UserID = uid;
1544 else
1545 UserID = userID;
a2115c88 1546
a2115c88
GT
1547 // Oracle user names may only be in uppercase, so force
1548 // the name to uppercase
1549 if (Dbms() == dbmsORACLE)
6919c53f 1550 UserID = UserID.Upper();
a2115c88 1551
6919c53f 1552 TableName = tableName;
a2115c88
GT
1553 // Oracle table names are uppercase only, so force
1554 // the name to uppercase just in case programmer forgot to do this
1555 if (Dbms() == dbmsORACLE)
6919c53f 1556 TableName = TableName.Upper();
a2115c88 1557
108106cf 1558 SQLFreeStmt(hstmt, SQL_CLOSE);
a2115c88
GT
1559 RETCODE retcode;
1560
1561 // MySQL and Access cannot accept a user name when looking up table names, so we
1562 // use the call below that leaves out the user name
6919c53f 1563 if (wxStrcmp(UserID,"") &&
a2115c88
GT
1564 Dbms() != dbmsMY_SQL &&
1565 Dbms() != dbmsACCESS)
1566 {
1567 retcode = SQLTables(hstmt,
6919c53f
GT
1568 NULL, 0, // All qualifiers
1569 (UCHAR *) UserID.GetData(), SQL_NTS, // All owners
1570 (UCHAR FAR *)TableName.GetData(), SQL_NTS,
1571 NULL, 0); // All table types
a2115c88
GT
1572 }
1573 else
1574 {
1575 retcode = SQLTables(hstmt,
6919c53f
GT
1576 NULL, 0, // All qualifiers
1577 NULL, 0, // All owners
1578 (UCHAR FAR *)TableName.GetData(), SQL_NTS,
1579 NULL, 0); // All table types
a2115c88 1580 }
108106cf
JS
1581 if (retcode != SQL_SUCCESS)
1582 return(DispAllErrors(henv, hdbc, hstmt));
1583
a2115c88
GT
1584 retcode = SQLFetch(hstmt);
1585 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
108106cf
JS
1586 {
1587 SQLFreeStmt(hstmt, SQL_CLOSE);
1588 return(DispAllErrors(henv, hdbc, hstmt));
1589 }
1590
1591 SQLFreeStmt(hstmt, SQL_CLOSE);
1592 return(TRUE);
1593
1594} // wxDB::TableExists()
1595
1596
1fc5dd6f 1597/********** wxDB::SqlLog() **********/
6919c53f 1598bool wxDB::SqlLog(enum sqlLog state, const char *filename, bool append)
1fc5dd6f
JS
1599{
1600 assert(state == sqlLogON || state == sqlLogOFF);
1601 assert(state == sqlLogOFF || filename);
1602
1603 if (state == sqlLogON)
1604 {
1605 if (fpSqlLog == 0)
1606 {
1607 fpSqlLog = fopen(filename, (append ? "at" : "wt"));
1608 if (fpSqlLog == NULL)
1609 return(FALSE);
1610 }
1611 }
1612 else // sqlLogOFF
1613 {
1614 if (fpSqlLog)
1615 {
1616 if (fclose(fpSqlLog))
1617 return(FALSE);
1618 fpSqlLog = 0;
1619 }
1620 }
1621
1622 sqlLogState = state;
1623 return(TRUE);
1624
1625} // wxDB::SqlLog()
1626
1627
1628/********** wxDB::WriteSqlLog() **********/
6919c53f 1629bool wxDB::WriteSqlLog(const char *logMsg)
1fc5dd6f
JS
1630{
1631 assert(logMsg);
1632
1633 if (fpSqlLog == 0 || sqlLogState == sqlLogOFF)
1634 return(FALSE);
1635
1636 if (fputs("\n", fpSqlLog) == EOF) return(FALSE);
1637 if (fputs(logMsg, fpSqlLog) == EOF) return(FALSE);
1638 if (fputs("\n", fpSqlLog) == EOF) return(FALSE);
1639
1640 return(TRUE);
1641
1642} // wxDB::WriteSqlLog()
1643
1644
a2115c88
GT
1645/********** wxDB::Dbms() **********/
1646/*
1647 * Be aware that not all database engines use the exact same syntax, and not
1648 * every ODBC compliant database is compliant to the same level of compliancy.
1649 * Some manufacturers support the minimum Level 1 compliancy, and others up
1650 * through Level 3. Others support subsets of features for levels above 1.
1651 *
1652 * If you find an inconsistency between the wxDB class and a specific database
1653 * engine, and an identifier to this section, and special handle the database in
1654 * the area where behavior is non-conforming with the other databases.
1655 *
1656 *
1657 * NOTES ABOUT ISSUES SPECIFIC TO EACH DATABASE ENGINE
1658 * ---------------------------------------------------
1659 *
1660 * ORACLE
1661 * - Currently the only database supported by the class to support VIEWS
1662 *
1663 * DBASE
1664 * - Does not support the SQL_TIMESTAMP structure
1665 * - Supports only one cursor and one connect (apparently? with Microsoft driver only?)
1666 * - Does not automatically create the primary index if the 'keyField' param of SetColDef
1667 * is TRUE. The user must create ALL indexes from their program.
1668 * - Table names can only be 8 characters long
1669 * - Column names can only be 10 characters long
1670 *
1671 * SYBASE (all)
1672 * - To lock a record during QUERY functions, the reserved word 'HOLDLOCK' must be added
1673 * after every table name involved in the query/join if that tables matching record(s)
1674 * are to be locked
1675 * - Ignores the keywords 'FOR UPDATE'. Use the HOLDLOCK functionality described above
1676 *
1677 * SYBASE (Enterprise)
1678 * - If a column is part of the Primary Key, the column cannot be NULL
1679 *
1680 * MY_SQL
1681 * - If a column is part of the Primary Key, the column cannot be NULL
1682 * - Cannot support selecting for update [::CanSelectForUpdate()]. Always returns FALSE
1683 *
1684 * POSTGRES
1685 * - Does not support the keywords 'ASC' or 'DESC' as of release v6.5.0
1686 *
1687 *
1688 */
1689DBMS wxDB::Dbms(void)
1690{
a3439c7d 1691 wxChar baseName[25+1];
aa33452c 1692
a3439c7d 1693 wxStrncpy(baseName,dbInf.dbmsName,25);
9199e66f 1694 if (!wxStricmp(dbInf.dbmsName,"Adaptive Server Anywhere"))
a2115c88 1695 return(dbmsSYBASE_ASA);
a3439c7d 1696 if (!wxStricmp(dbInf.dbmsName,"SQL Server")) // Sybase Adaptive Server
a2115c88 1697 return(dbmsSYBASE_ASE);
9199e66f 1698 if (!wxStricmp(dbInf.dbmsName,"Microsoft SQL Server"))
a2115c88 1699 return(dbmsMS_SQL_SERVER);
9199e66f 1700 if (!wxStricmp(dbInf.dbmsName,"MySQL"))
a2115c88 1701 return(dbmsMY_SQL);
9199e66f 1702 if (!wxStricmp(dbInf.dbmsName,"PostgreSQL")) // v6.5.0
a2115c88 1703 return(dbmsPOSTGRES);
a3439c7d
GT
1704
1705 baseName[8] = 0;
1706 if (!wxStricmp(baseName,"Informix"))
1707 return(dbmsINFORMIX);
1708
1709 baseName[6] = 0;
1710 if (!wxStricmp(baseName,"Oracle"))
1711 return(dbmsORACLE);
9199e66f 1712 if (!wxStricmp(dbInf.dbmsName,"ACCESS"))
a2115c88 1713 return(dbmsACCESS);
a3439c7d
GT
1714 if (!wxStricmp(dbInf.dbmsName,"MySQL"))
1715 return(dbmsMY_SQL);
1716
aa33452c 1717 baseName[5] = 0;
aa33452c 1718 if (!wxStricmp(baseName,"DBASE"))
a2115c88 1719 return(dbmsDBASE);
e8af3814 1720
a2115c88 1721 return(dbmsUNIDENTIFIED);
a2115c88
GT
1722} // wxDB::Dbms()
1723
1724
108106cf 1725/********** GetDbConnection() **********/
a3439c7d 1726wxDB WXDLLEXPORT *GetDbConnection(DbStuff *pDbStuff, bool FwdOnlyCursors)
108106cf
JS
1727{
1728 DbList *pList;
1729
1730 // Scan the linked list searching for an available database connection
1731 // that's already been opened but is currently not in use.
1732 for (pList = PtrBegDbList; pList; pList = pList->PtrNext)
1733 {
1734 // The database connection must be for the same datasource
1735 // name and must currently not be in use.
9199e66f 1736 if (pList->Free && (! wxStrcmp(pDbStuff->Dsn, pList->Dsn))) // Found a free connection
108106cf
JS
1737 {
1738 pList->Free = FALSE;
1739 return(pList->PtrDb);
1740 }
1741 }
1742
1743 // No available connections. A new connection must be made and
1744 // appended to the end of the linked list.
1745 if (PtrBegDbList)
1746 {
1747 // Find the end of the list
1748 for (pList = PtrBegDbList; pList->PtrNext; pList = pList->PtrNext);
1749 // Append a new list item
1750 pList->PtrNext = new DbList;
1751 pList->PtrNext->PtrPrev = pList;
1752 pList = pList->PtrNext;
1753 }
1754 else // Empty list
1755 {
1756 // Create the first node on the list
1757 pList = PtrBegDbList = new DbList;
1758 pList->PtrPrev = 0;
1759 }
1760
1761 // Initialize new node in the linked list
1762 pList->PtrNext = 0;
1763 pList->Free = FALSE;
e8af3814 1764 wxStrcpy(pList->Dsn, pDbStuff->Dsn);
a3439c7d 1765 pList->PtrDb = new wxDB(pDbStuff->Henv,FwdOnlyCursors);
108106cf
JS
1766
1767 // Connect to the datasource
1768 if (pList->PtrDb->Open(pDbStuff->Dsn, pDbStuff->Uid, pDbStuff->AuthStr))
a2115c88
GT
1769 {
1770 pList->PtrDb->SqlLog(SQLLOGstate,SQLLOGfn,TRUE);
108106cf 1771 return(pList->PtrDb);
a2115c88 1772 }
108106cf
JS
1773 else // Unable to connect, destroy list item
1774 {
1775 if (pList->PtrPrev)
1776 pList->PtrPrev->PtrNext = 0;
1777 else
1778 PtrBegDbList = 0; // Empty list again
1779 pList->PtrDb->CommitTrans(); // Commit any open transactions on wxDB object
1780 pList->PtrDb->Close(); // Close the wxDB object
1781 delete pList->PtrDb; // Deletes the wxDB object
1782 delete pList; // Deletes the linked list object
1783 return(0);
1784 }
1785
1786} // GetDbConnection()
1787
1788/********** FreeDbConnection() **********/
a1218415 1789bool WXDLLEXPORT FreeDbConnection(wxDB *pDb)
108106cf
JS
1790{
1791 DbList *pList;
1792
1793 // Scan the linked list searching for the database connection
1794 for (pList = PtrBegDbList; pList; pList = pList->PtrNext)
1795 {
1796 if (pList->PtrDb == pDb) // Found it!!!
1797 return(pList->Free = TRUE);
1798 }
1799
1800 // Never found the database object, return failure
1801 return(FALSE);
1802
1803} // FreeDbConnection()
1804
1805/********** CloseDbConnections() **********/
a1218415 1806void WXDLLEXPORT CloseDbConnections(void)
108106cf
JS
1807{
1808 DbList *pList, *pNext;
1809
1810 // Traverse the linked list closing database connections and freeing memory as I go.
1811 for (pList = PtrBegDbList; pList; pList = pNext)
1812 {
1813 pNext = pList->PtrNext; // Save the pointer to next
1814 pList->PtrDb->CommitTrans(); // Commit any open transactions on wxDB object
1815 pList->PtrDb->Close(); // Close the wxDB object
1816 delete pList->PtrDb; // Deletes the wxDB object
1817 delete pList; // Deletes the linked list object
1818 }
1819
1820 // Mark the list as empty
1821 PtrBegDbList = 0;
1822
1823} // CloseDbConnections()
1824
1825/********** NumberDbConnectionsInUse() **********/
a1218415 1826int WXDLLEXPORT NumberDbConnectionsInUse(void)
108106cf
JS
1827{
1828 DbList *pList;
1829 int cnt = 0;
1830
1831 // Scan the linked list counting db connections that are currently in use
1832 for (pList = PtrBegDbList; pList; pList = pList->PtrNext)
1833 {
1834 if (pList->Free == FALSE)
1835 cnt++;
1836 }
1837
1838 return(cnt);
1839
1840} // NumberDbConnectionsInUse()
1841
a2115c88
GT
1842/********** SqlLog() **********/
1843bool SqlLog(enum sqlLog state, char *filename)
1844{
1845 bool append = FALSE;
1846 DbList *pList;
1847
1848 for (pList = PtrBegDbList; pList; pList = pList->PtrNext)
1849 {
1850 if (!pList->PtrDb->SqlLog(state,filename,append))
1851 return(FALSE);
1852 append = TRUE;
1853 }
1854
1855 SQLLOGstate = state;
e8af3814 1856 wxStrcpy(SQLLOGfn,filename);
a2115c88
GT
1857
1858 return(TRUE);
1859
1860} // SqlLog()
1861
108106cf 1862/********** GetDataSource() **********/
6919c53f 1863bool GetDataSource(HENV henv, const char *Dsn, SWORD DsnMax, const char *DsDesc, SWORD DsDescMax,
108106cf
JS
1864 UWORD direction)
1865{
1866 SWORD cb;
1867
1868 if (SQLDataSources(henv, direction, (UCHAR FAR *) Dsn, DsnMax, &cb,
1869 (UCHAR FAR *) DsDesc, DsDescMax, &cb) == SQL_SUCCESS)
1870 return(TRUE);
1871 else
1872 return(FALSE);
1873
1874} // GetDataSource()
1875
1876#endif
1fc5dd6f
JS
1877 // wxUSE_ODBC
1878