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 ///////////////////////////////////////////////////////////////////////////////
28 #pragma implementation "db.h"
36 #include "wx/wxprec.h"
54 DbList
*PtrBegDbList
= 0;
56 /********** wxDB Constructor **********/
57 wxDB::wxDB(HENV
&aHenv
)
63 nativeError
= cbErrorMsg
= 0;
64 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
65 strcpy(errorList
[i
], "");
67 // Init typeInf structures
68 strcpy(typeInfVarchar
.TypeName
,"");
69 typeInfVarchar
.FsqlType
= 0;
70 typeInfVarchar
.Precision
= 0;
71 typeInfVarchar
.CaseSensitive
= 0;
72 typeInfVarchar
.MaximumScale
= 0;
74 strcpy(typeInfInteger
.TypeName
,"");
75 typeInfInteger
.FsqlType
= 0;
76 typeInfInteger
.Precision
= 0;
77 typeInfInteger
.CaseSensitive
= 0;
78 typeInfInteger
.MaximumScale
= 0;
80 strcpy(typeInfFloat
.TypeName
,"");
81 typeInfFloat
.FsqlType
= 0;
82 typeInfFloat
.Precision
= 0;
83 typeInfFloat
.CaseSensitive
= 0;
84 typeInfFloat
.MaximumScale
= 0;
86 strcpy(typeInfDate
.TypeName
,"");
87 typeInfDate
.FsqlType
= 0;
88 typeInfDate
.Precision
= 0;
89 typeInfDate
.CaseSensitive
= 0;
90 typeInfDate
.MaximumScale
= 0;
92 // Error reporting is turned OFF by default
95 // Copy the HENV into the db class
98 // Allocate a data source connection handle
99 if (SQLAllocConnect(henv
, &hdbc
) != SQL_SUCCESS
)
102 // Initialize the db status flag
105 // Mark database as not open as of yet
110 /********** wxDB::Open() **********/
111 bool wxDB::Open(char *Dsn
, char *Uid
, char *AuthStr
)
118 #ifndef FWD_ONLY_CURSORS
122 // Specify that the ODBC cursor library be used, if needed. This must be
123 // specified before the connection is made.
124 retcode
= SQLSetConnectOption(hdbc
, SQL_ODBC_CURSORS
, SQL_CUR_USE_IF_NEEDED
);
127 if (retcode
== SQL_SUCCESS
)
128 cout
<< "SQLSetConnectOption(CURSOR_LIB) successful" << endl
;
130 cout
<< "SQLSetConnectOption(CURSOR_LIB) failed" << endl
;
135 // Connect to the data source
136 if (SQLConnect(hdbc
, (UCHAR FAR
*) Dsn
, SQL_NTS
,
137 (UCHAR FAR
*) Uid
, SQL_NTS
,
138 (UCHAR FAR
*) AuthStr
, SQL_NTS
) != SQL_SUCCESS
)
139 return(DispAllErrors(henv
, hdbc
));
141 // Mark database as open
144 // Allocate a statement handle for the database connection
145 if (SQLAllocStmt(hdbc
, &hstmt
) != SQL_SUCCESS
)
146 return(DispAllErrors(henv
, hdbc
));
148 // Set Connection Options
149 if (! setConnectionOptions())
152 // Query the data source for inf. about itself
156 // Query the data source regarding data type information
159 // The way I determined which SQL data types to use was by calling SQLGetInfo
160 // for all of the possible SQL data types to see which ones were supported. If
161 // a type is not supported, the SQLFetch() that's called from getDataTypeInfo()
162 // fails with SQL_NO_DATA_FOUND. This is ugly because I'm sure the three SQL data
163 // types I've selected below will not alway's be what we want. These are just
164 // what happened to work against an Oracle 7/Intersolv combination. The following is
165 // a complete list of the results I got back against the Oracle 7 database:
167 // SQL_BIGINT SQL_NO_DATA_FOUND
168 // SQL_BINARY SQL_NO_DATA_FOUND
169 // SQL_BIT SQL_NO_DATA_FOUND
170 // SQL_CHAR type name = 'CHAR', Precision = 255
171 // SQL_DATE SQL_NO_DATA_FOUND
172 // SQL_DECIMAL type name = 'NUMBER', Precision = 38
173 // SQL_DOUBLE type name = 'NUMBER', Precision = 15
174 // SQL_FLOAT SQL_NO_DATA_FOUND
175 // SQL_INTEGER SQL_NO_DATA_FOUND
176 // SQL_LONGVARBINARY type name = 'LONG RAW', Precision = 2 billion
177 // SQL_LONGVARCHAR type name = 'LONG', Precision = 2 billion
178 // SQL_NUMERIC SQL_NO_DATA_FOUND
179 // SQL_REAL SQL_NO_DATA_FOUND
180 // SQL_SMALLINT SQL_NO_DATA_FOUND
181 // SQL_TIME SQL_NO_DATA_FOUND
182 // SQL_TIMESTAMP type name = 'DATE', Precision = 19
183 // SQL_VARBINARY type name = 'RAW', Precision = 255
184 // SQL_VARCHAR type name = 'VARCHAR2', Precision = 2000
185 // =====================================================================
186 // Results from a Microsoft Access 7.0 db, using a driver from Microsoft
188 // SQL_VARCHAR type name = 'TEXT', Precision = 255
189 // SQL_TIMESTAMP type name = 'DATETIME'
190 // SQL_DECIMAL SQL_NO_DATA_FOUND
191 // SQL_NUMERIC type name = 'CURRENCY', Precision = 19
192 // SQL_FLOAT SQL_NO_DATA_FOUND
193 // SQL_REAL type name = 'SINGLE', Precision = 7
194 // SQL_DOUBLE type name = 'DOUBLE', Precision = 15
195 // SQL_INTEGER type name = 'LONG', Precision = 10
197 // VARCHAR = Variable length character string
198 if (! getDataTypeInfo(SQL_VARCHAR
, typeInfVarchar
))
199 if (! getDataTypeInfo(SQL_CHAR
, typeInfVarchar
))
202 typeInfVarchar
.FsqlType
= SQL_CHAR
;
204 typeInfVarchar
.FsqlType
= SQL_VARCHAR
;
207 if (! getDataTypeInfo(SQL_DOUBLE
, typeInfFloat
))
208 if (! getDataTypeInfo(SQL_REAL
, typeInfFloat
))
209 if (! getDataTypeInfo(SQL_FLOAT
, typeInfFloat
))
210 if (! getDataTypeInfo(SQL_DECIMAL
, typeInfFloat
))
211 if (! getDataTypeInfo(SQL_NUMERIC
, typeInfFloat
))
214 typeInfFloat
.FsqlType
= SQL_NUMERIC
;
216 typeInfFloat
.FsqlType
= SQL_DECIMAL
;
218 typeInfFloat
.FsqlType
= SQL_FLOAT
;
220 typeInfFloat
.FsqlType
= SQL_REAL
;
222 typeInfFloat
.FsqlType
= SQL_DOUBLE
;
225 if (! getDataTypeInfo(SQL_INTEGER
, typeInfInteger
))
226 // If SQL_INTEGER is not supported, use the floating point
227 // data type to store integers as well as floats
228 if (! getDataTypeInfo(typeInfFloat
.FsqlType
, typeInfInteger
))
231 typeInfInteger
.FsqlType
= typeInfFloat
.FsqlType
;
233 typeInfInteger
.FsqlType
= SQL_INTEGER
;
236 if (! getDataTypeInfo(SQL_TIMESTAMP
, typeInfDate
))
239 typeInfDate
.FsqlType
= SQL_TIMESTAMP
;
242 cout
<< "VARCHAR DATA TYPE: " << typeInfVarchar
.TypeName
<< endl
;
243 cout
<< "INTEGER DATA TYPE: " << typeInfInteger
.TypeName
<< endl
;
244 cout
<< "FLOAT DATA TYPE: " << typeInfFloat
.TypeName
<< endl
;
245 cout
<< "DATE DATA TYPE: " << typeInfDate
.TypeName
<< endl
;
249 // Completed Successfully
254 // The Intersolv/Oracle 7 driver was "Not Capable" of setting the login timeout.
256 /********** wxDB::setConnectionOptions() **********/
257 bool wxDB::setConnectionOptions(void)
259 SQLSetConnectOption(hdbc
, SQL_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
);
260 SQLSetConnectOption(hdbc
, SQL_OPT_TRACE
, SQL_OPT_TRACE_OFF
);
262 // Display the connection options to verify them
265 cout
<< ">>>>> CONNECTION OPTIONS <<<<<<" << endl
;
267 if (SQLGetConnectOption(hdbc
, SQL_AUTOCOMMIT
, &l
) != SQL_SUCCESS
)
268 return(DispAllErrors(henv
, hdbc
));
269 cout
<< "AUTOCOMMIT: " << (l
== SQL_AUTOCOMMIT_OFF
? "OFF" : "ON") << endl
;
271 if (SQLGetConnectOption(hdbc
, SQL_ODBC_CURSORS
, &l
) != SQL_SUCCESS
)
272 return(DispAllErrors(henv
, hdbc
));
273 cout
<< "ODBC CURSORS: ";
276 case(SQL_CUR_USE_IF_NEEDED
):
277 cout
<< "SQL_CUR_USE_IF_NEEDED";
279 case(SQL_CUR_USE_ODBC
):
280 cout
<< "SQL_CUR_USE_ODBC";
282 case(SQL_CUR_USE_DRIVER
):
283 cout
<< "SQL_CUR_USE_DRIVER";
288 if (SQLGetConnectOption(hdbc
, SQL_OPT_TRACE
, &l
) != SQL_SUCCESS
)
289 return(DispAllErrors(henv
, hdbc
));
290 cout
<< "TRACING: " << (l
== SQL_OPT_TRACE_OFF
? "OFF" : "ON") << endl
;
295 // Completed Successfully
298 } // wxDB::setConnectionOptions()
300 /********** wxDB::getDbInfo() **********/
301 bool wxDB::getDbInfo(void)
305 if (SQLGetInfo(hdbc
, SQL_SERVER_NAME
, dbInf
.serverName
, 40, &cb
) != SQL_SUCCESS
)
306 return(DispAllErrors(henv
, hdbc
));
308 if (SQLGetInfo(hdbc
, SQL_DATABASE_NAME
, dbInf
.databaseName
, 128, &cb
) != SQL_SUCCESS
)
309 return(DispAllErrors(henv
, hdbc
));
311 if (SQLGetInfo(hdbc
, SQL_DBMS_NAME
, dbInf
.dbmsName
, 40, &cb
) != SQL_SUCCESS
)
312 return(DispAllErrors(henv
, hdbc
));
314 if (SQLGetInfo(hdbc
, SQL_DBMS_VER
, dbInf
.dbmsVer
, 20, &cb
) != SQL_SUCCESS
)
315 return(DispAllErrors(henv
, hdbc
));
317 if (SQLGetInfo(hdbc
, SQL_ACTIVE_CONNECTIONS
, &dbInf
.maxConnections
, sizeof(dbInf
.maxConnections
), &cb
) != SQL_SUCCESS
)
318 return(DispAllErrors(henv
, hdbc
));
320 if (SQLGetInfo(hdbc
, SQL_ACTIVE_STATEMENTS
, &dbInf
.maxStmts
, sizeof(dbInf
.maxStmts
), &cb
) != SQL_SUCCESS
)
321 return(DispAllErrors(henv
, hdbc
));
323 if (SQLGetInfo(hdbc
, SQL_DRIVER_NAME
, dbInf
.driverName
, 40, &cb
) != SQL_SUCCESS
)
324 return(DispAllErrors(henv
, hdbc
));
326 if (SQLGetInfo(hdbc
, SQL_DRIVER_ODBC_VER
, dbInf
.odbcVer
, 20, &cb
) != SQL_SUCCESS
)
327 return(DispAllErrors(henv
, hdbc
));
329 if (SQLGetInfo(hdbc
, SQL_ODBC_VER
, dbInf
.drvMgrOdbcVer
, 20, &cb
) != SQL_SUCCESS
)
330 return(DispAllErrors(henv
, hdbc
));
332 if (SQLGetInfo(hdbc
, SQL_DRIVER_VER
, dbInf
.driverVer
, 40, &cb
) != SQL_SUCCESS
)
333 return(DispAllErrors(henv
, hdbc
));
335 if (SQLGetInfo(hdbc
, SQL_ODBC_API_CONFORMANCE
, &dbInf
.apiConfLvl
, sizeof(dbInf
.apiConfLvl
), &cb
) != SQL_SUCCESS
)
336 return(DispAllErrors(henv
, hdbc
));
338 if (SQLGetInfo(hdbc
, SQL_ODBC_SAG_CLI_CONFORMANCE
, &dbInf
.cliConfLvl
, sizeof(dbInf
.cliConfLvl
), &cb
) != SQL_SUCCESS
)
339 return(DispAllErrors(henv
, hdbc
));
341 if (SQLGetInfo(hdbc
, SQL_ODBC_SQL_CONFORMANCE
, &dbInf
.sqlConfLvl
, sizeof(dbInf
.sqlConfLvl
), &cb
) != SQL_SUCCESS
)
342 return(DispAllErrors(henv
, hdbc
));
344 if (SQLGetInfo(hdbc
, SQL_OUTER_JOINS
, dbInf
.outerJoins
, 2, &cb
) != SQL_SUCCESS
)
345 return(DispAllErrors(henv
, hdbc
));
347 if (SQLGetInfo(hdbc
, SQL_PROCEDURES
, dbInf
.procedureSupport
, 2, &cb
) != SQL_SUCCESS
)
348 return(DispAllErrors(henv
, hdbc
));
350 if (SQLGetInfo(hdbc
, SQL_CURSOR_COMMIT_BEHAVIOR
, &dbInf
.cursorCommitBehavior
, sizeof(dbInf
.cursorCommitBehavior
), &cb
) != SQL_SUCCESS
)
351 return(DispAllErrors(henv
, hdbc
));
353 if (SQLGetInfo(hdbc
, SQL_CURSOR_ROLLBACK_BEHAVIOR
, &dbInf
.cursorRollbackBehavior
, sizeof(dbInf
.cursorRollbackBehavior
), &cb
) != SQL_SUCCESS
)
354 return(DispAllErrors(henv
, hdbc
));
356 if (SQLGetInfo(hdbc
, SQL_NON_NULLABLE_COLUMNS
, &dbInf
.supportNotNullClause
, sizeof(dbInf
.supportNotNullClause
), &cb
) != SQL_SUCCESS
)
357 return(DispAllErrors(henv
, hdbc
));
359 if (SQLGetInfo(hdbc
, SQL_ODBC_SQL_OPT_IEF
, dbInf
.supportIEF
, 2, &cb
) != SQL_SUCCESS
)
360 return(DispAllErrors(henv
, hdbc
));
362 if (SQLGetInfo(hdbc
, SQL_DEFAULT_TXN_ISOLATION
, &dbInf
.txnIsolation
, sizeof(dbInf
.txnIsolation
), &cb
) != SQL_SUCCESS
)
363 return(DispAllErrors(henv
, hdbc
));
365 if (SQLGetInfo(hdbc
, SQL_TXN_ISOLATION_OPTION
, &dbInf
.txnIsolationOptions
, sizeof(dbInf
.txnIsolationOptions
), &cb
) != SQL_SUCCESS
)
366 return(DispAllErrors(henv
, hdbc
));
368 if (SQLGetInfo(hdbc
, SQL_FETCH_DIRECTION
, &dbInf
.fetchDirections
, sizeof(dbInf
.fetchDirections
), &cb
) != SQL_SUCCESS
)
369 return(DispAllErrors(henv
, hdbc
));
371 if (SQLGetInfo(hdbc
, SQL_LOCK_TYPES
, &dbInf
.lockTypes
, sizeof(dbInf
.lockTypes
), &cb
) != SQL_SUCCESS
)
372 return(DispAllErrors(henv
, hdbc
));
374 if (SQLGetInfo(hdbc
, SQL_POS_OPERATIONS
, &dbInf
.posOperations
, sizeof(dbInf
.posOperations
), &cb
) != SQL_SUCCESS
)
375 return(DispAllErrors(henv
, hdbc
));
377 if (SQLGetInfo(hdbc
, SQL_POSITIONED_STATEMENTS
, &dbInf
.posStmts
, sizeof(dbInf
.posStmts
), &cb
) != SQL_SUCCESS
)
378 return(DispAllErrors(henv
, hdbc
));
380 if (SQLGetInfo(hdbc
, SQL_SCROLL_CONCURRENCY
, &dbInf
.scrollConcurrency
, sizeof(dbInf
.scrollConcurrency
), &cb
) != SQL_SUCCESS
)
381 return(DispAllErrors(henv
, hdbc
));
383 if (SQLGetInfo(hdbc
, SQL_SCROLL_OPTIONS
, &dbInf
.scrollOptions
, sizeof(dbInf
.scrollOptions
), &cb
) != SQL_SUCCESS
)
384 return(DispAllErrors(henv
, hdbc
));
386 if (SQLGetInfo(hdbc
, SQL_STATIC_SENSITIVITY
, &dbInf
.staticSensitivity
, sizeof(dbInf
.staticSensitivity
), &cb
) != SQL_SUCCESS
)
387 return(DispAllErrors(henv
, hdbc
));
389 if (SQLGetInfo(hdbc
, SQL_TXN_CAPABLE
, &dbInf
.txnCapable
, sizeof(dbInf
.txnCapable
), &cb
) != SQL_SUCCESS
)
390 return(DispAllErrors(henv
, hdbc
));
392 if (SQLGetInfo(hdbc
, SQL_LOGIN_TIMEOUT
, &dbInf
.loginTimeout
, sizeof(dbInf
.loginTimeout
), &cb
) != SQL_SUCCESS
)
393 return(DispAllErrors(henv
, hdbc
));
396 cout
<< ">>>>> DATA SOURCE INFORMATION <<<<<" << endl
;
397 cout
<< "SERVER Name: " << dbInf
.serverName
<< endl
;
398 cout
<< "DBMS Name: " << dbInf
.dbmsName
<< "; DBMS Version: " << dbInf
.dbmsVer
<< endl
;
399 cout
<< "ODBC Version: " << dbInf
.odbcVer
<< "; Driver Version: " << dbInf
.driverVer
<< endl
;
401 cout
<< "API Conf. Level: ";
402 switch(dbInf
.apiConfLvl
)
404 case SQL_OAC_NONE
: cout
<< "None"; break;
405 case SQL_OAC_LEVEL1
: cout
<< "Level 1"; break;
406 case SQL_OAC_LEVEL2
: cout
<< "Level 2"; break;
410 cout
<< "SAG CLI Conf. Level: ";
411 switch(dbInf
.cliConfLvl
)
413 case SQL_OSCC_NOT_COMPLIANT
: cout
<< "Not Compliant"; break;
414 case SQL_OSCC_COMPLIANT
: cout
<< "Compliant"; break;
418 cout
<< "SQL Conf. Level: ";
419 switch(dbInf
.sqlConfLvl
)
421 case SQL_OSC_MINIMUM
: cout
<< "Minimum Grammer"; break;
422 case SQL_OSC_CORE
: cout
<< "Core Grammer"; break;
423 case SQL_OSC_EXTENDED
: cout
<< "Extended Grammer"; break;
427 cout
<< "Max. Connections: " << dbInf
.maxConnections
<< endl
;
428 cout
<< "Outer Joins: " << dbInf
.outerJoins
<< endl
;
429 cout
<< "Support for Procedures: " << dbInf
.procedureSupport
<< endl
;
431 cout
<< "Cursor COMMIT Behavior: ";
432 switch(dbInf
.cursorCommitBehavior
)
434 case SQL_CB_DELETE
: cout
<< "Delete cursors"; break;
435 case SQL_CB_CLOSE
: cout
<< "Close cursors"; break;
436 case SQL_CB_PRESERVE
: cout
<< "Preserve cursors"; break;
440 cout
<< "Cursor ROLLBACK Behavior: ";
441 switch(dbInf
.cursorRollbackBehavior
)
443 case SQL_CB_DELETE
: cout
<< "Delete cursors"; break;
444 case SQL_CB_CLOSE
: cout
<< "Close cursors"; break;
445 case SQL_CB_PRESERVE
: cout
<< "Preserve cursors"; break;
449 cout
<< "Support NOT NULL clause: ";
450 switch(dbInf
.supportNotNullClause
)
452 case SQL_NNC_NULL
: cout
<< "No"; break;
453 case SQL_NNC_NON_NULL
: cout
<< "Yes"; break;
457 cout
<< "Support IEF (Ref. Integrity): " << dbInf
.supportIEF
<< endl
;
458 cout
<< "Login Timeout: " << dbInf
.loginTimeout
<< endl
;
460 cout
<< endl
<< endl
<< "more ..." << endl
;
463 cout
<< "Default Transaction Isolation: ";
464 switch(dbInf
.txnIsolation
)
466 case SQL_TXN_READ_UNCOMMITTED
: cout
<< "Read Uncommitted"; break;
467 case SQL_TXN_READ_COMMITTED
: cout
<< "Read Committed"; break;
468 case SQL_TXN_REPEATABLE_READ
: cout
<< "Repeatable Read"; break;
469 case SQL_TXN_SERIALIZABLE
: cout
<< "Serializable"; break;
471 case SQL_TXN_VERSIONING
: cout
<< "Versioning"; break;
476 cout
<< "Transaction Isolation Options: ";
477 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_UNCOMMITTED
)
478 cout
<< "Read Uncommitted, ";
479 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_COMMITTED
)
480 cout
<< "Read Committed, ";
481 if (dbInf
.txnIsolationOptions
& SQL_TXN_REPEATABLE_READ
)
482 cout
<< "Repeatable Read, ";
483 if (dbInf
.txnIsolationOptions
& SQL_TXN_SERIALIZABLE
)
484 cout
<< "Serializable, ";
486 if (dbInf
.txnIsolationOptions
& SQL_TXN_VERSIONING
)
487 cout
<< "Versioning";
491 cout
<< "Fetch Directions Supported:" << endl
<< " ";
492 if (dbInf
.fetchDirections
& SQL_FD_FETCH_NEXT
)
494 if (dbInf
.fetchDirections
& SQL_FD_FETCH_PRIOR
)
496 if (dbInf
.fetchDirections
& SQL_FD_FETCH_FIRST
)
498 if (dbInf
.fetchDirections
& SQL_FD_FETCH_LAST
)
500 if (dbInf
.fetchDirections
& SQL_FD_FETCH_ABSOLUTE
)
501 cout
<< "Absolute, ";
502 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RELATIVE
)
503 cout
<< "Relative, ";
505 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RESUME
)
508 if (dbInf
.fetchDirections
& SQL_FD_FETCH_BOOKMARK
)
512 cout
<< "Lock Types Supported (SQLSetPos): ";
513 if (dbInf
.lockTypes
& SQL_LCK_NO_CHANGE
)
514 cout
<< "No Change, ";
515 if (dbInf
.lockTypes
& SQL_LCK_EXCLUSIVE
)
516 cout
<< "Exclusive, ";
517 if (dbInf
.lockTypes
& SQL_LCK_UNLOCK
)
521 cout
<< "Position Operations Supported (SQLSetPos): ";
522 if (dbInf
.posOperations
& SQL_POS_POSITION
)
523 cout
<< "Position, ";
524 if (dbInf
.posOperations
& SQL_POS_REFRESH
)
526 if (dbInf
.posOperations
& SQL_POS_UPDATE
)
528 if (dbInf
.posOperations
& SQL_POS_DELETE
)
530 if (dbInf
.posOperations
& SQL_POS_ADD
)
534 cout
<< "Positioned Statements Supported: ";
535 if (dbInf
.posStmts
& SQL_PS_POSITIONED_DELETE
)
536 cout
<< "Pos delete, ";
537 if (dbInf
.posStmts
& SQL_PS_POSITIONED_UPDATE
)
538 cout
<< "Pos update, ";
539 if (dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
540 cout
<< "Select for update";
543 cout
<< "Scroll Concurrency: ";
544 if (dbInf
.scrollConcurrency
& SQL_SCCO_READ_ONLY
)
545 cout
<< "Read Only, ";
546 if (dbInf
.scrollConcurrency
& SQL_SCCO_LOCK
)
548 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_ROWVER
)
549 cout
<< "Opt. Rowver, ";
550 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_VALUES
)
551 cout
<< "Opt. Values";
554 cout
<< "Scroll Options: ";
555 if (dbInf
.scrollOptions
& SQL_SO_FORWARD_ONLY
)
556 cout
<< "Fwd Only, ";
557 if (dbInf
.scrollOptions
& SQL_SO_STATIC
)
559 if (dbInf
.scrollOptions
& SQL_SO_KEYSET_DRIVEN
)
560 cout
<< "Keyset Driven, ";
561 if (dbInf
.scrollOptions
& SQL_SO_DYNAMIC
)
563 if (dbInf
.scrollOptions
& SQL_SO_MIXED
)
567 cout
<< "Static Sensitivity: ";
568 if (dbInf
.staticSensitivity
& SQL_SS_ADDITIONS
)
569 cout
<< "Additions, ";
570 if (dbInf
.staticSensitivity
& SQL_SS_DELETIONS
)
571 cout
<< "Deletions, ";
572 if (dbInf
.staticSensitivity
& SQL_SS_UPDATES
)
576 cout
<< "Transaction Capable?: ";
577 switch(dbInf
.txnCapable
)
579 case SQL_TC_NONE
: cout
<< "No"; break;
580 case SQL_TC_DML
: cout
<< "DML Only"; break;
581 case SQL_TC_DDL_COMMIT
: cout
<< "DDL Commit"; break;
582 case SQL_TC_DDL_IGNORE
: cout
<< "DDL Ignore"; break;
583 case SQL_TC_ALL
: cout
<< "DDL & DML"; break;
591 // Completed Successfully
594 } // wxDB::getDbInfo()
596 /********** wxDB::getDataTypeInfo() **********/
597 bool wxDB::getDataTypeInfo(SWORD fSqlType
, SqlTypeInfo
&structSQLTypeInfo
)
599 // fSqlType will be something like SQL_VARCHAR. This parameter determines
600 // the data type inf. is gathered for.
602 // SqlTypeInfo is a structure that is filled in with data type information,
607 // Get information about the data type specified
608 if (SQLGetTypeInfo(hstmt
, fSqlType
) != SQL_SUCCESS
)
609 return(DispAllErrors(henv
, hdbc
, hstmt
));
611 if ((retcode
= SQLFetch(hstmt
)) != SQL_SUCCESS
)
614 if (retcode
== SQL_NO_DATA_FOUND
)
615 cout
<< "SQL_NO_DATA_FOUND fetching inf. about data type." << endl
;
617 DispAllErrors(henv
, hdbc
, hstmt
);
618 SQLFreeStmt(hstmt
, SQL_CLOSE
);
621 // Obtain columns from the record
622 if (SQLGetData(hstmt
, 1, SQL_C_CHAR
, structSQLTypeInfo
.TypeName
, DB_TYPE_NAME_LEN
, &cbRet
) != SQL_SUCCESS
)
623 return(DispAllErrors(henv
, hdbc
, hstmt
));
624 if (SQLGetData(hstmt
, 3, SQL_C_LONG
, &structSQLTypeInfo
.Precision
, 0, &cbRet
) != SQL_SUCCESS
)
625 return(DispAllErrors(henv
, hdbc
, hstmt
));
626 if (SQLGetData(hstmt
, 8, SQL_C_SHORT
, &structSQLTypeInfo
.CaseSensitive
, 0, &cbRet
) != SQL_SUCCESS
)
627 return(DispAllErrors(henv
, hdbc
, hstmt
));
628 // if (SQLGetData(hstmt, 14, SQL_C_SHORT, &structSQLTypeInfo.MinimumScale, 0, &cbRet) != SQL_SUCCESS)
629 // return(DispAllErrors(henv, hdbc, hstmt));
630 if (SQLGetData(hstmt
, 15, SQL_C_SHORT
, &structSQLTypeInfo
.MaximumScale
, 0, &cbRet
) != SQL_SUCCESS
)
631 return(DispAllErrors(henv
, hdbc
, hstmt
));
633 if (structSQLTypeInfo
.MaximumScale
< 0)
634 structSQLTypeInfo
.MaximumScale
= 0;
636 // Close the statement handle which closes open cursors
637 if (SQLFreeStmt(hstmt
, SQL_CLOSE
) != SQL_SUCCESS
)
638 return(DispAllErrors(henv
, hdbc
, hstmt
));
640 // Completed Successfully
643 } // wxDB::getDataTypeInfo()
645 /********** wxDB::Close() **********/
646 void wxDB::Close(void)
648 // Free statement handle
651 if (SQLFreeStmt(hstmt
, SQL_DROP
) != SQL_SUCCESS
)
652 DispAllErrors(henv
, hdbc
);
655 // Disconnect from the datasource
656 if (SQLDisconnect(hdbc
) != SQL_SUCCESS
)
657 DispAllErrors(henv
, hdbc
);
659 // Free the connection to the datasource
660 if (SQLFreeConnect(hdbc
) != SQL_SUCCESS
)
661 DispAllErrors(henv
, hdbc
);
665 /********** wxDB::CommitTrans() **********/
666 bool wxDB::CommitTrans(void)
668 // Commit the transaction
669 if (SQLTransact(henv
, hdbc
, SQL_COMMIT
) != SQL_SUCCESS
)
670 return(DispAllErrors(henv
, hdbc
));
672 // Completed successfully
675 } // wxDB::CommitTrans()
677 /********** wxDB::RollbackTrans() **********/
678 bool wxDB::RollbackTrans(void)
680 // Rollback the transaction
681 if (SQLTransact(henv
, hdbc
, SQL_ROLLBACK
) != SQL_SUCCESS
)
682 return(DispAllErrors(henv
, hdbc
));
684 // Completed successfully
687 } // wxDB::RollbackTrans()
689 /********** wxDB::DispAllErrors() **********/
690 bool wxDB::DispAllErrors(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
692 char odbcErrMsg
[DB_MAX_ERROR_MSG_LEN
];
694 while (SQLError(aHenv
, aHdbc
, aHstmt
, (UCHAR FAR
*) sqlState
, &nativeError
, (UCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
696 sprintf(odbcErrMsg
, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState
, nativeError
, errorMsg
);
697 logError(odbcErrMsg
, sqlState
);
701 // When run in console mode, use standard out to display errors.
702 cout
<< odbcErrMsg
<< endl
;
703 cout
<< "Press any key to continue..." << endl
;
709 return(FALSE
); // This function alway's returns false.
711 } // wxDB::DispAllErrors()
713 /********** wxDB::GetNextError() **********/
714 bool wxDB::GetNextError(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
716 if (SQLError(aHenv
, aHdbc
, aHstmt
, (UCHAR FAR
*) sqlState
, &nativeError
, (UCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
721 } // wxDB::GetNextError()
723 /********** wxDB::DispNextError() **********/
724 void wxDB::DispNextError(void)
726 char odbcErrMsg
[DB_MAX_ERROR_MSG_LEN
];
728 sprintf(odbcErrMsg
, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState
, nativeError
, errorMsg
);
729 logError(odbcErrMsg
, sqlState
);
735 // When run in console mode, use standard out to display errors.
736 cout
<< odbcErrMsg
<< endl
;
737 cout
<< "Press any key to continue..." << endl
;
741 } // wxDB::DispNextError()
743 /********** wxDB::logError() **********/
744 void wxDB::logError(char *errMsg
, char *SQLState
)
746 assert(errMsg
&& strlen(errMsg
));
748 static int pLast
= -1;
751 if (++pLast
== DB_MAX_ERROR_HISTORY
)
753 for (int i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
754 strcpy(errorList
[i
], errorList
[i
+1]);
758 strcpy(errorList
[pLast
], errMsg
);
760 if (SQLState
&& strlen(SQLState
))
761 if ((dbStatus
= TranslateSqlState(SQLState
)) != DB_ERR_FUNCTION_SEQUENCE_ERROR
)
762 DB_STATUS
= dbStatus
;
764 } // wxDB::logError()
766 /**********wxDB::TranslateSqlState() **********/
767 int wxDB::TranslateSqlState(char *SQLState
)
769 if (!strcmp(SQLState
, "01000"))
770 return(DB_ERR_GENERAL_WARNING
);
771 if (!strcmp(SQLState
, "01002"))
772 return(DB_ERR_DISCONNECT_ERROR
);
773 if (!strcmp(SQLState
, "01004"))
774 return(DB_ERR_DATA_TRUNCATED
);
775 if (!strcmp(SQLState
, "01006"))
776 return(DB_ERR_PRIV_NOT_REVOKED
);
777 if (!strcmp(SQLState
, "01S00"))
778 return(DB_ERR_INVALID_CONN_STR_ATTR
);
779 if (!strcmp(SQLState
, "01S01"))
780 return(DB_ERR_ERROR_IN_ROW
);
781 if (!strcmp(SQLState
, "01S02"))
782 return(DB_ERR_OPTION_VALUE_CHANGED
);
783 if (!strcmp(SQLState
, "01S03"))
784 return(DB_ERR_NO_ROWS_UPD_OR_DEL
);
785 if (!strcmp(SQLState
, "01S04"))
786 return(DB_ERR_MULTI_ROWS_UPD_OR_DEL
);
787 if (!strcmp(SQLState
, "07001"))
788 return(DB_ERR_WRONG_NO_OF_PARAMS
);
789 if (!strcmp(SQLState
, "07006"))
790 return(DB_ERR_DATA_TYPE_ATTR_VIOL
);
791 if (!strcmp(SQLState
, "08001"))
792 return(DB_ERR_UNABLE_TO_CONNECT
);
793 if (!strcmp(SQLState
, "08002"))
794 return(DB_ERR_CONNECTION_IN_USE
);
795 if (!strcmp(SQLState
, "08003"))
796 return(DB_ERR_CONNECTION_NOT_OPEN
);
797 if (!strcmp(SQLState
, "08004"))
798 return(DB_ERR_REJECTED_CONNECTION
);
799 if (!strcmp(SQLState
, "08007"))
800 return(DB_ERR_CONN_FAIL_IN_TRANS
);
801 if (!strcmp(SQLState
, "08S01"))
802 return(DB_ERR_COMM_LINK_FAILURE
);
803 if (!strcmp(SQLState
, "21S01"))
804 return(DB_ERR_INSERT_VALUE_LIST_MISMATCH
);
805 if (!strcmp(SQLState
, "21S02"))
806 return(DB_ERR_DERIVED_TABLE_MISMATCH
);
807 if (!strcmp(SQLState
, "22001"))
808 return(DB_ERR_STRING_RIGHT_TRUNC
);
809 if (!strcmp(SQLState
, "22003"))
810 return(DB_ERR_NUMERIC_VALUE_OUT_OF_RNG
);
811 if (!strcmp(SQLState
, "22005"))
812 return(DB_ERR_ERROR_IN_ASSIGNMENT
);
813 if (!strcmp(SQLState
, "22008"))
814 return(DB_ERR_DATETIME_FLD_OVERFLOW
);
815 if (!strcmp(SQLState
, "22012"))
816 return(DB_ERR_DIVIDE_BY_ZERO
);
817 if (!strcmp(SQLState
, "22026"))
818 return(DB_ERR_STR_DATA_LENGTH_MISMATCH
);
819 if (!strcmp(SQLState
, "23000"))
820 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
821 if (!strcmp(SQLState
, "24000"))
822 return(DB_ERR_INVALID_CURSOR_STATE
);
823 if (!strcmp(SQLState
, "25000"))
824 return(DB_ERR_INVALID_TRANS_STATE
);
825 if (!strcmp(SQLState
, "28000"))
826 return(DB_ERR_INVALID_AUTH_SPEC
);
827 if (!strcmp(SQLState
, "34000"))
828 return(DB_ERR_INVALID_CURSOR_NAME
);
829 if (!strcmp(SQLState
, "37000"))
830 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL
);
831 if (!strcmp(SQLState
, "3C000"))
832 return(DB_ERR_DUPLICATE_CURSOR_NAME
);
833 if (!strcmp(SQLState
, "40001"))
834 return(DB_ERR_SERIALIZATION_FAILURE
);
835 if (!strcmp(SQLState
, "42000"))
836 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2
);
837 if (!strcmp(SQLState
, "70100"))
838 return(DB_ERR_OPERATION_ABORTED
);
839 if (!strcmp(SQLState
, "IM001"))
840 return(DB_ERR_UNSUPPORTED_FUNCTION
);
841 if (!strcmp(SQLState
, "IM002"))
842 return(DB_ERR_NO_DATA_SOURCE
);
843 if (!strcmp(SQLState
, "IM003"))
844 return(DB_ERR_DRIVER_LOAD_ERROR
);
845 if (!strcmp(SQLState
, "IM004"))
846 return(DB_ERR_SQLALLOCENV_FAILED
);
847 if (!strcmp(SQLState
, "IM005"))
848 return(DB_ERR_SQLALLOCCONNECT_FAILED
);
849 if (!strcmp(SQLState
, "IM006"))
850 return(DB_ERR_SQLSETCONNECTOPTION_FAILED
);
851 if (!strcmp(SQLState
, "IM007"))
852 return(DB_ERR_NO_DATA_SOURCE_DLG_PROHIB
);
853 if (!strcmp(SQLState
, "IM008"))
854 return(DB_ERR_DIALOG_FAILED
);
855 if (!strcmp(SQLState
, "IM009"))
856 return(DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL
);
857 if (!strcmp(SQLState
, "IM010"))
858 return(DB_ERR_DATA_SOURCE_NAME_TOO_LONG
);
859 if (!strcmp(SQLState
, "IM011"))
860 return(DB_ERR_DRIVER_NAME_TOO_LONG
);
861 if (!strcmp(SQLState
, "IM012"))
862 return(DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR
);
863 if (!strcmp(SQLState
, "IM013"))
864 return(DB_ERR_TRACE_FILE_ERROR
);
865 if (!strcmp(SQLState
, "S0001"))
866 return(DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS
);
867 if (!strcmp(SQLState
, "S0002"))
868 return(DB_ERR_TABLE_NOT_FOUND
);
869 if (!strcmp(SQLState
, "S0011"))
870 return(DB_ERR_INDEX_ALREADY_EXISTS
);
871 if (!strcmp(SQLState
, "S0012"))
872 return(DB_ERR_INDEX_NOT_FOUND
);
873 if (!strcmp(SQLState
, "S0021"))
874 return(DB_ERR_COLUMN_ALREADY_EXISTS
);
875 if (!strcmp(SQLState
, "S0022"))
876 return(DB_ERR_COLUMN_NOT_FOUND
);
877 if (!strcmp(SQLState
, "S0023"))
878 return(DB_ERR_NO_DEFAULT_FOR_COLUMN
);
879 if (!strcmp(SQLState
, "S1000"))
880 return(DB_ERR_GENERAL_ERROR
);
881 if (!strcmp(SQLState
, "S1001"))
882 return(DB_ERR_MEMORY_ALLOCATION_FAILURE
);
883 if (!strcmp(SQLState
, "S1002"))
884 return(DB_ERR_INVALID_COLUMN_NUMBER
);
885 if (!strcmp(SQLState
, "S1003"))
886 return(DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE
);
887 if (!strcmp(SQLState
, "S1004"))
888 return(DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE
);
889 if (!strcmp(SQLState
, "S1008"))
890 return(DB_ERR_OPERATION_CANCELLED
);
891 if (!strcmp(SQLState
, "S1009"))
892 return(DB_ERR_INVALID_ARGUMENT_VALUE
);
893 if (!strcmp(SQLState
, "S1010"))
894 return(DB_ERR_FUNCTION_SEQUENCE_ERROR
);
895 if (!strcmp(SQLState
, "S1011"))
896 return(DB_ERR_OPERATION_INVALID_AT_THIS_TIME
);
897 if (!strcmp(SQLState
, "S1012"))
898 return(DB_ERR_INVALID_TRANS_OPERATION_CODE
);
899 if (!strcmp(SQLState
, "S1015"))
900 return(DB_ERR_NO_CURSOR_NAME_AVAIL
);
901 if (!strcmp(SQLState
, "S1090"))
902 return(DB_ERR_INVALID_STR_OR_BUF_LEN
);
903 if (!strcmp(SQLState
, "S1091"))
904 return(DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE
);
905 if (!strcmp(SQLState
, "S1092"))
906 return(DB_ERR_OPTION_TYPE_OUT_OF_RANGE
);
907 if (!strcmp(SQLState
, "S1093"))
908 return(DB_ERR_INVALID_PARAM_NO
);
909 if (!strcmp(SQLState
, "S1094"))
910 return(DB_ERR_INVALID_SCALE_VALUE
);
911 if (!strcmp(SQLState
, "S1095"))
912 return(DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE
);
913 if (!strcmp(SQLState
, "S1096"))
914 return(DB_ERR_INF_TYPE_OUT_OF_RANGE
);
915 if (!strcmp(SQLState
, "S1097"))
916 return(DB_ERR_COLUMN_TYPE_OUT_OF_RANGE
);
917 if (!strcmp(SQLState
, "S1098"))
918 return(DB_ERR_SCOPE_TYPE_OUT_OF_RANGE
);
919 if (!strcmp(SQLState
, "S1099"))
920 return(DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE
);
921 if (!strcmp(SQLState
, "S1100"))
922 return(DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE
);
923 if (!strcmp(SQLState
, "S1101"))
924 return(DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE
);
925 if (!strcmp(SQLState
, "S1103"))
926 return(DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE
);
927 if (!strcmp(SQLState
, "S1104"))
928 return(DB_ERR_INVALID_PRECISION_VALUE
);
929 if (!strcmp(SQLState
, "S1105"))
930 return(DB_ERR_INVALID_PARAM_TYPE
);
931 if (!strcmp(SQLState
, "S1106"))
932 return(DB_ERR_FETCH_TYPE_OUT_OF_RANGE
);
933 if (!strcmp(SQLState
, "S1107"))
934 return(DB_ERR_ROW_VALUE_OUT_OF_RANGE
);
935 if (!strcmp(SQLState
, "S1108"))
936 return(DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE
);
937 if (!strcmp(SQLState
, "S1109"))
938 return(DB_ERR_INVALID_CURSOR_POSITION
);
939 if (!strcmp(SQLState
, "S1110"))
940 return(DB_ERR_INVALID_DRIVER_COMPLETION
);
941 if (!strcmp(SQLState
, "S1111"))
942 return(DB_ERR_INVALID_BOOKMARK_VALUE
);
943 if (!strcmp(SQLState
, "S1C00"))
944 return(DB_ERR_DRIVER_NOT_CAPABLE
);
945 if (!strcmp(SQLState
, "S1T00"))
946 return(DB_ERR_TIMEOUT_EXPIRED
);
951 } // wxDB::TranslateSqlState()
953 /********** wxDB::Grant() **********/
954 bool wxDB::Grant(int privileges
, char *tableName
, char *userList
)
956 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
958 // Build the grant statement
959 strcpy(sqlStmt
, "GRANT ");
960 if (privileges
== DB_GRANT_ALL
)
961 strcat(sqlStmt
, "ALL");
965 if (privileges
& DB_GRANT_SELECT
)
967 strcat(sqlStmt
, "SELECT");
970 if (privileges
& DB_GRANT_INSERT
)
973 strcat(sqlStmt
, ", ");
974 strcat(sqlStmt
, "INSERT");
976 if (privileges
& DB_GRANT_UPDATE
)
979 strcat(sqlStmt
, ", ");
980 strcat(sqlStmt
, "UPDATE");
982 if (privileges
& DB_GRANT_DELETE
)
985 strcat(sqlStmt
, ", ");
986 strcat(sqlStmt
, "DELETE");
990 strcat(sqlStmt
, " ON ");
991 strcat(sqlStmt
, tableName
);
992 strcat(sqlStmt
, " TO ");
993 strcat(sqlStmt
, userList
);
996 cout
<< endl
<< sqlStmt
<< endl
;
999 return(ExecSql(sqlStmt
));
1003 /********** wxDB::CreateView() **********/
1004 bool wxDB::CreateView(char *viewName
, char *colList
, char *pSqlStmt
)
1006 char sqlStmt
[DB_MAX_STATEMENT_LEN
];
1008 // Drop the view first
1009 sprintf(sqlStmt
, "DROP VIEW %s", viewName
);
1010 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
, SQL_NTS
) != SQL_SUCCESS
)
1012 // Check for sqlState = S0002, "Table or view not found".
1013 // Ignore this error, bomb out on any other error.
1014 // SQL Sybase Anwhere v5.5 returns an access violation error here
1015 // (sqlstate = 42000) rather than an S0002.
1016 GetNextError(henv
, hdbc
, hstmt
);
1017 if (strcmp(sqlState
, "S0002") && strcmp(sqlState
, "42000"))
1020 DispAllErrors(henv
, hdbc
, hstmt
);
1027 cout
<< endl
<< sqlStmt
<< endl
;
1030 // Build the create view statement
1031 strcpy(sqlStmt
, "CREATE VIEW ");
1032 strcat(sqlStmt
, viewName
);
1034 if (strlen(colList
))
1036 strcat(sqlStmt
, " (");
1037 strcat(sqlStmt
, colList
);
1038 strcat(sqlStmt
, ")");
1041 strcat(sqlStmt
, " AS ");
1042 strcat(sqlStmt
, pSqlStmt
);
1045 cout
<< sqlStmt
<< endl
;
1048 return(ExecSql(sqlStmt
));
1050 } // wxDB::CreateView()
1052 /********** wxDB::ExecSql() **********/
1053 bool wxDB::ExecSql(char *pSqlStmt
)
1055 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) == SQL_SUCCESS
)
1059 DispAllErrors(henv
, hdbc
, hstmt
);
1063 } // wxDB::ExecSql()
1065 /********** wxDB::GetColumns() **********/
1067 * 1) The last array element of the tableName[] argument must be zero (null).
1068 * This is how the end of the array is detected.
1069 * 2) This function returns an array of CcolInf structures. If no columns
1070 * were found, or an error occured, this pointer will be zero (null). THE
1071 * CALLING FUNCTION IS RESPONSIBLE FOR DELETING THE MEMORY RETURNED WHEN IT
1072 * IS FINISHED WITH IT. i.e.
1074 * CcolInf *colInf = pDb->GetColumns(tableList);
1077 * // Use the column inf
1079 * // Destroy the memory
1083 CcolInf
*wxDB::GetColumns(char *tableName
[])
1087 CcolInf
*colInf
= 0;
1090 char tblName
[DB_MAX_TABLE_NAME_LEN
+1];
1091 char colName
[DB_MAX_COLUMN_NAME_LEN
+1];
1094 // Pass 1 - Determine how many columns there are.
1095 // Pass 2 - Allocate the CcolInf array and fill in
1096 // the array with the column information.
1097 for (int pass
= 1; pass
<= 2; pass
++)
1101 if (noCols
== 0) // Probably a bogus table name(s)
1103 // Allocate n CcolInf objects to hold the column information
1104 colInf
= new CcolInf
[noCols
+1];
1107 // Mark the end of the array
1108 strcpy(colInf
[noCols
].tableName
, "");
1109 strcpy(colInf
[noCols
].colName
, "");
1110 colInf
[noCols
].sqlDataType
= 0;
1112 // Loop through each table name
1113 for (int tbl
= 0; tableName
[tbl
]; tbl
++)
1115 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1116 retcode
= SQLColumns(hstmt
,
1117 NULL
, 0, // All qualifiers
1118 NULL
, 0, // All owners
1119 (UCHAR
*) tableName
[tbl
], SQL_NTS
,
1120 NULL
, 0); // All columns
1121 if (retcode
!= SQL_SUCCESS
)
1122 { // Error occured, abort
1123 DispAllErrors(henv
, hdbc
, hstmt
);
1128 SQLBindCol(hstmt
, 3, SQL_C_CHAR
, tblName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
1129 SQLBindCol(hstmt
, 4, SQL_C_CHAR
, colName
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1130 SQLBindCol(hstmt
, 5, SQL_C_SSHORT
, &sqlDataType
, 0, &cb
);
1131 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
1133 if (pass
== 1) // First pass, just add up the number of columns
1135 else // Pass 2; Fill in the array of structures
1137 if (colNo
< noCols
) // Some extra error checking to prevent memory overwrites
1139 strcpy(colInf
[colNo
].tableName
, tblName
);
1140 strcpy(colInf
[colNo
].colName
, colName
);
1141 colInf
[colNo
].sqlDataType
= sqlDataType
;
1146 if (retcode
!= SQL_NO_DATA_FOUND
)
1147 { // Error occured, abort
1148 DispAllErrors(henv
, hdbc
, hstmt
);
1156 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1159 } // wxDB::GetColumns()
1162 // Table name can refer to a table, view, alias or synonym. Returns true
1163 // if the object exists in the database. This function does not indicate
1164 // whether or not the user has privleges to query or perform other functions
1166 bool wxDB::TableExists(char *tableName
)
1168 assert(tableName
&& strlen(tableName
));
1170 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1171 RETCODE retcode
= SQLTables(hstmt
,
1172 NULL
, 0, // All qualifiers
1173 NULL
, 0, // All owners
1174 (UCHAR FAR
*)tableName
, SQL_NTS
,
1175 NULL
, 0); // All table types
1176 if (retcode
!= SQL_SUCCESS
)
1177 return(DispAllErrors(henv
, hdbc
, hstmt
));
1179 if (SQLFetch(hstmt
) != SQL_SUCCESS
)
1181 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1182 return(DispAllErrors(henv
, hdbc
, hstmt
));
1185 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1188 } // wxDB::TableExists()
1191 /********** GetDbConnection() **********/
1192 wxDB
*GetDbConnection(DbStuff
*pDbStuff
)
1196 // Scan the linked list searching for an available database connection
1197 // that's already been opened but is currently not in use.
1198 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1200 // The database connection must be for the same datasource
1201 // name and must currently not be in use.
1202 if (pList
->Free
&& (! strcmp(pDbStuff
->Dsn
, pList
->Dsn
))) // Found a free connection
1204 pList
->Free
= FALSE
;
1205 return(pList
->PtrDb
);
1209 // No available connections. A new connection must be made and
1210 // appended to the end of the linked list.
1213 // Find the end of the list
1214 for (pList
= PtrBegDbList
; pList
->PtrNext
; pList
= pList
->PtrNext
);
1215 // Append a new list item
1216 pList
->PtrNext
= new DbList
;
1217 pList
->PtrNext
->PtrPrev
= pList
;
1218 pList
= pList
->PtrNext
;
1222 // Create the first node on the list
1223 pList
= PtrBegDbList
= new DbList
;
1227 // Initialize new node in the linked list
1229 pList
->Free
= FALSE
;
1230 strcpy(pList
->Dsn
, pDbStuff
->Dsn
);
1231 pList
->PtrDb
= new wxDB(pDbStuff
->Henv
);
1233 // Connect to the datasource
1234 if (pList
->PtrDb
->Open(pDbStuff
->Dsn
, pDbStuff
->Uid
, pDbStuff
->AuthStr
))
1235 return(pList
->PtrDb
);
1236 else // Unable to connect, destroy list item
1239 pList
->PtrPrev
->PtrNext
= 0;
1241 PtrBegDbList
= 0; // Empty list again
1242 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDB object
1243 pList
->PtrDb
->Close(); // Close the wxDB object
1244 delete pList
->PtrDb
; // Deletes the wxDB object
1245 delete pList
; // Deletes the linked list object
1249 } // GetDbConnection()
1251 /********** FreeDbConnection() **********/
1252 bool FreeDbConnection(wxDB
*pDb
)
1256 // Scan the linked list searching for the database connection
1257 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1259 if (pList
->PtrDb
== pDb
) // Found it!!!
1260 return(pList
->Free
= TRUE
);
1263 // Never found the database object, return failure
1266 } // FreeDbConnection()
1268 /********** CloseDbConnections() **********/
1269 void CloseDbConnections(void)
1271 DbList
*pList
, *pNext
;
1273 // Traverse the linked list closing database connections and freeing memory as I go.
1274 for (pList
= PtrBegDbList
; pList
; pList
= pNext
)
1276 pNext
= pList
->PtrNext
; // Save the pointer to next
1277 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDB object
1278 pList
->PtrDb
->Close(); // Close the wxDB object
1279 delete pList
->PtrDb
; // Deletes the wxDB object
1280 delete pList
; // Deletes the linked list object
1283 // Mark the list as empty
1286 } // CloseDbConnections()
1288 /********** NumberDbConnectionsInUse() **********/
1289 int NumberDbConnectionsInUse(void)
1294 // Scan the linked list counting db connections that are currently in use
1295 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
1297 if (pList
->Free
== FALSE
)
1303 } // NumberDbConnectionsInUse()
1305 /********** GetDataSource() **********/
1306 bool GetDataSource(HENV henv
, char *Dsn
, SWORD DsnMax
, char *DsDesc
, SWORD DsDescMax
,
1311 if (SQLDataSources(henv
, direction
, (UCHAR FAR
*) Dsn
, DsnMax
, &cb
,
1312 (UCHAR FAR
*) DsDesc
, DsDescMax
, &cb
) == SQL_SUCCESS
)
1317 } // GetDataSource()