1 ///////////////////////////////////////////////////////////////////////////////
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.
9 // -Added support for SQL statement logging and database cataloging
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
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 ///////////////////////////////////////////////////////////////////////////////
36 #include "wx/wxprec.h"
38 // Use this line for wxWindows v1.x
40 // Use this line for wxWindows v2.x
41 #include "wx/version.h"
43 #if wxMAJOR_VERSION == 2
45 #pragma implementation "db.h"
49 #ifdef DBDEBUG_CONSOLE
57 #if wxMAJOR_VERSION == 2
59 #include "wx/string.h"
60 #include "wx/object.h"
63 #include "wx/msgdlg.h"
65 #include "wx/filefn.h"
66 #include "wx/wxchar.h"
69 #if wxMAJOR_VERSION == 1
70 # if defined(wx_msw) || defined(wx_x)
87 #if wxMAJOR_VERSION == 1
89 #elif wxMAJOR_VERSION == 2
93 DbList WXDLLEXPORT
*PtrBegDbList
= 0;
96 extern wxList TablesInUse
;
100 // SQL Log defaults to be used by GetDbConnection
101 enum sqlLog SQLLOGstate
= sqlLogOFF
;
103 char SQLLOGfn
[DB_PATH_MAX
+1] = "sqllog.txt";
105 // The wxDB::errorList is copied to this variable when the wxDB object
106 // is closed. This way, the error list is still available after the
107 // database object is closed. This is necessary if the database
108 // connection fails so the calling application can show the operator
109 // why the connection failed. Note: as each wxDB object is closed, it
110 // will overwrite the errors of the previously destroyed wxDB object in
112 char DBerrorList
[DB_MAX_ERROR_HISTORY
][DB_MAX_ERROR_MSG_LEN
];
114 /********** wxDB Constructor **********/
115 wxDB::wxDB(HENV
&aHenv
)
119 fpSqlLog
= 0; // Sql Log file pointer
120 sqlLogState
= sqlLogOFF
; // By default, logging is turned off
125 nativeError
= cbErrorMsg
= 0;
126 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
127 strcpy(errorList
[i
], "");
129 // Init typeInf structures
130 strcpy(typeInfVarchar
.TypeName
,"");
131 typeInfVarchar
.FsqlType
= 0;
132 typeInfVarchar
.Precision
= 0;
133 typeInfVarchar
.CaseSensitive
= 0;
134 typeInfVarchar
.MaximumScale
= 0;
136 strcpy(typeInfInteger
.TypeName
,"");
137 typeInfInteger
.FsqlType
= 0;
138 typeInfInteger
.Precision
= 0;
139 typeInfInteger
.CaseSensitive
= 0;
140 typeInfInteger
.MaximumScale
= 0;
142 strcpy(typeInfFloat
.TypeName
,"");
143 typeInfFloat
.FsqlType
= 0;
144 typeInfFloat
.Precision
= 0;
145 typeInfFloat
.CaseSensitive
= 0;
146 typeInfFloat
.MaximumScale
= 0;
148 strcpy(typeInfDate
.TypeName
,"");
149 typeInfDate
.FsqlType
= 0;
150 typeInfDate
.Precision
= 0;
151 typeInfDate
.CaseSensitive
= 0;
152 typeInfDate
.MaximumScale
= 0;
154 // Error reporting is turned OFF by default
157 // Copy the HENV into the db class
160 // Allocate a data source connection handle
161 if (SQLAllocConnect(henv
, &hdbc
) != SQL_SUCCESS
)
164 // Initialize the db status flag
167 // Mark database as not open as of yet
172 /********** wxDB::Open() **********/
173 bool wxDB::Open(char *Dsn
, char *Uid
, char *AuthStr
)
175 assert(Dsn
&& strlen(Dsn
));
182 #if !wxODBC_FWD_ONLY_CURSORS
184 // Specify that the ODBC cursor library be used, if needed. This must be
185 // specified before the connection is made.
186 retcode
= SQLSetConnectOption(hdbc
, SQL_ODBC_CURSORS
, SQL_CUR_USE_IF_NEEDED
);
188 #ifdef DBDEBUG_CONSOLE
189 if (retcode
== SQL_SUCCESS
)
190 cout
<< "SQLSetConnectOption(CURSOR_LIB) successful" << endl
;
192 cout
<< "SQLSetConnectOption(CURSOR_LIB) failed" << endl
;
197 // Connect to the data source
198 retcode
= SQLConnect(hdbc
, (UCHAR FAR
*) Dsn
, SQL_NTS
,
199 (UCHAR FAR
*) Uid
, SQL_NTS
,
200 (UCHAR FAR
*) AuthStr
,SQL_NTS
);
201 if (retcode
== SQL_SUCCESS_WITH_INFO
)
202 DispAllErrors(henv
, hdbc
);
203 else if (retcode
!= SQL_SUCCESS
)
204 return(DispAllErrors(henv
, hdbc
));
207 If using Intersolv branded ODBC drivers, this is the place where you would substitute
208 your branded driver license information
210 SQLSetConnectOption(hdbc, 1041, (UDWORD) "");
211 SQLSetConnectOption(hdbc, 1042, (UDWORD) "");
213 // Mark database as open
216 // Allocate a statement handle for the database connection
217 if (SQLAllocStmt(hdbc
, &hstmt
) != SQL_SUCCESS
)
218 return(DispAllErrors(henv
, hdbc
));
220 // Set Connection Options
221 if (! setConnectionOptions())
224 // Query the data source for inf. about itself
228 // Query the data source regarding data type information
231 // The way I determined which SQL data types to use was by calling SQLGetInfo
232 // for all of the possible SQL data types to see which ones were supported. If
233 // a type is not supported, the SQLFetch() that's called from getDataTypeInfo()
234 // fails with SQL_NO_DATA_FOUND. This is ugly because I'm sure the three SQL data
235 // types I've selected below will not alway's be what we want. These are just
236 // what happened to work against an Oracle 7/Intersolv combination. The following is
237 // a complete list of the results I got back against the Oracle 7 database:
239 // SQL_BIGINT SQL_NO_DATA_FOUND
240 // SQL_BINARY SQL_NO_DATA_FOUND
241 // SQL_BIT SQL_NO_DATA_FOUND
242 // SQL_CHAR type name = 'CHAR', Precision = 255
243 // SQL_DATE SQL_NO_DATA_FOUND
244 // SQL_DECIMAL type name = 'NUMBER', Precision = 38
245 // SQL_DOUBLE type name = 'NUMBER', Precision = 15
246 // SQL_FLOAT SQL_NO_DATA_FOUND
247 // SQL_INTEGER SQL_NO_DATA_FOUND
248 // SQL_LONGVARBINARY type name = 'LONG RAW', Precision = 2 billion
249 // SQL_LONGVARCHAR type name = 'LONG', Precision = 2 billion
250 // SQL_NUMERIC SQL_NO_DATA_FOUND
251 // SQL_REAL SQL_NO_DATA_FOUND
252 // SQL_SMALLINT SQL_NO_DATA_FOUND
253 // SQL_TIME SQL_NO_DATA_FOUND
254 // SQL_TIMESTAMP type name = 'DATE', Precision = 19
255 // SQL_VARBINARY type name = 'RAW', Precision = 255
256 // SQL_VARCHAR type name = 'VARCHAR2', Precision = 2000
257 // =====================================================================
258 // Results from a Microsoft Access 7.0 db, using a driver from Microsoft
260 // SQL_VARCHAR type name = 'TEXT', Precision = 255
261 // SQL_TIMESTAMP type name = 'DATETIME'
262 // SQL_DECIMAL SQL_NO_DATA_FOUND
263 // SQL_NUMERIC type name = 'CURRENCY', Precision = 19
264 // SQL_FLOAT SQL_NO_DATA_FOUND
265 // SQL_REAL type name = 'SINGLE', Precision = 7
266 // SQL_DOUBLE type name = 'DOUBLE', Precision = 15
267 // SQL_INTEGER type name = 'LONG', Precision = 10
269 // VARCHAR = Variable length character string
270 if (! getDataTypeInfo(SQL_VARCHAR
, typeInfVarchar
))
271 if (! getDataTypeInfo(SQL_CHAR
, typeInfVarchar
))
274 typeInfVarchar
.FsqlType
= SQL_CHAR
;
276 typeInfVarchar
.FsqlType
= SQL_VARCHAR
;
279 if (! getDataTypeInfo(SQL_DOUBLE
, typeInfFloat
))
280 if (! getDataTypeInfo(SQL_REAL
, typeInfFloat
))
281 if (! getDataTypeInfo(SQL_FLOAT
, typeInfFloat
))
282 if (! getDataTypeInfo(SQL_DECIMAL
, typeInfFloat
))
283 if (! getDataTypeInfo(SQL_NUMERIC
, typeInfFloat
))
286 typeInfFloat
.FsqlType
= SQL_NUMERIC
;
288 typeInfFloat
.FsqlType
= SQL_DECIMAL
;
290 typeInfFloat
.FsqlType
= SQL_FLOAT
;
292 typeInfFloat
.FsqlType
= SQL_REAL
;
294 typeInfFloat
.FsqlType
= SQL_DOUBLE
;
297 if (! getDataTypeInfo(SQL_INTEGER
, typeInfInteger
))
298 // If SQL_INTEGER is not supported, use the floating point
299 // data type to store integers as well as floats
300 if (! getDataTypeInfo(typeInfFloat
.FsqlType
, typeInfInteger
))
303 typeInfInteger
.FsqlType
= typeInfFloat
.FsqlType
;
305 typeInfInteger
.FsqlType
= SQL_INTEGER
;
308 if (Dbms() != dbmsDBASE
)
310 if (! getDataTypeInfo(SQL_TIMESTAMP
, typeInfDate
))
313 typeInfDate
.FsqlType
= SQL_TIMESTAMP
;
317 if (! getDataTypeInfo(SQL_DATE
, typeInfDate
))
320 typeInfDate
.FsqlType
= SQL_DATE
;
323 #ifdef DBDEBUG_CONSOLE
324 cout
<< "VARCHAR DATA TYPE: " << typeInfVarchar
.TypeName
<< endl
;
325 cout
<< "INTEGER DATA TYPE: " << typeInfInteger
.TypeName
<< endl
;
326 cout
<< "FLOAT DATA TYPE: " << typeInfFloat
.TypeName
<< endl
;
327 cout
<< "DATE DATA TYPE: " << typeInfDate
.TypeName
<< endl
;
331 // Completed Successfully
336 // The Intersolv/Oracle 7 driver was "Not Capable" of setting the login timeout.
338 /********** wxDB::setConnectionOptions() **********/
339 bool wxDB::setConnectionOptions(void)
341 SQLSetConnectOption(hdbc
, SQL_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
);
342 SQLSetConnectOption(hdbc
, SQL_OPT_TRACE
, SQL_OPT_TRACE_OFF
);
344 // Display the connection options to verify them
345 #ifdef DBDEBUG_CONSOLE
347 cout
<< ">>>>> CONNECTION OPTIONS <<<<<<" << endl
;
349 if (SQLGetConnectOption(hdbc
, SQL_AUTOCOMMIT
, &l
) != SQL_SUCCESS
)
350 return(DispAllErrors(henv
, hdbc
));
351 cout
<< "AUTOCOMMIT: " << (l
== SQL_AUTOCOMMIT_OFF
? "OFF" : "ON") << endl
;
353 if (SQLGetConnectOption(hdbc
, SQL_ODBC_CURSORS
, &l
) != SQL_SUCCESS
)
354 return(DispAllErrors(henv
, hdbc
));
355 cout
<< "ODBC CURSORS: ";
358 case(SQL_CUR_USE_IF_NEEDED
):
359 cout
<< "SQL_CUR_USE_IF_NEEDED";
361 case(SQL_CUR_USE_ODBC
):
362 cout
<< "SQL_CUR_USE_ODBC";
364 case(SQL_CUR_USE_DRIVER
):
365 cout
<< "SQL_CUR_USE_DRIVER";
370 if (SQLGetConnectOption(hdbc
, SQL_OPT_TRACE
, &l
) != SQL_SUCCESS
)
371 return(DispAllErrors(henv
, hdbc
));
372 cout
<< "TRACING: " << (l
== SQL_OPT_TRACE_OFF
? "OFF" : "ON") << endl
;
377 // Completed Successfully
380 } // wxDB::setConnectionOptions()
382 /********** wxDB::getDbInfo() **********/
383 bool wxDB::getDbInfo(void)
388 if (SQLGetInfo(hdbc
, SQL_SERVER_NAME
, (UCHAR
*) dbInf
.serverName
, 80, &cb
) != SQL_SUCCESS
)
389 return(DispAllErrors(henv
, hdbc
));
391 if (SQLGetInfo(hdbc
, SQL_DATABASE_NAME
, (UCHAR
*) dbInf
.databaseName
, 128, &cb
) != SQL_SUCCESS
)
392 return(DispAllErrors(henv
, hdbc
));
394 if (SQLGetInfo(hdbc
, SQL_DBMS_NAME
, (UCHAR
*) dbInf
.dbmsName
, 40, &cb
) != SQL_SUCCESS
)
395 return(DispAllErrors(henv
, hdbc
));
398 // After upgrading to MSVC6, the original 20 char buffer below was insufficient,
399 // causing database connectivity to fail in some cases.
400 retcode
= SQLGetInfo(hdbc
, SQL_DBMS_VER
, (UCHAR
*) dbInf
.dbmsVer
, 64, &cb
);
401 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
402 return(DispAllErrors(henv
, hdbc
));
404 if (SQLGetInfo(hdbc
, SQL_ACTIVE_CONNECTIONS
, (UCHAR
*) &dbInf
.maxConnections
, sizeof(dbInf
.maxConnections
), &cb
) != SQL_SUCCESS
)
405 return(DispAllErrors(henv
, hdbc
));
407 if (SQLGetInfo(hdbc
, SQL_ACTIVE_STATEMENTS
, (UCHAR
*) &dbInf
.maxStmts
, sizeof(dbInf
.maxStmts
), &cb
) != SQL_SUCCESS
)
408 return(DispAllErrors(henv
, hdbc
));
410 if (SQLGetInfo(hdbc
, SQL_DRIVER_NAME
, (UCHAR
*) dbInf
.driverName
, 40, &cb
) != SQL_SUCCESS
)
411 return(DispAllErrors(henv
, hdbc
));
413 if (SQLGetInfo(hdbc
, SQL_DRIVER_ODBC_VER
, (UCHAR
*) dbInf
.odbcVer
, 60, &cb
) == SQL_ERROR
)
414 return(DispAllErrors(henv
, hdbc
));
416 retcode
= SQLGetInfo(hdbc
, SQL_ODBC_VER
, (UCHAR
*) dbInf
.drvMgrOdbcVer
, 60, &cb
);
417 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
418 return(DispAllErrors(henv
, hdbc
));
420 if (SQLGetInfo(hdbc
, SQL_DRIVER_VER
, (UCHAR
*) dbInf
.driverVer
, 60, &cb
) == SQL_ERROR
)
421 return(DispAllErrors(henv
, hdbc
));
423 if (SQLGetInfo(hdbc
, SQL_ODBC_API_CONFORMANCE
, (UCHAR
*) &dbInf
.apiConfLvl
, sizeof(dbInf
.apiConfLvl
), &cb
) != SQL_SUCCESS
)
424 return(DispAllErrors(henv
, hdbc
));
426 if (SQLGetInfo(hdbc
, SQL_ODBC_SAG_CLI_CONFORMANCE
, (UCHAR
*) &dbInf
.cliConfLvl
, sizeof(dbInf
.cliConfLvl
), &cb
) != SQL_SUCCESS
)
427 return(DispAllErrors(henv
, hdbc
));
429 if (SQLGetInfo(hdbc
, SQL_ODBC_SQL_CONFORMANCE
, (UCHAR
*) &dbInf
.sqlConfLvl
, sizeof(dbInf
.sqlConfLvl
), &cb
) != SQL_SUCCESS
)
430 return(DispAllErrors(henv
, hdbc
));
432 if (SQLGetInfo(hdbc
, SQL_OUTER_JOINS
, (UCHAR
*) dbInf
.outerJoins
, 2, &cb
) != SQL_SUCCESS
)
433 return(DispAllErrors(henv
, hdbc
));
435 if (SQLGetInfo(hdbc
, SQL_PROCEDURES
, (UCHAR
*) dbInf
.procedureSupport
, 2, &cb
) != SQL_SUCCESS
)
436 return(DispAllErrors(henv
, hdbc
));
438 if (SQLGetInfo(hdbc
, SQL_CURSOR_COMMIT_BEHAVIOR
, (UCHAR
*) &dbInf
.cursorCommitBehavior
, sizeof(dbInf
.cursorCommitBehavior
), &cb
) != SQL_SUCCESS
)
439 return(DispAllErrors(henv
, hdbc
));
441 if (SQLGetInfo(hdbc
, SQL_CURSOR_ROLLBACK_BEHAVIOR
, (UCHAR
*) &dbInf
.cursorRollbackBehavior
, sizeof(dbInf
.cursorRollbackBehavior
), &cb
) != SQL_SUCCESS
)
442 return(DispAllErrors(henv
, hdbc
));
444 if (SQLGetInfo(hdbc
, SQL_NON_NULLABLE_COLUMNS
, (UCHAR
*) &dbInf
.supportNotNullClause
, sizeof(dbInf
.supportNotNullClause
), &cb
) != SQL_SUCCESS
)
445 return(DispAllErrors(henv
, hdbc
));
447 if (SQLGetInfo(hdbc
, SQL_ODBC_SQL_OPT_IEF
, (UCHAR
*) dbInf
.supportIEF
, 2, &cb
) != SQL_SUCCESS
)
448 return(DispAllErrors(henv
, hdbc
));
450 if (SQLGetInfo(hdbc
, SQL_DEFAULT_TXN_ISOLATION
, (UCHAR
*) &dbInf
.txnIsolation
, sizeof(dbInf
.txnIsolation
), &cb
) != SQL_SUCCESS
)
451 return(DispAllErrors(henv
, hdbc
));
453 if (SQLGetInfo(hdbc
, SQL_TXN_ISOLATION_OPTION
, (UCHAR
*) &dbInf
.txnIsolationOptions
, sizeof(dbInf
.txnIsolationOptions
), &cb
) != SQL_SUCCESS
)
454 return(DispAllErrors(henv
, hdbc
));
456 if (SQLGetInfo(hdbc
, SQL_FETCH_DIRECTION
, (UCHAR
*) &dbInf
.fetchDirections
, sizeof(dbInf
.fetchDirections
), &cb
) != SQL_SUCCESS
)
457 return(DispAllErrors(henv
, hdbc
));
459 if (SQLGetInfo(hdbc
, SQL_LOCK_TYPES
, (UCHAR
*) &dbInf
.lockTypes
, sizeof(dbInf
.lockTypes
), &cb
) != SQL_SUCCESS
)
460 return(DispAllErrors(henv
, hdbc
));
462 if (SQLGetInfo(hdbc
, SQL_POS_OPERATIONS
, (UCHAR
*) &dbInf
.posOperations
, sizeof(dbInf
.posOperations
), &cb
) != SQL_SUCCESS
)
463 return(DispAllErrors(henv
, hdbc
));
465 if (SQLGetInfo(hdbc
, SQL_POSITIONED_STATEMENTS
, (UCHAR
*) &dbInf
.posStmts
, sizeof(dbInf
.posStmts
), &cb
) != SQL_SUCCESS
)
466 return(DispAllErrors(henv
, hdbc
));
468 if (SQLGetInfo(hdbc
, SQL_SCROLL_CONCURRENCY
, (UCHAR
*) &dbInf
.scrollConcurrency
, sizeof(dbInf
.scrollConcurrency
), &cb
) != SQL_SUCCESS
)
469 return(DispAllErrors(henv
, hdbc
));
471 if (SQLGetInfo(hdbc
, SQL_SCROLL_OPTIONS
, (UCHAR
*) &dbInf
.scrollOptions
, sizeof(dbInf
.scrollOptions
), &cb
) != SQL_SUCCESS
)
472 return(DispAllErrors(henv
, hdbc
));
474 if (SQLGetInfo(hdbc
, SQL_STATIC_SENSITIVITY
, (UCHAR
*) &dbInf
.staticSensitivity
, sizeof(dbInf
.staticSensitivity
), &cb
) != SQL_SUCCESS
)
475 return(DispAllErrors(henv
, hdbc
));
477 if (SQLGetInfo(hdbc
, SQL_TXN_CAPABLE
, (UCHAR
*) &dbInf
.txnCapable
, sizeof(dbInf
.txnCapable
), &cb
) != SQL_SUCCESS
)
478 return(DispAllErrors(henv
, hdbc
));
480 if (SQLGetInfo(hdbc
, SQL_LOGIN_TIMEOUT
, (UCHAR
*) &dbInf
.loginTimeout
, sizeof(dbInf
.loginTimeout
), &cb
) != SQL_SUCCESS
)
481 return(DispAllErrors(henv
, hdbc
));
483 #ifdef DBDEBUG_CONSOLE
484 cout
<< ">>>>> DATA SOURCE INFORMATION <<<<<" << endl
;
485 cout
<< "SERVER Name: " << dbInf
.serverName
<< endl
;
486 cout
<< "DBMS Name: " << dbInf
.dbmsName
<< "; DBMS Version: " << dbInf
.dbmsVer
<< endl
;
487 cout
<< "ODBC Version: " << dbInf
.odbcVer
<< "; Driver Version: " << dbInf
.driverVer
<< endl
;
489 cout
<< "API Conf. Level: ";
490 switch(dbInf
.apiConfLvl
)
492 case SQL_OAC_NONE
: cout
<< "None"; break;
493 case SQL_OAC_LEVEL1
: cout
<< "Level 1"; break;
494 case SQL_OAC_LEVEL2
: cout
<< "Level 2"; break;
498 cout
<< "SAG CLI Conf. Level: ";
499 switch(dbInf
.cliConfLvl
)
501 case SQL_OSCC_NOT_COMPLIANT
: cout
<< "Not Compliant"; break;
502 case SQL_OSCC_COMPLIANT
: cout
<< "Compliant"; break;
506 cout
<< "SQL Conf. Level: ";
507 switch(dbInf
.sqlConfLvl
)
509 case SQL_OSC_MINIMUM
: cout
<< "Minimum Grammer"; break;
510 case SQL_OSC_CORE
: cout
<< "Core Grammer"; break;
511 case SQL_OSC_EXTENDED
: cout
<< "Extended Grammer"; break;
515 cout
<< "Max. Connections: " << dbInf
.maxConnections
<< endl
;
516 cout
<< "Outer Joins: " << dbInf
.outerJoins
<< endl
;
517 cout
<< "Support for Procedures: " << dbInf
.procedureSupport
<< endl
;
519 cout
<< "Cursor COMMIT Behavior: ";
520 switch(dbInf
.cursorCommitBehavior
)
522 case SQL_CB_DELETE
: cout
<< "Delete cursors"; break;
523 case SQL_CB_CLOSE
: cout
<< "Close cursors"; break;
524 case SQL_CB_PRESERVE
: cout
<< "Preserve cursors"; break;
528 cout
<< "Cursor ROLLBACK Behavior: ";
529 switch(dbInf
.cursorRollbackBehavior
)
531 case SQL_CB_DELETE
: cout
<< "Delete cursors"; break;
532 case SQL_CB_CLOSE
: cout
<< "Close cursors"; break;
533 case SQL_CB_PRESERVE
: cout
<< "Preserve cursors"; break;
537 cout
<< "Support NOT NULL clause: ";
538 switch(dbInf
.supportNotNullClause
)
540 case SQL_NNC_NULL
: cout
<< "No"; break;
541 case SQL_NNC_NON_NULL
: cout
<< "Yes"; break;
545 cout
<< "Support IEF (Ref. Integrity): " << dbInf
.supportIEF
<< endl
;
546 cout
<< "Login Timeout: " << dbInf
.loginTimeout
<< endl
;
548 cout
<< endl
<< endl
<< "more ..." << endl
;
551 cout
<< "Default Transaction Isolation: ";
552 switch(dbInf
.txnIsolation
)
554 case SQL_TXN_READ_UNCOMMITTED
: cout
<< "Read Uncommitted"; break;
555 case SQL_TXN_READ_COMMITTED
: cout
<< "Read Committed"; break;
556 case SQL_TXN_REPEATABLE_READ
: cout
<< "Repeatable Read"; break;
557 case SQL_TXN_SERIALIZABLE
: cout
<< "Serializable"; break;
559 case SQL_TXN_VERSIONING
: cout
<< "Versioning"; break;
564 cout
<< "Transaction Isolation Options: ";
565 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_UNCOMMITTED
)
566 cout
<< "Read Uncommitted, ";
567 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_COMMITTED
)
568 cout
<< "Read Committed, ";
569 if (dbInf
.txnIsolationOptions
& SQL_TXN_REPEATABLE_READ
)
570 cout
<< "Repeatable Read, ";
571 if (dbInf
.txnIsolationOptions
& SQL_TXN_SERIALIZABLE
)
572 cout
<< "Serializable, ";
574 if (dbInf
.txnIsolationOptions
& SQL_TXN_VERSIONING
)
575 cout
<< "Versioning";
579 cout
<< "Fetch Directions Supported:" << endl
<< " ";
580 if (dbInf
.fetchDirections
& SQL_FD_FETCH_NEXT
)
582 if (dbInf
.fetchDirections
& SQL_FD_FETCH_PRIOR
)
584 if (dbInf
.fetchDirections
& SQL_FD_FETCH_FIRST
)
586 if (dbInf
.fetchDirections
& SQL_FD_FETCH_LAST
)
588 if (dbInf
.fetchDirections
& SQL_FD_FETCH_ABSOLUTE
)
589 cout
<< "Absolute, ";
590 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RELATIVE
)
591 cout
<< "Relative, ";
593 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RESUME
)
596 if (dbInf
.fetchDirections
& SQL_FD_FETCH_BOOKMARK
)
600 cout
<< "Lock Types Supported (SQLSetPos): ";
601 if (dbInf
.lockTypes
& SQL_LCK_NO_CHANGE
)
602 cout
<< "No Change, ";
603 if (dbInf
.lockTypes
& SQL_LCK_EXCLUSIVE
)
604 cout
<< "Exclusive, ";
605 if (dbInf
.lockTypes
& SQL_LCK_UNLOCK
)
609 cout
<< "Position Operations Supported (SQLSetPos): ";
610 if (dbInf
.posOperations
& SQL_POS_POSITION
)
611 cout
<< "Position, ";
612 if (dbInf
.posOperations
& SQL_POS_REFRESH
)
614 if (dbInf
.posOperations
& SQL_POS_UPDATE
)
616 if (dbInf
.posOperations
& SQL_POS_DELETE
)
618 if (dbInf
.posOperations
& SQL_POS_ADD
)
622 cout
<< "Positioned Statements Supported: ";
623 if (dbInf
.posStmts
& SQL_PS_POSITIONED_DELETE
)
624 cout
<< "Pos delete, ";
625 if (dbInf
.posStmts
& SQL_PS_POSITIONED_UPDATE
)
626 cout
<< "Pos update, ";
627 if (dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
628 cout
<< "Select for update";
631 cout
<< "Scroll Concurrency: ";
632 if (dbInf
.scrollConcurrency
& SQL_SCCO_READ_ONLY
)
633 cout
<< "Read Only, ";
634 if (dbInf
.scrollConcurrency
& SQL_SCCO_LOCK
)
636 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_ROWVER
)
637 cout
<< "Opt. Rowver, ";
638 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_VALUES
)
639 cout
<< "Opt. Values";
642 cout
<< "Scroll Options: ";
643 if (dbInf
.scrollOptions
& SQL_SO_FORWARD_ONLY
)
644 cout
<< "Fwd Only, ";
645 if (dbInf
.scrollOptions
& SQL_SO_STATIC
)
647 if (dbInf
.scrollOptions
& SQL_SO_KEYSET_DRIVEN
)
648 cout
<< "Keyset Driven, ";
649 if (dbInf
.scrollOptions
& SQL_SO_DYNAMIC
)
651 if (dbInf
.scrollOptions
& SQL_SO_MIXED
)
655 cout
<< "Static Sensitivity: ";
656 if (dbInf
.staticSensitivity
& SQL_SS_ADDITIONS
)
657 cout
<< "Additions, ";
658 if (dbInf
.staticSensitivity
& SQL_SS_DELETIONS
)
659 cout
<< "Deletions, ";
660 if (dbInf
.staticSensitivity
& SQL_SS_UPDATES
)
664 cout
<< "Transaction Capable?: ";
665 switch(dbInf
.txnCapable
)
667 case SQL_TC_NONE
: cout
<< "No"; break;
668 case SQL_TC_DML
: cout
<< "DML Only"; break;
669 case SQL_TC_DDL_COMMIT
: cout
<< "DDL Commit"; break;
670 case SQL_TC_DDL_IGNORE
: cout
<< "DDL Ignore"; break;
671 case SQL_TC_ALL
: cout
<< "DDL & DML"; break;
679 // Completed Successfully
682 } // wxDB::getDbInfo()
684 /********** wxDB::getDataTypeInfo() **********/
685 bool wxDB::getDataTypeInfo(SWORD fSqlType
, SqlTypeInfo
&structSQLTypeInfo
)
687 // fSqlType will be something like SQL_VARCHAR. This parameter determines
688 // the data type inf. is gathered for.
690 // SqlTypeInfo is a structure that is filled in with data type information,
695 // Get information about the data type specified
696 if (SQLGetTypeInfo(hstmt
, fSqlType
) != SQL_SUCCESS
)
697 return(DispAllErrors(henv
, hdbc
, hstmt
));
699 if ((retcode
= SQLFetch(hstmt
)) != SQL_SUCCESS
)
701 #ifdef DBDEBUG_CONSOLE
702 if (retcode
== SQL_NO_DATA_FOUND
)
703 cout
<< "SQL_NO_DATA_FOUND fetching inf. about data type." << endl
;
705 DispAllErrors(henv
, hdbc
, hstmt
);
706 SQLFreeStmt(hstmt
, SQL_CLOSE
);
709 // Obtain columns from the record
710 if (SQLGetData(hstmt
, 1, SQL_C_CHAR
, (UCHAR
*) structSQLTypeInfo
.TypeName
, DB_TYPE_NAME_LEN
, &cbRet
) != SQL_SUCCESS
)
711 return(DispAllErrors(henv
, hdbc
, hstmt
));
712 if (SQLGetData(hstmt
, 3, SQL_C_LONG
, (UCHAR
*) &structSQLTypeInfo
.Precision
, 0, &cbRet
) != SQL_SUCCESS
)
713 return(DispAllErrors(henv
, hdbc
, hstmt
));
714 if (SQLGetData(hstmt
, 8, SQL_C_SHORT
, (UCHAR
*) &structSQLTypeInfo
.CaseSensitive
, 0, &cbRet
) != SQL_SUCCESS
)
715 return(DispAllErrors(henv
, hdbc
, hstmt
));
716 // if (SQLGetData(hstmt, 14, SQL_C_SHORT, (UCHAR*) &structSQLTypeInfo.MinimumScale, 0, &cbRet) != SQL_SUCCESS)
717 // return(DispAllErrors(henv, hdbc, hstmt));
719 //#ifdef __UNIX__ // BJO : IODBC knows about 5, not 15...
720 // if (SQLGetData(hstmt, 5, SQL_C_SHORT,(UCHAR*) &structSQLTypeInfo.MaximumScale, 0, &cbRet) != SQL_SUCCESS)
721 // return(DispAllErrors(henv, hdbc, hstmt));
723 if (SQLGetData(hstmt
, 15, SQL_C_SHORT
,(UCHAR
*) &structSQLTypeInfo
.MaximumScale
, 0, &cbRet
) != SQL_SUCCESS
)
724 return(DispAllErrors(henv
, hdbc
, hstmt
));
727 if (structSQLTypeInfo
.MaximumScale
< 0)
728 structSQLTypeInfo
.MaximumScale
= 0;
730 // Close the statement handle which closes open cursors
731 if (SQLFreeStmt(hstmt
, SQL_CLOSE
) != SQL_SUCCESS
)
732 return(DispAllErrors(henv
, hdbc
, hstmt
));
734 // Completed Successfully
737 } // wxDB::getDataTypeInfo()
739 /********** wxDB::Close() **********/
740 void wxDB::Close(void)
742 // Close the Sql Log file
749 // Free statement handle
752 if (SQLFreeStmt(hstmt
, SQL_DROP
) != SQL_SUCCESS
)
753 DispAllErrors(henv
, hdbc
);
756 // Disconnect from the datasource
757 if (SQLDisconnect(hdbc
) != SQL_SUCCESS
)
758 DispAllErrors(henv
, hdbc
);
760 // Free the connection to the datasource
761 if (SQLFreeConnect(hdbc
) != SQL_SUCCESS
)
762 DispAllErrors(henv
, hdbc
);
764 // There should be zero Ctable objects still connected to this db object
765 assert(nTables
== 0);
768 CstructTablesInUse
*tiu
;
770 pNode
= TablesInUse
.First();
775 tiu
= (CstructTablesInUse
*)pNode
->Data();
776 if (tiu
->pDb
== this)
778 sprintf(s
, "(%-20s) tableID:[%6lu] pDb:[%p]", tiu
->tableName
,tiu
->tableID
,tiu
->pDb
);
779 sprintf(s2
,"Orphaned found using pDb:[%p]",this);
782 pNode
= pNode
->Next();
786 // Copy the error messages to a global variable
788 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
789 strcpy(DBerrorList
[i
],errorList
[i
]);
793 /********** wxDB::CommitTrans() **********/
794 bool wxDB::CommitTrans(void)
798 // Commit the transaction
799 if (SQLTransact(henv
, hdbc
, SQL_COMMIT
) != SQL_SUCCESS
)
800 return(DispAllErrors(henv
, hdbc
));
803 // Completed successfully
806 } // wxDB::CommitTrans()
808 /********** wxDB::RollbackTrans() **********/
809 bool wxDB::RollbackTrans(void)
811 // Rollback the transaction
812 if (SQLTransact(henv
, hdbc
, SQL_ROLLBACK
) != SQL_SUCCESS
)
813 return(DispAllErrors(henv
, hdbc
));
815 // Completed successfully
818 } // wxDB::RollbackTrans()
820 /********** wxDB::DispAllErrors() **********/
821 bool wxDB::DispAllErrors(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
823 char odbcErrMsg
[DB_MAX_ERROR_MSG_LEN
];
825 while (SQLError(aHenv
, aHdbc
, aHstmt
, (UCHAR FAR
*) sqlState
, &nativeError
, (UCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
827 sprintf(odbcErrMsg
, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState
, nativeError
, errorMsg
);
828 logError(odbcErrMsg
, sqlState
);
831 #ifdef DBDEBUG_CONSOLE
832 // When run in console mode, use standard out to display errors.
833 cout
<< odbcErrMsg
<< endl
;
834 cout
<< "Press any key to continue..." << endl
;
840 wxMessageBox(odbcErrMsg
);
844 return(FALSE
); // This function always returns false.
846 } // wxDB::DispAllErrors()
848 /********** wxDB::GetNextError() **********/
849 bool wxDB::GetNextError(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
851 if (SQLError(aHenv
, aHdbc
, aHstmt
, (UCHAR FAR
*) sqlState
, &nativeError
, (UCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
856 } // wxDB::GetNextError()
858 /********** wxDB::DispNextError() **********/
859 void wxDB::DispNextError(void)
861 char odbcErrMsg
[DB_MAX_ERROR_MSG_LEN
];
863 sprintf(odbcErrMsg
, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState
, nativeError
, errorMsg
);
864 logError(odbcErrMsg
, sqlState
);
869 #ifdef DBDEBUG_CONSOLE
870 // When run in console mode, use standard out to display errors.
871 cout
<< odbcErrMsg
<< endl
;
872 cout
<< "Press any key to continue..." << endl
;
876 } // wxDB::DispNextError()
878 /********** wxDB::logError() **********/
879 void wxDB::logError(char *errMsg
, char *SQLState
)
881 assert(errMsg
&& strlen(errMsg
));
883 static int pLast
= -1;
886 if (++pLast
== DB_MAX_ERROR_HISTORY
)
889 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
890 strcpy(errorList
[i
], errorList
[i
+1]);
894 strcpy(errorList
[pLast
], errMsg
);
896 if (SQLState
&& strlen(SQLState
))
897 if ((dbStatus
= TranslateSqlState(SQLState
)) != DB_ERR_FUNCTION_SEQUENCE_ERROR
)
898 DB_STATUS
= dbStatus
;
900 // Add the errmsg to the sql log
903 } // wxDB::logError()
905 /**********wxDB::TranslateSqlState() **********/
906 int wxDB::TranslateSqlState(char *SQLState
)
908 if (!wxStrcmp(SQLState
, "01000"))
909 return(DB_ERR_GENERAL_WARNING
);
910 if (!wxStrcmp(SQLState
, "01002"))
911 return(DB_ERR_DISCONNECT_ERROR
);
912 if (!wxStrcmp(SQLState
, "01004"))
913 return(DB_ERR_DATA_TRUNCATED
);
914 if (!wxStrcmp(SQLState
, "01006"))
915 return(DB_ERR_PRIV_NOT_REVOKED
);
916 if (!wxStrcmp(SQLState
, "01S00"))
917 return(DB_ERR_INVALID_CONN_STR_ATTR
);
918 if (!wxStrcmp(SQLState
, "01S01"))
919 return(DB_ERR_ERROR_IN_ROW
);
920 if (!wxStrcmp(SQLState
, "01S02"))
921 return(DB_ERR_OPTION_VALUE_CHANGED
);
922 if (!wxStrcmp(SQLState
, "01S03"))
923 return(DB_ERR_NO_ROWS_UPD_OR_DEL
);
924 if (!wxStrcmp(SQLState
, "01S04"))
925 return(DB_ERR_MULTI_ROWS_UPD_OR_DEL
);
926 if (!wxStrcmp(SQLState
, "07001"))
927 return(DB_ERR_WRONG_NO_OF_PARAMS
);
928 if (!wxStrcmp(SQLState
, "07006"))
929 return(DB_ERR_DATA_TYPE_ATTR_VIOL
);
930 if (!wxStrcmp(SQLState
, "08001"))
931 return(DB_ERR_UNABLE_TO_CONNECT
);
932 if (!wxStrcmp(SQLState
, "08002"))
933 return(DB_ERR_CONNECTION_IN_USE
);
934 if (!wxStrcmp(SQLState
, "08003"))
935 return(DB_ERR_CONNECTION_NOT_OPEN
);
936 if (!wxStrcmp(SQLState
, "08004"))
937 return(DB_ERR_REJECTED_CONNECTION
);
938 if (!wxStrcmp(SQLState
, "08007"))
939 return(DB_ERR_CONN_FAIL_IN_TRANS
);
940 if (!wxStrcmp(SQLState
, "08S01"))
941 return(DB_ERR_COMM_LINK_FAILURE
);
942 if (!wxStrcmp(SQLState
, "21S01"))
943 return(DB_ERR_INSERT_VALUE_LIST_MISMATCH
);
944 if (!wxStrcmp(SQLState
, "21S02"))
945 return(DB_ERR_DERIVED_TABLE_MISMATCH
);
946 if (!wxStrcmp(SQLState
, "22001"))
947 return(DB_ERR_STRING_RIGHT_TRUNC
);
948 if (!wxStrcmp(SQLState
, "22003"))
949 return(DB_ERR_NUMERIC_VALUE_OUT_OF_RNG
);
950 if (!wxStrcmp(SQLState
, "22005"))
951 return(DB_ERR_ERROR_IN_ASSIGNMENT
);
952 if (!wxStrcmp(SQLState
, "22008"))
953 return(DB_ERR_DATETIME_FLD_OVERFLOW
);
954 if (!wxStrcmp(SQLState
, "22012"))
955 return(DB_ERR_DIVIDE_BY_ZERO
);
956 if (!wxStrcmp(SQLState
, "22026"))
957 return(DB_ERR_STR_DATA_LENGTH_MISMATCH
);
958 if (!wxStrcmp(SQLState
, "23000"))
959 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
960 if (!wxStrcmp(SQLState
, "24000"))
961 return(DB_ERR_INVALID_CURSOR_STATE
);
962 if (!wxStrcmp(SQLState
, "25000"))
963 return(DB_ERR_INVALID_TRANS_STATE
);
964 if (!wxStrcmp(SQLState
, "28000"))
965 return(DB_ERR_INVALID_AUTH_SPEC
);
966 if (!wxStrcmp(SQLState
, "34000"))
967 return(DB_ERR_INVALID_CURSOR_NAME
);
968 if (!wxStrcmp(SQLState
, "37000"))
969 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL
);
970 if (!wxStrcmp(SQLState
, "3C000"))
971 return(DB_ERR_DUPLICATE_CURSOR_NAME
);
972 if (!wxStrcmp(SQLState
, "40001"))
973 return(DB_ERR_SERIALIZATION_FAILURE
);
974 if (!wxStrcmp(SQLState
, "42000"))
975 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2
);
976 if (!wxStrcmp(SQLState
, "70100"))
977 return(DB_ERR_OPERATION_ABORTED
);
978 if (!wxStrcmp(SQLState
, "IM001"))
979 return(DB_ERR_UNSUPPORTED_FUNCTION
);
980 if (!wxStrcmp(SQLState
, "IM002"))
981 return(DB_ERR_NO_DATA_SOURCE
);
982 if (!wxStrcmp(SQLState
, "IM003"))
983 return(DB_ERR_DRIVER_LOAD_ERROR
);
984 if (!wxStrcmp(SQLState
, "IM004"))
985 return(DB_ERR_SQLALLOCENV_FAILED
);
986 if (!wxStrcmp(SQLState
, "IM005"))
987 return(DB_ERR_SQLALLOCCONNECT_FAILED
);
988 if (!wxStrcmp(SQLState
, "IM006"))
989 return(DB_ERR_SQLSETCONNECTOPTION_FAILED
);
990 if (!wxStrcmp(SQLState
, "IM007"))
991 return(DB_ERR_NO_DATA_SOURCE_DLG_PROHIB
);
992 if (!wxStrcmp(SQLState
, "IM008"))
993 return(DB_ERR_DIALOG_FAILED
);
994 if (!wxStrcmp(SQLState
, "IM009"))
995 return(DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL
);
996 if (!wxStrcmp(SQLState
, "IM010"))
997 return(DB_ERR_DATA_SOURCE_NAME_TOO_LONG
);
998 if (!wxStrcmp(SQLState
, "IM011"))
999 return(DB_ERR_DRIVER_NAME_TOO_LONG
);
1000 if (!wxStrcmp(SQLState
, "IM012"))
1001 return(DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR
);
1002 if (!wxStrcmp(SQLState
, "IM013"))
1003 return(DB_ERR_TRACE_FILE_ERROR
);
1004 if (!wxStrcmp(SQLState
, "S0001"))
1005 return(DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS
);
1006 if (!wxStrcmp(SQLState
, "S0002"))
1007 return(DB_ERR_TABLE_NOT_FOUND
);
1008 if (!wxStrcmp(SQLState
, "S0011"))
1009 return(DB_ERR_INDEX_ALREADY_EXISTS
);
1010 if (!wxStrcmp(SQLState
, "S0012"))
1011 return(DB_ERR_INDEX_NOT_FOUND
);
1012 if (!wxStrcmp(SQLState
, "S0021"))
1013 return(DB_ERR_COLUMN_ALREADY_EXISTS
);
1014 if (!wxStrcmp(SQLState
, "S0022"))
1015 return(DB_ERR_COLUMN_NOT_FOUND
);
1016 if (!wxStrcmp(SQLState
, "S0023"))
1017 return(DB_ERR_NO_DEFAULT_FOR_COLUMN
);
1018 if (!wxStrcmp(SQLState
, "S1000"))
1019 return(DB_ERR_GENERAL_ERROR
);
1020 if (!wxStrcmp(SQLState
, "S1001"))
1021 return(DB_ERR_MEMORY_ALLOCATION_FAILURE
);
1022 if (!wxStrcmp(SQLState
, "S1002"))
1023 return(DB_ERR_INVALID_COLUMN_NUMBER
);
1024 if (!wxStrcmp(SQLState
, "S1003"))
1025 return(DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE
);
1026 if (!wxStrcmp(SQLState
, "S1004"))
1027 return(DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE
);
1028 if (!wxStrcmp(SQLState
, "S1008"))
1029 return(DB_ERR_OPERATION_CANCELLED
);
1030 if (!wxStrcmp(SQLState
, "S1009"))
1031 return(DB_ERR_INVALID_ARGUMENT_VALUE
);
1032 if (!wxStrcmp(SQLState
, "S1010"))
1033 return(DB_ERR_FUNCTION_SEQUENCE_ERROR
);
1034 if (!wxStrcmp(SQLState
, "S1011"))
1035 return(DB_ERR_OPERATION_INVALID_AT_THIS_TIME
);
1036 if (!wxStrcmp(SQLState
, "S1012"))
1037 return(DB_ERR_INVALID_TRANS_OPERATION_CODE
);
1038 if (!wxStrcmp(SQLState
, "S1015"))
1039 return(DB_ERR_NO_CURSOR_NAME_AVAIL
);
1040 if (!wxStrcmp(SQLState
, "S1090"))
1041 return(DB_ERR_INVALID_STR_OR_BUF_LEN
);
1042 if (!wxStrcmp(SQLState
, "S1091"))
1043 return(DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE
);
1044 if (!wxStrcmp(SQLState
, "S1092"))
1045 return(DB_ERR_OPTION_TYPE_OUT_OF_RANGE
);
1046 if (!wxStrcmp(SQLState
, "S1093"))
1047 return(DB_ERR_INVALID_PARAM_NO
);
1048 if (!wxStrcmp(SQLState
, "S1094"))
1049 return(DB_ERR_INVALID_SCALE_VALUE
);
1050 if (!wxStrcmp(SQLState
, "S1095"))
1051 return(DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE
);
1052 if (!wxStrcmp(SQLState
, "S1096"))
1053 return(DB_ERR_INF_TYPE_OUT_OF_RANGE
);
1054 if (!wxStrcmp(SQLState
, "S1097"))
1055 return(DB_ERR_COLUMN_TYPE_OUT_OF_RANGE
);
1056 if (!wxStrcmp(SQLState
, "S1098"))
1057 return(DB_ERR_SCOPE_TYPE_OUT_OF_RANGE
);
1058 if (!wxStrcmp(SQLState
, "S1099"))
1059 return(DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE
);
1060 if (!wxStrcmp(SQLState
, "S1100"))
1061 return(DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE
);
1062 if (!wxStrcmp(SQLState
, "S1101"))
1063 return(DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE
);
1064 if (!wxStrcmp(SQLState
, "S1103"))
1065 return(DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE
);
1066 if (!wxStrcmp(SQLState
, "S1104"))
1067 return(DB_ERR_INVALID_PRECISION_VALUE
);
1068 if (!wxStrcmp(SQLState
, "S1105"))
1069 return(DB_ERR_INVALID_PARAM_TYPE
);
1070 if (!wxStrcmp(SQLState
, "S1106"))
1071 return(DB_ERR_FETCH_TYPE_OUT_OF_RANGE
);
1072 if (!wxStrcmp(SQLState
, "S1107"))
1073 return(DB_ERR_ROW_VALUE_OUT_OF_RANGE
);
1074 if (!wxStrcmp(SQLState
, "S1108"))
1075 return(DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE
);
1076 if (!wxStrcmp(SQLState
, "S1109"))
1077 return(DB_ERR_INVALID_CURSOR_POSITION
);
1078 if (!wxStrcmp(SQLState
, "S1110"))
1079 return(DB_ERR_INVALID_DRIVER_COMPLETION
);
1080 if (!wxStrcmp(SQLState
, "S1111"))
1081 return(DB_ERR_INVALID_BOOKMARK_VALUE
);
1082 if (!wxStrcmp(SQLState
, "S1C00"))
1083 return(DB_ERR_DRIVER_NOT_CAPABLE
);
1084 if (!wxStrcmp(SQLState
, "S1T00"))
1085 return(DB_ERR_TIMEOUT_EXPIRED
);
1090 } // wxDB::TranslateSqlState()
1092 /********** wxDB::Grant() **********/
1093 bool wxDB::Grant(int privileges
, char *tableName
, char *userList
)
1095 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1097 // Build the grant statement
1098 strcpy(sqlStmt
, "GRANT ");
1099 if (privileges
== DB_GRANT_ALL
)
1100 strcat(sqlStmt
, "ALL");
1104 if (privileges
& DB_GRANT_SELECT
)
1106 strcat(sqlStmt
, "SELECT");
1109 if (privileges
& DB_GRANT_INSERT
)
1112 strcat(sqlStmt
, ", ");
1113 strcat(sqlStmt
, "INSERT");
1115 if (privileges
& DB_GRANT_UPDATE
)
1118 strcat(sqlStmt
, ", ");
1119 strcat(sqlStmt
, "UPDATE");
1121 if (privileges
& DB_GRANT_DELETE
)
1124 strcat(sqlStmt
, ", ");
1125 strcat(sqlStmt
, "DELETE");
1129 strcat(sqlStmt
, " ON ");
1130 strcat(sqlStmt
, tableName
);
1131 strcat(sqlStmt
, " TO ");
1132 strcat(sqlStmt
, userList
);
1134 #ifdef DBDEBUG_CONSOLE
1135 cout
<< endl
<< sqlStmt
<< endl
;
1138 WriteSqlLog(sqlStmt
);
1140 return(ExecSql(sqlStmt
));
1144 /********** wxDB::CreateView() **********/
1145 bool wxDB::CreateView(char *viewName
, char *colList
, char *pSqlStmt
, bool attemptDrop
)
1147 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1149 // Drop the view first
1150 if (attemptDrop
&& !DropView(viewName
))
1153 // Build the create view statement
1154 strcpy(sqlStmt
, "CREATE VIEW ");
1155 strcat(sqlStmt
, viewName
);
1157 if (strlen(colList
))
1159 strcat(sqlStmt
, " (");
1160 strcat(sqlStmt
, colList
);
1161 strcat(sqlStmt
, ")");
1164 strcat(sqlStmt
, " AS ");
1165 strcat(sqlStmt
, pSqlStmt
);
1167 WriteSqlLog(sqlStmt
);
1169 #ifdef DBDEBUG_CONSOLE
1170 cout
<< sqlStmt
<< endl
;
1173 return(ExecSql(sqlStmt
));
1175 } // wxDB::CreateView()
1177 /********** wxDB::DropView() **********/
1178 bool wxDB::DropView(char *viewName
)
1180 // NOTE: This function returns TRUE if the View does not exist, but
1181 // only for identified databases. Code will need to be added
1182 // below for any other databases when those databases are defined
1183 // to handle this situation consistently
1185 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1187 sprintf(sqlStmt
, "DROP VIEW %s", viewName
);
1189 WriteSqlLog(sqlStmt
);
1191 #ifdef DBDEBUG_CONSOLE
1192 cout
<< endl
<< sqlStmt
<< endl
;
1195 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1197 // Check for "Base table not found" error and ignore
1198 GetNextError(henv
, hdbc
, hstmt
);
1199 if (wxStrcmp(sqlState
,"S0002")) // "Base table not found"
1201 // Check for product specific error codes
1202 if (!((Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(sqlState
,"42000")))) // 5.x (and lower?)
1205 DispAllErrors(henv
, hdbc
, hstmt
);
1212 // Commit the transaction
1213 if (! CommitTrans())
1218 } // wxDB::DropView()
1221 /********** wxDB::ExecSql() **********/
1222 bool wxDB::ExecSql(char *pSqlStmt
)
1224 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1225 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) == SQL_SUCCESS
)
1229 DispAllErrors(henv
, hdbc
, hstmt
);
1233 } // wxDB::ExecSql()
1235 /********** wxDB::GetNext() **********/
1236 bool wxDB::GetNext(void)
1238 if (SQLFetch(hstmt
) == SQL_SUCCESS
)
1242 DispAllErrors(henv
, hdbc
, hstmt
);
1246 } // wxDB::GetNext()
1248 /********** wxDB::GetData() **********/
1249 bool wxDB::GetData(UWORD colNo
, SWORD cType
, PTR pData
, SDWORD maxLen
, SDWORD FAR
*cbReturned
)
1254 if (SQLGetData(hstmt
, colNo
, cType
, pData
, maxLen
, cbReturned
) == SQL_SUCCESS
)
1258 DispAllErrors(henv
, hdbc
, hstmt
);
1262 } // wxDB::GetData()
1264 /********** wxDB::GetColumns() **********/
1266 * 1) The last array element of the tableName[] argument must be zero (null).
1267 * This is how the end of the array is detected.
1268 * 2) This function returns an array of CcolInf structures. If no columns
1269 * were found, or an error occured, this pointer will be zero (null). THE
1270 * CALLING FUNCTION IS RESPONSIBLE FOR DELETING THE MEMORY RETURNED WHEN IT
1271 * IS FINISHED WITH IT. i.e.
1273 * CcolInf *colInf = pDb->GetColumns(tableList, userID);
1276 * // Use the column inf
1278 * // Destroy the memory
1282 CcolInf
*wxDB::GetColumns(char *tableName
[], char *userID
)
1286 CcolInf
*colInf
= 0;
1289 char tblName
[DB_MAX_TABLE_NAME_LEN
+1];
1290 char colName
[DB_MAX_COLUMN_NAME_LEN
+1];
1292 char userIdUC
[80+1];
1293 char tableNameUC
[DB_MAX_TABLE_NAME_LEN
+1];
1295 if (!userID
|| !strlen(userID
))
1298 // dBase does not use user names, and some drivers fail if you try to pass one
1299 if (Dbms() == dbmsDBASE
)
1302 // Oracle user names may only be in uppercase, so force
1303 // the name to uppercase
1304 if (Dbms() == dbmsORACLE
)
1307 for (char *p
= userID
; *p
; p
++)
1308 userIdUC
[i
++] = toupper(*p
);
1313 // Pass 1 - Determine how many columns there are.
1314 // Pass 2 - Allocate the CcolInf array and fill in
1315 // the array with the column information.
1317 for (pass
= 1; pass
<= 2; pass
++)
1321 if (noCols
== 0) // Probably a bogus table name(s)
1323 // Allocate n CcolInf objects to hold the column information
1324 colInf
= new CcolInf
[noCols
+1];
1327 // Mark the end of the array
1328 strcpy(colInf
[noCols
].tableName
, "");
1329 strcpy(colInf
[noCols
].colName
, "");
1330 colInf
[noCols
].sqlDataType
= 0;
1332 // Loop through each table name
1334 for (tbl
= 0; tableName
[tbl
]; tbl
++)
1336 // Oracle table names are uppercase only, so force
1337 // the name to uppercase just in case programmer forgot to do this
1338 if (Dbms() == dbmsORACLE
)
1341 for (char *p
= tableName
[tbl
]; *p
; p
++)
1342 tableNameUC
[i
++] = toupper(*p
);
1346 sprintf(tableNameUC
,tableName
[tbl
]);
1348 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1350 // MySQL and Access cannot accept a user name when looking up column names, so we
1351 // use the call below that leaves out the user name
1352 if (wxStrcmp(userID
,"") &&
1353 Dbms() != dbmsMY_SQL
&&
1354 Dbms() != dbmsACCESS
)
1356 retcode
= SQLColumns(hstmt
,
1357 NULL
, 0, // All qualifiers
1358 (UCHAR
*) userID
, SQL_NTS
, // Owner
1359 (UCHAR
*) tableNameUC
, SQL_NTS
,
1360 NULL
, 0); // All columns
1364 retcode
= SQLColumns(hstmt
,
1365 NULL
, 0, // All qualifiers
1367 (UCHAR
*) tableNameUC
, SQL_NTS
,
1368 NULL
, 0); // All columns
1370 if (retcode
!= SQL_SUCCESS
)
1371 { // Error occured, abort
1372 DispAllErrors(henv
, hdbc
, hstmt
);
1375 SQLFreeStmt(hstmt
, SQL_UNBIND
);
1376 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1379 SQLBindCol(hstmt
, 3, SQL_C_CHAR
, (UCHAR
*) tblName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
1380 SQLBindCol(hstmt
, 4, SQL_C_CHAR
, (UCHAR
*) colName
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1381 SQLBindCol(hstmt
, 5, SQL_C_SSHORT
, (UCHAR
*) &sqlDataType
, 0, &cb
);
1382 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
1384 if (pass
== 1) // First pass, just add up the number of columns
1386 else // Pass 2; Fill in the array of structures
1388 if (colNo
< noCols
) // Some extra error checking to prevent memory overwrites
1390 strcpy(colInf
[colNo
].tableName
, tblName
);
1391 strcpy(colInf
[colNo
].colName
, colName
);
1392 colInf
[colNo
].sqlDataType
= sqlDataType
;
1397 if (retcode
!= SQL_NO_DATA_FOUND
)
1398 { // Error occured, abort
1399 DispAllErrors(henv
, hdbc
, hstmt
);
1402 SQLFreeStmt(hstmt
, SQL_UNBIND
);
1403 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1409 SQLFreeStmt(hstmt
, SQL_UNBIND
);
1410 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1413 } // wxDB::GetColumns()
1416 /********** wxDB::Catalog() **********/
1417 bool wxDB::Catalog(char *userID
, char *fileName
)
1419 assert(fileName
&& strlen(fileName
));
1423 char tblName
[DB_MAX_TABLE_NAME_LEN
+1];
1424 char tblNameSave
[DB_MAX_TABLE_NAME_LEN
+1];
1425 char colName
[DB_MAX_COLUMN_NAME_LEN
+1];
1427 char typeName
[30+1];
1428 SWORD precision
, length
;
1430 FILE *fp
= fopen(fileName
,"wt");
1434 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1436 if (!userID
|| !strlen(userID
))
1439 char userIdUC
[80+1];
1440 // Oracle user names may only be in uppercase, so force
1441 // the name to uppercase
1442 if (Dbms() == dbmsORACLE
)
1445 for (char *p
= userID
; *p
; p
++)
1446 userIdUC
[i
++] = toupper(*p
);
1451 if (wxStrcmp(userID
,""))
1453 retcode
= SQLColumns(hstmt
,
1454 NULL
, 0, // All qualifiers
1455 (UCHAR
*) userID
, SQL_NTS
, // User specified
1456 NULL
, 0, // All tables
1457 NULL
, 0); // All columns
1461 retcode
= SQLColumns(hstmt
,
1462 NULL
, 0, // All qualifiers
1463 NULL
, 0, // User specified
1464 NULL
, 0, // All tables
1465 NULL
, 0); // All columns
1467 if (retcode
!= SQL_SUCCESS
)
1469 DispAllErrors(henv
, hdbc
, hstmt
);
1474 SQLBindCol(hstmt
, 3, SQL_C_CHAR
, (UCHAR
*) tblName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
1475 SQLBindCol(hstmt
, 4, SQL_C_CHAR
, (UCHAR
*) colName
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1476 SQLBindCol(hstmt
, 5, SQL_C_SSHORT
, (UCHAR
*) &sqlDataType
, 0, &cb
);
1477 SQLBindCol(hstmt
, 6, SQL_C_CHAR
, (UCHAR
*) typeName
, sizeof(typeName
), &cb
);
1478 SQLBindCol(hstmt
, 7, SQL_C_SSHORT
, (UCHAR
*) &precision
, 0, &cb
);
1479 SQLBindCol(hstmt
, 8, SQL_C_SSHORT
, (UCHAR
*) &length
, 0, &cb
);
1482 strcpy(tblNameSave
,"");
1485 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
1487 if (wxStrcmp(tblName
,tblNameSave
))
1491 fputs("================================ ", fp
);
1492 fputs("================================ ", fp
);
1493 fputs("===================== ", fp
);
1494 fputs("========= ", fp
);
1495 fputs("=========\n", fp
);
1496 sprintf(outStr
, "%-32s %-32s %-21s %9s %9s\n",
1497 "TABLE NAME", "COLUMN NAME", "DATA TYPE", "PRECISION", "LENGTH");
1499 fputs("================================ ", fp
);
1500 fputs("================================ ", fp
);
1501 fputs("===================== ", fp
);
1502 fputs("========= ", fp
);
1503 fputs("=========\n", fp
);
1504 strcpy(tblNameSave
,tblName
);
1506 sprintf(outStr
, "%-32s %-32s (%04d)%-15s %9d %9d\n",
1507 tblName
, colName
, sqlDataType
, typeName
, precision
, length
);
1508 if (fputs(outStr
, fp
) == EOF
)
1510 SQLFreeStmt(hstmt
, SQL_UNBIND
);
1511 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1518 if (retcode
!= SQL_NO_DATA_FOUND
)
1519 DispAllErrors(henv
, hdbc
, hstmt
);
1521 SQLFreeStmt(hstmt
, SQL_UNBIND
);
1522 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1525 return(retcode
== SQL_NO_DATA_FOUND
);
1527 } // wxDB::Catalog()
1530 // Table name can refer to a table, view, alias or synonym. Returns true
1531 // if the object exists in the database. This function does not indicate
1532 // whether or not the user has privleges to query or perform other functions
1534 bool wxDB::TableExists(char *tableName
, char *userID
, char *tablePath
)
1536 assert(tableName
&& strlen(tableName
));
1538 if (Dbms() == dbmsDBASE
)
1541 if (tablePath
&& strlen(tablePath
))
1542 dbName
.sprintf("%s/%s.dbf",tablePath
,tableName
);
1544 dbName
.sprintf("%s.dbf",tableName
);
1546 glt
= wxFileExists(dbName
.GetData());
1550 if (!userID
|| !strlen(userID
))
1553 char userIdUC
[80+1];
1554 // Oracle user names may only be in uppercase, so force
1555 // the name to uppercase
1556 if (Dbms() == dbmsORACLE
)
1559 for (char *p
= userID
; *p
; p
++)
1560 userIdUC
[i
++] = toupper(*p
);
1565 char tableNameUC
[DB_MAX_TABLE_NAME_LEN
+1];
1566 // Oracle table names are uppercase only, so force
1567 // the name to uppercase just in case programmer forgot to do this
1568 if (Dbms() == dbmsORACLE
)
1571 for (char *p
= tableName
; *p
; p
++)
1572 tableNameUC
[i
++] = toupper(*p
);
1576 sprintf(tableNameUC
,tableName
);
1578 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1581 // MySQL and Access cannot accept a user name when looking up table names, so we
1582 // use the call below that leaves out the user name
1583 if (wxStrcmp(userID
,"") &&
1584 Dbms() != dbmsMY_SQL
&&
1585 Dbms() != dbmsACCESS
)
1587 retcode
= SQLTables(hstmt
,
1588 NULL
, 0, // All qualifiers
1589 (UCHAR
*) userID
, SQL_NTS
, // All owners
1590 (UCHAR FAR
*)tableNameUC
, SQL_NTS
,
1591 NULL
, 0); // All table types
1595 retcode
= SQLTables(hstmt
,
1596 NULL
, 0, // All qualifiers
1597 NULL
, 0, // All owners
1598 (UCHAR FAR
*)tableNameUC
, SQL_NTS
,
1599 NULL
, 0); // All table types
1601 if (retcode
!= SQL_SUCCESS
)
1602 return(DispAllErrors(henv
, hdbc
, hstmt
));
1604 retcode
= SQLFetch(hstmt
);
1605 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1607 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1608 return(DispAllErrors(henv
, hdbc
, hstmt
));
1611 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1614 } // wxDB::TableExists()
1617 /********** wxDB::SqlLog() **********/
1618 bool wxDB::SqlLog(enum sqlLog state
, char *filename
, bool append
)
1620 assert(state
== sqlLogON
|| state
== sqlLogOFF
);
1621 assert(state
== sqlLogOFF
|| filename
);
1623 if (state
== sqlLogON
)
1627 fpSqlLog
= fopen(filename
, (append
? "at" : "wt"));
1628 if (fpSqlLog
== NULL
)
1636 if (fclose(fpSqlLog
))
1642 sqlLogState
= state
;
1648 /********** wxDB::WriteSqlLog() **********/
1649 bool wxDB::WriteSqlLog(char *logMsg
)
1653 if (fpSqlLog
== 0 || sqlLogState
== sqlLogOFF
)
1656 if (fputs("\n", fpSqlLog
) == EOF
) return(FALSE
);
1657 if (fputs(logMsg
, fpSqlLog
) == EOF
) return(FALSE
);
1658 if (fputs("\n", fpSqlLog
) == EOF
) return(FALSE
);
1662 } // wxDB::WriteSqlLog()
1665 /********** wxDB::Dbms() **********/
1667 * Be aware that not all database engines use the exact same syntax, and not
1668 * every ODBC compliant database is compliant to the same level of compliancy.
1669 * Some manufacturers support the minimum Level 1 compliancy, and others up
1670 * through Level 3. Others support subsets of features for levels above 1.
1672 * If you find an inconsistency between the wxDB class and a specific database
1673 * engine, and an identifier to this section, and special handle the database in
1674 * the area where behavior is non-conforming with the other databases.
1677 * NOTES ABOUT ISSUES SPECIFIC TO EACH DATABASE ENGINE
1678 * ---------------------------------------------------
1681 * - Currently the only database supported by the class to support VIEWS
1684 * - Does not support the SQL_TIMESTAMP structure
1685 * - Supports only one cursor and one connect (apparently? with Microsoft driver only?)
1686 * - Does not automatically create the primary index if the 'keyField' param of SetColDef
1687 * is TRUE. The user must create ALL indexes from their program.
1688 * - Table names can only be 8 characters long
1689 * - Column names can only be 10 characters long
1692 * - To lock a record during QUERY functions, the reserved word 'HOLDLOCK' must be added
1693 * after every table name involved in the query/join if that tables matching record(s)
1695 * - Ignores the keywords 'FOR UPDATE'. Use the HOLDLOCK functionality described above
1697 * SYBASE (Enterprise)
1698 * - If a column is part of the Primary Key, the column cannot be NULL
1701 * - If a column is part of the Primary Key, the column cannot be NULL
1702 * - Cannot support selecting for update [::CanSelectForUpdate()]. Always returns FALSE
1705 * - Does not support the keywords 'ASC' or 'DESC' as of release v6.5.0
1709 DBMS
wxDB::Dbms(void)
1711 wxChar baseName
[20];
1713 wxStrncpy(baseName
,dbInf
.dbmsName
,6);
1715 // if (!wxStrnicmp(dbInf.dbmsName,"Oracle",6))
1716 if (!wxStricmp(baseName
,"Oracle"))
1718 if (!wxStricmp(dbInf
.dbmsName
,"Adaptive Server Anywhere"))
1719 return(dbmsSYBASE_ASA
);
1720 if (!wxStricmp(dbInf
.dbmsName
,"SQL Server")) // Sybase Adaptive Server Enterprise
1721 return(dbmsSYBASE_ASE
);
1722 if (!wxStricmp(dbInf
.dbmsName
,"Microsoft SQL Server"))
1723 return(dbmsMS_SQL_SERVER
);
1724 if (!wxStricmp(dbInf
.dbmsName
,"MySQL"))
1726 if (!wxStricmp(dbInf
.dbmsName
,"PostgreSQL")) // v6.5.0
1727 return(dbmsPOSTGRES
);
1728 if (!wxStricmp(dbInf
.dbmsName
,"ACCESS"))
1730 wxStrncpy(baseName
,dbInf
.dbmsName
,5);
1732 // if (!wxStrnicmp(dbInf.dbmsName,"DBASE",5))
1733 if (!wxStricmp(baseName
,"DBASE"))
1735 return(dbmsUNIDENTIFIED
);
1740 /********** GetDbConnection() **********/
1741 wxDB WXDLLEXPORT
*GetDbConnection(DbStuff
*pDbStuff
)
1745 // Scan the linked list searching for an available database connection
1746 // that's already been opened but is currently not in use.
1747 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1749 // The database connection must be for the same datasource
1750 // name and must currently not be in use.
1751 if (pList
->Free
&& (! wxStrcmp(pDbStuff
->Dsn
, pList
->Dsn
))) // Found a free connection
1753 pList
->Free
= FALSE
;
1754 return(pList
->PtrDb
);
1758 // No available connections. A new connection must be made and
1759 // appended to the end of the linked list.
1762 // Find the end of the list
1763 for (pList
= PtrBegDbList
; pList
->PtrNext
; pList
= pList
->PtrNext
);
1764 // Append a new list item
1765 pList
->PtrNext
= new DbList
;
1766 pList
->PtrNext
->PtrPrev
= pList
;
1767 pList
= pList
->PtrNext
;
1771 // Create the first node on the list
1772 pList
= PtrBegDbList
= new DbList
;
1776 // Initialize new node in the linked list
1778 pList
->Free
= FALSE
;
1779 strcpy(pList
->Dsn
, pDbStuff
->Dsn
);
1780 pList
->PtrDb
= new wxDB(pDbStuff
->Henv
);
1782 // Connect to the datasource
1783 if (pList
->PtrDb
->Open(pDbStuff
->Dsn
, pDbStuff
->Uid
, pDbStuff
->AuthStr
))
1785 pList
->PtrDb
->SqlLog(SQLLOGstate
,SQLLOGfn
,TRUE
);
1786 return(pList
->PtrDb
);
1788 else // Unable to connect, destroy list item
1791 pList
->PtrPrev
->PtrNext
= 0;
1793 PtrBegDbList
= 0; // Empty list again
1794 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDB object
1795 pList
->PtrDb
->Close(); // Close the wxDB object
1796 delete pList
->PtrDb
; // Deletes the wxDB object
1797 delete pList
; // Deletes the linked list object
1801 } // GetDbConnection()
1803 /********** FreeDbConnection() **********/
1804 bool WXDLLEXPORT
FreeDbConnection(wxDB
*pDb
)
1808 // Scan the linked list searching for the database connection
1809 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1811 if (pList
->PtrDb
== pDb
) // Found it!!!
1812 return(pList
->Free
= TRUE
);
1815 // Never found the database object, return failure
1818 } // FreeDbConnection()
1820 /********** CloseDbConnections() **********/
1821 void WXDLLEXPORT
CloseDbConnections(void)
1823 DbList
*pList
, *pNext
;
1825 // Traverse the linked list closing database connections and freeing memory as I go.
1826 for (pList
= PtrBegDbList
; pList
; pList
= pNext
)
1828 pNext
= pList
->PtrNext
; // Save the pointer to next
1829 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDB object
1830 pList
->PtrDb
->Close(); // Close the wxDB object
1831 delete pList
->PtrDb
; // Deletes the wxDB object
1832 delete pList
; // Deletes the linked list object
1835 // Mark the list as empty
1838 } // CloseDbConnections()
1840 /********** NumberDbConnectionsInUse() **********/
1841 int WXDLLEXPORT
NumberDbConnectionsInUse(void)
1846 // Scan the linked list counting db connections that are currently in use
1847 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1849 if (pList
->Free
== FALSE
)
1855 } // NumberDbConnectionsInUse()
1857 /********** SqlLog() **********/
1858 bool SqlLog(enum sqlLog state
, char *filename
)
1860 bool append
= FALSE
;
1863 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1865 if (!pList
->PtrDb
->SqlLog(state
,filename
,append
))
1870 SQLLOGstate
= state
;
1871 strcpy(SQLLOGfn
,filename
);
1877 /********** GetDataSource() **********/
1878 bool GetDataSource(HENV henv
, char *Dsn
, SWORD DsnMax
, char *DsDesc
, SWORD DsDescMax
,
1883 if (SQLDataSources(henv
, direction
, (UCHAR FAR
*) Dsn
, DsnMax
, &cb
,
1884 (UCHAR FAR
*) DsDesc
, DsDescMax
, &cb
) == SQL_SUCCESS
)
1889 } // GetDataSource()