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