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.
10 // Copyright: (c) 1996 Remstar International, Inc.
11 // Licence: wxWindows licence, plus:
12 // Notice: This class library and its intellectual design are free of charge for use,
13 // modification, enhancement, debugging under the following conditions:
14 // 1) These classes may only be used as part of the implementation of a
15 // wxWindows-based application
16 // 2) All enhancements and bug fixes are to be submitted back to the wxWindows
17 // user groups free of all charges for use with the wxWindows library.
18 // 3) These classes may not be distributed as part of any other class library,
19 // DLL, text (written or electronic), other than a complete distribution of
20 // the wxWindows GUI development toolkit.
21 ///////////////////////////////////////////////////////////////////////////////
29 #pragma implementation "db.h"
38 #include "wx/wxprec.h"
56 DbList
*PtrBegDbList
= 0;
58 /********** wxDB Constructor **********/
59 wxDB::wxDB(HENV
&aHenv
)
65 nativeError
= cbErrorMsg
= 0;
66 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
67 strcpy(errorList
[i
], "");
69 // Init typeInf structures
70 strcpy(typeInfVarchar
.TypeName
,"");
71 typeInfVarchar
.FsqlType
= 0;
72 typeInfVarchar
.Precision
= 0;
73 typeInfVarchar
.CaseSensitive
= 0;
74 typeInfVarchar
.MaximumScale
= 0;
76 strcpy(typeInfInteger
.TypeName
,"");
77 typeInfInteger
.FsqlType
= 0;
78 typeInfInteger
.Precision
= 0;
79 typeInfInteger
.CaseSensitive
= 0;
80 typeInfInteger
.MaximumScale
= 0;
82 strcpy(typeInfFloat
.TypeName
,"");
83 typeInfFloat
.FsqlType
= 0;
84 typeInfFloat
.Precision
= 0;
85 typeInfFloat
.CaseSensitive
= 0;
86 typeInfFloat
.MaximumScale
= 0;
88 strcpy(typeInfDate
.TypeName
,"");
89 typeInfDate
.FsqlType
= 0;
90 typeInfDate
.Precision
= 0;
91 typeInfDate
.CaseSensitive
= 0;
92 typeInfDate
.MaximumScale
= 0;
94 // Error reporting is turned OFF by default
97 // Copy the HENV into the db class
100 // Allocate a data source connection handle
101 if (SQLAllocConnect(henv
, &hdbc
) != SQL_SUCCESS
)
104 // Initialize the db status flag
107 // Mark database as not open as of yet
112 /********** wxDB::Open() **********/
113 bool wxDB::Open(char *Dsn
, char *Uid
, char *AuthStr
)
120 #ifndef FWD_ONLY_CURSORS
124 // Specify that the ODBC cursor library be used, if needed. This must be
125 // specified before the connection is made.
126 retcode
= SQLSetConnectOption(hdbc
, SQL_ODBC_CURSORS
, SQL_CUR_USE_IF_NEEDED
);
129 if (retcode
== SQL_SUCCESS
)
130 cout
<< "SQLSetConnectOption(CURSOR_LIB) successful" << endl
;
132 cout
<< "SQLSetConnectOption(CURSOR_LIB) failed" << endl
;
137 // Connect to the data source
138 if (SQLConnect(hdbc
, (UCHAR FAR
*) Dsn
, SQL_NTS
,
139 (UCHAR FAR
*) Uid
, SQL_NTS
,
140 (UCHAR FAR
*) AuthStr
, SQL_NTS
) != SQL_SUCCESS
)
141 return(DispAllErrors(henv
, hdbc
));
143 // Mark database as open
146 // Allocate a statement handle for the database connection
147 if (SQLAllocStmt(hdbc
, &hstmt
) != SQL_SUCCESS
)
148 return(DispAllErrors(henv
, hdbc
));
150 // Set Connection Options
151 if (! setConnectionOptions())
154 // Query the data source for inf. about itself
158 // Query the data source regarding data type information
161 // The way I determined which SQL data types to use was by calling SQLGetInfo
162 // for all of the possible SQL data types to see which ones were supported. If
163 // a type is not supported, the SQLFetch() that's called from getDataTypeInfo()
164 // fails with SQL_NO_DATA_FOUND. This is ugly because I'm sure the three SQL data
165 // types I've selected below will not alway's be what we want. These are just
166 // what happened to work against an Oracle 7/Intersolv combination. The following is
167 // a complete list of the results I got back against the Oracle 7 database:
169 // SQL_BIGINT SQL_NO_DATA_FOUND
170 // SQL_BINARY SQL_NO_DATA_FOUND
171 // SQL_BIT SQL_NO_DATA_FOUND
172 // SQL_CHAR type name = 'CHAR', Precision = 255
173 // SQL_DATE SQL_NO_DATA_FOUND
174 // SQL_DECIMAL type name = 'NUMBER', Precision = 38
175 // SQL_DOUBLE type name = 'NUMBER', Precision = 15
176 // SQL_FLOAT SQL_NO_DATA_FOUND
177 // SQL_INTEGER SQL_NO_DATA_FOUND
178 // SQL_LONGVARBINARY type name = 'LONG RAW', Precision = 2 billion
179 // SQL_LONGVARCHAR type name = 'LONG', Precision = 2 billion
180 // SQL_NUMERIC SQL_NO_DATA_FOUND
181 // SQL_REAL SQL_NO_DATA_FOUND
182 // SQL_SMALLINT SQL_NO_DATA_FOUND
183 // SQL_TIME SQL_NO_DATA_FOUND
184 // SQL_TIMESTAMP type name = 'DATE', Precision = 19
185 // SQL_VARBINARY type name = 'RAW', Precision = 255
186 // SQL_VARCHAR type name = 'VARCHAR2', Precision = 2000
187 // =====================================================================
188 // Results from a Microsoft Access 7.0 db, using a driver from Microsoft
190 // SQL_VARCHAR type name = 'TEXT', Precision = 255
191 // SQL_TIMESTAMP type name = 'DATETIME'
192 // SQL_DECIMAL SQL_NO_DATA_FOUND
193 // SQL_NUMERIC type name = 'CURRENCY', Precision = 19
194 // SQL_FLOAT SQL_NO_DATA_FOUND
195 // SQL_REAL type name = 'SINGLE', Precision = 7
196 // SQL_DOUBLE type name = 'DOUBLE', Precision = 15
197 // SQL_INTEGER type name = 'LONG', Precision = 10
199 // VARCHAR = Variable length character string
200 if (! getDataTypeInfo(SQL_VARCHAR
, typeInfVarchar
))
201 if (! getDataTypeInfo(SQL_CHAR
, typeInfVarchar
))
204 typeInfVarchar
.FsqlType
= SQL_CHAR
;
206 typeInfVarchar
.FsqlType
= SQL_VARCHAR
;
209 if (! getDataTypeInfo(SQL_DOUBLE
, typeInfFloat
))
210 if (! getDataTypeInfo(SQL_REAL
, typeInfFloat
))
211 if (! getDataTypeInfo(SQL_FLOAT
, typeInfFloat
))
212 if (! getDataTypeInfo(SQL_DECIMAL
, typeInfFloat
))
213 if (! getDataTypeInfo(SQL_NUMERIC
, typeInfFloat
))
216 typeInfFloat
.FsqlType
= SQL_NUMERIC
;
218 typeInfFloat
.FsqlType
= SQL_DECIMAL
;
220 typeInfFloat
.FsqlType
= SQL_FLOAT
;
222 typeInfFloat
.FsqlType
= SQL_REAL
;
224 typeInfFloat
.FsqlType
= SQL_DOUBLE
;
227 if (! getDataTypeInfo(SQL_INTEGER
, typeInfInteger
))
228 // If SQL_INTEGER is not supported, use the floating point
229 // data type to store integers as well as floats
230 if (! getDataTypeInfo(typeInfFloat
.FsqlType
, typeInfInteger
))
233 typeInfInteger
.FsqlType
= typeInfFloat
.FsqlType
;
235 typeInfInteger
.FsqlType
= SQL_INTEGER
;
238 if (! getDataTypeInfo(SQL_TIMESTAMP
, typeInfDate
))
241 typeInfDate
.FsqlType
= SQL_TIMESTAMP
;
244 cout
<< "VARCHAR DATA TYPE: " << typeInfVarchar
.TypeName
<< endl
;
245 cout
<< "INTEGER DATA TYPE: " << typeInfInteger
.TypeName
<< endl
;
246 cout
<< "FLOAT DATA TYPE: " << typeInfFloat
.TypeName
<< endl
;
247 cout
<< "DATE DATA TYPE: " << typeInfDate
.TypeName
<< endl
;
251 // Completed Successfully
256 // The Intersolv/Oracle 7 driver was "Not Capable" of setting the login timeout.
258 /********** wxDB::setConnectionOptions() **********/
259 bool wxDB::setConnectionOptions(void)
261 SQLSetConnectOption(hdbc
, SQL_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
);
262 SQLSetConnectOption(hdbc
, SQL_OPT_TRACE
, SQL_OPT_TRACE_OFF
);
264 // Display the connection options to verify them
267 cout
<< ">>>>> CONNECTION OPTIONS <<<<<<" << endl
;
269 if (SQLGetConnectOption(hdbc
, SQL_AUTOCOMMIT
, &l
) != SQL_SUCCESS
)
270 return(DispAllErrors(henv
, hdbc
));
271 cout
<< "AUTOCOMMIT: " << (l
== SQL_AUTOCOMMIT_OFF
? "OFF" : "ON") << endl
;
273 if (SQLGetConnectOption(hdbc
, SQL_ODBC_CURSORS
, &l
) != SQL_SUCCESS
)
274 return(DispAllErrors(henv
, hdbc
));
275 cout
<< "ODBC CURSORS: ";
278 case(SQL_CUR_USE_IF_NEEDED
):
279 cout
<< "SQL_CUR_USE_IF_NEEDED";
281 case(SQL_CUR_USE_ODBC
):
282 cout
<< "SQL_CUR_USE_ODBC";
284 case(SQL_CUR_USE_DRIVER
):
285 cout
<< "SQL_CUR_USE_DRIVER";
290 if (SQLGetConnectOption(hdbc
, SQL_OPT_TRACE
, &l
) != SQL_SUCCESS
)
291 return(DispAllErrors(henv
, hdbc
));
292 cout
<< "TRACING: " << (l
== SQL_OPT_TRACE_OFF
? "OFF" : "ON") << endl
;
297 // Completed Successfully
300 } // wxDB::setConnectionOptions()
302 /********** wxDB::getDbInfo() **********/
303 bool wxDB::getDbInfo(void)
307 if (SQLGetInfo(hdbc
, SQL_SERVER_NAME
, dbInf
.serverName
, 40, &cb
) != SQL_SUCCESS
)
308 return(DispAllErrors(henv
, hdbc
));
310 if (SQLGetInfo(hdbc
, SQL_DATABASE_NAME
, dbInf
.databaseName
, 128, &cb
) != SQL_SUCCESS
)
311 return(DispAllErrors(henv
, hdbc
));
313 if (SQLGetInfo(hdbc
, SQL_DBMS_NAME
, dbInf
.dbmsName
, 40, &cb
) != SQL_SUCCESS
)
314 return(DispAllErrors(henv
, hdbc
));
316 if (SQLGetInfo(hdbc
, SQL_DBMS_VER
, dbInf
.dbmsVer
, 20, &cb
) != SQL_SUCCESS
)
317 return(DispAllErrors(henv
, hdbc
));
319 if (SQLGetInfo(hdbc
, SQL_ACTIVE_CONNECTIONS
, &dbInf
.maxConnections
, sizeof(dbInf
.maxConnections
), &cb
) != SQL_SUCCESS
)
320 return(DispAllErrors(henv
, hdbc
));
322 if (SQLGetInfo(hdbc
, SQL_ACTIVE_STATEMENTS
, &dbInf
.maxStmts
, sizeof(dbInf
.maxStmts
), &cb
) != SQL_SUCCESS
)
323 return(DispAllErrors(henv
, hdbc
));
325 if (SQLGetInfo(hdbc
, SQL_DRIVER_NAME
, dbInf
.driverName
, 40, &cb
) != SQL_SUCCESS
)
326 return(DispAllErrors(henv
, hdbc
));
328 if (SQLGetInfo(hdbc
, SQL_DRIVER_ODBC_VER
, dbInf
.odbcVer
, 20, &cb
) != SQL_SUCCESS
)
329 return(DispAllErrors(henv
, hdbc
));
331 if (SQLGetInfo(hdbc
, SQL_ODBC_VER
, dbInf
.drvMgrOdbcVer
, 20, &cb
) != SQL_SUCCESS
)
332 return(DispAllErrors(henv
, hdbc
));
334 if (SQLGetInfo(hdbc
, SQL_DRIVER_VER
, dbInf
.driverVer
, 40, &cb
) != SQL_SUCCESS
)
335 return(DispAllErrors(henv
, hdbc
));
337 if (SQLGetInfo(hdbc
, SQL_ODBC_API_CONFORMANCE
, &dbInf
.apiConfLvl
, sizeof(dbInf
.apiConfLvl
), &cb
) != SQL_SUCCESS
)
338 return(DispAllErrors(henv
, hdbc
));
340 if (SQLGetInfo(hdbc
, SQL_ODBC_SAG_CLI_CONFORMANCE
, &dbInf
.cliConfLvl
, sizeof(dbInf
.cliConfLvl
), &cb
) != SQL_SUCCESS
)
341 return(DispAllErrors(henv
, hdbc
));
343 if (SQLGetInfo(hdbc
, SQL_ODBC_SQL_CONFORMANCE
, &dbInf
.sqlConfLvl
, sizeof(dbInf
.sqlConfLvl
), &cb
) != SQL_SUCCESS
)
344 return(DispAllErrors(henv
, hdbc
));
346 if (SQLGetInfo(hdbc
, SQL_OUTER_JOINS
, dbInf
.outerJoins
, 2, &cb
) != SQL_SUCCESS
)
347 return(DispAllErrors(henv
, hdbc
));
349 if (SQLGetInfo(hdbc
, SQL_PROCEDURES
, dbInf
.procedureSupport
, 2, &cb
) != SQL_SUCCESS
)
350 return(DispAllErrors(henv
, hdbc
));
352 if (SQLGetInfo(hdbc
, SQL_CURSOR_COMMIT_BEHAVIOR
, &dbInf
.cursorCommitBehavior
, sizeof(dbInf
.cursorCommitBehavior
), &cb
) != SQL_SUCCESS
)
353 return(DispAllErrors(henv
, hdbc
));
355 if (SQLGetInfo(hdbc
, SQL_CURSOR_ROLLBACK_BEHAVIOR
, &dbInf
.cursorRollbackBehavior
, sizeof(dbInf
.cursorRollbackBehavior
), &cb
) != SQL_SUCCESS
)
356 return(DispAllErrors(henv
, hdbc
));
358 if (SQLGetInfo(hdbc
, SQL_NON_NULLABLE_COLUMNS
, &dbInf
.supportNotNullClause
, sizeof(dbInf
.supportNotNullClause
), &cb
) != SQL_SUCCESS
)
359 return(DispAllErrors(henv
, hdbc
));
361 if (SQLGetInfo(hdbc
, SQL_ODBC_SQL_OPT_IEF
, dbInf
.supportIEF
, 2, &cb
) != SQL_SUCCESS
)
362 return(DispAllErrors(henv
, hdbc
));
364 if (SQLGetInfo(hdbc
, SQL_DEFAULT_TXN_ISOLATION
, &dbInf
.txnIsolation
, sizeof(dbInf
.txnIsolation
), &cb
) != SQL_SUCCESS
)
365 return(DispAllErrors(henv
, hdbc
));
367 if (SQLGetInfo(hdbc
, SQL_TXN_ISOLATION_OPTION
, &dbInf
.txnIsolationOptions
, sizeof(dbInf
.txnIsolationOptions
), &cb
) != SQL_SUCCESS
)
368 return(DispAllErrors(henv
, hdbc
));
370 if (SQLGetInfo(hdbc
, SQL_FETCH_DIRECTION
, &dbInf
.fetchDirections
, sizeof(dbInf
.fetchDirections
), &cb
) != SQL_SUCCESS
)
371 return(DispAllErrors(henv
, hdbc
));
373 if (SQLGetInfo(hdbc
, SQL_LOCK_TYPES
, &dbInf
.lockTypes
, sizeof(dbInf
.lockTypes
), &cb
) != SQL_SUCCESS
)
374 return(DispAllErrors(henv
, hdbc
));
376 if (SQLGetInfo(hdbc
, SQL_POS_OPERATIONS
, &dbInf
.posOperations
, sizeof(dbInf
.posOperations
), &cb
) != SQL_SUCCESS
)
377 return(DispAllErrors(henv
, hdbc
));
379 if (SQLGetInfo(hdbc
, SQL_POSITIONED_STATEMENTS
, &dbInf
.posStmts
, sizeof(dbInf
.posStmts
), &cb
) != SQL_SUCCESS
)
380 return(DispAllErrors(henv
, hdbc
));
382 if (SQLGetInfo(hdbc
, SQL_SCROLL_CONCURRENCY
, &dbInf
.scrollConcurrency
, sizeof(dbInf
.scrollConcurrency
), &cb
) != SQL_SUCCESS
)
383 return(DispAllErrors(henv
, hdbc
));
385 if (SQLGetInfo(hdbc
, SQL_SCROLL_OPTIONS
, &dbInf
.scrollOptions
, sizeof(dbInf
.scrollOptions
), &cb
) != SQL_SUCCESS
)
386 return(DispAllErrors(henv
, hdbc
));
388 if (SQLGetInfo(hdbc
, SQL_STATIC_SENSITIVITY
, &dbInf
.staticSensitivity
, sizeof(dbInf
.staticSensitivity
), &cb
) != SQL_SUCCESS
)
389 return(DispAllErrors(henv
, hdbc
));
391 if (SQLGetInfo(hdbc
, SQL_TXN_CAPABLE
, &dbInf
.txnCapable
, sizeof(dbInf
.txnCapable
), &cb
) != SQL_SUCCESS
)
392 return(DispAllErrors(henv
, hdbc
));
394 if (SQLGetInfo(hdbc
, SQL_LOGIN_TIMEOUT
, &dbInf
.loginTimeout
, sizeof(dbInf
.loginTimeout
), &cb
) != SQL_SUCCESS
)
395 return(DispAllErrors(henv
, hdbc
));
398 cout
<< ">>>>> DATA SOURCE INFORMATION <<<<<" << endl
;
399 cout
<< "SERVER Name: " << dbInf
.serverName
<< endl
;
400 cout
<< "DBMS Name: " << dbInf
.dbmsName
<< "; DBMS Version: " << dbInf
.dbmsVer
<< endl
;
401 cout
<< "ODBC Version: " << dbInf
.odbcVer
<< "; Driver Version: " << dbInf
.driverVer
<< endl
;
403 cout
<< "API Conf. Level: ";
404 switch(dbInf
.apiConfLvl
)
406 case SQL_OAC_NONE
: cout
<< "None"; break;
407 case SQL_OAC_LEVEL1
: cout
<< "Level 1"; break;
408 case SQL_OAC_LEVEL2
: cout
<< "Level 2"; break;
412 cout
<< "SAG CLI Conf. Level: ";
413 switch(dbInf
.cliConfLvl
)
415 case SQL_OSCC_NOT_COMPLIANT
: cout
<< "Not Compliant"; break;
416 case SQL_OSCC_COMPLIANT
: cout
<< "Compliant"; break;
420 cout
<< "SQL Conf. Level: ";
421 switch(dbInf
.sqlConfLvl
)
423 case SQL_OSC_MINIMUM
: cout
<< "Minimum Grammer"; break;
424 case SQL_OSC_CORE
: cout
<< "Core Grammer"; break;
425 case SQL_OSC_EXTENDED
: cout
<< "Extended Grammer"; break;
429 cout
<< "Max. Connections: " << dbInf
.maxConnections
<< endl
;
430 cout
<< "Outer Joins: " << dbInf
.outerJoins
<< endl
;
431 cout
<< "Support for Procedures: " << dbInf
.procedureSupport
<< endl
;
433 cout
<< "Cursor COMMIT Behavior: ";
434 switch(dbInf
.cursorCommitBehavior
)
436 case SQL_CB_DELETE
: cout
<< "Delete cursors"; break;
437 case SQL_CB_CLOSE
: cout
<< "Close cursors"; break;
438 case SQL_CB_PRESERVE
: cout
<< "Preserve cursors"; break;
442 cout
<< "Cursor ROLLBACK Behavior: ";
443 switch(dbInf
.cursorRollbackBehavior
)
445 case SQL_CB_DELETE
: cout
<< "Delete cursors"; break;
446 case SQL_CB_CLOSE
: cout
<< "Close cursors"; break;
447 case SQL_CB_PRESERVE
: cout
<< "Preserve cursors"; break;
451 cout
<< "Support NOT NULL clause: ";
452 switch(dbInf
.supportNotNullClause
)
454 case SQL_NNC_NULL
: cout
<< "No"; break;
455 case SQL_NNC_NON_NULL
: cout
<< "Yes"; break;
459 cout
<< "Support IEF (Ref. Integrity): " << dbInf
.supportIEF
<< endl
;
460 cout
<< "Login Timeout: " << dbInf
.loginTimeout
<< endl
;
462 cout
<< endl
<< endl
<< "more ..." << endl
;
465 cout
<< "Default Transaction Isolation: ";
466 switch(dbInf
.txnIsolation
)
468 case SQL_TXN_READ_UNCOMMITTED
: cout
<< "Read Uncommitted"; break;
469 case SQL_TXN_READ_COMMITTED
: cout
<< "Read Committed"; break;
470 case SQL_TXN_REPEATABLE_READ
: cout
<< "Repeatable Read"; break;
471 case SQL_TXN_SERIALIZABLE
: cout
<< "Serializable"; break;
473 case SQL_TXN_VERSIONING
: cout
<< "Versioning"; break;
478 cout
<< "Transaction Isolation Options: ";
479 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_UNCOMMITTED
)
480 cout
<< "Read Uncommitted, ";
481 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_COMMITTED
)
482 cout
<< "Read Committed, ";
483 if (dbInf
.txnIsolationOptions
& SQL_TXN_REPEATABLE_READ
)
484 cout
<< "Repeatable Read, ";
485 if (dbInf
.txnIsolationOptions
& SQL_TXN_SERIALIZABLE
)
486 cout
<< "Serializable, ";
488 if (dbInf
.txnIsolationOptions
& SQL_TXN_VERSIONING
)
489 cout
<< "Versioning";
493 cout
<< "Fetch Directions Supported:" << endl
<< " ";
494 if (dbInf
.fetchDirections
& SQL_FD_FETCH_NEXT
)
496 if (dbInf
.fetchDirections
& SQL_FD_FETCH_PRIOR
)
498 if (dbInf
.fetchDirections
& SQL_FD_FETCH_FIRST
)
500 if (dbInf
.fetchDirections
& SQL_FD_FETCH_LAST
)
502 if (dbInf
.fetchDirections
& SQL_FD_FETCH_ABSOLUTE
)
503 cout
<< "Absolute, ";
504 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RELATIVE
)
505 cout
<< "Relative, ";
507 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RESUME
)
510 if (dbInf
.fetchDirections
& SQL_FD_FETCH_BOOKMARK
)
514 cout
<< "Lock Types Supported (SQLSetPos): ";
515 if (dbInf
.lockTypes
& SQL_LCK_NO_CHANGE
)
516 cout
<< "No Change, ";
517 if (dbInf
.lockTypes
& SQL_LCK_EXCLUSIVE
)
518 cout
<< "Exclusive, ";
519 if (dbInf
.lockTypes
& SQL_LCK_UNLOCK
)
523 cout
<< "Position Operations Supported (SQLSetPos): ";
524 if (dbInf
.posOperations
& SQL_POS_POSITION
)
525 cout
<< "Position, ";
526 if (dbInf
.posOperations
& SQL_POS_REFRESH
)
528 if (dbInf
.posOperations
& SQL_POS_UPDATE
)
530 if (dbInf
.posOperations
& SQL_POS_DELETE
)
532 if (dbInf
.posOperations
& SQL_POS_ADD
)
536 cout
<< "Positioned Statements Supported: ";
537 if (dbInf
.posStmts
& SQL_PS_POSITIONED_DELETE
)
538 cout
<< "Pos delete, ";
539 if (dbInf
.posStmts
& SQL_PS_POSITIONED_UPDATE
)
540 cout
<< "Pos update, ";
541 if (dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
542 cout
<< "Select for update";
545 cout
<< "Scroll Concurrency: ";
546 if (dbInf
.scrollConcurrency
& SQL_SCCO_READ_ONLY
)
547 cout
<< "Read Only, ";
548 if (dbInf
.scrollConcurrency
& SQL_SCCO_LOCK
)
550 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_ROWVER
)
551 cout
<< "Opt. Rowver, ";
552 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_VALUES
)
553 cout
<< "Opt. Values";
556 cout
<< "Scroll Options: ";
557 if (dbInf
.scrollOptions
& SQL_SO_FORWARD_ONLY
)
558 cout
<< "Fwd Only, ";
559 if (dbInf
.scrollOptions
& SQL_SO_STATIC
)
561 if (dbInf
.scrollOptions
& SQL_SO_KEYSET_DRIVEN
)
562 cout
<< "Keyset Driven, ";
563 if (dbInf
.scrollOptions
& SQL_SO_DYNAMIC
)
565 if (dbInf
.scrollOptions
& SQL_SO_MIXED
)
569 cout
<< "Static Sensitivity: ";
570 if (dbInf
.staticSensitivity
& SQL_SS_ADDITIONS
)
571 cout
<< "Additions, ";
572 if (dbInf
.staticSensitivity
& SQL_SS_DELETIONS
)
573 cout
<< "Deletions, ";
574 if (dbInf
.staticSensitivity
& SQL_SS_UPDATES
)
578 cout
<< "Transaction Capable?: ";
579 switch(dbInf
.txnCapable
)
581 case SQL_TC_NONE
: cout
<< "No"; break;
582 case SQL_TC_DML
: cout
<< "DML Only"; break;
583 case SQL_TC_DDL_COMMIT
: cout
<< "DDL Commit"; break;
584 case SQL_TC_DDL_IGNORE
: cout
<< "DDL Ignore"; break;
585 case SQL_TC_ALL
: cout
<< "DDL & DML"; break;
593 // Completed Successfully
596 } // wxDB::getDbInfo()
598 /********** wxDB::getDataTypeInfo() **********/
599 bool wxDB::getDataTypeInfo(SWORD fSqlType
, SqlTypeInfo
&structSQLTypeInfo
)
601 // fSqlType will be something like SQL_VARCHAR. This parameter determines
602 // the data type inf. is gathered for.
604 // SqlTypeInfo is a structure that is filled in with data type information,
609 // Get information about the data type specified
610 if (SQLGetTypeInfo(hstmt
, fSqlType
) != SQL_SUCCESS
)
611 return(DispAllErrors(henv
, hdbc
, hstmt
));
613 if ((retcode
= SQLFetch(hstmt
)) != SQL_SUCCESS
)
616 if (retcode
== SQL_NO_DATA_FOUND
)
617 cout
<< "SQL_NO_DATA_FOUND fetching inf. about data type." << endl
;
619 DispAllErrors(henv
, hdbc
, hstmt
);
620 SQLFreeStmt(hstmt
, SQL_CLOSE
);
623 // Obtain columns from the record
624 if (SQLGetData(hstmt
, 1, SQL_C_CHAR
, structSQLTypeInfo
.TypeName
, DB_TYPE_NAME_LEN
, &cbRet
) != SQL_SUCCESS
)
625 return(DispAllErrors(henv
, hdbc
, hstmt
));
626 if (SQLGetData(hstmt
, 3, SQL_C_LONG
, &structSQLTypeInfo
.Precision
, 0, &cbRet
) != SQL_SUCCESS
)
627 return(DispAllErrors(henv
, hdbc
, hstmt
));
628 if (SQLGetData(hstmt
, 8, SQL_C_SHORT
, &structSQLTypeInfo
.CaseSensitive
, 0, &cbRet
) != SQL_SUCCESS
)
629 return(DispAllErrors(henv
, hdbc
, hstmt
));
630 // if (SQLGetData(hstmt, 14, SQL_C_SHORT, &structSQLTypeInfo.MinimumScale, 0, &cbRet) != SQL_SUCCESS)
631 // return(DispAllErrors(henv, hdbc, hstmt));
632 if (SQLGetData(hstmt
, 15, SQL_C_SHORT
, &structSQLTypeInfo
.MaximumScale
, 0, &cbRet
) != SQL_SUCCESS
)
633 return(DispAllErrors(henv
, hdbc
, hstmt
));
635 if (structSQLTypeInfo
.MaximumScale
< 0)
636 structSQLTypeInfo
.MaximumScale
= 0;
638 // Close the statement handle which closes open cursors
639 if (SQLFreeStmt(hstmt
, SQL_CLOSE
) != SQL_SUCCESS
)
640 return(DispAllErrors(henv
, hdbc
, hstmt
));
642 // Completed Successfully
645 } // wxDB::getDataTypeInfo()
647 /********** wxDB::Close() **********/
648 void wxDB::Close(void)
650 // Free statement handle
653 if (SQLFreeStmt(hstmt
, SQL_DROP
) != SQL_SUCCESS
)
654 DispAllErrors(henv
, hdbc
);
657 // Disconnect from the datasource
658 if (SQLDisconnect(hdbc
) != SQL_SUCCESS
)
659 DispAllErrors(henv
, hdbc
);
661 // Free the connection to the datasource
662 if (SQLFreeConnect(hdbc
) != SQL_SUCCESS
)
663 DispAllErrors(henv
, hdbc
);
667 /********** wxDB::CommitTrans() **********/
668 bool wxDB::CommitTrans(void)
670 // Commit the transaction
671 if (SQLTransact(henv
, hdbc
, SQL_COMMIT
) != SQL_SUCCESS
)
672 return(DispAllErrors(henv
, hdbc
));
674 // Completed successfully
677 } // wxDB::CommitTrans()
679 /********** wxDB::RollbackTrans() **********/
680 bool wxDB::RollbackTrans(void)
682 // Rollback the transaction
683 if (SQLTransact(henv
, hdbc
, SQL_ROLLBACK
) != SQL_SUCCESS
)
684 return(DispAllErrors(henv
, hdbc
));
686 // Completed successfully
689 } // wxDB::RollbackTrans()
691 /********** wxDB::DispAllErrors() **********/
692 bool wxDB::DispAllErrors(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
694 char odbcErrMsg
[DB_MAX_ERROR_MSG_LEN
];
696 while (SQLError(aHenv
, aHdbc
, aHstmt
, (UCHAR FAR
*) sqlState
, &nativeError
, (UCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
698 sprintf(odbcErrMsg
, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState
, nativeError
, errorMsg
);
699 logError(odbcErrMsg
, sqlState
);
703 // When run in console mode, use standard out to display errors.
704 cout
<< odbcErrMsg
<< endl
;
705 cout
<< "Press any key to continue..." << endl
;
711 return(FALSE
); // This function alway's returns false.
713 } // wxDB::DispAllErrors()
715 /********** wxDB::GetNextError() **********/
716 bool wxDB::GetNextError(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
718 if (SQLError(aHenv
, aHdbc
, aHstmt
, (UCHAR FAR
*) sqlState
, &nativeError
, (UCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
723 } // wxDB::GetNextError()
725 /********** wxDB::DispNextError() **********/
726 void wxDB::DispNextError(void)
728 char odbcErrMsg
[DB_MAX_ERROR_MSG_LEN
];
730 sprintf(odbcErrMsg
, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState
, nativeError
, errorMsg
);
731 logError(odbcErrMsg
, sqlState
);
737 // When run in console mode, use standard out to display errors.
738 cout
<< odbcErrMsg
<< endl
;
739 cout
<< "Press any key to continue..." << endl
;
743 } // wxDB::DispNextError()
745 /********** wxDB::logError() **********/
746 void wxDB::logError(char *errMsg
, char *SQLState
)
748 assert(errMsg
&& strlen(errMsg
));
750 static int pLast
= -1;
753 if (++pLast
== DB_MAX_ERROR_HISTORY
)
755 for (int i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
756 strcpy(errorList
[i
], errorList
[i
+1]);
760 strcpy(errorList
[pLast
], errMsg
);
762 if (SQLState
&& strlen(SQLState
))
763 if ((dbStatus
= TranslateSqlState(SQLState
)) != DB_ERR_FUNCTION_SEQUENCE_ERROR
)
764 DB_STATUS
= dbStatus
;
766 } // wxDB::logError()
768 /**********wxDB::TranslateSqlState() **********/
769 int wxDB::TranslateSqlState(char *SQLState
)
771 if (!strcmp(SQLState
, "01000"))
772 return(DB_ERR_GENERAL_WARNING
);
773 if (!strcmp(SQLState
, "01002"))
774 return(DB_ERR_DISCONNECT_ERROR
);
775 if (!strcmp(SQLState
, "01004"))
776 return(DB_ERR_DATA_TRUNCATED
);
777 if (!strcmp(SQLState
, "01006"))
778 return(DB_ERR_PRIV_NOT_REVOKED
);
779 if (!strcmp(SQLState
, "01S00"))
780 return(DB_ERR_INVALID_CONN_STR_ATTR
);
781 if (!strcmp(SQLState
, "01S01"))
782 return(DB_ERR_ERROR_IN_ROW
);
783 if (!strcmp(SQLState
, "01S02"))
784 return(DB_ERR_OPTION_VALUE_CHANGED
);
785 if (!strcmp(SQLState
, "01S03"))
786 return(DB_ERR_NO_ROWS_UPD_OR_DEL
);
787 if (!strcmp(SQLState
, "01S04"))
788 return(DB_ERR_MULTI_ROWS_UPD_OR_DEL
);
789 if (!strcmp(SQLState
, "07001"))
790 return(DB_ERR_WRONG_NO_OF_PARAMS
);
791 if (!strcmp(SQLState
, "07006"))
792 return(DB_ERR_DATA_TYPE_ATTR_VIOL
);
793 if (!strcmp(SQLState
, "08001"))
794 return(DB_ERR_UNABLE_TO_CONNECT
);
795 if (!strcmp(SQLState
, "08002"))
796 return(DB_ERR_CONNECTION_IN_USE
);
797 if (!strcmp(SQLState
, "08003"))
798 return(DB_ERR_CONNECTION_NOT_OPEN
);
799 if (!strcmp(SQLState
, "08004"))
800 return(DB_ERR_REJECTED_CONNECTION
);
801 if (!strcmp(SQLState
, "08007"))
802 return(DB_ERR_CONN_FAIL_IN_TRANS
);
803 if (!strcmp(SQLState
, "08S01"))
804 return(DB_ERR_COMM_LINK_FAILURE
);
805 if (!strcmp(SQLState
, "21S01"))
806 return(DB_ERR_INSERT_VALUE_LIST_MISMATCH
);
807 if (!strcmp(SQLState
, "21S02"))
808 return(DB_ERR_DERIVED_TABLE_MISMATCH
);
809 if (!strcmp(SQLState
, "22001"))
810 return(DB_ERR_STRING_RIGHT_TRUNC
);
811 if (!strcmp(SQLState
, "22003"))
812 return(DB_ERR_NUMERIC_VALUE_OUT_OF_RNG
);
813 if (!strcmp(SQLState
, "22005"))
814 return(DB_ERR_ERROR_IN_ASSIGNMENT
);
815 if (!strcmp(SQLState
, "22008"))
816 return(DB_ERR_DATETIME_FLD_OVERFLOW
);
817 if (!strcmp(SQLState
, "22012"))
818 return(DB_ERR_DIVIDE_BY_ZERO
);
819 if (!strcmp(SQLState
, "22026"))
820 return(DB_ERR_STR_DATA_LENGTH_MISMATCH
);
821 if (!strcmp(SQLState
, "23000"))
822 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
823 if (!strcmp(SQLState
, "24000"))
824 return(DB_ERR_INVALID_CURSOR_STATE
);
825 if (!strcmp(SQLState
, "25000"))
826 return(DB_ERR_INVALID_TRANS_STATE
);
827 if (!strcmp(SQLState
, "28000"))
828 return(DB_ERR_INVALID_AUTH_SPEC
);
829 if (!strcmp(SQLState
, "34000"))
830 return(DB_ERR_INVALID_CURSOR_NAME
);
831 if (!strcmp(SQLState
, "37000"))
832 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL
);
833 if (!strcmp(SQLState
, "3C000"))
834 return(DB_ERR_DUPLICATE_CURSOR_NAME
);
835 if (!strcmp(SQLState
, "40001"))
836 return(DB_ERR_SERIALIZATION_FAILURE
);
837 if (!strcmp(SQLState
, "42000"))
838 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2
);
839 if (!strcmp(SQLState
, "70100"))
840 return(DB_ERR_OPERATION_ABORTED
);
841 if (!strcmp(SQLState
, "IM001"))
842 return(DB_ERR_UNSUPPORTED_FUNCTION
);
843 if (!strcmp(SQLState
, "IM002"))
844 return(DB_ERR_NO_DATA_SOURCE
);
845 if (!strcmp(SQLState
, "IM003"))
846 return(DB_ERR_DRIVER_LOAD_ERROR
);
847 if (!strcmp(SQLState
, "IM004"))
848 return(DB_ERR_SQLALLOCENV_FAILED
);
849 if (!strcmp(SQLState
, "IM005"))
850 return(DB_ERR_SQLALLOCCONNECT_FAILED
);
851 if (!strcmp(SQLState
, "IM006"))
852 return(DB_ERR_SQLSETCONNECTOPTION_FAILED
);
853 if (!strcmp(SQLState
, "IM007"))
854 return(DB_ERR_NO_DATA_SOURCE_DLG_PROHIB
);
855 if (!strcmp(SQLState
, "IM008"))
856 return(DB_ERR_DIALOG_FAILED
);
857 if (!strcmp(SQLState
, "IM009"))
858 return(DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL
);
859 if (!strcmp(SQLState
, "IM010"))
860 return(DB_ERR_DATA_SOURCE_NAME_TOO_LONG
);
861 if (!strcmp(SQLState
, "IM011"))
862 return(DB_ERR_DRIVER_NAME_TOO_LONG
);
863 if (!strcmp(SQLState
, "IM012"))
864 return(DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR
);
865 if (!strcmp(SQLState
, "IM013"))
866 return(DB_ERR_TRACE_FILE_ERROR
);
867 if (!strcmp(SQLState
, "S0001"))
868 return(DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS
);
869 if (!strcmp(SQLState
, "S0002"))
870 return(DB_ERR_TABLE_NOT_FOUND
);
871 if (!strcmp(SQLState
, "S0011"))
872 return(DB_ERR_INDEX_ALREADY_EXISTS
);
873 if (!strcmp(SQLState
, "S0012"))
874 return(DB_ERR_INDEX_NOT_FOUND
);
875 if (!strcmp(SQLState
, "S0021"))
876 return(DB_ERR_COLUMN_ALREADY_EXISTS
);
877 if (!strcmp(SQLState
, "S0022"))
878 return(DB_ERR_COLUMN_NOT_FOUND
);
879 if (!strcmp(SQLState
, "S0023"))
880 return(DB_ERR_NO_DEFAULT_FOR_COLUMN
);
881 if (!strcmp(SQLState
, "S1000"))
882 return(DB_ERR_GENERAL_ERROR
);
883 if (!strcmp(SQLState
, "S1001"))
884 return(DB_ERR_MEMORY_ALLOCATION_FAILURE
);
885 if (!strcmp(SQLState
, "S1002"))
886 return(DB_ERR_INVALID_COLUMN_NUMBER
);
887 if (!strcmp(SQLState
, "S1003"))
888 return(DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE
);
889 if (!strcmp(SQLState
, "S1004"))
890 return(DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE
);
891 if (!strcmp(SQLState
, "S1008"))
892 return(DB_ERR_OPERATION_CANCELLED
);
893 if (!strcmp(SQLState
, "S1009"))
894 return(DB_ERR_INVALID_ARGUMENT_VALUE
);
895 if (!strcmp(SQLState
, "S1010"))
896 return(DB_ERR_FUNCTION_SEQUENCE_ERROR
);
897 if (!strcmp(SQLState
, "S1011"))
898 return(DB_ERR_OPERATION_INVALID_AT_THIS_TIME
);
899 if (!strcmp(SQLState
, "S1012"))
900 return(DB_ERR_INVALID_TRANS_OPERATION_CODE
);
901 if (!strcmp(SQLState
, "S1015"))
902 return(DB_ERR_NO_CURSOR_NAME_AVAIL
);
903 if (!strcmp(SQLState
, "S1090"))
904 return(DB_ERR_INVALID_STR_OR_BUF_LEN
);
905 if (!strcmp(SQLState
, "S1091"))
906 return(DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE
);
907 if (!strcmp(SQLState
, "S1092"))
908 return(DB_ERR_OPTION_TYPE_OUT_OF_RANGE
);
909 if (!strcmp(SQLState
, "S1093"))
910 return(DB_ERR_INVALID_PARAM_NO
);
911 if (!strcmp(SQLState
, "S1094"))
912 return(DB_ERR_INVALID_SCALE_VALUE
);
913 if (!strcmp(SQLState
, "S1095"))
914 return(DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE
);
915 if (!strcmp(SQLState
, "S1096"))
916 return(DB_ERR_INF_TYPE_OUT_OF_RANGE
);
917 if (!strcmp(SQLState
, "S1097"))
918 return(DB_ERR_COLUMN_TYPE_OUT_OF_RANGE
);
919 if (!strcmp(SQLState
, "S1098"))
920 return(DB_ERR_SCOPE_TYPE_OUT_OF_RANGE
);
921 if (!strcmp(SQLState
, "S1099"))
922 return(DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE
);
923 if (!strcmp(SQLState
, "S1100"))
924 return(DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE
);
925 if (!strcmp(SQLState
, "S1101"))
926 return(DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE
);
927 if (!strcmp(SQLState
, "S1103"))
928 return(DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE
);
929 if (!strcmp(SQLState
, "S1104"))
930 return(DB_ERR_INVALID_PRECISION_VALUE
);
931 if (!strcmp(SQLState
, "S1105"))
932 return(DB_ERR_INVALID_PARAM_TYPE
);
933 if (!strcmp(SQLState
, "S1106"))
934 return(DB_ERR_FETCH_TYPE_OUT_OF_RANGE
);
935 if (!strcmp(SQLState
, "S1107"))
936 return(DB_ERR_ROW_VALUE_OUT_OF_RANGE
);
937 if (!strcmp(SQLState
, "S1108"))
938 return(DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE
);
939 if (!strcmp(SQLState
, "S1109"))
940 return(DB_ERR_INVALID_CURSOR_POSITION
);
941 if (!strcmp(SQLState
, "S1110"))
942 return(DB_ERR_INVALID_DRIVER_COMPLETION
);
943 if (!strcmp(SQLState
, "S1111"))
944 return(DB_ERR_INVALID_BOOKMARK_VALUE
);
945 if (!strcmp(SQLState
, "S1C00"))
946 return(DB_ERR_DRIVER_NOT_CAPABLE
);
947 if (!strcmp(SQLState
, "S1T00"))
948 return(DB_ERR_TIMEOUT_EXPIRED
);
953 } // wxDB::TranslateSqlState()
955 /********** wxDB::Grant() **********/
956 bool wxDB::Grant(int privileges
, char *tableName
, char *userList
)
958 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
960 // Build the grant statement
961 strcpy(sqlStmt
, "GRANT ");
962 if (privileges
== DB_GRANT_ALL
)
963 strcat(sqlStmt
, "ALL");
967 if (privileges
& DB_GRANT_SELECT
)
969 strcat(sqlStmt
, "SELECT");
972 if (privileges
& DB_GRANT_INSERT
)
975 strcat(sqlStmt
, ", ");
976 strcat(sqlStmt
, "INSERT");
978 if (privileges
& DB_GRANT_UPDATE
)
981 strcat(sqlStmt
, ", ");
982 strcat(sqlStmt
, "UPDATE");
984 if (privileges
& DB_GRANT_DELETE
)
987 strcat(sqlStmt
, ", ");
988 strcat(sqlStmt
, "DELETE");
992 strcat(sqlStmt
, " ON ");
993 strcat(sqlStmt
, tableName
);
994 strcat(sqlStmt
, " TO ");
995 strcat(sqlStmt
, userList
);
998 cout
<< endl
<< sqlStmt
<< endl
;
1001 return(ExecSql(sqlStmt
));
1005 /********** wxDB::CreateView() **********/
1006 bool wxDB::CreateView(char *viewName
, char *colList
, char *pSqlStmt
)
1008 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1010 // Drop the view first
1011 sprintf(sqlStmt
, "DROP VIEW %s", viewName
);
1012 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1014 // Check for sqlState = S0002, "Table or view not found".
1015 // Ignore this error, bomb out on any other error.
1016 // SQL Sybase Anwhere v5.5 returns an access violation error here
1017 // (sqlstate = 42000) rather than an S0002.
1018 GetNextError(henv
, hdbc
, hstmt
);
1019 if (strcmp(sqlState
, "S0002") && strcmp(sqlState
, "42000"))
1022 DispAllErrors(henv
, hdbc
, hstmt
);
1029 cout
<< endl
<< sqlStmt
<< endl
;
1032 // Build the create view statement
1033 strcpy(sqlStmt
, "CREATE VIEW ");
1034 strcat(sqlStmt
, viewName
);
1036 if (strlen(colList
))
1038 strcat(sqlStmt
, " (");
1039 strcat(sqlStmt
, colList
);
1040 strcat(sqlStmt
, ")");
1043 strcat(sqlStmt
, " AS ");
1044 strcat(sqlStmt
, pSqlStmt
);
1047 cout
<< sqlStmt
<< endl
;
1050 return(ExecSql(sqlStmt
));
1052 } // wxDB::CreateView()
1054 /********** wxDB::ExecSql() **********/
1055 bool wxDB::ExecSql(char *pSqlStmt
)
1057 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) == SQL_SUCCESS
)
1061 DispAllErrors(henv
, hdbc
, hstmt
);
1065 } // wxDB::ExecSql()
1067 /********** wxDB::GetColumns() **********/
1069 * 1) The last array element of the tableName[] argument must be zero (null).
1070 * This is how the end of the array is detected.
1071 * 2) This function returns an array of CcolInf structures. If no columns
1072 * were found, or an error occured, this pointer will be zero (null). THE
1073 * CALLING FUNCTION IS RESPONSIBLE FOR DELETING THE MEMORY RETURNED WHEN IT
1074 * IS FINISHED WITH IT. i.e.
1076 * CcolInf *colInf = pDb->GetColumns(tableList);
1079 * // Use the column inf
1081 * // Destroy the memory
1085 CcolInf
*wxDB::GetColumns(char *tableName
[])
1089 CcolInf
*colInf
= 0;
1092 char tblName
[DB_MAX_TABLE_NAME_LEN
+1];
1093 char colName
[DB_MAX_COLUMN_NAME_LEN
+1];
1096 // Pass 1 - Determine how many columns there are.
1097 // Pass 2 - Allocate the CcolInf array and fill in
1098 // the array with the column information.
1099 for (int pass
= 1; pass
<= 2; pass
++)
1103 if (noCols
== 0) // Probably a bogus table name(s)
1105 // Allocate n CcolInf objects to hold the column information
1106 colInf
= new CcolInf
[noCols
+1];
1109 // Mark the end of the array
1110 strcpy(colInf
[noCols
].tableName
, "");
1111 strcpy(colInf
[noCols
].colName
, "");
1112 colInf
[noCols
].sqlDataType
= 0;
1114 // Loop through each table name
1115 for (int tbl
= 0; tableName
[tbl
]; tbl
++)
1117 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1118 retcode
= SQLColumns(hstmt
,
1119 NULL
, 0, // All qualifiers
1120 NULL
, 0, // All owners
1121 (UCHAR
*) tableName
[tbl
], SQL_NTS
,
1122 NULL
, 0); // All columns
1123 if (retcode
!= SQL_SUCCESS
)
1124 { // Error occured, abort
1125 DispAllErrors(henv
, hdbc
, hstmt
);
1130 SQLBindCol(hstmt
, 3, SQL_C_CHAR
, tblName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
1131 SQLBindCol(hstmt
, 4, SQL_C_CHAR
, colName
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1132 SQLBindCol(hstmt
, 5, SQL_C_SSHORT
, &sqlDataType
, 0, &cb
);
1133 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
1135 if (pass
== 1) // First pass, just add up the number of columns
1137 else // Pass 2; Fill in the array of structures
1139 if (colNo
< noCols
) // Some extra error checking to prevent memory overwrites
1141 strcpy(colInf
[colNo
].tableName
, tblName
);
1142 strcpy(colInf
[colNo
].colName
, colName
);
1143 colInf
[colNo
].sqlDataType
= sqlDataType
;
1148 if (retcode
!= SQL_NO_DATA_FOUND
)
1149 { // Error occured, abort
1150 DispAllErrors(henv
, hdbc
, hstmt
);
1158 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1161 } // wxDB::GetColumns()
1164 // Table name can refer to a table, view, alias or synonym. Returns true
1165 // if the object exists in the database. This function does not indicate
1166 // whether or not the user has privleges to query or perform other functions
1168 bool wxDB::TableExists(char *tableName
)
1170 assert(tableName
&& strlen(tableName
));
1172 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1173 RETCODE retcode
= SQLTables(hstmt
,
1174 NULL
, 0, // All qualifiers
1175 NULL
, 0, // All owners
1176 (UCHAR FAR
*)tableName
, SQL_NTS
,
1177 NULL
, 0); // All table types
1178 if (retcode
!= SQL_SUCCESS
)
1179 return(DispAllErrors(henv
, hdbc
, hstmt
));
1181 if (SQLFetch(hstmt
) != SQL_SUCCESS
)
1183 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1184 return(DispAllErrors(henv
, hdbc
, hstmt
));
1187 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1190 } // wxDB::TableExists()
1193 /********** GetDbConnection() **********/
1194 wxDB
*GetDbConnection(DbStuff
*pDbStuff
)
1198 // Scan the linked list searching for an available database connection
1199 // that's already been opened but is currently not in use.
1200 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1202 // The database connection must be for the same datasource
1203 // name and must currently not be in use.
1204 if (pList
->Free
&& (! strcmp(pDbStuff
->Dsn
, pList
->Dsn
))) // Found a free connection
1206 pList
->Free
= FALSE
;
1207 return(pList
->PtrDb
);
1211 // No available connections. A new connection must be made and
1212 // appended to the end of the linked list.
1215 // Find the end of the list
1216 for (pList
= PtrBegDbList
; pList
->PtrNext
; pList
= pList
->PtrNext
);
1217 // Append a new list item
1218 pList
->PtrNext
= new DbList
;
1219 pList
->PtrNext
->PtrPrev
= pList
;
1220 pList
= pList
->PtrNext
;
1224 // Create the first node on the list
1225 pList
= PtrBegDbList
= new DbList
;
1229 // Initialize new node in the linked list
1231 pList
->Free
= FALSE
;
1232 strcpy(pList
->Dsn
, pDbStuff
->Dsn
);
1233 pList
->PtrDb
= new wxDB(pDbStuff
->Henv
);
1235 // Connect to the datasource
1236 if (pList
->PtrDb
->Open(pDbStuff
->Dsn
, pDbStuff
->Uid
, pDbStuff
->AuthStr
))
1237 return(pList
->PtrDb
);
1238 else // Unable to connect, destroy list item
1241 pList
->PtrPrev
->PtrNext
= 0;
1243 PtrBegDbList
= 0; // Empty list again
1244 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDB object
1245 pList
->PtrDb
->Close(); // Close the wxDB object
1246 delete pList
->PtrDb
; // Deletes the wxDB object
1247 delete pList
; // Deletes the linked list object
1251 } // GetDbConnection()
1253 /********** FreeDbConnection() **********/
1254 bool FreeDbConnection(wxDB
*pDb
)
1258 // Scan the linked list searching for the database connection
1259 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1261 if (pList
->PtrDb
== pDb
) // Found it!!!
1262 return(pList
->Free
= TRUE
);
1265 // Never found the database object, return failure
1268 } // FreeDbConnection()
1270 /********** CloseDbConnections() **********/
1271 void CloseDbConnections(void)
1273 DbList
*pList
, *pNext
;
1275 // Traverse the linked list closing database connections and freeing memory as I go.
1276 for (pList
= PtrBegDbList
; pList
; pList
= pNext
)
1278 pNext
= pList
->PtrNext
; // Save the pointer to next
1279 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDB object
1280 pList
->PtrDb
->Close(); // Close the wxDB object
1281 delete pList
->PtrDb
; // Deletes the wxDB object
1282 delete pList
; // Deletes the linked list object
1285 // Mark the list as empty
1288 } // CloseDbConnections()
1290 /********** NumberDbConnectionsInUse() **********/
1291 int NumberDbConnectionsInUse(void)
1296 // Scan the linked list counting db connections that are currently in use
1297 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1299 if (pList
->Free
== FALSE
)
1305 } // NumberDbConnectionsInUse()
1307 /********** GetDataSource() **********/
1308 bool GetDataSource(HENV henv
, char *Dsn
, SWORD DsnMax
, char *DsDesc
, SWORD DsDescMax
,
1313 if (SQLDataSources(henv
, direction
, (UCHAR FAR
*) Dsn
, DsnMax
, &cb
,
1314 (UCHAR FAR
*) DsDesc
, DsDescMax
, &cb
) == SQL_SUCCESS
)
1319 } // GetDataSource()