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 // Use this line for wxWindows v1.x
38 // Use this line for wxWindows v2.x
39 #include "wx/version.h"
40 #include "wx/wxprec.h"
42 #if wxMAJOR_VERSION == 2
44 #pragma implementation "db.h"
48 #ifdef DBDEBUG_CONSOLE
56 #if wxMAJOR_VERSION == 2
58 #include "wx/string.h"
60 #include "wx/filefn.h"
63 #if wxMAJOR_VERSION == 1
64 # if defined(wx_msw) || defined(wx_x)
81 #if wxMAJOR_VERSION == 1
83 #elif wxMAJOR_VERSION == 2
87 DbList WXDLLEXPORT
*PtrBegDbList
= 0;
90 #if wxMAJOR_VERSION == 2
91 #include "wx/object.h"
94 #include "wx/msgdlg.h"
96 extern wxList TablesInUse
;
99 // SQL Log defaults to be used by GetDbConnection
100 enum sqlLog SQLLOGstate
= sqlLogOFF
;
102 char SQLLOGfn
[DB_PATH_MAX
+1] = "sqllog.txt";
104 // The wxDB::errorList is copied to this variable when the wxDB object
105 // is closed. This way, the error list is still available after the
106 // database object is closed. This is necessary if the database
107 // connection fails so the calling application can show the operator
108 // why the connection failed. Note: as each wxDB object is closed, it
109 // will overwrite the errors of the previously destroyed wxDB object in
111 char DBerrorList
[DB_MAX_ERROR_HISTORY
][DB_MAX_ERROR_MSG_LEN
];
113 /********** wxDB Constructor **********/
114 wxDB::wxDB(HENV
&aHenv
)
118 fpSqlLog
= 0; // Sql Log file pointer
119 sqlLogState
= sqlLogOFF
; // By default, logging is turned off
124 nativeError
= cbErrorMsg
= 0;
125 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
126 strcpy(errorList
[i
], "");
128 // Init typeInf structures
129 strcpy(typeInfVarchar
.TypeName
,"");
130 typeInfVarchar
.FsqlType
= 0;
131 typeInfVarchar
.Precision
= 0;
132 typeInfVarchar
.CaseSensitive
= 0;
133 typeInfVarchar
.MaximumScale
= 0;
135 strcpy(typeInfInteger
.TypeName
,"");
136 typeInfInteger
.FsqlType
= 0;
137 typeInfInteger
.Precision
= 0;
138 typeInfInteger
.CaseSensitive
= 0;
139 typeInfInteger
.MaximumScale
= 0;
141 strcpy(typeInfFloat
.TypeName
,"");
142 typeInfFloat
.FsqlType
= 0;
143 typeInfFloat
.Precision
= 0;
144 typeInfFloat
.CaseSensitive
= 0;
145 typeInfFloat
.MaximumScale
= 0;
147 strcpy(typeInfDate
.TypeName
,"");
148 typeInfDate
.FsqlType
= 0;
149 typeInfDate
.Precision
= 0;
150 typeInfDate
.CaseSensitive
= 0;
151 typeInfDate
.MaximumScale
= 0;
153 // Error reporting is turned OFF by default
156 // Copy the HENV into the db class
159 // Allocate a data source connection handle
160 if (SQLAllocConnect(henv
, &hdbc
) != SQL_SUCCESS
)
163 // Initialize the db status flag
166 // Mark database as not open as of yet
171 /********** wxDB::Open() **********/
172 bool wxDB::Open(char *Dsn
, char *Uid
, char *AuthStr
)
174 assert(Dsn
&& strlen(Dsn
));
181 #ifndef FWD_ONLY_CURSORS
183 // Specify that the ODBC cursor library be used, if needed. This must be
184 // specified before the connection is made.
185 retcode
= SQLSetConnectOption(hdbc
, SQL_ODBC_CURSORS
, SQL_CUR_USE_IF_NEEDED
);
187 #ifdef DBDEBUG_CONSOLE
188 if (retcode
== SQL_SUCCESS
)
189 cout
<< "SQLSetConnectOption(CURSOR_LIB) successful" << endl
;
191 cout
<< "SQLSetConnectOption(CURSOR_LIB) failed" << endl
;
196 // Connect to the data source
197 retcode
= SQLConnect(hdbc
, (UCHAR FAR
*) Dsn
, SQL_NTS
,
198 (UCHAR FAR
*) Uid
, SQL_NTS
,
199 (UCHAR FAR
*) AuthStr
,SQL_NTS
);
200 if (retcode
== SQL_SUCCESS_WITH_INFO
)
201 DispAllErrors(henv
, hdbc
);
202 else if (retcode
!= SQL_SUCCESS
)
203 return(DispAllErrors(henv
, hdbc
));
206 If using Intersolv branded ODBC drivers, this is the place where you would substitute
207 your branded driver license information
209 SQLSetConnectOption(hdbc, 1041, (UDWORD) "");
210 SQLSetConnectOption(hdbc, 1042, (UDWORD) "");
212 // Mark database as open
215 // Allocate a statement handle for the database connection
216 if (SQLAllocStmt(hdbc
, &hstmt
) != SQL_SUCCESS
)
217 return(DispAllErrors(henv
, hdbc
));
219 // Set Connection Options
220 if (! setConnectionOptions())
223 // Query the data source for inf. about itself
227 // Query the data source regarding data type information
230 // The way I determined which SQL data types to use was by calling SQLGetInfo
231 // for all of the possible SQL data types to see which ones were supported. If
232 // a type is not supported, the SQLFetch() that's called from getDataTypeInfo()
233 // fails with SQL_NO_DATA_FOUND. This is ugly because I'm sure the three SQL data
234 // types I've selected below will not alway's be what we want. These are just
235 // what happened to work against an Oracle 7/Intersolv combination. The following is
236 // a complete list of the results I got back against the Oracle 7 database:
238 // SQL_BIGINT SQL_NO_DATA_FOUND
239 // SQL_BINARY SQL_NO_DATA_FOUND
240 // SQL_BIT SQL_NO_DATA_FOUND
241 // SQL_CHAR type name = 'CHAR', Precision = 255
242 // SQL_DATE SQL_NO_DATA_FOUND
243 // SQL_DECIMAL type name = 'NUMBER', Precision = 38
244 // SQL_DOUBLE type name = 'NUMBER', Precision = 15
245 // SQL_FLOAT SQL_NO_DATA_FOUND
246 // SQL_INTEGER SQL_NO_DATA_FOUND
247 // SQL_LONGVARBINARY type name = 'LONG RAW', Precision = 2 billion
248 // SQL_LONGVARCHAR type name = 'LONG', Precision = 2 billion
249 // SQL_NUMERIC SQL_NO_DATA_FOUND
250 // SQL_REAL SQL_NO_DATA_FOUND
251 // SQL_SMALLINT SQL_NO_DATA_FOUND
252 // SQL_TIME SQL_NO_DATA_FOUND
253 // SQL_TIMESTAMP type name = 'DATE', Precision = 19
254 // SQL_VARBINARY type name = 'RAW', Precision = 255
255 // SQL_VARCHAR type name = 'VARCHAR2', Precision = 2000
256 // =====================================================================
257 // Results from a Microsoft Access 7.0 db, using a driver from Microsoft
259 // SQL_VARCHAR type name = 'TEXT', Precision = 255
260 // SQL_TIMESTAMP type name = 'DATETIME'
261 // SQL_DECIMAL SQL_NO_DATA_FOUND
262 // SQL_NUMERIC type name = 'CURRENCY', Precision = 19
263 // SQL_FLOAT SQL_NO_DATA_FOUND
264 // SQL_REAL type name = 'SINGLE', Precision = 7
265 // SQL_DOUBLE type name = 'DOUBLE', Precision = 15
266 // SQL_INTEGER type name = 'LONG', Precision = 10
268 // VARCHAR = Variable length character string
269 if (! getDataTypeInfo(SQL_VARCHAR
, typeInfVarchar
))
270 if (! getDataTypeInfo(SQL_CHAR
, typeInfVarchar
))
273 typeInfVarchar
.FsqlType
= SQL_CHAR
;
275 typeInfVarchar
.FsqlType
= SQL_VARCHAR
;
278 if (! getDataTypeInfo(SQL_DOUBLE
, typeInfFloat
))
279 if (! getDataTypeInfo(SQL_REAL
, typeInfFloat
))
280 if (! getDataTypeInfo(SQL_FLOAT
, typeInfFloat
))
281 if (! getDataTypeInfo(SQL_DECIMAL
, typeInfFloat
))
282 if (! getDataTypeInfo(SQL_NUMERIC
, typeInfFloat
))
285 typeInfFloat
.FsqlType
= SQL_NUMERIC
;
287 typeInfFloat
.FsqlType
= SQL_DECIMAL
;
289 typeInfFloat
.FsqlType
= SQL_FLOAT
;
291 typeInfFloat
.FsqlType
= SQL_REAL
;
293 typeInfFloat
.FsqlType
= SQL_DOUBLE
;
296 if (! getDataTypeInfo(SQL_INTEGER
, typeInfInteger
))
297 // If SQL_INTEGER is not supported, use the floating point
298 // data type to store integers as well as floats
299 if (! getDataTypeInfo(typeInfFloat
.FsqlType
, typeInfInteger
))
302 typeInfInteger
.FsqlType
= typeInfFloat
.FsqlType
;
304 typeInfInteger
.FsqlType
= SQL_INTEGER
;
307 if (Dbms() != dbmsDBASE
)
309 if (! getDataTypeInfo(SQL_TIMESTAMP
, typeInfDate
))
312 typeInfDate
.FsqlType
= SQL_TIMESTAMP
;
316 if (! getDataTypeInfo(SQL_DATE
, typeInfDate
))
319 typeInfDate
.FsqlType
= SQL_DATE
;
322 #ifdef DBDEBUG_CONSOLE
323 cout
<< "VARCHAR DATA TYPE: " << typeInfVarchar
.TypeName
<< endl
;
324 cout
<< "INTEGER DATA TYPE: " << typeInfInteger
.TypeName
<< endl
;
325 cout
<< "FLOAT DATA TYPE: " << typeInfFloat
.TypeName
<< endl
;
326 cout
<< "DATE DATA TYPE: " << typeInfDate
.TypeName
<< endl
;
330 // Completed Successfully
335 // The Intersolv/Oracle 7 driver was "Not Capable" of setting the login timeout.
337 /********** wxDB::setConnectionOptions() **********/
338 bool wxDB::setConnectionOptions(void)
340 SQLSetConnectOption(hdbc
, SQL_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
);
341 SQLSetConnectOption(hdbc
, SQL_OPT_TRACE
, SQL_OPT_TRACE_OFF
);
343 // Display the connection options to verify them
344 #ifdef DBDEBUG_CONSOLE
346 cout
<< ">>>>> CONNECTION OPTIONS <<<<<<" << endl
;
348 if (SQLGetConnectOption(hdbc
, SQL_AUTOCOMMIT
, &l
) != SQL_SUCCESS
)
349 return(DispAllErrors(henv
, hdbc
));
350 cout
<< "AUTOCOMMIT: " << (l
== SQL_AUTOCOMMIT_OFF
? "OFF" : "ON") << endl
;
352 if (SQLGetConnectOption(hdbc
, SQL_ODBC_CURSORS
, &l
) != SQL_SUCCESS
)
353 return(DispAllErrors(henv
, hdbc
));
354 cout
<< "ODBC CURSORS: ";
357 case(SQL_CUR_USE_IF_NEEDED
):
358 cout
<< "SQL_CUR_USE_IF_NEEDED";
360 case(SQL_CUR_USE_ODBC
):
361 cout
<< "SQL_CUR_USE_ODBC";
363 case(SQL_CUR_USE_DRIVER
):
364 cout
<< "SQL_CUR_USE_DRIVER";
369 if (SQLGetConnectOption(hdbc
, SQL_OPT_TRACE
, &l
) != SQL_SUCCESS
)
370 return(DispAllErrors(henv
, hdbc
));
371 cout
<< "TRACING: " << (l
== SQL_OPT_TRACE_OFF
? "OFF" : "ON") << endl
;
376 // Completed Successfully
379 } // wxDB::setConnectionOptions()
381 /********** wxDB::getDbInfo() **********/
382 bool wxDB::getDbInfo(void)
387 if (SQLGetInfo(hdbc
, SQL_SERVER_NAME
, (UCHAR
*) dbInf
.serverName
, 80, &cb
) != SQL_SUCCESS
)
388 return(DispAllErrors(henv
, hdbc
));
390 if (SQLGetInfo(hdbc
, SQL_DATABASE_NAME
, (UCHAR
*) dbInf
.databaseName
, 128, &cb
) != SQL_SUCCESS
)
391 return(DispAllErrors(henv
, hdbc
));
393 if (SQLGetInfo(hdbc
, SQL_DBMS_NAME
, (UCHAR
*) dbInf
.dbmsName
, 40, &cb
) != SQL_SUCCESS
)
394 return(DispAllErrors(henv
, hdbc
));
397 // After upgrading to MSVC6, the original 20 char buffer below was insufficient,
398 // causing database connectivity to fail in some cases.
399 retcode
= SQLGetInfo(hdbc
, SQL_DBMS_VER
, (UCHAR
*) dbInf
.dbmsVer
, 64, &cb
);
400 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
401 return(DispAllErrors(henv
, hdbc
));
403 if (SQLGetInfo(hdbc
, SQL_ACTIVE_CONNECTIONS
, (UCHAR
*) &dbInf
.maxConnections
, sizeof(dbInf
.maxConnections
), &cb
) != SQL_SUCCESS
)
404 return(DispAllErrors(henv
, hdbc
));
406 if (SQLGetInfo(hdbc
, SQL_ACTIVE_STATEMENTS
, (UCHAR
*) &dbInf
.maxStmts
, sizeof(dbInf
.maxStmts
), &cb
) != SQL_SUCCESS
)
407 return(DispAllErrors(henv
, hdbc
));
409 if (SQLGetInfo(hdbc
, SQL_DRIVER_NAME
, (UCHAR
*) dbInf
.driverName
, 40, &cb
) != SQL_SUCCESS
)
410 return(DispAllErrors(henv
, hdbc
));
412 if (SQLGetInfo(hdbc
, SQL_DRIVER_ODBC_VER
, (UCHAR
*) dbInf
.odbcVer
, 60, &cb
) == SQL_ERROR
)
413 return(DispAllErrors(henv
, hdbc
));
415 retcode
= SQLGetInfo(hdbc
, SQL_ODBC_VER
, (UCHAR
*) dbInf
.drvMgrOdbcVer
, 60, &cb
);
416 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
417 return(DispAllErrors(henv
, hdbc
));
419 if (SQLGetInfo(hdbc
, SQL_DRIVER_VER
, (UCHAR
*) dbInf
.driverVer
, 60, &cb
) == SQL_ERROR
)
420 return(DispAllErrors(henv
, hdbc
));
422 if (SQLGetInfo(hdbc
, SQL_ODBC_API_CONFORMANCE
, (UCHAR
*) &dbInf
.apiConfLvl
, sizeof(dbInf
.apiConfLvl
), &cb
) != SQL_SUCCESS
)
423 return(DispAllErrors(henv
, hdbc
));
425 if (SQLGetInfo(hdbc
, SQL_ODBC_SAG_CLI_CONFORMANCE
, (UCHAR
*) &dbInf
.cliConfLvl
, sizeof(dbInf
.cliConfLvl
), &cb
) != SQL_SUCCESS
)
426 return(DispAllErrors(henv
, hdbc
));
428 if (SQLGetInfo(hdbc
, SQL_ODBC_SQL_CONFORMANCE
, (UCHAR
*) &dbInf
.sqlConfLvl
, sizeof(dbInf
.sqlConfLvl
), &cb
) != SQL_SUCCESS
)
429 return(DispAllErrors(henv
, hdbc
));
431 if (SQLGetInfo(hdbc
, SQL_OUTER_JOINS
, (UCHAR
*) dbInf
.outerJoins
, 2, &cb
) != SQL_SUCCESS
)
432 return(DispAllErrors(henv
, hdbc
));
434 if (SQLGetInfo(hdbc
, SQL_PROCEDURES
, (UCHAR
*) dbInf
.procedureSupport
, 2, &cb
) != SQL_SUCCESS
)
435 return(DispAllErrors(henv
, hdbc
));
437 if (SQLGetInfo(hdbc
, SQL_CURSOR_COMMIT_BEHAVIOR
, (UCHAR
*) &dbInf
.cursorCommitBehavior
, sizeof(dbInf
.cursorCommitBehavior
), &cb
) != SQL_SUCCESS
)
438 return(DispAllErrors(henv
, hdbc
));
440 if (SQLGetInfo(hdbc
, SQL_CURSOR_ROLLBACK_BEHAVIOR
, (UCHAR
*) &dbInf
.cursorRollbackBehavior
, sizeof(dbInf
.cursorRollbackBehavior
), &cb
) != SQL_SUCCESS
)
441 return(DispAllErrors(henv
, hdbc
));
443 if (SQLGetInfo(hdbc
, SQL_NON_NULLABLE_COLUMNS
, (UCHAR
*) &dbInf
.supportNotNullClause
, sizeof(dbInf
.supportNotNullClause
), &cb
) != SQL_SUCCESS
)
444 return(DispAllErrors(henv
, hdbc
));
446 if (SQLGetInfo(hdbc
, SQL_ODBC_SQL_OPT_IEF
, (UCHAR
*) dbInf
.supportIEF
, 2, &cb
) != SQL_SUCCESS
)
447 return(DispAllErrors(henv
, hdbc
));
449 if (SQLGetInfo(hdbc
, SQL_DEFAULT_TXN_ISOLATION
, (UCHAR
*) &dbInf
.txnIsolation
, sizeof(dbInf
.txnIsolation
), &cb
) != SQL_SUCCESS
)
450 return(DispAllErrors(henv
, hdbc
));
452 if (SQLGetInfo(hdbc
, SQL_TXN_ISOLATION_OPTION
, (UCHAR
*) &dbInf
.txnIsolationOptions
, sizeof(dbInf
.txnIsolationOptions
), &cb
) != SQL_SUCCESS
)
453 return(DispAllErrors(henv
, hdbc
));
455 if (SQLGetInfo(hdbc
, SQL_FETCH_DIRECTION
, (UCHAR
*) &dbInf
.fetchDirections
, sizeof(dbInf
.fetchDirections
), &cb
) != SQL_SUCCESS
)
456 return(DispAllErrors(henv
, hdbc
));
458 if (SQLGetInfo(hdbc
, SQL_LOCK_TYPES
, (UCHAR
*) &dbInf
.lockTypes
, sizeof(dbInf
.lockTypes
), &cb
) != SQL_SUCCESS
)
459 return(DispAllErrors(henv
, hdbc
));
461 if (SQLGetInfo(hdbc
, SQL_POS_OPERATIONS
, (UCHAR
*) &dbInf
.posOperations
, sizeof(dbInf
.posOperations
), &cb
) != SQL_SUCCESS
)
462 return(DispAllErrors(henv
, hdbc
));
464 if (SQLGetInfo(hdbc
, SQL_POSITIONED_STATEMENTS
, (UCHAR
*) &dbInf
.posStmts
, sizeof(dbInf
.posStmts
), &cb
) != SQL_SUCCESS
)
465 return(DispAllErrors(henv
, hdbc
));
467 if (SQLGetInfo(hdbc
, SQL_SCROLL_CONCURRENCY
, (UCHAR
*) &dbInf
.scrollConcurrency
, sizeof(dbInf
.scrollConcurrency
), &cb
) != SQL_SUCCESS
)
468 return(DispAllErrors(henv
, hdbc
));
470 if (SQLGetInfo(hdbc
, SQL_SCROLL_OPTIONS
, (UCHAR
*) &dbInf
.scrollOptions
, sizeof(dbInf
.scrollOptions
), &cb
) != SQL_SUCCESS
)
471 return(DispAllErrors(henv
, hdbc
));
473 if (SQLGetInfo(hdbc
, SQL_STATIC_SENSITIVITY
, (UCHAR
*) &dbInf
.staticSensitivity
, sizeof(dbInf
.staticSensitivity
), &cb
) != SQL_SUCCESS
)
474 return(DispAllErrors(henv
, hdbc
));
476 if (SQLGetInfo(hdbc
, SQL_TXN_CAPABLE
, (UCHAR
*) &dbInf
.txnCapable
, sizeof(dbInf
.txnCapable
), &cb
) != SQL_SUCCESS
)
477 return(DispAllErrors(henv
, hdbc
));
479 if (SQLGetInfo(hdbc
, SQL_LOGIN_TIMEOUT
, (UCHAR
*) &dbInf
.loginTimeout
, sizeof(dbInf
.loginTimeout
), &cb
) != SQL_SUCCESS
)
480 return(DispAllErrors(henv
, hdbc
));
482 #ifdef DBDEBUG_CONSOLE
483 cout
<< ">>>>> DATA SOURCE INFORMATION <<<<<" << endl
;
484 cout
<< "SERVER Name: " << dbInf
.serverName
<< endl
;
485 cout
<< "DBMS Name: " << dbInf
.dbmsName
<< "; DBMS Version: " << dbInf
.dbmsVer
<< endl
;
486 cout
<< "ODBC Version: " << dbInf
.odbcVer
<< "; Driver Version: " << dbInf
.driverVer
<< endl
;
488 cout
<< "API Conf. Level: ";
489 switch(dbInf
.apiConfLvl
)
491 case SQL_OAC_NONE
: cout
<< "None"; break;
492 case SQL_OAC_LEVEL1
: cout
<< "Level 1"; break;
493 case SQL_OAC_LEVEL2
: cout
<< "Level 2"; break;
497 cout
<< "SAG CLI Conf. Level: ";
498 switch(dbInf
.cliConfLvl
)
500 case SQL_OSCC_NOT_COMPLIANT
: cout
<< "Not Compliant"; break;
501 case SQL_OSCC_COMPLIANT
: cout
<< "Compliant"; break;
505 cout
<< "SQL Conf. Level: ";
506 switch(dbInf
.sqlConfLvl
)
508 case SQL_OSC_MINIMUM
: cout
<< "Minimum Grammer"; break;
509 case SQL_OSC_CORE
: cout
<< "Core Grammer"; break;
510 case SQL_OSC_EXTENDED
: cout
<< "Extended Grammer"; break;
514 cout
<< "Max. Connections: " << dbInf
.maxConnections
<< endl
;
515 cout
<< "Outer Joins: " << dbInf
.outerJoins
<< endl
;
516 cout
<< "Support for Procedures: " << dbInf
.procedureSupport
<< endl
;
518 cout
<< "Cursor COMMIT Behavior: ";
519 switch(dbInf
.cursorCommitBehavior
)
521 case SQL_CB_DELETE
: cout
<< "Delete cursors"; break;
522 case SQL_CB_CLOSE
: cout
<< "Close cursors"; break;
523 case SQL_CB_PRESERVE
: cout
<< "Preserve cursors"; break;
527 cout
<< "Cursor ROLLBACK Behavior: ";
528 switch(dbInf
.cursorRollbackBehavior
)
530 case SQL_CB_DELETE
: cout
<< "Delete cursors"; break;
531 case SQL_CB_CLOSE
: cout
<< "Close cursors"; break;
532 case SQL_CB_PRESERVE
: cout
<< "Preserve cursors"; break;
536 cout
<< "Support NOT NULL clause: ";
537 switch(dbInf
.supportNotNullClause
)
539 case SQL_NNC_NULL
: cout
<< "No"; break;
540 case SQL_NNC_NON_NULL
: cout
<< "Yes"; break;
544 cout
<< "Support IEF (Ref. Integrity): " << dbInf
.supportIEF
<< endl
;
545 cout
<< "Login Timeout: " << dbInf
.loginTimeout
<< endl
;
547 cout
<< endl
<< endl
<< "more ..." << endl
;
550 cout
<< "Default Transaction Isolation: ";
551 switch(dbInf
.txnIsolation
)
553 case SQL_TXN_READ_UNCOMMITTED
: cout
<< "Read Uncommitted"; break;
554 case SQL_TXN_READ_COMMITTED
: cout
<< "Read Committed"; break;
555 case SQL_TXN_REPEATABLE_READ
: cout
<< "Repeatable Read"; break;
556 case SQL_TXN_SERIALIZABLE
: cout
<< "Serializable"; break;
558 case SQL_TXN_VERSIONING
: cout
<< "Versioning"; break;
563 cout
<< "Transaction Isolation Options: ";
564 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_UNCOMMITTED
)
565 cout
<< "Read Uncommitted, ";
566 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_COMMITTED
)
567 cout
<< "Read Committed, ";
568 if (dbInf
.txnIsolationOptions
& SQL_TXN_REPEATABLE_READ
)
569 cout
<< "Repeatable Read, ";
570 if (dbInf
.txnIsolationOptions
& SQL_TXN_SERIALIZABLE
)
571 cout
<< "Serializable, ";
573 if (dbInf
.txnIsolationOptions
& SQL_TXN_VERSIONING
)
574 cout
<< "Versioning";
578 cout
<< "Fetch Directions Supported:" << endl
<< " ";
579 if (dbInf
.fetchDirections
& SQL_FD_FETCH_NEXT
)
581 if (dbInf
.fetchDirections
& SQL_FD_FETCH_PRIOR
)
583 if (dbInf
.fetchDirections
& SQL_FD_FETCH_FIRST
)
585 if (dbInf
.fetchDirections
& SQL_FD_FETCH_LAST
)
587 if (dbInf
.fetchDirections
& SQL_FD_FETCH_ABSOLUTE
)
588 cout
<< "Absolute, ";
589 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RELATIVE
)
590 cout
<< "Relative, ";
592 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RESUME
)
595 if (dbInf
.fetchDirections
& SQL_FD_FETCH_BOOKMARK
)
599 cout
<< "Lock Types Supported (SQLSetPos): ";
600 if (dbInf
.lockTypes
& SQL_LCK_NO_CHANGE
)
601 cout
<< "No Change, ";
602 if (dbInf
.lockTypes
& SQL_LCK_EXCLUSIVE
)
603 cout
<< "Exclusive, ";
604 if (dbInf
.lockTypes
& SQL_LCK_UNLOCK
)
608 cout
<< "Position Operations Supported (SQLSetPos): ";
609 if (dbInf
.posOperations
& SQL_POS_POSITION
)
610 cout
<< "Position, ";
611 if (dbInf
.posOperations
& SQL_POS_REFRESH
)
613 if (dbInf
.posOperations
& SQL_POS_UPDATE
)
615 if (dbInf
.posOperations
& SQL_POS_DELETE
)
617 if (dbInf
.posOperations
& SQL_POS_ADD
)
621 cout
<< "Positioned Statements Supported: ";
622 if (dbInf
.posStmts
& SQL_PS_POSITIONED_DELETE
)
623 cout
<< "Pos delete, ";
624 if (dbInf
.posStmts
& SQL_PS_POSITIONED_UPDATE
)
625 cout
<< "Pos update, ";
626 if (dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
627 cout
<< "Select for update";
630 cout
<< "Scroll Concurrency: ";
631 if (dbInf
.scrollConcurrency
& SQL_SCCO_READ_ONLY
)
632 cout
<< "Read Only, ";
633 if (dbInf
.scrollConcurrency
& SQL_SCCO_LOCK
)
635 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_ROWVER
)
636 cout
<< "Opt. Rowver, ";
637 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_VALUES
)
638 cout
<< "Opt. Values";
641 cout
<< "Scroll Options: ";
642 if (dbInf
.scrollOptions
& SQL_SO_FORWARD_ONLY
)
643 cout
<< "Fwd Only, ";
644 if (dbInf
.scrollOptions
& SQL_SO_STATIC
)
646 if (dbInf
.scrollOptions
& SQL_SO_KEYSET_DRIVEN
)
647 cout
<< "Keyset Driven, ";
648 if (dbInf
.scrollOptions
& SQL_SO_DYNAMIC
)
650 if (dbInf
.scrollOptions
& SQL_SO_MIXED
)
654 cout
<< "Static Sensitivity: ";
655 if (dbInf
.staticSensitivity
& SQL_SS_ADDITIONS
)
656 cout
<< "Additions, ";
657 if (dbInf
.staticSensitivity
& SQL_SS_DELETIONS
)
658 cout
<< "Deletions, ";
659 if (dbInf
.staticSensitivity
& SQL_SS_UPDATES
)
663 cout
<< "Transaction Capable?: ";
664 switch(dbInf
.txnCapable
)
666 case SQL_TC_NONE
: cout
<< "No"; break;
667 case SQL_TC_DML
: cout
<< "DML Only"; break;
668 case SQL_TC_DDL_COMMIT
: cout
<< "DDL Commit"; break;
669 case SQL_TC_DDL_IGNORE
: cout
<< "DDL Ignore"; break;
670 case SQL_TC_ALL
: cout
<< "DDL & DML"; break;
678 // Completed Successfully
681 } // wxDB::getDbInfo()
683 /********** wxDB::getDataTypeInfo() **********/
684 bool wxDB::getDataTypeInfo(SWORD fSqlType
, SqlTypeInfo
&structSQLTypeInfo
)
686 // fSqlType will be something like SQL_VARCHAR. This parameter determines
687 // the data type inf. is gathered for.
689 // SqlTypeInfo is a structure that is filled in with data type information,
694 // Get information about the data type specified
695 if (SQLGetTypeInfo(hstmt
, fSqlType
) != SQL_SUCCESS
)
696 return(DispAllErrors(henv
, hdbc
, hstmt
));
698 if ((retcode
= SQLFetch(hstmt
)) != SQL_SUCCESS
)
700 #ifdef DBDEBUG_CONSOLE
701 if (retcode
== SQL_NO_DATA_FOUND
)
702 cout
<< "SQL_NO_DATA_FOUND fetching inf. about data type." << endl
;
704 DispAllErrors(henv
, hdbc
, hstmt
);
705 SQLFreeStmt(hstmt
, SQL_CLOSE
);
708 // Obtain columns from the record
709 if (SQLGetData(hstmt
, 1, SQL_C_CHAR
, (UCHAR
*) structSQLTypeInfo
.TypeName
, DB_TYPE_NAME_LEN
, &cbRet
) != SQL_SUCCESS
)
710 return(DispAllErrors(henv
, hdbc
, hstmt
));
711 if (SQLGetData(hstmt
, 3, SQL_C_LONG
, (UCHAR
*) &structSQLTypeInfo
.Precision
, 0, &cbRet
) != SQL_SUCCESS
)
712 return(DispAllErrors(henv
, hdbc
, hstmt
));
713 if (SQLGetData(hstmt
, 8, SQL_C_SHORT
, (UCHAR
*) &structSQLTypeInfo
.CaseSensitive
, 0, &cbRet
) != SQL_SUCCESS
)
714 return(DispAllErrors(henv
, hdbc
, hstmt
));
715 // if (SQLGetData(hstmt, 14, SQL_C_SHORT, (UCHAR*) &structSQLTypeInfo.MinimumScale, 0, &cbRet) != SQL_SUCCESS)
716 // return(DispAllErrors(henv, hdbc, hstmt));
718 //#ifdef __UNIX__ // BJO : IODBC knows about 5, not 15...
719 // if (SQLGetData(hstmt, 5, SQL_C_SHORT,(UCHAR*) &structSQLTypeInfo.MaximumScale, 0, &cbRet) != SQL_SUCCESS)
720 // return(DispAllErrors(henv, hdbc, hstmt));
722 if (SQLGetData(hstmt
, 15, SQL_C_SHORT
,(UCHAR
*) &structSQLTypeInfo
.MaximumScale
, 0, &cbRet
) != SQL_SUCCESS
)
723 return(DispAllErrors(henv
, hdbc
, hstmt
));
726 if (structSQLTypeInfo
.MaximumScale
< 0)
727 structSQLTypeInfo
.MaximumScale
= 0;
729 // Close the statement handle which closes open cursors
730 if (SQLFreeStmt(hstmt
, SQL_CLOSE
) != SQL_SUCCESS
)
731 return(DispAllErrors(henv
, hdbc
, hstmt
));
733 // Completed Successfully
736 } // wxDB::getDataTypeInfo()
738 /********** wxDB::Close() **********/
739 void wxDB::Close(void)
741 // Close the Sql Log file
748 // Free statement handle
751 if (SQLFreeStmt(hstmt
, SQL_DROP
) != SQL_SUCCESS
)
752 DispAllErrors(henv
, hdbc
);
755 // Disconnect from the datasource
756 if (SQLDisconnect(hdbc
) != SQL_SUCCESS
)
757 DispAllErrors(henv
, hdbc
);
759 // Free the connection to the datasource
760 if (SQLFreeConnect(hdbc
) != SQL_SUCCESS
)
761 DispAllErrors(henv
, hdbc
);
763 // There should be zero Ctable objects still connected to this db object
764 assert(nTables
== 0);
767 CstructTablesInUse
*tiu
;
769 pNode
= TablesInUse
.First();
774 tiu
= (CstructTablesInUse
*)pNode
->Data();
775 if (tiu
->pDb
== this)
777 sprintf(s
, "(%-20s) tableID:[%6lu] pDb:[%lu]", tiu
->tableName
,tiu
->tableID
,tiu
->pDb
);
778 sprintf(s2
,"Orphaned found using pDb:[%lu]",this);
781 pNode
= pNode
->Next();
785 // Copy the error messages to a global variable
787 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
788 strcpy(DBerrorList
[i
],errorList
[i
]);
792 /********** wxDB::CommitTrans() **********/
793 bool wxDB::CommitTrans(void)
797 // Commit the transaction
798 if (SQLTransact(henv
, hdbc
, SQL_COMMIT
) != SQL_SUCCESS
)
799 return(DispAllErrors(henv
, hdbc
));
802 // Completed successfully
805 } // wxDB::CommitTrans()
807 /********** wxDB::RollbackTrans() **********/
808 bool wxDB::RollbackTrans(void)
810 // Rollback the transaction
811 if (SQLTransact(henv
, hdbc
, SQL_ROLLBACK
) != SQL_SUCCESS
)
812 return(DispAllErrors(henv
, hdbc
));
814 // Completed successfully
817 } // wxDB::RollbackTrans()
819 /********** wxDB::DispAllErrors() **********/
820 bool wxDB::DispAllErrors(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
822 char odbcErrMsg
[DB_MAX_ERROR_MSG_LEN
];
824 while (SQLError(aHenv
, aHdbc
, aHstmt
, (UCHAR FAR
*) sqlState
, &nativeError
, (UCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
826 sprintf(odbcErrMsg
, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState
, nativeError
, errorMsg
);
827 logError(odbcErrMsg
, sqlState
);
830 #ifdef DBDEBUG_CONSOLE
831 // When run in console mode, use standard out to display errors.
832 cout
<< odbcErrMsg
<< endl
;
833 cout
<< "Press any key to continue..." << endl
;
839 wxMessageBox(odbcErrMsg
);
843 return(FALSE
); // This function always returns false.
845 } // wxDB::DispAllErrors()
847 /********** wxDB::GetNextError() **********/
848 bool wxDB::GetNextError(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
850 if (SQLError(aHenv
, aHdbc
, aHstmt
, (UCHAR FAR
*) sqlState
, &nativeError
, (UCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
855 } // wxDB::GetNextError()
857 /********** wxDB::DispNextError() **********/
858 void wxDB::DispNextError(void)
860 char odbcErrMsg
[DB_MAX_ERROR_MSG_LEN
];
862 sprintf(odbcErrMsg
, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState
, nativeError
, errorMsg
);
863 logError(odbcErrMsg
, sqlState
);
868 #ifdef DBDEBUG_CONSOLE
869 // When run in console mode, use standard out to display errors.
870 cout
<< odbcErrMsg
<< endl
;
871 cout
<< "Press any key to continue..." << endl
;
875 } // wxDB::DispNextError()
877 /********** wxDB::logError() **********/
878 void wxDB::logError(char *errMsg
, char *SQLState
)
880 assert(errMsg
&& strlen(errMsg
));
882 static int pLast
= -1;
885 if (++pLast
== DB_MAX_ERROR_HISTORY
)
888 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
889 strcpy(errorList
[i
], errorList
[i
+1]);
893 strcpy(errorList
[pLast
], errMsg
);
895 if (SQLState
&& strlen(SQLState
))
896 if ((dbStatus
= TranslateSqlState(SQLState
)) != DB_ERR_FUNCTION_SEQUENCE_ERROR
)
897 DB_STATUS
= dbStatus
;
899 // Add the errmsg to the sql log
902 } // wxDB::logError()
904 /**********wxDB::TranslateSqlState() **********/
905 int wxDB::TranslateSqlState(char *SQLState
)
907 if (!strcmp(SQLState
, "01000"))
908 return(DB_ERR_GENERAL_WARNING
);
909 if (!strcmp(SQLState
, "01002"))
910 return(DB_ERR_DISCONNECT_ERROR
);
911 if (!strcmp(SQLState
, "01004"))
912 return(DB_ERR_DATA_TRUNCATED
);
913 if (!strcmp(SQLState
, "01006"))
914 return(DB_ERR_PRIV_NOT_REVOKED
);
915 if (!strcmp(SQLState
, "01S00"))
916 return(DB_ERR_INVALID_CONN_STR_ATTR
);
917 if (!strcmp(SQLState
, "01S01"))
918 return(DB_ERR_ERROR_IN_ROW
);
919 if (!strcmp(SQLState
, "01S02"))
920 return(DB_ERR_OPTION_VALUE_CHANGED
);
921 if (!strcmp(SQLState
, "01S03"))
922 return(DB_ERR_NO_ROWS_UPD_OR_DEL
);
923 if (!strcmp(SQLState
, "01S04"))
924 return(DB_ERR_MULTI_ROWS_UPD_OR_DEL
);
925 if (!strcmp(SQLState
, "07001"))
926 return(DB_ERR_WRONG_NO_OF_PARAMS
);
927 if (!strcmp(SQLState
, "07006"))
928 return(DB_ERR_DATA_TYPE_ATTR_VIOL
);
929 if (!strcmp(SQLState
, "08001"))
930 return(DB_ERR_UNABLE_TO_CONNECT
);
931 if (!strcmp(SQLState
, "08002"))
932 return(DB_ERR_CONNECTION_IN_USE
);
933 if (!strcmp(SQLState
, "08003"))
934 return(DB_ERR_CONNECTION_NOT_OPEN
);
935 if (!strcmp(SQLState
, "08004"))
936 return(DB_ERR_REJECTED_CONNECTION
);
937 if (!strcmp(SQLState
, "08007"))
938 return(DB_ERR_CONN_FAIL_IN_TRANS
);
939 if (!strcmp(SQLState
, "08S01"))
940 return(DB_ERR_COMM_LINK_FAILURE
);
941 if (!strcmp(SQLState
, "21S01"))
942 return(DB_ERR_INSERT_VALUE_LIST_MISMATCH
);
943 if (!strcmp(SQLState
, "21S02"))
944 return(DB_ERR_DERIVED_TABLE_MISMATCH
);
945 if (!strcmp(SQLState
, "22001"))
946 return(DB_ERR_STRING_RIGHT_TRUNC
);
947 if (!strcmp(SQLState
, "22003"))
948 return(DB_ERR_NUMERIC_VALUE_OUT_OF_RNG
);
949 if (!strcmp(SQLState
, "22005"))
950 return(DB_ERR_ERROR_IN_ASSIGNMENT
);
951 if (!strcmp(SQLState
, "22008"))
952 return(DB_ERR_DATETIME_FLD_OVERFLOW
);
953 if (!strcmp(SQLState
, "22012"))
954 return(DB_ERR_DIVIDE_BY_ZERO
);
955 if (!strcmp(SQLState
, "22026"))
956 return(DB_ERR_STR_DATA_LENGTH_MISMATCH
);
957 if (!strcmp(SQLState
, "23000"))
958 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
959 if (!strcmp(SQLState
, "24000"))
960 return(DB_ERR_INVALID_CURSOR_STATE
);
961 if (!strcmp(SQLState
, "25000"))
962 return(DB_ERR_INVALID_TRANS_STATE
);
963 if (!strcmp(SQLState
, "28000"))
964 return(DB_ERR_INVALID_AUTH_SPEC
);
965 if (!strcmp(SQLState
, "34000"))
966 return(DB_ERR_INVALID_CURSOR_NAME
);
967 if (!strcmp(SQLState
, "37000"))
968 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL
);
969 if (!strcmp(SQLState
, "3C000"))
970 return(DB_ERR_DUPLICATE_CURSOR_NAME
);
971 if (!strcmp(SQLState
, "40001"))
972 return(DB_ERR_SERIALIZATION_FAILURE
);
973 if (!strcmp(SQLState
, "42000"))
974 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2
);
975 if (!strcmp(SQLState
, "70100"))
976 return(DB_ERR_OPERATION_ABORTED
);
977 if (!strcmp(SQLState
, "IM001"))
978 return(DB_ERR_UNSUPPORTED_FUNCTION
);
979 if (!strcmp(SQLState
, "IM002"))
980 return(DB_ERR_NO_DATA_SOURCE
);
981 if (!strcmp(SQLState
, "IM003"))
982 return(DB_ERR_DRIVER_LOAD_ERROR
);
983 if (!strcmp(SQLState
, "IM004"))
984 return(DB_ERR_SQLALLOCENV_FAILED
);
985 if (!strcmp(SQLState
, "IM005"))
986 return(DB_ERR_SQLALLOCCONNECT_FAILED
);
987 if (!strcmp(SQLState
, "IM006"))
988 return(DB_ERR_SQLSETCONNECTOPTION_FAILED
);
989 if (!strcmp(SQLState
, "IM007"))
990 return(DB_ERR_NO_DATA_SOURCE_DLG_PROHIB
);
991 if (!strcmp(SQLState
, "IM008"))
992 return(DB_ERR_DIALOG_FAILED
);
993 if (!strcmp(SQLState
, "IM009"))
994 return(DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL
);
995 if (!strcmp(SQLState
, "IM010"))
996 return(DB_ERR_DATA_SOURCE_NAME_TOO_LONG
);
997 if (!strcmp(SQLState
, "IM011"))
998 return(DB_ERR_DRIVER_NAME_TOO_LONG
);
999 if (!strcmp(SQLState
, "IM012"))
1000 return(DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR
);
1001 if (!strcmp(SQLState
, "IM013"))
1002 return(DB_ERR_TRACE_FILE_ERROR
);
1003 if (!strcmp(SQLState
, "S0001"))
1004 return(DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS
);
1005 if (!strcmp(SQLState
, "S0002"))
1006 return(DB_ERR_TABLE_NOT_FOUND
);
1007 if (!strcmp(SQLState
, "S0011"))
1008 return(DB_ERR_INDEX_ALREADY_EXISTS
);
1009 if (!strcmp(SQLState
, "S0012"))
1010 return(DB_ERR_INDEX_NOT_FOUND
);
1011 if (!strcmp(SQLState
, "S0021"))
1012 return(DB_ERR_COLUMN_ALREADY_EXISTS
);
1013 if (!strcmp(SQLState
, "S0022"))
1014 return(DB_ERR_COLUMN_NOT_FOUND
);
1015 if (!strcmp(SQLState
, "S0023"))
1016 return(DB_ERR_NO_DEFAULT_FOR_COLUMN
);
1017 if (!strcmp(SQLState
, "S1000"))
1018 return(DB_ERR_GENERAL_ERROR
);
1019 if (!strcmp(SQLState
, "S1001"))
1020 return(DB_ERR_MEMORY_ALLOCATION_FAILURE
);
1021 if (!strcmp(SQLState
, "S1002"))
1022 return(DB_ERR_INVALID_COLUMN_NUMBER
);
1023 if (!strcmp(SQLState
, "S1003"))
1024 return(DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE
);
1025 if (!strcmp(SQLState
, "S1004"))
1026 return(DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE
);
1027 if (!strcmp(SQLState
, "S1008"))
1028 return(DB_ERR_OPERATION_CANCELLED
);
1029 if (!strcmp(SQLState
, "S1009"))
1030 return(DB_ERR_INVALID_ARGUMENT_VALUE
);
1031 if (!strcmp(SQLState
, "S1010"))
1032 return(DB_ERR_FUNCTION_SEQUENCE_ERROR
);
1033 if (!strcmp(SQLState
, "S1011"))
1034 return(DB_ERR_OPERATION_INVALID_AT_THIS_TIME
);
1035 if (!strcmp(SQLState
, "S1012"))
1036 return(DB_ERR_INVALID_TRANS_OPERATION_CODE
);
1037 if (!strcmp(SQLState
, "S1015"))
1038 return(DB_ERR_NO_CURSOR_NAME_AVAIL
);
1039 if (!strcmp(SQLState
, "S1090"))
1040 return(DB_ERR_INVALID_STR_OR_BUF_LEN
);
1041 if (!strcmp(SQLState
, "S1091"))
1042 return(DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE
);
1043 if (!strcmp(SQLState
, "S1092"))
1044 return(DB_ERR_OPTION_TYPE_OUT_OF_RANGE
);
1045 if (!strcmp(SQLState
, "S1093"))
1046 return(DB_ERR_INVALID_PARAM_NO
);
1047 if (!strcmp(SQLState
, "S1094"))
1048 return(DB_ERR_INVALID_SCALE_VALUE
);
1049 if (!strcmp(SQLState
, "S1095"))
1050 return(DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE
);
1051 if (!strcmp(SQLState
, "S1096"))
1052 return(DB_ERR_INF_TYPE_OUT_OF_RANGE
);
1053 if (!strcmp(SQLState
, "S1097"))
1054 return(DB_ERR_COLUMN_TYPE_OUT_OF_RANGE
);
1055 if (!strcmp(SQLState
, "S1098"))
1056 return(DB_ERR_SCOPE_TYPE_OUT_OF_RANGE
);
1057 if (!strcmp(SQLState
, "S1099"))
1058 return(DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE
);
1059 if (!strcmp(SQLState
, "S1100"))
1060 return(DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE
);
1061 if (!strcmp(SQLState
, "S1101"))
1062 return(DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE
);
1063 if (!strcmp(SQLState
, "S1103"))
1064 return(DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE
);
1065 if (!strcmp(SQLState
, "S1104"))
1066 return(DB_ERR_INVALID_PRECISION_VALUE
);
1067 if (!strcmp(SQLState
, "S1105"))
1068 return(DB_ERR_INVALID_PARAM_TYPE
);
1069 if (!strcmp(SQLState
, "S1106"))
1070 return(DB_ERR_FETCH_TYPE_OUT_OF_RANGE
);
1071 if (!strcmp(SQLState
, "S1107"))
1072 return(DB_ERR_ROW_VALUE_OUT_OF_RANGE
);
1073 if (!strcmp(SQLState
, "S1108"))
1074 return(DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE
);
1075 if (!strcmp(SQLState
, "S1109"))
1076 return(DB_ERR_INVALID_CURSOR_POSITION
);
1077 if (!strcmp(SQLState
, "S1110"))
1078 return(DB_ERR_INVALID_DRIVER_COMPLETION
);
1079 if (!strcmp(SQLState
, "S1111"))
1080 return(DB_ERR_INVALID_BOOKMARK_VALUE
);
1081 if (!strcmp(SQLState
, "S1C00"))
1082 return(DB_ERR_DRIVER_NOT_CAPABLE
);
1083 if (!strcmp(SQLState
, "S1T00"))
1084 return(DB_ERR_TIMEOUT_EXPIRED
);
1089 } // wxDB::TranslateSqlState()
1091 /********** wxDB::Grant() **********/
1092 bool wxDB::Grant(int privileges
, char *tableName
, char *userList
)
1094 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1096 // Build the grant statement
1097 strcpy(sqlStmt
, "GRANT ");
1098 if (privileges
== DB_GRANT_ALL
)
1099 strcat(sqlStmt
, "ALL");
1103 if (privileges
& DB_GRANT_SELECT
)
1105 strcat(sqlStmt
, "SELECT");
1108 if (privileges
& DB_GRANT_INSERT
)
1111 strcat(sqlStmt
, ", ");
1112 strcat(sqlStmt
, "INSERT");
1114 if (privileges
& DB_GRANT_UPDATE
)
1117 strcat(sqlStmt
, ", ");
1118 strcat(sqlStmt
, "UPDATE");
1120 if (privileges
& DB_GRANT_DELETE
)
1123 strcat(sqlStmt
, ", ");
1124 strcat(sqlStmt
, "DELETE");
1128 strcat(sqlStmt
, " ON ");
1129 strcat(sqlStmt
, tableName
);
1130 strcat(sqlStmt
, " TO ");
1131 strcat(sqlStmt
, userList
);
1133 #ifdef DBDEBUG_CONSOLE
1134 cout
<< endl
<< sqlStmt
<< endl
;
1137 WriteSqlLog(sqlStmt
);
1139 return(ExecSql(sqlStmt
));
1143 /********** wxDB::CreateView() **********/
1144 bool wxDB::CreateView(char *viewName
, char *colList
, char *pSqlStmt
, bool attemptDrop
)
1146 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1148 // Drop the view first
1149 if (attemptDrop
&& !DropView(viewName
))
1152 // Build the create view statement
1153 strcpy(sqlStmt
, "CREATE VIEW ");
1154 strcat(sqlStmt
, viewName
);
1156 if (strlen(colList
))
1158 strcat(sqlStmt
, " (");
1159 strcat(sqlStmt
, colList
);
1160 strcat(sqlStmt
, ")");
1163 strcat(sqlStmt
, " AS ");
1164 strcat(sqlStmt
, pSqlStmt
);
1166 WriteSqlLog(sqlStmt
);
1168 #ifdef DBDEBUG_CONSOLE
1169 cout
<< sqlStmt
<< endl
;
1172 return(ExecSql(sqlStmt
));
1174 } // wxDB::CreateView()
1176 /********** wxDB::DropView() **********/
1177 bool wxDB::DropView(char *viewName
)
1179 // NOTE: This function returns TRUE if the View does not exist, but
1180 // only for identified databases. Code will need to be added
1181 // below for any other databases when those databases are defined
1182 // to handle this situation consistently
1184 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1186 sprintf(sqlStmt
, "DROP VIEW %s", viewName
);
1188 WriteSqlLog(sqlStmt
);
1190 #ifdef DBDEBUG_CONSOLE
1191 cout
<< endl
<< sqlStmt
<< endl
;
1194 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1196 // Check for "Base table not found" error and ignore
1197 GetNextError(henv
, hdbc
, hstmt
);
1198 if (strcmp(sqlState
,"S0002")) // "Base table not found"
1200 // Check for product specific error codes
1201 if (!((Dbms() == dbmsSYBASE_ASA
&& !strcmp(sqlState
,"42000")))) // 5.x (and lower?)
1204 DispAllErrors(henv
, hdbc
, hstmt
);
1211 // Commit the transaction
1212 if (! CommitTrans())
1217 } // wxDB::DropView()
1220 /********** wxDB::ExecSql() **********/
1221 bool wxDB::ExecSql(char *pSqlStmt
)
1223 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1224 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) == SQL_SUCCESS
)
1228 DispAllErrors(henv
, hdbc
, hstmt
);
1232 } // wxDB::ExecSql()
1234 /********** wxDB::GetNext() **********/
1235 bool wxDB::GetNext(void)
1237 if (SQLFetch(hstmt
) == SQL_SUCCESS
)
1241 DispAllErrors(henv
, hdbc
, hstmt
);
1245 } // wxDB::GetNext()
1247 /********** wxDB::GetData() **********/
1248 bool wxDB::GetData(UWORD colNo
, SWORD cType
, PTR pData
, SDWORD maxLen
, SDWORD FAR
*cbReturned
)
1253 if (SQLGetData(hstmt
, colNo
, cType
, pData
, maxLen
, cbReturned
) == SQL_SUCCESS
)
1257 DispAllErrors(henv
, hdbc
, hstmt
);
1261 } // wxDB::GetData()
1263 /********** wxDB::GetColumns() **********/
1265 * 1) The last array element of the tableName[] argument must be zero (null).
1266 * This is how the end of the array is detected.
1267 * 2) This function returns an array of CcolInf structures. If no columns
1268 * were found, or an error occured, this pointer will be zero (null). THE
1269 * CALLING FUNCTION IS RESPONSIBLE FOR DELETING THE MEMORY RETURNED WHEN IT
1270 * IS FINISHED WITH IT. i.e.
1272 * CcolInf *colInf = pDb->GetColumns(tableList, userID);
1275 * // Use the column inf
1277 * // Destroy the memory
1281 CcolInf
*wxDB::GetColumns(char *tableName
[], char *userID
)
1285 CcolInf
*colInf
= 0;
1288 char tblName
[DB_MAX_TABLE_NAME_LEN
+1];
1289 char colName
[DB_MAX_COLUMN_NAME_LEN
+1];
1291 char userIdUC
[80+1];
1292 char tableNameUC
[DB_MAX_TABLE_NAME_LEN
+1];
1294 if (!userID
|| !strlen(userID
))
1297 // dBase does not use user names, and some drivers fail if you try to pass one
1298 if (Dbms() == dbmsDBASE
)
1301 // Oracle user names may only be in uppercase, so force
1302 // the name to uppercase
1303 if (Dbms() == dbmsORACLE
)
1306 for (char *p
= userID
; *p
; p
++)
1307 userIdUC
[i
++] = toupper(*p
);
1312 // Pass 1 - Determine how many columns there are.
1313 // Pass 2 - Allocate the CcolInf array and fill in
1314 // the array with the column information.
1316 for (pass
= 1; pass
<= 2; pass
++)
1320 if (noCols
== 0) // Probably a bogus table name(s)
1322 // Allocate n CcolInf objects to hold the column information
1323 colInf
= new CcolInf
[noCols
+1];
1326 // Mark the end of the array
1327 strcpy(colInf
[noCols
].tableName
, "");
1328 strcpy(colInf
[noCols
].colName
, "");
1329 colInf
[noCols
].sqlDataType
= 0;
1331 // Loop through each table name
1333 for (tbl
= 0; tableName
[tbl
]; tbl
++)
1335 // Oracle table names are uppercase only, so force
1336 // the name to uppercase just in case programmer forgot to do this
1337 if (Dbms() == dbmsORACLE
)
1340 for (char *p
= tableName
[tbl
]; *p
; p
++)
1341 tableNameUC
[i
++] = toupper(*p
);
1345 sprintf(tableNameUC
,tableName
[tbl
]);
1347 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1349 // MySQL and Access cannot accept a user name when looking up column names, so we
1350 // use the call below that leaves out the user name
1351 if (strcmp(userID
,"") &&
1352 Dbms() != dbmsMY_SQL
&&
1353 Dbms() != dbmsACCESS
)
1355 retcode
= SQLColumns(hstmt
,
1356 NULL
, 0, // All qualifiers
1357 (UCHAR
*) userID
, SQL_NTS
, // Owner
1358 (UCHAR
*) tableNameUC
, SQL_NTS
,
1359 NULL
, 0); // All columns
1363 retcode
= SQLColumns(hstmt
,
1364 NULL
, 0, // All qualifiers
1366 (UCHAR
*) tableNameUC
, SQL_NTS
,
1367 NULL
, 0); // All columns
1369 if (retcode
!= SQL_SUCCESS
)
1370 { // Error occured, abort
1371 DispAllErrors(henv
, hdbc
, hstmt
);
1374 SQLFreeStmt(hstmt
, SQL_UNBIND
);
1375 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1378 SQLBindCol(hstmt
, 3, SQL_C_CHAR
, (UCHAR
*) tblName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
1379 SQLBindCol(hstmt
, 4, SQL_C_CHAR
, (UCHAR
*) colName
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1380 SQLBindCol(hstmt
, 5, SQL_C_SSHORT
, (UCHAR
*) &sqlDataType
, 0, &cb
);
1381 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
1383 if (pass
== 1) // First pass, just add up the number of columns
1385 else // Pass 2; Fill in the array of structures
1387 if (colNo
< noCols
) // Some extra error checking to prevent memory overwrites
1389 strcpy(colInf
[colNo
].tableName
, tblName
);
1390 strcpy(colInf
[colNo
].colName
, colName
);
1391 colInf
[colNo
].sqlDataType
= sqlDataType
;
1396 if (retcode
!= SQL_NO_DATA_FOUND
)
1397 { // Error occured, abort
1398 DispAllErrors(henv
, hdbc
, hstmt
);
1401 SQLFreeStmt(hstmt
, SQL_UNBIND
);
1402 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1408 SQLFreeStmt(hstmt
, SQL_UNBIND
);
1409 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1412 } // wxDB::GetColumns()
1415 /********** wxDB::Catalog() **********/
1416 bool wxDB::Catalog(char *userID
, char *fileName
)
1418 assert(fileName
&& strlen(fileName
));
1422 char tblName
[DB_MAX_TABLE_NAME_LEN
+1];
1423 char tblNameSave
[DB_MAX_TABLE_NAME_LEN
+1];
1424 char colName
[DB_MAX_COLUMN_NAME_LEN
+1];
1426 char typeName
[30+1];
1427 SWORD precision
, length
;
1429 FILE *fp
= fopen(fileName
,"wt");
1433 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1435 if (!userID
|| !strlen(userID
))
1438 char userIdUC
[80+1];
1439 // Oracle user names may only be in uppercase, so force
1440 // the name to uppercase
1441 if (Dbms() == dbmsORACLE
)
1444 for (char *p
= userID
; *p
; p
++)
1445 userIdUC
[i
++] = toupper(*p
);
1450 if (strcmp(userID
,""))
1452 retcode
= SQLColumns(hstmt
,
1453 NULL
, 0, // All qualifiers
1454 (UCHAR
*) userID
, SQL_NTS
, // User specified
1455 NULL
, 0, // All tables
1456 NULL
, 0); // All columns
1460 retcode
= SQLColumns(hstmt
,
1461 NULL
, 0, // All qualifiers
1462 NULL
, 0, // User specified
1463 NULL
, 0, // All tables
1464 NULL
, 0); // All columns
1466 if (retcode
!= SQL_SUCCESS
)
1468 DispAllErrors(henv
, hdbc
, hstmt
);
1473 SQLBindCol(hstmt
, 3, SQL_C_CHAR
, (UCHAR
*) tblName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
1474 SQLBindCol(hstmt
, 4, SQL_C_CHAR
, (UCHAR
*) colName
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1475 SQLBindCol(hstmt
, 5, SQL_C_SSHORT
, (UCHAR
*) &sqlDataType
, 0, &cb
);
1476 SQLBindCol(hstmt
, 6, SQL_C_CHAR
, (UCHAR
*) typeName
, sizeof(typeName
), &cb
);
1477 SQLBindCol(hstmt
, 7, SQL_C_SSHORT
, (UCHAR
*) &precision
, 0, &cb
);
1478 SQLBindCol(hstmt
, 8, SQL_C_SSHORT
, (UCHAR
*) &length
, 0, &cb
);
1481 strcpy(tblNameSave
,"");
1484 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
1486 if (strcmp(tblName
,tblNameSave
))
1490 fputs("================================ ", fp
);
1491 fputs("================================ ", fp
);
1492 fputs("===================== ", fp
);
1493 fputs("========= ", fp
);
1494 fputs("=========\n", fp
);
1495 sprintf(outStr
, "%-32s %-32s %-21s %9s %9s\n",
1496 "TABLE NAME", "COLUMN NAME", "DATA TYPE", "PRECISION", "LENGTH");
1498 fputs("================================ ", fp
);
1499 fputs("================================ ", fp
);
1500 fputs("===================== ", fp
);
1501 fputs("========= ", fp
);
1502 fputs("=========\n", fp
);
1503 strcpy(tblNameSave
,tblName
);
1505 sprintf(outStr
, "%-32s %-32s (%04d)%-15s %9d %9d\n",
1506 tblName
, colName
, sqlDataType
, typeName
, precision
, length
);
1507 if (fputs(outStr
, fp
) == EOF
)
1509 SQLFreeStmt(hstmt
, SQL_UNBIND
);
1510 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1517 if (retcode
!= SQL_NO_DATA_FOUND
)
1518 DispAllErrors(henv
, hdbc
, hstmt
);
1520 SQLFreeStmt(hstmt
, SQL_UNBIND
);
1521 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1524 return(retcode
== SQL_NO_DATA_FOUND
);
1526 } // wxDB::Catalog()
1529 // Table name can refer to a table, view, alias or synonym. Returns true
1530 // if the object exists in the database. This function does not indicate
1531 // whether or not the user has privleges to query or perform other functions
1533 bool wxDB::TableExists(char *tableName
, char *userID
, char *tablePath
)
1535 assert(tableName
&& strlen(tableName
));
1537 if (Dbms() == dbmsDBASE
)
1540 if (tablePath
&& strlen(tablePath
))
1541 dbName
.sprintf("%s/%s.dbf",tablePath
,tableName
);
1543 dbName
.sprintf("%s.dbf",tableName
);
1545 glt
= wxFileExists(dbName
.GetData());
1549 if (!userID
|| !strlen(userID
))
1552 char userIdUC
[80+1];
1553 // Oracle user names may only be in uppercase, so force
1554 // the name to uppercase
1555 if (Dbms() == dbmsORACLE
)
1558 for (char *p
= userID
; *p
; p
++)
1559 userIdUC
[i
++] = toupper(*p
);
1564 char tableNameUC
[DB_MAX_TABLE_NAME_LEN
+1];
1565 // Oracle table names are uppercase only, so force
1566 // the name to uppercase just in case programmer forgot to do this
1567 if (Dbms() == dbmsORACLE
)
1570 for (char *p
= tableName
; *p
; p
++)
1571 tableNameUC
[i
++] = toupper(*p
);
1575 sprintf(tableNameUC
,tableName
);
1577 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1580 // MySQL and Access cannot accept a user name when looking up table names, so we
1581 // use the call below that leaves out the user name
1582 if (strcmp(userID
,"") &&
1583 Dbms() != dbmsMY_SQL
&&
1584 Dbms() != dbmsACCESS
)
1586 retcode
= SQLTables(hstmt
,
1587 NULL
, 0, // All qualifiers
1588 (UCHAR
*) userID
, SQL_NTS
, // All owners
1589 (UCHAR FAR
*)tableNameUC
, SQL_NTS
,
1590 NULL
, 0); // All table types
1594 retcode
= SQLTables(hstmt
,
1595 NULL
, 0, // All qualifiers
1596 NULL
, 0, // All owners
1597 (UCHAR FAR
*)tableNameUC
, SQL_NTS
,
1598 NULL
, 0); // All table types
1600 if (retcode
!= SQL_SUCCESS
)
1601 return(DispAllErrors(henv
, hdbc
, hstmt
));
1603 retcode
= SQLFetch(hstmt
);
1604 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1606 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1607 return(DispAllErrors(henv
, hdbc
, hstmt
));
1610 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1613 } // wxDB::TableExists()
1616 /********** wxDB::SqlLog() **********/
1617 bool wxDB::SqlLog(enum sqlLog state
, char *filename
, bool append
)
1619 assert(state
== sqlLogON
|| state
== sqlLogOFF
);
1620 assert(state
== sqlLogOFF
|| filename
);
1622 if (state
== sqlLogON
)
1626 fpSqlLog
= fopen(filename
, (append
? "at" : "wt"));
1627 if (fpSqlLog
== NULL
)
1635 if (fclose(fpSqlLog
))
1641 sqlLogState
= state
;
1647 /********** wxDB::WriteSqlLog() **********/
1648 bool wxDB::WriteSqlLog(char *logMsg
)
1652 if (fpSqlLog
== 0 || sqlLogState
== sqlLogOFF
)
1655 if (fputs("\n", fpSqlLog
) == EOF
) return(FALSE
);
1656 if (fputs(logMsg
, fpSqlLog
) == EOF
) return(FALSE
);
1657 if (fputs("\n", fpSqlLog
) == EOF
) return(FALSE
);
1661 } // wxDB::WriteSqlLog()
1664 /********** wxDB::Dbms() **********/
1666 * Be aware that not all database engines use the exact same syntax, and not
1667 * every ODBC compliant database is compliant to the same level of compliancy.
1668 * Some manufacturers support the minimum Level 1 compliancy, and others up
1669 * through Level 3. Others support subsets of features for levels above 1.
1671 * If you find an inconsistency between the wxDB class and a specific database
1672 * engine, and an identifier to this section, and special handle the database in
1673 * the area where behavior is non-conforming with the other databases.
1676 * NOTES ABOUT ISSUES SPECIFIC TO EACH DATABASE ENGINE
1677 * ---------------------------------------------------
1680 * - Currently the only database supported by the class to support VIEWS
1683 * - Does not support the SQL_TIMESTAMP structure
1684 * - Supports only one cursor and one connect (apparently? with Microsoft driver only?)
1685 * - Does not automatically create the primary index if the 'keyField' param of SetColDef
1686 * is TRUE. The user must create ALL indexes from their program.
1687 * - Table names can only be 8 characters long
1688 * - Column names can only be 10 characters long
1691 * - To lock a record during QUERY functions, the reserved word 'HOLDLOCK' must be added
1692 * after every table name involved in the query/join if that tables matching record(s)
1694 * - Ignores the keywords 'FOR UPDATE'. Use the HOLDLOCK functionality described above
1696 * SYBASE (Enterprise)
1697 * - If a column is part of the Primary Key, the column cannot be NULL
1700 * - If a column is part of the Primary Key, the column cannot be NULL
1701 * - Cannot support selecting for update [::CanSelectForUpdate()]. Always returns FALSE
1704 * - Does not support the keywords 'ASC' or 'DESC' as of release v6.5.0
1708 DBMS
wxDB::Dbms(void)
1710 if (!strnicmp(dbInf
.dbmsName
,"Oracle",6))
1712 if (!stricmp(dbInf
.dbmsName
,"Adaptive Server Anywhere"))
1713 return(dbmsSYBASE_ASA
);
1714 if (!stricmp(dbInf
.dbmsName
,"SQL Server")) // Sybase Adaptive Server Enterprise
1715 return(dbmsSYBASE_ASE
);
1716 if (!stricmp(dbInf
.dbmsName
,"Microsoft SQL Server"))
1717 return(dbmsMS_SQL_SERVER
);
1718 if (!stricmp(dbInf
.dbmsName
,"MySQL"))
1720 if (!stricmp(dbInf
.dbmsName
,"PostgreSQL")) // v6.5.0
1721 return(dbmsPOSTGRES
);
1722 if (!stricmp(dbInf
.dbmsName
,"ACCESS"))
1724 if (!strnicmp(dbInf
.dbmsName
,"DBASE",5))
1726 return(dbmsUNIDENTIFIED
);
1731 /********** GetDbConnection() **********/
1732 wxDB WXDLLEXPORT
*GetDbConnection(DbStuff
*pDbStuff
)
1736 // Scan the linked list searching for an available database connection
1737 // that's already been opened but is currently not in use.
1738 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1740 // The database connection must be for the same datasource
1741 // name and must currently not be in use.
1742 if (pList
->Free
&& (! strcmp(pDbStuff
->Dsn
, pList
->Dsn
))) // Found a free connection
1744 pList
->Free
= FALSE
;
1745 return(pList
->PtrDb
);
1749 // No available connections. A new connection must be made and
1750 // appended to the end of the linked list.
1753 // Find the end of the list
1754 for (pList
= PtrBegDbList
; pList
->PtrNext
; pList
= pList
->PtrNext
);
1755 // Append a new list item
1756 pList
->PtrNext
= new DbList
;
1757 pList
->PtrNext
->PtrPrev
= pList
;
1758 pList
= pList
->PtrNext
;
1762 // Create the first node on the list
1763 pList
= PtrBegDbList
= new DbList
;
1767 // Initialize new node in the linked list
1769 pList
->Free
= FALSE
;
1770 strcpy(pList
->Dsn
, pDbStuff
->Dsn
);
1771 pList
->PtrDb
= new wxDB(pDbStuff
->Henv
);
1773 // Connect to the datasource
1774 if (pList
->PtrDb
->Open(pDbStuff
->Dsn
, pDbStuff
->Uid
, pDbStuff
->AuthStr
))
1776 pList
->PtrDb
->SqlLog(SQLLOGstate
,SQLLOGfn
,TRUE
);
1777 return(pList
->PtrDb
);
1779 else // Unable to connect, destroy list item
1782 pList
->PtrPrev
->PtrNext
= 0;
1784 PtrBegDbList
= 0; // Empty list again
1785 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDB object
1786 pList
->PtrDb
->Close(); // Close the wxDB object
1787 delete pList
->PtrDb
; // Deletes the wxDB object
1788 delete pList
; // Deletes the linked list object
1792 } // GetDbConnection()
1794 /********** FreeDbConnection() **********/
1795 bool WXDLLEXPORT
FreeDbConnection(wxDB
*pDb
)
1799 // Scan the linked list searching for the database connection
1800 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1802 if (pList
->PtrDb
== pDb
) // Found it!!!
1803 return(pList
->Free
= TRUE
);
1806 // Never found the database object, return failure
1809 } // FreeDbConnection()
1811 /********** CloseDbConnections() **********/
1812 void WXDLLEXPORT
CloseDbConnections(void)
1814 DbList
*pList
, *pNext
;
1816 // Traverse the linked list closing database connections and freeing memory as I go.
1817 for (pList
= PtrBegDbList
; pList
; pList
= pNext
)
1819 pNext
= pList
->PtrNext
; // Save the pointer to next
1820 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDB object
1821 pList
->PtrDb
->Close(); // Close the wxDB object
1822 delete pList
->PtrDb
; // Deletes the wxDB object
1823 delete pList
; // Deletes the linked list object
1826 // Mark the list as empty
1829 } // CloseDbConnections()
1831 /********** NumberDbConnectionsInUse() **********/
1832 int WXDLLEXPORT
NumberDbConnectionsInUse(void)
1837 // Scan the linked list counting db connections that are currently in use
1838 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1840 if (pList
->Free
== FALSE
)
1846 } // NumberDbConnectionsInUse()
1848 /********** SqlLog() **********/
1849 bool SqlLog(enum sqlLog state
, char *filename
)
1851 bool append
= FALSE
;
1854 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1856 if (!pList
->PtrDb
->SqlLog(state
,filename
,append
))
1861 SQLLOGstate
= state
;
1862 strcpy(SQLLOGfn
,filename
);
1868 /********** GetDataSource() **********/
1869 bool GetDataSource(HENV henv
, char *Dsn
, SWORD DsnMax
, char *DsDesc
, SWORD DsDescMax
,
1874 if (SQLDataSources(henv
, direction
, (UCHAR FAR
*) Dsn
, DsnMax
, &cb
,
1875 (UCHAR FAR
*) DsDesc
, DsDescMax
, &cb
) == SQL_SUCCESS
)
1880 } // GetDataSource()