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