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