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