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