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.
8 // Mods: Dec, 1998: Added support for SQL statement logging and database
12 // Copyright: (c) 1996 Remstar International, Inc.
13 // Licence: wxWindows licence, plus:
14 // Notice: This class library and its intellectual design are free of charge for use,
15 // modification, enhancement, debugging under the following conditions:
16 // 1) These classes may only be used as part of the implementation of a
17 // wxWindows-based application
18 // 2) All enhancements and bug fixes are to be submitted back to the wxWindows
19 // user groups free of all charges for use with the wxWindows library.
20 // 3) These classes may not be distributed as part of any other class library,
21 // DLL, text (written or electronic), other than a complete distribution of
22 // the wxWindows GUI development toolkit.
23 ///////////////////////////////////////////////////////////////////////////////
26 #pragma implementation "db.h"
35 #ifdef DBDEBUG_CONSOLE
40 #include "wx/wxprec.h"
59 DbList
*PtrBegDbList
= 0;
61 /********** wxDB Constructor **********/
62 wxDB::wxDB(HENV
&aHenv
)
66 fpSqlLog
= 0; // Sql Log file pointer
67 sqlLogState
= sqlLogOFF
; // By default, logging is turned off
71 nativeError
= cbErrorMsg
= 0;
72 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
73 strcpy(errorList
[i
], "");
75 // Init typeInf structures
76 strcpy(typeInfVarchar
.TypeName
,"");
77 typeInfVarchar
.FsqlType
= 0;
78 typeInfVarchar
.Precision
= 0;
79 typeInfVarchar
.CaseSensitive
= 0;
80 typeInfVarchar
.MaximumScale
= 0;
82 strcpy(typeInfInteger
.TypeName
,"");
83 typeInfInteger
.FsqlType
= 0;
84 typeInfInteger
.Precision
= 0;
85 typeInfInteger
.CaseSensitive
= 0;
86 typeInfInteger
.MaximumScale
= 0;
88 strcpy(typeInfFloat
.TypeName
,"");
89 typeInfFloat
.FsqlType
= 0;
90 typeInfFloat
.Precision
= 0;
91 typeInfFloat
.CaseSensitive
= 0;
92 typeInfFloat
.MaximumScale
= 0;
94 strcpy(typeInfDate
.TypeName
,"");
95 typeInfDate
.FsqlType
= 0;
96 typeInfDate
.Precision
= 0;
97 typeInfDate
.CaseSensitive
= 0;
98 typeInfDate
.MaximumScale
= 0;
100 // Error reporting is turned OFF by default
103 // Copy the HENV into the db class
106 // Allocate a data source connection handle
107 if (SQLAllocConnect(henv
, &hdbc
) != SQL_SUCCESS
)
110 // Initialize the db status flag
113 // Mark database as not open as of yet
118 /********** wxDB::Open() **********/
119 bool wxDB::Open(char *Dsn
, char *Uid
, char *AuthStr
)
121 assert(Dsn
&& strlen(Dsn
));
126 #ifndef FWD_ONLY_CURSORS
130 // Specify that the ODBC cursor library be used, if needed. This must be
131 // specified before the connection is made.
132 retcode
= SQLSetConnectOption(hdbc
, SQL_ODBC_CURSORS
, SQL_CUR_USE_IF_NEEDED
);
134 #ifdef DBDEBUG_CONSOLE
135 if (retcode
== SQL_SUCCESS
)
136 cout
<< "SQLSetConnectOption(CURSOR_LIB) successful" << endl
;
138 cout
<< "SQLSetConnectOption(CURSOR_LIB) failed" << endl
;
143 // Connect to the data source
144 if (SQLConnect(hdbc
, (UCHAR FAR
*) Dsn
, SQL_NTS
,
145 (UCHAR FAR
*) Uid
, SQL_NTS
,
146 (UCHAR FAR
*) AuthStr
, SQL_NTS
) != SQL_SUCCESS
)
147 return(DispAllErrors(henv
, hdbc
));
149 // Mark database as open
152 // Allocate a statement handle for the database connection
153 if (SQLAllocStmt(hdbc
, &hstmt
) != SQL_SUCCESS
)
154 return(DispAllErrors(henv
, hdbc
));
156 // Set Connection Options
157 if (! setConnectionOptions())
160 // Query the data source for inf. about itself
164 // Query the data source regarding data type information
167 // The way I determined which SQL data types to use was by calling SQLGetInfo
168 // for all of the possible SQL data types to see which ones were supported. If
169 // a type is not supported, the SQLFetch() that's called from getDataTypeInfo()
170 // fails with SQL_NO_DATA_FOUND. This is ugly because I'm sure the three SQL data
171 // types I've selected below will not alway's be what we want. These are just
172 // what happened to work against an Oracle 7/Intersolv combination. The following is
173 // a complete list of the results I got back against the Oracle 7 database:
175 // SQL_BIGINT SQL_NO_DATA_FOUND
176 // SQL_BINARY SQL_NO_DATA_FOUND
177 // SQL_BIT SQL_NO_DATA_FOUND
178 // SQL_CHAR type name = 'CHAR', Precision = 255
179 // SQL_DATE SQL_NO_DATA_FOUND
180 // SQL_DECIMAL type name = 'NUMBER', Precision = 38
181 // SQL_DOUBLE type name = 'NUMBER', Precision = 15
182 // SQL_FLOAT SQL_NO_DATA_FOUND
183 // SQL_INTEGER SQL_NO_DATA_FOUND
184 // SQL_LONGVARBINARY type name = 'LONG RAW', Precision = 2 billion
185 // SQL_LONGVARCHAR type name = 'LONG', Precision = 2 billion
186 // SQL_NUMERIC SQL_NO_DATA_FOUND
187 // SQL_REAL SQL_NO_DATA_FOUND
188 // SQL_SMALLINT SQL_NO_DATA_FOUND
189 // SQL_TIME SQL_NO_DATA_FOUND
190 // SQL_TIMESTAMP type name = 'DATE', Precision = 19
191 // SQL_VARBINARY type name = 'RAW', Precision = 255
192 // SQL_VARCHAR type name = 'VARCHAR2', Precision = 2000
193 // =====================================================================
194 // Results from a Microsoft Access 7.0 db, using a driver from Microsoft
196 // SQL_VARCHAR type name = 'TEXT', Precision = 255
197 // SQL_TIMESTAMP type name = 'DATETIME'
198 // SQL_DECIMAL SQL_NO_DATA_FOUND
199 // SQL_NUMERIC type name = 'CURRENCY', Precision = 19
200 // SQL_FLOAT SQL_NO_DATA_FOUND
201 // SQL_REAL type name = 'SINGLE', Precision = 7
202 // SQL_DOUBLE type name = 'DOUBLE', Precision = 15
203 // SQL_INTEGER type name = 'LONG', Precision = 10
205 // VARCHAR = Variable length character string
206 if (! getDataTypeInfo(SQL_VARCHAR
, typeInfVarchar
))
207 if (! getDataTypeInfo(SQL_CHAR
, typeInfVarchar
))
210 typeInfVarchar
.FsqlType
= SQL_CHAR
;
212 typeInfVarchar
.FsqlType
= SQL_VARCHAR
;
215 if (! getDataTypeInfo(SQL_DOUBLE
, typeInfFloat
))
216 if (! getDataTypeInfo(SQL_REAL
, typeInfFloat
))
217 if (! getDataTypeInfo(SQL_FLOAT
, typeInfFloat
))
218 if (! getDataTypeInfo(SQL_DECIMAL
, typeInfFloat
))
219 if (! getDataTypeInfo(SQL_NUMERIC
, typeInfFloat
))
222 typeInfFloat
.FsqlType
= SQL_NUMERIC
;
224 typeInfFloat
.FsqlType
= SQL_DECIMAL
;
226 typeInfFloat
.FsqlType
= SQL_FLOAT
;
228 typeInfFloat
.FsqlType
= SQL_REAL
;
230 typeInfFloat
.FsqlType
= SQL_DOUBLE
;
233 if (! getDataTypeInfo(SQL_INTEGER
, typeInfInteger
))
234 // If SQL_INTEGER is not supported, use the floating point
235 // data type to store integers as well as floats
236 if (! getDataTypeInfo(typeInfFloat
.FsqlType
, typeInfInteger
))
239 typeInfInteger
.FsqlType
= typeInfFloat
.FsqlType
;
241 typeInfInteger
.FsqlType
= SQL_INTEGER
;
244 if (! getDataTypeInfo(SQL_TIMESTAMP
, typeInfDate
))
247 typeInfDate
.FsqlType
= SQL_TIMESTAMP
;
249 #ifdef DBDEBUG_CONSOLE
250 cout
<< "VARCHAR DATA TYPE: " << typeInfVarchar
.TypeName
<< endl
;
251 cout
<< "INTEGER DATA TYPE: " << typeInfInteger
.TypeName
<< endl
;
252 cout
<< "FLOAT DATA TYPE: " << typeInfFloat
.TypeName
<< endl
;
253 cout
<< "DATE DATA TYPE: " << typeInfDate
.TypeName
<< endl
;
257 // Completed Successfully
262 // The Intersolv/Oracle 7 driver was "Not Capable" of setting the login timeout.
264 /********** wxDB::setConnectionOptions() **********/
265 bool wxDB::setConnectionOptions(void)
267 SQLSetConnectOption(hdbc
, SQL_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
);
268 SQLSetConnectOption(hdbc
, SQL_OPT_TRACE
, SQL_OPT_TRACE_OFF
);
270 // Display the connection options to verify them
271 #ifdef DBDEBUG_CONSOLE
273 cout
<< ">>>>> CONNECTION OPTIONS <<<<<<" << endl
;
275 if (SQLGetConnectOption(hdbc
, SQL_AUTOCOMMIT
, &l
) != SQL_SUCCESS
)
276 return(DispAllErrors(henv
, hdbc
));
277 cout
<< "AUTOCOMMIT: " << (l
== SQL_AUTOCOMMIT_OFF
? "OFF" : "ON") << endl
;
279 if (SQLGetConnectOption(hdbc
, SQL_ODBC_CURSORS
, &l
) != SQL_SUCCESS
)
280 return(DispAllErrors(henv
, hdbc
));
281 cout
<< "ODBC CURSORS: ";
284 case(SQL_CUR_USE_IF_NEEDED
):
285 cout
<< "SQL_CUR_USE_IF_NEEDED";
287 case(SQL_CUR_USE_ODBC
):
288 cout
<< "SQL_CUR_USE_ODBC";
290 case(SQL_CUR_USE_DRIVER
):
291 cout
<< "SQL_CUR_USE_DRIVER";
296 if (SQLGetConnectOption(hdbc
, SQL_OPT_TRACE
, &l
) != SQL_SUCCESS
)
297 return(DispAllErrors(henv
, hdbc
));
298 cout
<< "TRACING: " << (l
== SQL_OPT_TRACE_OFF
? "OFF" : "ON") << endl
;
303 // Completed Successfully
306 } // wxDB::setConnectionOptions()
308 /********** wxDB::getDbInfo() **********/
309 bool wxDB::getDbInfo(void)
313 if (SQLGetInfo(hdbc
, SQL_SERVER_NAME
, (UCHAR
*) dbInf
.serverName
, 40, &cb
) != SQL_SUCCESS
)
314 return(DispAllErrors(henv
, hdbc
));
316 if (SQLGetInfo(hdbc
, SQL_DATABASE_NAME
, (UCHAR
*) dbInf
.databaseName
, 128, &cb
) != SQL_SUCCESS
)
317 return(DispAllErrors(henv
, hdbc
));
319 if (SQLGetInfo(hdbc
, SQL_DBMS_NAME
, (UCHAR
*) dbInf
.dbmsName
, 40, &cb
) != SQL_SUCCESS
)
320 return(DispAllErrors(henv
, hdbc
));
322 if (SQLGetInfo(hdbc
, SQL_DBMS_VER
, (UCHAR
*) dbInf
.dbmsVer
, 20, &cb
) != SQL_SUCCESS
)
323 return(DispAllErrors(henv
, hdbc
));
325 if (SQLGetInfo(hdbc
, SQL_ACTIVE_CONNECTIONS
, (UCHAR
*) &dbInf
.maxConnections
, sizeof(dbInf
.maxConnections
), &cb
) != SQL_SUCCESS
)
326 return(DispAllErrors(henv
, hdbc
));
328 if (SQLGetInfo(hdbc
, SQL_ACTIVE_STATEMENTS
, (UCHAR
*) &dbInf
.maxStmts
, sizeof(dbInf
.maxStmts
), &cb
) != SQL_SUCCESS
)
329 return(DispAllErrors(henv
, hdbc
));
331 if (SQLGetInfo(hdbc
, SQL_DRIVER_NAME
, (UCHAR
*) dbInf
.driverName
, 40, &cb
) != SQL_SUCCESS
)
332 return(DispAllErrors(henv
, hdbc
));
334 if (SQLGetInfo(hdbc
, SQL_DRIVER_ODBC_VER
, (UCHAR
*) dbInf
.odbcVer
, 20, &cb
) != SQL_SUCCESS
)
335 return(DispAllErrors(henv
, hdbc
));
337 if (SQLGetInfo(hdbc
, SQL_ODBC_VER
, (UCHAR
*) dbInf
.drvMgrOdbcVer
, 20, &cb
) != SQL_SUCCESS
)
338 return(DispAllErrors(henv
, hdbc
));
340 if (SQLGetInfo(hdbc
, SQL_DRIVER_VER
, (UCHAR
*) dbInf
.driverVer
, 40, &cb
) != SQL_SUCCESS
)
341 return(DispAllErrors(henv
, hdbc
));
343 if (SQLGetInfo(hdbc
, SQL_ODBC_API_CONFORMANCE
, (UCHAR
*) &dbInf
.apiConfLvl
, sizeof(dbInf
.apiConfLvl
), &cb
) != SQL_SUCCESS
)
344 return(DispAllErrors(henv
, hdbc
));
346 if (SQLGetInfo(hdbc
, SQL_ODBC_SAG_CLI_CONFORMANCE
, (UCHAR
*) &dbInf
.cliConfLvl
, sizeof(dbInf
.cliConfLvl
), &cb
) != SQL_SUCCESS
)
347 return(DispAllErrors(henv
, hdbc
));
349 if (SQLGetInfo(hdbc
, SQL_ODBC_SQL_CONFORMANCE
, (UCHAR
*) &dbInf
.sqlConfLvl
, sizeof(dbInf
.sqlConfLvl
), &cb
) != SQL_SUCCESS
)
350 return(DispAllErrors(henv
, hdbc
));
352 if (SQLGetInfo(hdbc
, SQL_OUTER_JOINS
, (UCHAR
*) dbInf
.outerJoins
, 2, &cb
) != SQL_SUCCESS
)
353 return(DispAllErrors(henv
, hdbc
));
355 if (SQLGetInfo(hdbc
, SQL_PROCEDURES
, (UCHAR
*) dbInf
.procedureSupport
, 2, &cb
) != SQL_SUCCESS
)
356 return(DispAllErrors(henv
, hdbc
));
358 if (SQLGetInfo(hdbc
, SQL_CURSOR_COMMIT_BEHAVIOR
, (UCHAR
*) &dbInf
.cursorCommitBehavior
, sizeof(dbInf
.cursorCommitBehavior
), &cb
) != SQL_SUCCESS
)
359 return(DispAllErrors(henv
, hdbc
));
361 if (SQLGetInfo(hdbc
, SQL_CURSOR_ROLLBACK_BEHAVIOR
, (UCHAR
*) &dbInf
.cursorRollbackBehavior
, sizeof(dbInf
.cursorRollbackBehavior
), &cb
) != SQL_SUCCESS
)
362 return(DispAllErrors(henv
, hdbc
));
364 if (SQLGetInfo(hdbc
, SQL_NON_NULLABLE_COLUMNS
, (UCHAR
*) &dbInf
.supportNotNullClause
, sizeof(dbInf
.supportNotNullClause
), &cb
) != SQL_SUCCESS
)
365 return(DispAllErrors(henv
, hdbc
));
367 if (SQLGetInfo(hdbc
, SQL_ODBC_SQL_OPT_IEF
, (UCHAR
*) dbInf
.supportIEF
, 2, &cb
) != SQL_SUCCESS
)
368 return(DispAllErrors(henv
, hdbc
));
370 if (SQLGetInfo(hdbc
, SQL_DEFAULT_TXN_ISOLATION
, (UCHAR
*) &dbInf
.txnIsolation
, sizeof(dbInf
.txnIsolation
), &cb
) != SQL_SUCCESS
)
371 return(DispAllErrors(henv
, hdbc
));
373 if (SQLGetInfo(hdbc
, SQL_TXN_ISOLATION_OPTION
, (UCHAR
*) &dbInf
.txnIsolationOptions
, sizeof(dbInf
.txnIsolationOptions
), &cb
) != SQL_SUCCESS
)
374 return(DispAllErrors(henv
, hdbc
));
376 if (SQLGetInfo(hdbc
, SQL_FETCH_DIRECTION
, (UCHAR
*) &dbInf
.fetchDirections
, sizeof(dbInf
.fetchDirections
), &cb
) != SQL_SUCCESS
)
377 return(DispAllErrors(henv
, hdbc
));
379 if (SQLGetInfo(hdbc
, SQL_LOCK_TYPES
, (UCHAR
*) &dbInf
.lockTypes
, sizeof(dbInf
.lockTypes
), &cb
) != SQL_SUCCESS
)
380 return(DispAllErrors(henv
, hdbc
));
382 if (SQLGetInfo(hdbc
, SQL_POS_OPERATIONS
, (UCHAR
*) &dbInf
.posOperations
, sizeof(dbInf
.posOperations
), &cb
) != SQL_SUCCESS
)
383 return(DispAllErrors(henv
, hdbc
));
385 if (SQLGetInfo(hdbc
, SQL_POSITIONED_STATEMENTS
, (UCHAR
*) &dbInf
.posStmts
, sizeof(dbInf
.posStmts
), &cb
) != SQL_SUCCESS
)
386 return(DispAllErrors(henv
, hdbc
));
388 if (SQLGetInfo(hdbc
, SQL_SCROLL_CONCURRENCY
, (UCHAR
*) &dbInf
.scrollConcurrency
, sizeof(dbInf
.scrollConcurrency
), &cb
) != SQL_SUCCESS
)
389 return(DispAllErrors(henv
, hdbc
));
391 if (SQLGetInfo(hdbc
, SQL_SCROLL_OPTIONS
, (UCHAR
*) &dbInf
.scrollOptions
, sizeof(dbInf
.scrollOptions
), &cb
) != SQL_SUCCESS
)
392 return(DispAllErrors(henv
, hdbc
));
394 if (SQLGetInfo(hdbc
, SQL_STATIC_SENSITIVITY
, (UCHAR
*) &dbInf
.staticSensitivity
, sizeof(dbInf
.staticSensitivity
), &cb
) != SQL_SUCCESS
)
395 return(DispAllErrors(henv
, hdbc
));
397 if (SQLGetInfo(hdbc
, SQL_TXN_CAPABLE
, (UCHAR
*) &dbInf
.txnCapable
, sizeof(dbInf
.txnCapable
), &cb
) != SQL_SUCCESS
)
398 return(DispAllErrors(henv
, hdbc
));
400 if (SQLGetInfo(hdbc
, SQL_LOGIN_TIMEOUT
, (UCHAR
*) &dbInf
.loginTimeout
, sizeof(dbInf
.loginTimeout
), &cb
) != SQL_SUCCESS
)
401 return(DispAllErrors(henv
, hdbc
));
403 #ifdef DBDEBUG_CONSOLE
404 cout
<< ">>>>> DATA SOURCE INFORMATION <<<<<" << endl
;
405 cout
<< "SERVER Name: " << dbInf
.serverName
<< endl
;
406 cout
<< "DBMS Name: " << dbInf
.dbmsName
<< "; DBMS Version: " << dbInf
.dbmsVer
<< endl
;
407 cout
<< "ODBC Version: " << dbInf
.odbcVer
<< "; Driver Version: " << dbInf
.driverVer
<< endl
;
409 cout
<< "API Conf. Level: ";
410 switch(dbInf
.apiConfLvl
)
412 case SQL_OAC_NONE
: cout
<< "None"; break;
413 case SQL_OAC_LEVEL1
: cout
<< "Level 1"; break;
414 case SQL_OAC_LEVEL2
: cout
<< "Level 2"; break;
418 cout
<< "SAG CLI Conf. Level: ";
419 switch(dbInf
.cliConfLvl
)
421 case SQL_OSCC_NOT_COMPLIANT
: cout
<< "Not Compliant"; break;
422 case SQL_OSCC_COMPLIANT
: cout
<< "Compliant"; break;
426 cout
<< "SQL Conf. Level: ";
427 switch(dbInf
.sqlConfLvl
)
429 case SQL_OSC_MINIMUM
: cout
<< "Minimum Grammer"; break;
430 case SQL_OSC_CORE
: cout
<< "Core Grammer"; break;
431 case SQL_OSC_EXTENDED
: cout
<< "Extended Grammer"; break;
435 cout
<< "Max. Connections: " << dbInf
.maxConnections
<< endl
;
436 cout
<< "Outer Joins: " << dbInf
.outerJoins
<< endl
;
437 cout
<< "Support for Procedures: " << dbInf
.procedureSupport
<< endl
;
439 cout
<< "Cursor COMMIT Behavior: ";
440 switch(dbInf
.cursorCommitBehavior
)
442 case SQL_CB_DELETE
: cout
<< "Delete cursors"; break;
443 case SQL_CB_CLOSE
: cout
<< "Close cursors"; break;
444 case SQL_CB_PRESERVE
: cout
<< "Preserve cursors"; break;
448 cout
<< "Cursor ROLLBACK Behavior: ";
449 switch(dbInf
.cursorRollbackBehavior
)
451 case SQL_CB_DELETE
: cout
<< "Delete cursors"; break;
452 case SQL_CB_CLOSE
: cout
<< "Close cursors"; break;
453 case SQL_CB_PRESERVE
: cout
<< "Preserve cursors"; break;
457 cout
<< "Support NOT NULL clause: ";
458 switch(dbInf
.supportNotNullClause
)
460 case SQL_NNC_NULL
: cout
<< "No"; break;
461 case SQL_NNC_NON_NULL
: cout
<< "Yes"; break;
465 cout
<< "Support IEF (Ref. Integrity): " << dbInf
.supportIEF
<< endl
;
466 cout
<< "Login Timeout: " << dbInf
.loginTimeout
<< endl
;
468 cout
<< endl
<< endl
<< "more ..." << endl
;
471 cout
<< "Default Transaction Isolation: ";
472 switch(dbInf
.txnIsolation
)
474 case SQL_TXN_READ_UNCOMMITTED
: cout
<< "Read Uncommitted"; break;
475 case SQL_TXN_READ_COMMITTED
: cout
<< "Read Committed"; break;
476 case SQL_TXN_REPEATABLE_READ
: cout
<< "Repeatable Read"; break;
477 case SQL_TXN_SERIALIZABLE
: cout
<< "Serializable"; break;
479 case SQL_TXN_VERSIONING
: cout
<< "Versioning"; break;
484 cout
<< "Transaction Isolation Options: ";
485 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_UNCOMMITTED
)
486 cout
<< "Read Uncommitted, ";
487 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_COMMITTED
)
488 cout
<< "Read Committed, ";
489 if (dbInf
.txnIsolationOptions
& SQL_TXN_REPEATABLE_READ
)
490 cout
<< "Repeatable Read, ";
491 if (dbInf
.txnIsolationOptions
& SQL_TXN_SERIALIZABLE
)
492 cout
<< "Serializable, ";
494 if (dbInf
.txnIsolationOptions
& SQL_TXN_VERSIONING
)
495 cout
<< "Versioning";
499 cout
<< "Fetch Directions Supported:" << endl
<< " ";
500 if (dbInf
.fetchDirections
& SQL_FD_FETCH_NEXT
)
502 if (dbInf
.fetchDirections
& SQL_FD_FETCH_PRIOR
)
504 if (dbInf
.fetchDirections
& SQL_FD_FETCH_FIRST
)
506 if (dbInf
.fetchDirections
& SQL_FD_FETCH_LAST
)
508 if (dbInf
.fetchDirections
& SQL_FD_FETCH_ABSOLUTE
)
509 cout
<< "Absolute, ";
510 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RELATIVE
)
511 cout
<< "Relative, ";
513 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RESUME
)
516 if (dbInf
.fetchDirections
& SQL_FD_FETCH_BOOKMARK
)
520 cout
<< "Lock Types Supported (SQLSetPos): ";
521 if (dbInf
.lockTypes
& SQL_LCK_NO_CHANGE
)
522 cout
<< "No Change, ";
523 if (dbInf
.lockTypes
& SQL_LCK_EXCLUSIVE
)
524 cout
<< "Exclusive, ";
525 if (dbInf
.lockTypes
& SQL_LCK_UNLOCK
)
529 cout
<< "Position Operations Supported (SQLSetPos): ";
530 if (dbInf
.posOperations
& SQL_POS_POSITION
)
531 cout
<< "Position, ";
532 if (dbInf
.posOperations
& SQL_POS_REFRESH
)
534 if (dbInf
.posOperations
& SQL_POS_UPDATE
)
536 if (dbInf
.posOperations
& SQL_POS_DELETE
)
538 if (dbInf
.posOperations
& SQL_POS_ADD
)
542 cout
<< "Positioned Statements Supported: ";
543 if (dbInf
.posStmts
& SQL_PS_POSITIONED_DELETE
)
544 cout
<< "Pos delete, ";
545 if (dbInf
.posStmts
& SQL_PS_POSITIONED_UPDATE
)
546 cout
<< "Pos update, ";
547 if (dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
548 cout
<< "Select for update";
551 cout
<< "Scroll Concurrency: ";
552 if (dbInf
.scrollConcurrency
& SQL_SCCO_READ_ONLY
)
553 cout
<< "Read Only, ";
554 if (dbInf
.scrollConcurrency
& SQL_SCCO_LOCK
)
556 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_ROWVER
)
557 cout
<< "Opt. Rowver, ";
558 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_VALUES
)
559 cout
<< "Opt. Values";
562 cout
<< "Scroll Options: ";
563 if (dbInf
.scrollOptions
& SQL_SO_FORWARD_ONLY
)
564 cout
<< "Fwd Only, ";
565 if (dbInf
.scrollOptions
& SQL_SO_STATIC
)
567 if (dbInf
.scrollOptions
& SQL_SO_KEYSET_DRIVEN
)
568 cout
<< "Keyset Driven, ";
569 if (dbInf
.scrollOptions
& SQL_SO_DYNAMIC
)
571 if (dbInf
.scrollOptions
& SQL_SO_MIXED
)
575 cout
<< "Static Sensitivity: ";
576 if (dbInf
.staticSensitivity
& SQL_SS_ADDITIONS
)
577 cout
<< "Additions, ";
578 if (dbInf
.staticSensitivity
& SQL_SS_DELETIONS
)
579 cout
<< "Deletions, ";
580 if (dbInf
.staticSensitivity
& SQL_SS_UPDATES
)
584 cout
<< "Transaction Capable?: ";
585 switch(dbInf
.txnCapable
)
587 case SQL_TC_NONE
: cout
<< "No"; break;
588 case SQL_TC_DML
: cout
<< "DML Only"; break;
589 case SQL_TC_DDL_COMMIT
: cout
<< "DDL Commit"; break;
590 case SQL_TC_DDL_IGNORE
: cout
<< "DDL Ignore"; break;
591 case SQL_TC_ALL
: cout
<< "DDL & DML"; break;
599 // Completed Successfully
602 } // wxDB::getDbInfo()
604 /********** wxDB::getDataTypeInfo() **********/
605 bool wxDB::getDataTypeInfo(SWORD fSqlType
, SqlTypeInfo
&structSQLTypeInfo
)
607 // fSqlType will be something like SQL_VARCHAR. This parameter determines
608 // the data type inf. is gathered for.
610 // SqlTypeInfo is a structure that is filled in with data type information,
615 // Get information about the data type specified
616 if (SQLGetTypeInfo(hstmt
, fSqlType
) != SQL_SUCCESS
)
617 return(DispAllErrors(henv
, hdbc
, hstmt
));
619 if ((retcode
= SQLFetch(hstmt
)) != SQL_SUCCESS
)
621 #ifdef DBDEBUG_CONSOLE
622 if (retcode
== SQL_NO_DATA_FOUND
)
623 cout
<< "SQL_NO_DATA_FOUND fetching inf. about data type." << endl
;
625 DispAllErrors(henv
, hdbc
, hstmt
);
626 SQLFreeStmt(hstmt
, SQL_CLOSE
);
629 // Obtain columns from the record
630 if (SQLGetData(hstmt
, 1, SQL_C_CHAR
, (UCHAR
*) structSQLTypeInfo
.TypeName
, DB_TYPE_NAME_LEN
, &cbRet
) != SQL_SUCCESS
)
631 return(DispAllErrors(henv
, hdbc
, hstmt
));
632 if (SQLGetData(hstmt
, 3, SQL_C_LONG
, (UCHAR
*) &structSQLTypeInfo
.Precision
, 0, &cbRet
) != SQL_SUCCESS
)
633 return(DispAllErrors(henv
, hdbc
, hstmt
));
634 if (SQLGetData(hstmt
, 8, SQL_C_SHORT
, (UCHAR
*) &structSQLTypeInfo
.CaseSensitive
, 0, &cbRet
) != SQL_SUCCESS
)
635 return(DispAllErrors(henv
, hdbc
, hstmt
));
636 // if (SQLGetData(hstmt, 14, SQL_C_SHORT, (UCHAR*) &structSQLTypeInfo.MinimumScale, 0, &cbRet) != SQL_SUCCESS)
637 // return(DispAllErrors(henv, hdbc, hstmt));
638 if (SQLGetData(hstmt
, 15, SQL_C_SHORT
, (UCHAR
*) &structSQLTypeInfo
.MaximumScale
, 0, &cbRet
) != SQL_SUCCESS
)
639 return(DispAllErrors(henv
, hdbc
, hstmt
));
641 if (structSQLTypeInfo
.MaximumScale
< 0)
642 structSQLTypeInfo
.MaximumScale
= 0;
644 // Close the statement handle which closes open cursors
645 if (SQLFreeStmt(hstmt
, SQL_CLOSE
) != SQL_SUCCESS
)
646 return(DispAllErrors(henv
, hdbc
, hstmt
));
648 // Completed Successfully
651 } // wxDB::getDataTypeInfo()
653 /********** wxDB::Close() **********/
654 void wxDB::Close(void)
656 // Close the Sql Log file
663 // Free statement handle
666 if (SQLFreeStmt(hstmt
, SQL_DROP
) != SQL_SUCCESS
)
667 DispAllErrors(henv
, hdbc
);
670 // Disconnect from the datasource
671 if (SQLDisconnect(hdbc
) != SQL_SUCCESS
)
672 DispAllErrors(henv
, hdbc
);
674 // Free the connection to the datasource
675 if (SQLFreeConnect(hdbc
) != SQL_SUCCESS
)
676 DispAllErrors(henv
, hdbc
);
680 /********** wxDB::CommitTrans() **********/
681 bool wxDB::CommitTrans(void)
683 // Commit the transaction
684 if (SQLTransact(henv
, hdbc
, SQL_COMMIT
) != SQL_SUCCESS
)
685 return(DispAllErrors(henv
, hdbc
));
687 // Completed successfully
690 } // wxDB::CommitTrans()
692 /********** wxDB::RollbackTrans() **********/
693 bool wxDB::RollbackTrans(void)
695 // Rollback the transaction
696 if (SQLTransact(henv
, hdbc
, SQL_ROLLBACK
) != SQL_SUCCESS
)
697 return(DispAllErrors(henv
, hdbc
));
699 // Completed successfully
702 } // wxDB::RollbackTrans()
704 /********** wxDB::DispAllErrors() **********/
705 bool wxDB::DispAllErrors(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
707 char odbcErrMsg
[DB_MAX_ERROR_MSG_LEN
];
709 while (SQLError(aHenv
, aHdbc
, aHstmt
, (UCHAR FAR
*) sqlState
, &nativeError
, (UCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
711 sprintf(odbcErrMsg
, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState
, nativeError
, errorMsg
);
712 logError(odbcErrMsg
, sqlState
);
715 #ifdef DBDEBUG_CONSOLE
716 // When run in console mode, use standard out to display errors.
717 cout
<< odbcErrMsg
<< endl
;
718 cout
<< "Press any key to continue..." << endl
;
724 return(FALSE
); // This function alway's returns false.
726 } // wxDB::DispAllErrors()
728 /********** wxDB::GetNextError() **********/
729 bool wxDB::GetNextError(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
731 if (SQLError(aHenv
, aHdbc
, aHstmt
, (UCHAR FAR
*) sqlState
, &nativeError
, (UCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
736 } // wxDB::GetNextError()
738 /********** wxDB::DispNextError() **********/
739 void wxDB::DispNextError(void)
741 char odbcErrMsg
[DB_MAX_ERROR_MSG_LEN
];
743 sprintf(odbcErrMsg
, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState
, nativeError
, errorMsg
);
744 logError(odbcErrMsg
, sqlState
);
749 #ifdef DBDEBUG_CONSOLE
750 // When run in console mode, use standard out to display errors.
751 cout
<< odbcErrMsg
<< endl
;
752 cout
<< "Press any key to continue..." << endl
;
756 } // wxDB::DispNextError()
758 /********** wxDB::logError() **********/
759 void wxDB::logError(char *errMsg
, char *SQLState
)
761 assert(errMsg
&& strlen(errMsg
));
763 static int pLast
= -1;
766 if (++pLast
== DB_MAX_ERROR_HISTORY
)
768 for (int i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
769 strcpy(errorList
[i
], errorList
[i
+1]);
773 strcpy(errorList
[pLast
], errMsg
);
775 if (SQLState
&& strlen(SQLState
))
776 if ((dbStatus
= TranslateSqlState(SQLState
)) != DB_ERR_FUNCTION_SEQUENCE_ERROR
)
777 DB_STATUS
= dbStatus
;
779 } // wxDB::logError()
781 /**********wxDB::TranslateSqlState() **********/
782 int wxDB::TranslateSqlState(char *SQLState
)
784 if (!strcmp(SQLState
, "01000"))
785 return(DB_ERR_GENERAL_WARNING
);
786 if (!strcmp(SQLState
, "01002"))
787 return(DB_ERR_DISCONNECT_ERROR
);
788 if (!strcmp(SQLState
, "01004"))
789 return(DB_ERR_DATA_TRUNCATED
);
790 if (!strcmp(SQLState
, "01006"))
791 return(DB_ERR_PRIV_NOT_REVOKED
);
792 if (!strcmp(SQLState
, "01S00"))
793 return(DB_ERR_INVALID_CONN_STR_ATTR
);
794 if (!strcmp(SQLState
, "01S01"))
795 return(DB_ERR_ERROR_IN_ROW
);
796 if (!strcmp(SQLState
, "01S02"))
797 return(DB_ERR_OPTION_VALUE_CHANGED
);
798 if (!strcmp(SQLState
, "01S03"))
799 return(DB_ERR_NO_ROWS_UPD_OR_DEL
);
800 if (!strcmp(SQLState
, "01S04"))
801 return(DB_ERR_MULTI_ROWS_UPD_OR_DEL
);
802 if (!strcmp(SQLState
, "07001"))
803 return(DB_ERR_WRONG_NO_OF_PARAMS
);
804 if (!strcmp(SQLState
, "07006"))
805 return(DB_ERR_DATA_TYPE_ATTR_VIOL
);
806 if (!strcmp(SQLState
, "08001"))
807 return(DB_ERR_UNABLE_TO_CONNECT
);
808 if (!strcmp(SQLState
, "08002"))
809 return(DB_ERR_CONNECTION_IN_USE
);
810 if (!strcmp(SQLState
, "08003"))
811 return(DB_ERR_CONNECTION_NOT_OPEN
);
812 if (!strcmp(SQLState
, "08004"))
813 return(DB_ERR_REJECTED_CONNECTION
);
814 if (!strcmp(SQLState
, "08007"))
815 return(DB_ERR_CONN_FAIL_IN_TRANS
);
816 if (!strcmp(SQLState
, "08S01"))
817 return(DB_ERR_COMM_LINK_FAILURE
);
818 if (!strcmp(SQLState
, "21S01"))
819 return(DB_ERR_INSERT_VALUE_LIST_MISMATCH
);
820 if (!strcmp(SQLState
, "21S02"))
821 return(DB_ERR_DERIVED_TABLE_MISMATCH
);
822 if (!strcmp(SQLState
, "22001"))
823 return(DB_ERR_STRING_RIGHT_TRUNC
);
824 if (!strcmp(SQLState
, "22003"))
825 return(DB_ERR_NUMERIC_VALUE_OUT_OF_RNG
);
826 if (!strcmp(SQLState
, "22005"))
827 return(DB_ERR_ERROR_IN_ASSIGNMENT
);
828 if (!strcmp(SQLState
, "22008"))
829 return(DB_ERR_DATETIME_FLD_OVERFLOW
);
830 if (!strcmp(SQLState
, "22012"))
831 return(DB_ERR_DIVIDE_BY_ZERO
);
832 if (!strcmp(SQLState
, "22026"))
833 return(DB_ERR_STR_DATA_LENGTH_MISMATCH
);
834 if (!strcmp(SQLState
, "23000"))
835 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
836 if (!strcmp(SQLState
, "24000"))
837 return(DB_ERR_INVALID_CURSOR_STATE
);
838 if (!strcmp(SQLState
, "25000"))
839 return(DB_ERR_INVALID_TRANS_STATE
);
840 if (!strcmp(SQLState
, "28000"))
841 return(DB_ERR_INVALID_AUTH_SPEC
);
842 if (!strcmp(SQLState
, "34000"))
843 return(DB_ERR_INVALID_CURSOR_NAME
);
844 if (!strcmp(SQLState
, "37000"))
845 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL
);
846 if (!strcmp(SQLState
, "3C000"))
847 return(DB_ERR_DUPLICATE_CURSOR_NAME
);
848 if (!strcmp(SQLState
, "40001"))
849 return(DB_ERR_SERIALIZATION_FAILURE
);
850 if (!strcmp(SQLState
, "42000"))
851 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2
);
852 if (!strcmp(SQLState
, "70100"))
853 return(DB_ERR_OPERATION_ABORTED
);
854 if (!strcmp(SQLState
, "IM001"))
855 return(DB_ERR_UNSUPPORTED_FUNCTION
);
856 if (!strcmp(SQLState
, "IM002"))
857 return(DB_ERR_NO_DATA_SOURCE
);
858 if (!strcmp(SQLState
, "IM003"))
859 return(DB_ERR_DRIVER_LOAD_ERROR
);
860 if (!strcmp(SQLState
, "IM004"))
861 return(DB_ERR_SQLALLOCENV_FAILED
);
862 if (!strcmp(SQLState
, "IM005"))
863 return(DB_ERR_SQLALLOCCONNECT_FAILED
);
864 if (!strcmp(SQLState
, "IM006"))
865 return(DB_ERR_SQLSETCONNECTOPTION_FAILED
);
866 if (!strcmp(SQLState
, "IM007"))
867 return(DB_ERR_NO_DATA_SOURCE_DLG_PROHIB
);
868 if (!strcmp(SQLState
, "IM008"))
869 return(DB_ERR_DIALOG_FAILED
);
870 if (!strcmp(SQLState
, "IM009"))
871 return(DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL
);
872 if (!strcmp(SQLState
, "IM010"))
873 return(DB_ERR_DATA_SOURCE_NAME_TOO_LONG
);
874 if (!strcmp(SQLState
, "IM011"))
875 return(DB_ERR_DRIVER_NAME_TOO_LONG
);
876 if (!strcmp(SQLState
, "IM012"))
877 return(DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR
);
878 if (!strcmp(SQLState
, "IM013"))
879 return(DB_ERR_TRACE_FILE_ERROR
);
880 if (!strcmp(SQLState
, "S0001"))
881 return(DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS
);
882 if (!strcmp(SQLState
, "S0002"))
883 return(DB_ERR_TABLE_NOT_FOUND
);
884 if (!strcmp(SQLState
, "S0011"))
885 return(DB_ERR_INDEX_ALREADY_EXISTS
);
886 if (!strcmp(SQLState
, "S0012"))
887 return(DB_ERR_INDEX_NOT_FOUND
);
888 if (!strcmp(SQLState
, "S0021"))
889 return(DB_ERR_COLUMN_ALREADY_EXISTS
);
890 if (!strcmp(SQLState
, "S0022"))
891 return(DB_ERR_COLUMN_NOT_FOUND
);
892 if (!strcmp(SQLState
, "S0023"))
893 return(DB_ERR_NO_DEFAULT_FOR_COLUMN
);
894 if (!strcmp(SQLState
, "S1000"))
895 return(DB_ERR_GENERAL_ERROR
);
896 if (!strcmp(SQLState
, "S1001"))
897 return(DB_ERR_MEMORY_ALLOCATION_FAILURE
);
898 if (!strcmp(SQLState
, "S1002"))
899 return(DB_ERR_INVALID_COLUMN_NUMBER
);
900 if (!strcmp(SQLState
, "S1003"))
901 return(DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE
);
902 if (!strcmp(SQLState
, "S1004"))
903 return(DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE
);
904 if (!strcmp(SQLState
, "S1008"))
905 return(DB_ERR_OPERATION_CANCELLED
);
906 if (!strcmp(SQLState
, "S1009"))
907 return(DB_ERR_INVALID_ARGUMENT_VALUE
);
908 if (!strcmp(SQLState
, "S1010"))
909 return(DB_ERR_FUNCTION_SEQUENCE_ERROR
);
910 if (!strcmp(SQLState
, "S1011"))
911 return(DB_ERR_OPERATION_INVALID_AT_THIS_TIME
);
912 if (!strcmp(SQLState
, "S1012"))
913 return(DB_ERR_INVALID_TRANS_OPERATION_CODE
);
914 if (!strcmp(SQLState
, "S1015"))
915 return(DB_ERR_NO_CURSOR_NAME_AVAIL
);
916 if (!strcmp(SQLState
, "S1090"))
917 return(DB_ERR_INVALID_STR_OR_BUF_LEN
);
918 if (!strcmp(SQLState
, "S1091"))
919 return(DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE
);
920 if (!strcmp(SQLState
, "S1092"))
921 return(DB_ERR_OPTION_TYPE_OUT_OF_RANGE
);
922 if (!strcmp(SQLState
, "S1093"))
923 return(DB_ERR_INVALID_PARAM_NO
);
924 if (!strcmp(SQLState
, "S1094"))
925 return(DB_ERR_INVALID_SCALE_VALUE
);
926 if (!strcmp(SQLState
, "S1095"))
927 return(DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE
);
928 if (!strcmp(SQLState
, "S1096"))
929 return(DB_ERR_INF_TYPE_OUT_OF_RANGE
);
930 if (!strcmp(SQLState
, "S1097"))
931 return(DB_ERR_COLUMN_TYPE_OUT_OF_RANGE
);
932 if (!strcmp(SQLState
, "S1098"))
933 return(DB_ERR_SCOPE_TYPE_OUT_OF_RANGE
);
934 if (!strcmp(SQLState
, "S1099"))
935 return(DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE
);
936 if (!strcmp(SQLState
, "S1100"))
937 return(DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE
);
938 if (!strcmp(SQLState
, "S1101"))
939 return(DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE
);
940 if (!strcmp(SQLState
, "S1103"))
941 return(DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE
);
942 if (!strcmp(SQLState
, "S1104"))
943 return(DB_ERR_INVALID_PRECISION_VALUE
);
944 if (!strcmp(SQLState
, "S1105"))
945 return(DB_ERR_INVALID_PARAM_TYPE
);
946 if (!strcmp(SQLState
, "S1106"))
947 return(DB_ERR_FETCH_TYPE_OUT_OF_RANGE
);
948 if (!strcmp(SQLState
, "S1107"))
949 return(DB_ERR_ROW_VALUE_OUT_OF_RANGE
);
950 if (!strcmp(SQLState
, "S1108"))
951 return(DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE
);
952 if (!strcmp(SQLState
, "S1109"))
953 return(DB_ERR_INVALID_CURSOR_POSITION
);
954 if (!strcmp(SQLState
, "S1110"))
955 return(DB_ERR_INVALID_DRIVER_COMPLETION
);
956 if (!strcmp(SQLState
, "S1111"))
957 return(DB_ERR_INVALID_BOOKMARK_VALUE
);
958 if (!strcmp(SQLState
, "S1C00"))
959 return(DB_ERR_DRIVER_NOT_CAPABLE
);
960 if (!strcmp(SQLState
, "S1T00"))
961 return(DB_ERR_TIMEOUT_EXPIRED
);
966 } // wxDB::TranslateSqlState()
968 /********** wxDB::Grant() **********/
969 bool wxDB::Grant(int privileges
, char *tableName
, char *userList
)
971 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
973 // Build the grant statement
974 strcpy(sqlStmt
, "GRANT ");
975 if (privileges
== DB_GRANT_ALL
)
976 strcat(sqlStmt
, "ALL");
980 if (privileges
& DB_GRANT_SELECT
)
982 strcat(sqlStmt
, "SELECT");
985 if (privileges
& DB_GRANT_INSERT
)
988 strcat(sqlStmt
, ", ");
989 strcat(sqlStmt
, "INSERT");
991 if (privileges
& DB_GRANT_UPDATE
)
994 strcat(sqlStmt
, ", ");
995 strcat(sqlStmt
, "UPDATE");
997 if (privileges
& DB_GRANT_DELETE
)
1000 strcat(sqlStmt
, ", ");
1001 strcat(sqlStmt
, "DELETE");
1005 strcat(sqlStmt
, " ON ");
1006 strcat(sqlStmt
, tableName
);
1007 strcat(sqlStmt
, " TO ");
1008 strcat(sqlStmt
, userList
);
1010 #ifdef DBDEBUG_CONSOLE
1011 cout
<< endl
<< sqlStmt
<< endl
;
1014 WriteSqlLog(sqlStmt
);
1016 return(ExecSql(sqlStmt
));
1020 /********** wxDB::CreateView() **********/
1021 bool wxDB::CreateView(char *viewName
, char *colList
, char *pSqlStmt
)
1023 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1025 // Drop the view first
1026 sprintf(sqlStmt
, "DROP VIEW %s", viewName
);
1027 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1029 // Check for sqlState = S0002, "Table or view not found".
1030 // Ignore this error, bomb out on any other error.
1031 // SQL Sybase Anwhere v5.5 returns an access violation error here
1032 // (sqlstate = 42000) rather than an S0002.
1033 GetNextError(henv
, hdbc
, hstmt
);
1034 if (strcmp(sqlState
, "S0002") && strcmp(sqlState
, "42000"))
1037 DispAllErrors(henv
, hdbc
, hstmt
);
1043 WriteSqlLog(sqlStmt
);
1045 #ifdef DBDEBUG_CONSOLE
1046 cout
<< endl
<< sqlStmt
<< endl
;
1049 // Build the create view statement
1050 strcpy(sqlStmt
, "CREATE VIEW ");
1051 strcat(sqlStmt
, viewName
);
1053 if (strlen(colList
))
1055 strcat(sqlStmt
, " (");
1056 strcat(sqlStmt
, colList
);
1057 strcat(sqlStmt
, ")");
1060 strcat(sqlStmt
, " AS ");
1061 strcat(sqlStmt
, pSqlStmt
);
1063 WriteSqlLog(sqlStmt
);
1065 #ifdef DBDEBUG_CONSOLE
1066 cout
<< sqlStmt
<< endl
;
1069 return(ExecSql(sqlStmt
));
1071 } // wxDB::CreateView()
1073 /********** wxDB::ExecSql() **********/
1074 bool wxDB::ExecSql(char *pSqlStmt
)
1076 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) == SQL_SUCCESS
)
1080 DispAllErrors(henv
, hdbc
, hstmt
);
1084 } // wxDB::ExecSql()
1086 /********** wxDB::GetColumns() **********/
1088 * 1) The last array element of the tableName[] argument must be zero (null).
1089 * This is how the end of the array is detected.
1090 * 2) This function returns an array of CcolInf structures. If no columns
1091 * were found, or an error occured, this pointer will be zero (null). THE
1092 * CALLING FUNCTION IS RESPONSIBLE FOR DELETING THE MEMORY RETURNED WHEN IT
1093 * IS FINISHED WITH IT. i.e.
1095 * CcolInf *colInf = pDb->GetColumns(tableList);
1098 * // Use the column inf
1100 * // Destroy the memory
1104 CcolInf
*wxDB::GetColumns(char *tableName
[])
1108 CcolInf
*colInf
= 0;
1111 char tblName
[DB_MAX_TABLE_NAME_LEN
+1];
1112 char colName
[DB_MAX_COLUMN_NAME_LEN
+1];
1115 // Pass 1 - Determine how many columns there are.
1116 // Pass 2 - Allocate the CcolInf array and fill in
1117 // the array with the column information.
1118 for (int pass
= 1; pass
<= 2; pass
++)
1122 if (noCols
== 0) // Probably a bogus table name(s)
1124 // Allocate n CcolInf objects to hold the column information
1125 colInf
= new CcolInf
[noCols
+1];
1128 // Mark the end of the array
1129 strcpy(colInf
[noCols
].tableName
, "");
1130 strcpy(colInf
[noCols
].colName
, "");
1131 colInf
[noCols
].sqlDataType
= 0;
1133 // Loop through each table name
1134 for (int tbl
= 0; tableName
[tbl
]; tbl
++)
1136 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1137 retcode
= SQLColumns(hstmt
,
1138 NULL
, 0, // All qualifiers
1139 NULL
, 0, // All owners
1140 (UCHAR
*) tableName
[tbl
], SQL_NTS
,
1141 NULL
, 0); // All columns
1142 if (retcode
!= SQL_SUCCESS
)
1143 { // Error occured, abort
1144 DispAllErrors(henv
, hdbc
, hstmt
);
1149 SQLBindCol(hstmt
, 3, SQL_C_CHAR
, (UCHAR
*) tblName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
1150 SQLBindCol(hstmt
, 4, SQL_C_CHAR
, (UCHAR
*) colName
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1151 SQLBindCol(hstmt
, 5, SQL_C_SSHORT
, (UCHAR
*) &sqlDataType
, 0, &cb
);
1152 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
1154 if (pass
== 1) // First pass, just add up the number of columns
1156 else // Pass 2; Fill in the array of structures
1158 if (colNo
< noCols
) // Some extra error checking to prevent memory overwrites
1160 strcpy(colInf
[colNo
].tableName
, tblName
);
1161 strcpy(colInf
[colNo
].colName
, colName
);
1162 colInf
[colNo
].sqlDataType
= sqlDataType
;
1167 if (retcode
!= SQL_NO_DATA_FOUND
)
1168 { // Error occured, abort
1169 DispAllErrors(henv
, hdbc
, hstmt
);
1177 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1180 } // wxDB::GetColumns()
1183 /********** wxDB::Catalog() **********/
1184 bool wxDB::Catalog(char *userID
, char *fileName
)
1186 assert(userID
&& strlen(userID
));
1187 assert(fileName
&& strlen(fileName
));
1191 char tblName
[DB_MAX_TABLE_NAME_LEN
+1];
1192 char tblNameSave
[DB_MAX_TABLE_NAME_LEN
+1];
1193 char colName
[DB_MAX_COLUMN_NAME_LEN
+1];
1196 SWORD precision
, length
;
1198 FILE *fp
= fopen(fileName
,"wt");
1202 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1206 for (char *p
= userID
; *p
; p
++)
1207 userIdUC
[i
++] = toupper(*p
);
1210 retcode
= SQLColumns(hstmt
,
1211 NULL
, 0, // All qualifiers
1212 (UCHAR
*) userIdUC
, SQL_NTS
, // User specified
1213 NULL
, 0, // All tables
1214 NULL
, 0); // All columns
1215 if (retcode
!= SQL_SUCCESS
)
1217 DispAllErrors(henv
, hdbc
, hstmt
);
1222 SQLBindCol(hstmt
, 3, SQL_C_CHAR
, (UCHAR
*) tblName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
1223 SQLBindCol(hstmt
, 4, SQL_C_CHAR
, (UCHAR
*) colName
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1224 SQLBindCol(hstmt
, 5, SQL_C_SSHORT
, (UCHAR
*) &sqlDataType
, 0, &cb
);
1225 SQLBindCol(hstmt
, 6, SQL_C_CHAR
, (UCHAR
*) typeName
, 16, &cb
);
1226 SQLBindCol(hstmt
, 7, SQL_C_SSHORT
, (UCHAR
*) &precision
, 0, &cb
);
1227 SQLBindCol(hstmt
, 8, SQL_C_SSHORT
, (UCHAR
*) &length
, 0, &cb
);
1230 strcpy(tblNameSave
,"");
1233 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
1235 if (strcmp(tblName
,tblNameSave
))
1239 fputs("================================ ", fp
);
1240 fputs("================================ ", fp
);
1241 fputs("===================== ", fp
);
1242 fputs("========= ", fp
);
1243 fputs("=========\n", fp
);
1244 sprintf(outStr
, "%-32s %-32s %-21s %9s %9s\n",
1245 "TABLE NAME", "COLUMN NAME", "DATA TYPE", "PRECISION", "LENGTH");
1247 fputs("================================ ", fp
);
1248 fputs("================================ ", fp
);
1249 fputs("===================== ", fp
);
1250 fputs("========= ", fp
);
1251 fputs("=========\n", fp
);
1252 strcpy(tblNameSave
,tblName
);
1254 sprintf(outStr
, "%-32s %-32s (%04d)%-15s %9d %9d\n",
1255 tblName
, colName
, sqlDataType
, typeName
, precision
, length
);
1256 if (fputs(outStr
, fp
) == EOF
)
1264 if (retcode
!= SQL_NO_DATA_FOUND
)
1266 DispAllErrors(henv
, hdbc
, hstmt
);
1271 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1275 } // wxDB::Catalog()
1278 // Table name can refer to a table, view, alias or synonym. Returns true
1279 // if the object exists in the database. This function does not indicate
1280 // whether or not the user has privleges to query or perform other functions
1282 bool wxDB::TableExists(char *tableName
)
1284 assert(tableName
&& strlen(tableName
));
1286 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1287 RETCODE retcode
= SQLTables(hstmt
,
1288 NULL
, 0, // All qualifiers
1289 NULL
, 0, // All owners
1290 (UCHAR FAR
*)tableName
, SQL_NTS
,
1291 NULL
, 0); // All table types
1292 if (retcode
!= SQL_SUCCESS
)
1293 return(DispAllErrors(henv
, hdbc
, hstmt
));
1295 if (SQLFetch(hstmt
) != SQL_SUCCESS
)
1297 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1298 return(DispAllErrors(henv
, hdbc
, hstmt
));
1301 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1304 } // wxDB::TableExists()
1307 /********** wxDB::SqlLog() **********/
1308 bool wxDB::SqlLog(enum sqlLog state
, char *filename
, bool append
)
1310 assert(state
== sqlLogON
|| state
== sqlLogOFF
);
1311 assert(state
== sqlLogOFF
|| filename
);
1313 if (state
== sqlLogON
)
1317 fpSqlLog
= fopen(filename
, (append
? "at" : "wt"));
1318 if (fpSqlLog
== NULL
)
1326 if (fclose(fpSqlLog
))
1332 sqlLogState
= state
;
1338 /********** wxDB::WriteSqlLog() **********/
1339 bool wxDB::WriteSqlLog(char *logMsg
)
1343 if (fpSqlLog
== 0 || sqlLogState
== sqlLogOFF
)
1346 if (fputs("\n", fpSqlLog
) == EOF
) return(FALSE
);
1347 if (fputs(logMsg
, fpSqlLog
) == EOF
) return(FALSE
);
1348 if (fputs("\n", fpSqlLog
) == EOF
) return(FALSE
);
1352 } // wxDB::WriteSqlLog()
1355 /********** GetDbConnection() **********/
1356 wxDB
*GetDbConnection(DbStuff
*pDbStuff
)
1360 // Scan the linked list searching for an available database connection
1361 // that's already been opened but is currently not in use.
1362 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1364 // The database connection must be for the same datasource
1365 // name and must currently not be in use.
1366 if (pList
->Free
&& (! strcmp(pDbStuff
->Dsn
, pList
->Dsn
))) // Found a free connection
1368 pList
->Free
= FALSE
;
1369 return(pList
->PtrDb
);
1373 // No available connections. A new connection must be made and
1374 // appended to the end of the linked list.
1377 // Find the end of the list
1378 for (pList
= PtrBegDbList
; pList
->PtrNext
; pList
= pList
->PtrNext
);
1379 // Append a new list item
1380 pList
->PtrNext
= new DbList
;
1381 pList
->PtrNext
->PtrPrev
= pList
;
1382 pList
= pList
->PtrNext
;
1386 // Create the first node on the list
1387 pList
= PtrBegDbList
= new DbList
;
1391 // Initialize new node in the linked list
1393 pList
->Free
= FALSE
;
1394 strcpy(pList
->Dsn
, pDbStuff
->Dsn
);
1395 pList
->PtrDb
= new wxDB(pDbStuff
->Henv
);
1397 // Connect to the datasource
1398 if (pList
->PtrDb
->Open(pDbStuff
->Dsn
, pDbStuff
->Uid
, pDbStuff
->AuthStr
))
1399 return(pList
->PtrDb
);
1400 else // Unable to connect, destroy list item
1403 pList
->PtrPrev
->PtrNext
= 0;
1405 PtrBegDbList
= 0; // Empty list again
1406 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDB object
1407 pList
->PtrDb
->Close(); // Close the wxDB object
1408 delete pList
->PtrDb
; // Deletes the wxDB object
1409 delete pList
; // Deletes the linked list object
1413 } // GetDbConnection()
1415 /********** FreeDbConnection() **********/
1416 bool FreeDbConnection(wxDB
*pDb
)
1420 // Scan the linked list searching for the database connection
1421 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1423 if (pList
->PtrDb
== pDb
) // Found it!!!
1424 return(pList
->Free
= TRUE
);
1427 // Never found the database object, return failure
1430 } // FreeDbConnection()
1432 /********** CloseDbConnections() **********/
1433 void CloseDbConnections(void)
1435 DbList
*pList
, *pNext
;
1437 // Traverse the linked list closing database connections and freeing memory as I go.
1438 for (pList
= PtrBegDbList
; pList
; pList
= pNext
)
1440 pNext
= pList
->PtrNext
; // Save the pointer to next
1441 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDB object
1442 pList
->PtrDb
->Close(); // Close the wxDB object
1443 delete pList
->PtrDb
; // Deletes the wxDB object
1444 delete pList
; // Deletes the linked list object
1447 // Mark the list as empty
1450 } // CloseDbConnections()
1452 /********** NumberDbConnectionsInUse() **********/
1453 int NumberDbConnectionsInUse(void)
1458 // Scan the linked list counting db connections that are currently in use
1459 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1461 if (pList
->Free
== FALSE
)
1467 } // NumberDbConnectionsInUse()
1469 /********** GetDataSource() **********/
1470 bool GetDataSource(HENV henv
, char *Dsn
, SWORD DsnMax
, char *DsDesc
, SWORD DsDescMax
,
1475 if (SQLDataSources(henv
, direction
, (UCHAR FAR
*) Dsn
, DsnMax
, &cb
,
1476 (UCHAR FAR
*) DsDesc
, DsDescMax
, &cb
) == SQL_SUCCESS
)
1481 } // GetDataSource()