]> git.saurik.com Git - wxWidgets.git/blob - src/common/db.cpp
wxApp::(S|G)etVendorName added
[wxWidgets.git] / src / common / db.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: db.cpp
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.
6 // Author: Doug Card
7 // Modified by:
8 // Created: 9.96
9 // RCS-ID: $Id$
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 ///////////////////////////////////////////////////////////////////////////////
22
23 /*
24 // SYNOPSIS START
25 // SYNOPSIS STOP
26 */
27
28 #pragma implementation "db.h"
29
30 /*
31 #ifdef _CONSOLE
32 #include <iostream.h>
33 #endif
34 */
35
36 #include "wx/wxprec.h"
37
38 #ifdef __BORLANDC__
39 #pragma hdrstop
40 #endif //__BORLANDC__
41
42 #ifndef WX_PRECOMP
43 #include <wx/wx.h>
44 #endif //WX_PRECOMP
45
46 #if USE_ODBC
47
48 #include <wx/db.h>
49
50 #include <stdio.h>
51 #include <string.h>
52 #include <assert.h>
53
54 DbList *PtrBegDbList = 0;
55
56 /********** wxDB Constructor **********/
57 wxDB::wxDB(HENV &aHenv)
58 {
59 int i;
60
61 strcpy(sqlState,"");
62 strcpy(errorMsg,"");
63 nativeError = cbErrorMsg = 0;
64 for (i = 0; i < DB_MAX_ERROR_HISTORY; i++)
65 strcpy(errorList[i], "");
66
67 // Init typeInf structures
68 strcpy(typeInfVarchar.TypeName,"");
69 typeInfVarchar.FsqlType = 0;
70 typeInfVarchar.Precision = 0;
71 typeInfVarchar.CaseSensitive = 0;
72 typeInfVarchar.MaximumScale = 0;
73
74 strcpy(typeInfInteger.TypeName,"");
75 typeInfInteger.FsqlType = 0;
76 typeInfInteger.Precision = 0;
77 typeInfInteger.CaseSensitive = 0;
78 typeInfInteger.MaximumScale = 0;
79
80 strcpy(typeInfFloat.TypeName,"");
81 typeInfFloat.FsqlType = 0;
82 typeInfFloat.Precision = 0;
83 typeInfFloat.CaseSensitive = 0;
84 typeInfFloat.MaximumScale = 0;
85
86 strcpy(typeInfDate.TypeName,"");
87 typeInfDate.FsqlType = 0;
88 typeInfDate.Precision = 0;
89 typeInfDate.CaseSensitive = 0;
90 typeInfDate.MaximumScale = 0;
91
92 // Error reporting is turned OFF by default
93 silent = TRUE;
94
95 // Copy the HENV into the db class
96 henv = aHenv;
97
98 // Allocate a data source connection handle
99 if (SQLAllocConnect(henv, &hdbc) != SQL_SUCCESS)
100 DispAllErrors(henv);
101
102 // Initialize the db status flag
103 DB_STATUS = 0;
104
105 // Mark database as not open as of yet
106 dbIsOpen = FALSE;
107
108 } // wxDB::wxDB()
109
110 /********** wxDB::Open() **********/
111 bool wxDB::Open(char *Dsn, char *Uid, char *AuthStr)
112 {
113 assert(Dsn);
114 dsn = Dsn;
115 uid = Uid;
116 authStr = AuthStr;
117
118 #ifndef FWD_ONLY_CURSORS
119
120 RETCODE retcode;
121
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);
125
126 #ifdef _CONSOLE
127 if (retcode == SQL_SUCCESS)
128 cout << "SQLSetConnectOption(CURSOR_LIB) successful" << endl;
129 else
130 cout << "SQLSetConnectOption(CURSOR_LIB) failed" << endl;
131 #endif
132
133 #endif
134
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));
140
141 // Mark database as open
142 dbIsOpen = TRUE;
143
144 // Allocate a statement handle for the database connection
145 if (SQLAllocStmt(hdbc, &hstmt) != SQL_SUCCESS)
146 return(DispAllErrors(henv, hdbc));
147
148 // Set Connection Options
149 if (! setConnectionOptions())
150 return(FALSE);
151
152 // Query the data source for inf. about itself
153 if (! getDbInfo())
154 return(FALSE);
155
156 // Query the data source regarding data type information
157
158 //
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:
166 //
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
187 //
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
196
197 // VARCHAR = Variable length character string
198 if (! getDataTypeInfo(SQL_VARCHAR, typeInfVarchar))
199 if (! getDataTypeInfo(SQL_CHAR, typeInfVarchar))
200 return(FALSE);
201 else
202 typeInfVarchar.FsqlType = SQL_CHAR;
203 else
204 typeInfVarchar.FsqlType = SQL_VARCHAR;
205
206 // Float
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))
212 return(FALSE);
213 else
214 typeInfFloat.FsqlType = SQL_NUMERIC;
215 else
216 typeInfFloat.FsqlType = SQL_DECIMAL;
217 else
218 typeInfFloat.FsqlType = SQL_FLOAT;
219 else
220 typeInfFloat.FsqlType = SQL_REAL;
221 else
222 typeInfFloat.FsqlType = SQL_DOUBLE;
223
224 // Integer
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))
229 return(FALSE);
230 else
231 typeInfInteger.FsqlType = typeInfFloat.FsqlType;
232 else
233 typeInfInteger.FsqlType = SQL_INTEGER;
234
235 // Date/Time
236 if (! getDataTypeInfo(SQL_TIMESTAMP, typeInfDate))
237 return(FALSE);
238 else
239 typeInfDate.FsqlType = SQL_TIMESTAMP;
240
241 #ifdef _CONSOLE
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;
246 cout << endl;
247 #endif
248
249 // Completed Successfully
250 return(TRUE);
251
252 } // wxDB::Open()
253
254 // The Intersolv/Oracle 7 driver was "Not Capable" of setting the login timeout.
255
256 /********** wxDB::setConnectionOptions() **********/
257 bool wxDB::setConnectionOptions(void)
258 {
259 SQLSetConnectOption(hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
260 SQLSetConnectOption(hdbc, SQL_OPT_TRACE, SQL_OPT_TRACE_OFF);
261
262 // Display the connection options to verify them
263 #ifdef _CONSOLE
264 long l;
265 cout << ">>>>> CONNECTION OPTIONS <<<<<<" << endl;
266
267 if (SQLGetConnectOption(hdbc, SQL_AUTOCOMMIT, &l) != SQL_SUCCESS)
268 return(DispAllErrors(henv, hdbc));
269 cout << "AUTOCOMMIT: " << (l == SQL_AUTOCOMMIT_OFF ? "OFF" : "ON") << endl;
270
271 if (SQLGetConnectOption(hdbc, SQL_ODBC_CURSORS, &l) != SQL_SUCCESS)
272 return(DispAllErrors(henv, hdbc));
273 cout << "ODBC CURSORS: ";
274 switch(l)
275 {
276 case(SQL_CUR_USE_IF_NEEDED):
277 cout << "SQL_CUR_USE_IF_NEEDED";
278 break;
279 case(SQL_CUR_USE_ODBC):
280 cout << "SQL_CUR_USE_ODBC";
281 break;
282 case(SQL_CUR_USE_DRIVER):
283 cout << "SQL_CUR_USE_DRIVER";
284 break;
285 }
286 cout << endl;
287
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;
291
292 cout << endl;
293 #endif
294
295 // Completed Successfully
296 return(TRUE);
297
298 } // wxDB::setConnectionOptions()
299
300 /********** wxDB::getDbInfo() **********/
301 bool wxDB::getDbInfo(void)
302 {
303 SWORD cb;
304
305 if (SQLGetInfo(hdbc, SQL_SERVER_NAME, dbInf.serverName, 40, &cb) != SQL_SUCCESS)
306 return(DispAllErrors(henv, hdbc));
307
308 if (SQLGetInfo(hdbc, SQL_DATABASE_NAME, dbInf.databaseName, 128, &cb) != SQL_SUCCESS)
309 return(DispAllErrors(henv, hdbc));
310
311 if (SQLGetInfo(hdbc, SQL_DBMS_NAME, dbInf.dbmsName, 40, &cb) != SQL_SUCCESS)
312 return(DispAllErrors(henv, hdbc));
313
314 if (SQLGetInfo(hdbc, SQL_DBMS_VER, dbInf.dbmsVer, 20, &cb) != SQL_SUCCESS)
315 return(DispAllErrors(henv, hdbc));
316
317 if (SQLGetInfo(hdbc, SQL_ACTIVE_CONNECTIONS, &dbInf.maxConnections, sizeof(dbInf.maxConnections), &cb) != SQL_SUCCESS)
318 return(DispAllErrors(henv, hdbc));
319
320 if (SQLGetInfo(hdbc, SQL_ACTIVE_STATEMENTS, &dbInf.maxStmts, sizeof(dbInf.maxStmts), &cb) != SQL_SUCCESS)
321 return(DispAllErrors(henv, hdbc));
322
323 if (SQLGetInfo(hdbc, SQL_DRIVER_NAME, dbInf.driverName, 40, &cb) != SQL_SUCCESS)
324 return(DispAllErrors(henv, hdbc));
325
326 if (SQLGetInfo(hdbc, SQL_DRIVER_ODBC_VER, dbInf.odbcVer, 20, &cb) != SQL_SUCCESS)
327 return(DispAllErrors(henv, hdbc));
328
329 if (SQLGetInfo(hdbc, SQL_ODBC_VER, dbInf.drvMgrOdbcVer, 20, &cb) != SQL_SUCCESS)
330 return(DispAllErrors(henv, hdbc));
331
332 if (SQLGetInfo(hdbc, SQL_DRIVER_VER, dbInf.driverVer, 40, &cb) != SQL_SUCCESS)
333 return(DispAllErrors(henv, hdbc));
334
335 if (SQLGetInfo(hdbc, SQL_ODBC_API_CONFORMANCE, &dbInf.apiConfLvl, sizeof(dbInf.apiConfLvl), &cb) != SQL_SUCCESS)
336 return(DispAllErrors(henv, hdbc));
337
338 if (SQLGetInfo(hdbc, SQL_ODBC_SAG_CLI_CONFORMANCE, &dbInf.cliConfLvl, sizeof(dbInf.cliConfLvl), &cb) != SQL_SUCCESS)
339 return(DispAllErrors(henv, hdbc));
340
341 if (SQLGetInfo(hdbc, SQL_ODBC_SQL_CONFORMANCE, &dbInf.sqlConfLvl, sizeof(dbInf.sqlConfLvl), &cb) != SQL_SUCCESS)
342 return(DispAllErrors(henv, hdbc));
343
344 if (SQLGetInfo(hdbc, SQL_OUTER_JOINS, dbInf.outerJoins, 2, &cb) != SQL_SUCCESS)
345 return(DispAllErrors(henv, hdbc));
346
347 if (SQLGetInfo(hdbc, SQL_PROCEDURES, dbInf.procedureSupport, 2, &cb) != SQL_SUCCESS)
348 return(DispAllErrors(henv, hdbc));
349
350 if (SQLGetInfo(hdbc, SQL_CURSOR_COMMIT_BEHAVIOR, &dbInf.cursorCommitBehavior, sizeof(dbInf.cursorCommitBehavior), &cb) != SQL_SUCCESS)
351 return(DispAllErrors(henv, hdbc));
352
353 if (SQLGetInfo(hdbc, SQL_CURSOR_ROLLBACK_BEHAVIOR, &dbInf.cursorRollbackBehavior, sizeof(dbInf.cursorRollbackBehavior), &cb) != SQL_SUCCESS)
354 return(DispAllErrors(henv, hdbc));
355
356 if (SQLGetInfo(hdbc, SQL_NON_NULLABLE_COLUMNS, &dbInf.supportNotNullClause, sizeof(dbInf.supportNotNullClause), &cb) != SQL_SUCCESS)
357 return(DispAllErrors(henv, hdbc));
358
359 if (SQLGetInfo(hdbc, SQL_ODBC_SQL_OPT_IEF, dbInf.supportIEF, 2, &cb) != SQL_SUCCESS)
360 return(DispAllErrors(henv, hdbc));
361
362 if (SQLGetInfo(hdbc, SQL_DEFAULT_TXN_ISOLATION, &dbInf.txnIsolation, sizeof(dbInf.txnIsolation), &cb) != SQL_SUCCESS)
363 return(DispAllErrors(henv, hdbc));
364
365 if (SQLGetInfo(hdbc, SQL_TXN_ISOLATION_OPTION, &dbInf.txnIsolationOptions, sizeof(dbInf.txnIsolationOptions), &cb) != SQL_SUCCESS)
366 return(DispAllErrors(henv, hdbc));
367
368 if (SQLGetInfo(hdbc, SQL_FETCH_DIRECTION, &dbInf.fetchDirections, sizeof(dbInf.fetchDirections), &cb) != SQL_SUCCESS)
369 return(DispAllErrors(henv, hdbc));
370
371 if (SQLGetInfo(hdbc, SQL_LOCK_TYPES, &dbInf.lockTypes, sizeof(dbInf.lockTypes), &cb) != SQL_SUCCESS)
372 return(DispAllErrors(henv, hdbc));
373
374 if (SQLGetInfo(hdbc, SQL_POS_OPERATIONS, &dbInf.posOperations, sizeof(dbInf.posOperations), &cb) != SQL_SUCCESS)
375 return(DispAllErrors(henv, hdbc));
376
377 if (SQLGetInfo(hdbc, SQL_POSITIONED_STATEMENTS, &dbInf.posStmts, sizeof(dbInf.posStmts), &cb) != SQL_SUCCESS)
378 return(DispAllErrors(henv, hdbc));
379
380 if (SQLGetInfo(hdbc, SQL_SCROLL_CONCURRENCY, &dbInf.scrollConcurrency, sizeof(dbInf.scrollConcurrency), &cb) != SQL_SUCCESS)
381 return(DispAllErrors(henv, hdbc));
382
383 if (SQLGetInfo(hdbc, SQL_SCROLL_OPTIONS, &dbInf.scrollOptions, sizeof(dbInf.scrollOptions), &cb) != SQL_SUCCESS)
384 return(DispAllErrors(henv, hdbc));
385
386 if (SQLGetInfo(hdbc, SQL_STATIC_SENSITIVITY, &dbInf.staticSensitivity, sizeof(dbInf.staticSensitivity), &cb) != SQL_SUCCESS)
387 return(DispAllErrors(henv, hdbc));
388
389 if (SQLGetInfo(hdbc, SQL_TXN_CAPABLE, &dbInf.txnCapable, sizeof(dbInf.txnCapable), &cb) != SQL_SUCCESS)
390 return(DispAllErrors(henv, hdbc));
391
392 if (SQLGetInfo(hdbc, SQL_LOGIN_TIMEOUT, &dbInf.loginTimeout, sizeof(dbInf.loginTimeout), &cb) != SQL_SUCCESS)
393 return(DispAllErrors(henv, hdbc));
394
395 #ifdef _CONSOLE
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;
400
401 cout << "API Conf. Level: ";
402 switch(dbInf.apiConfLvl)
403 {
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;
407 }
408 cout << endl;
409
410 cout << "SAG CLI Conf. Level: ";
411 switch(dbInf.cliConfLvl)
412 {
413 case SQL_OSCC_NOT_COMPLIANT: cout << "Not Compliant"; break;
414 case SQL_OSCC_COMPLIANT: cout << "Compliant"; break;
415 }
416 cout << endl;
417
418 cout << "SQL Conf. Level: ";
419 switch(dbInf.sqlConfLvl)
420 {
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;
424 }
425 cout << endl;
426
427 cout << "Max. Connections: " << dbInf.maxConnections << endl;
428 cout << "Outer Joins: " << dbInf.outerJoins << endl;
429 cout << "Support for Procedures: " << dbInf.procedureSupport << endl;
430
431 cout << "Cursor COMMIT Behavior: ";
432 switch(dbInf.cursorCommitBehavior)
433 {
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;
437 }
438 cout << endl;
439
440 cout << "Cursor ROLLBACK Behavior: ";
441 switch(dbInf.cursorRollbackBehavior)
442 {
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;
446 }
447 cout << endl;
448
449 cout << "Support NOT NULL clause: ";
450 switch(dbInf.supportNotNullClause)
451 {
452 case SQL_NNC_NULL: cout << "No"; break;
453 case SQL_NNC_NON_NULL: cout << "Yes"; break;
454 }
455 cout << endl;
456
457 cout << "Support IEF (Ref. Integrity): " << dbInf.supportIEF << endl;
458 cout << "Login Timeout: " << dbInf.loginTimeout << endl;
459
460 cout << endl << endl << "more ..." << endl;
461 getchar();
462
463 cout << "Default Transaction Isolation: ";
464 switch(dbInf.txnIsolation)
465 {
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;
470 #ifdef ODBC_V20
471 case SQL_TXN_VERSIONING: cout << "Versioning"; break;
472 #endif
473 }
474 cout << endl;
475
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, ";
485 #ifdef ODBC_V20
486 if (dbInf.txnIsolationOptions & SQL_TXN_VERSIONING)
487 cout << "Versioning";
488 #endif
489 cout << endl;
490
491 cout << "Fetch Directions Supported:" << endl << " ";
492 if (dbInf.fetchDirections & SQL_FD_FETCH_NEXT)
493 cout << "Next, ";
494 if (dbInf.fetchDirections & SQL_FD_FETCH_PRIOR)
495 cout << "Prev, ";
496 if (dbInf.fetchDirections & SQL_FD_FETCH_FIRST)
497 cout << "First, ";
498 if (dbInf.fetchDirections & SQL_FD_FETCH_LAST)
499 cout << "Last, ";
500 if (dbInf.fetchDirections & SQL_FD_FETCH_ABSOLUTE)
501 cout << "Absolute, ";
502 if (dbInf.fetchDirections & SQL_FD_FETCH_RELATIVE)
503 cout << "Relative, ";
504 #ifdef ODBC_V20
505 if (dbInf.fetchDirections & SQL_FD_FETCH_RESUME)
506 cout << "Resume, ";
507 #endif
508 if (dbInf.fetchDirections & SQL_FD_FETCH_BOOKMARK)
509 cout << "Bookmark";
510 cout << endl;
511
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)
518 cout << "UnLock";
519 cout << endl;
520
521 cout << "Position Operations Supported (SQLSetPos): ";
522 if (dbInf.posOperations & SQL_POS_POSITION)
523 cout << "Position, ";
524 if (dbInf.posOperations & SQL_POS_REFRESH)
525 cout << "Refresh, ";
526 if (dbInf.posOperations & SQL_POS_UPDATE)
527 cout << "Upd, ";
528 if (dbInf.posOperations & SQL_POS_DELETE)
529 cout << "Del, ";
530 if (dbInf.posOperations & SQL_POS_ADD)
531 cout << "Add";
532 cout << endl;
533
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";
541 cout << endl;
542
543 cout << "Scroll Concurrency: ";
544 if (dbInf.scrollConcurrency & SQL_SCCO_READ_ONLY)
545 cout << "Read Only, ";
546 if (dbInf.scrollConcurrency & SQL_SCCO_LOCK)
547 cout << "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";
552 cout << endl;
553
554 cout << "Scroll Options: ";
555 if (dbInf.scrollOptions & SQL_SO_FORWARD_ONLY)
556 cout << "Fwd Only, ";
557 if (dbInf.scrollOptions & SQL_SO_STATIC)
558 cout << "Static, ";
559 if (dbInf.scrollOptions & SQL_SO_KEYSET_DRIVEN)
560 cout << "Keyset Driven, ";
561 if (dbInf.scrollOptions & SQL_SO_DYNAMIC)
562 cout << "Dynamic, ";
563 if (dbInf.scrollOptions & SQL_SO_MIXED)
564 cout << "Mixed";
565 cout << endl;
566
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)
573 cout << "Updates";
574 cout << endl;
575
576 cout << "Transaction Capable?: ";
577 switch(dbInf.txnCapable)
578 {
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;
584 }
585 cout << endl;
586
587 cout << endl;
588
589 #endif
590
591 // Completed Successfully
592 return(TRUE);
593
594 } // wxDB::getDbInfo()
595
596 /********** wxDB::getDataTypeInfo() **********/
597 bool wxDB::getDataTypeInfo(SWORD fSqlType, SqlTypeInfo &structSQLTypeInfo)
598 {
599 // fSqlType will be something like SQL_VARCHAR. This parameter determines
600 // the data type inf. is gathered for.
601 //
602 // SqlTypeInfo is a structure that is filled in with data type information,
603
604 RETCODE retcode;
605 SDWORD cbRet;
606
607 // Get information about the data type specified
608 if (SQLGetTypeInfo(hstmt, fSqlType) != SQL_SUCCESS)
609 return(DispAllErrors(henv, hdbc, hstmt));
610 // Fetch the record
611 if ((retcode = SQLFetch(hstmt)) != SQL_SUCCESS)
612 {
613 #ifdef _CONSOLE
614 if (retcode == SQL_NO_DATA_FOUND)
615 cout << "SQL_NO_DATA_FOUND fetching inf. about data type." << endl;
616 #endif
617 DispAllErrors(henv, hdbc, hstmt);
618 SQLFreeStmt(hstmt, SQL_CLOSE);
619 return(FALSE);
620 }
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));
632
633 if (structSQLTypeInfo.MaximumScale < 0)
634 structSQLTypeInfo.MaximumScale = 0;
635
636 // Close the statement handle which closes open cursors
637 if (SQLFreeStmt(hstmt, SQL_CLOSE) != SQL_SUCCESS)
638 return(DispAllErrors(henv, hdbc, hstmt));
639
640 // Completed Successfully
641 return(TRUE);
642
643 } // wxDB::getDataTypeInfo()
644
645 /********** wxDB::Close() **********/
646 void wxDB::Close(void)
647 {
648 // Free statement handle
649 if (dbIsOpen)
650 {
651 if (SQLFreeStmt(hstmt, SQL_DROP) != SQL_SUCCESS)
652 DispAllErrors(henv, hdbc);
653 }
654
655 // Disconnect from the datasource
656 if (SQLDisconnect(hdbc) != SQL_SUCCESS)
657 DispAllErrors(henv, hdbc);
658
659 // Free the connection to the datasource
660 if (SQLFreeConnect(hdbc) != SQL_SUCCESS)
661 DispAllErrors(henv, hdbc);
662
663 } // wxDB::Close()
664
665 /********** wxDB::CommitTrans() **********/
666 bool wxDB::CommitTrans(void)
667 {
668 // Commit the transaction
669 if (SQLTransact(henv, hdbc, SQL_COMMIT) != SQL_SUCCESS)
670 return(DispAllErrors(henv, hdbc));
671
672 // Completed successfully
673 return(TRUE);
674
675 } // wxDB::CommitTrans()
676
677 /********** wxDB::RollbackTrans() **********/
678 bool wxDB::RollbackTrans(void)
679 {
680 // Rollback the transaction
681 if (SQLTransact(henv, hdbc, SQL_ROLLBACK) != SQL_SUCCESS)
682 return(DispAllErrors(henv, hdbc));
683
684 // Completed successfully
685 return(TRUE);
686
687 } // wxDB::RollbackTrans()
688
689 /********** wxDB::DispAllErrors() **********/
690 bool wxDB::DispAllErrors(HENV aHenv, HDBC aHdbc, HSTMT aHstmt)
691 {
692 char odbcErrMsg[DB_MAX_ERROR_MSG_LEN];
693
694 while (SQLError(aHenv, aHdbc, aHstmt, (UCHAR FAR *) sqlState, &nativeError, (UCHAR FAR *) errorMsg, SQL_MAX_MESSAGE_LENGTH - 1, &cbErrorMsg) == SQL_SUCCESS)
695 {
696 sprintf(odbcErrMsg, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState, nativeError, errorMsg);
697 logError(odbcErrMsg, sqlState);
698 if (!silent)
699 {
700 #ifdef _CONSOLE
701 // When run in console mode, use standard out to display errors.
702 cout << odbcErrMsg << endl;
703 cout << "Press any key to continue..." << endl;
704 getchar();
705 #endif
706 }
707 }
708
709 return(FALSE); // This function alway's returns false.
710
711 } // wxDB::DispAllErrors()
712
713 /********** wxDB::GetNextError() **********/
714 bool wxDB::GetNextError(HENV aHenv, HDBC aHdbc, HSTMT aHstmt)
715 {
716 if (SQLError(aHenv, aHdbc, aHstmt, (UCHAR FAR *) sqlState, &nativeError, (UCHAR FAR *) errorMsg, SQL_MAX_MESSAGE_LENGTH - 1, &cbErrorMsg) == SQL_SUCCESS)
717 return(TRUE);
718 else
719 return(FALSE);
720
721 } // wxDB::GetNextError()
722
723 /********** wxDB::DispNextError() **********/
724 void wxDB::DispNextError(void)
725 {
726 char odbcErrMsg[DB_MAX_ERROR_MSG_LEN];
727
728 sprintf(odbcErrMsg, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState, nativeError, errorMsg);
729 logError(odbcErrMsg, sqlState);
730
731 if (silent)
732 return;
733
734 #ifdef _CONSOLE
735 // When run in console mode, use standard out to display errors.
736 cout << odbcErrMsg << endl;
737 cout << "Press any key to continue..." << endl;
738 getchar();
739 #endif
740
741 } // wxDB::DispNextError()
742
743 /********** wxDB::logError() **********/
744 void wxDB::logError(char *errMsg, char *SQLState)
745 {
746 assert(errMsg && strlen(errMsg));
747
748 static int pLast = -1;
749 int dbStatus;
750
751 if (++pLast == DB_MAX_ERROR_HISTORY)
752 {
753 for (int i = 0; i < DB_MAX_ERROR_HISTORY; i++)
754 strcpy(errorList[i], errorList[i+1]);
755 pLast--;
756 }
757
758 strcpy(errorList[pLast], errMsg);
759
760 if (SQLState && strlen(SQLState))
761 if ((dbStatus = TranslateSqlState(SQLState)) != DB_ERR_FUNCTION_SEQUENCE_ERROR)
762 DB_STATUS = dbStatus;
763
764 } // wxDB::logError()
765
766 /**********wxDB::TranslateSqlState() **********/
767 int wxDB::TranslateSqlState(char *SQLState)
768 {
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);
947
948 // No match
949 return(0);
950
951 } // wxDB::TranslateSqlState()
952
953 /********** wxDB::Grant() **********/
954 bool wxDB::Grant(int privileges, char *tableName, char *userList)
955 {
956 char sqlStmt[DB_MAX_STATEMENT_LEN];
957
958 // Build the grant statement
959 strcpy(sqlStmt, "GRANT ");
960 if (privileges == DB_GRANT_ALL)
961 strcat(sqlStmt, "ALL");
962 else
963 {
964 int c = 0;
965 if (privileges & DB_GRANT_SELECT)
966 {
967 strcat(sqlStmt, "SELECT");
968 c++;
969 }
970 if (privileges & DB_GRANT_INSERT)
971 {
972 if (c++)
973 strcat(sqlStmt, ", ");
974 strcat(sqlStmt, "INSERT");
975 }
976 if (privileges & DB_GRANT_UPDATE)
977 {
978 if (c++)
979 strcat(sqlStmt, ", ");
980 strcat(sqlStmt, "UPDATE");
981 }
982 if (privileges & DB_GRANT_DELETE)
983 {
984 if (c++)
985 strcat(sqlStmt, ", ");
986 strcat(sqlStmt, "DELETE");
987 }
988 }
989
990 strcat(sqlStmt, " ON ");
991 strcat(sqlStmt, tableName);
992 strcat(sqlStmt, " TO ");
993 strcat(sqlStmt, userList);
994
995 #ifdef _CONSOLE
996 cout << endl << sqlStmt << endl;
997 #endif
998
999 return(ExecSql(sqlStmt));
1000
1001 } // wxDB::Grant()
1002
1003 /********** wxDB::CreateView() **********/
1004 bool wxDB::CreateView(char *viewName, char *colList, char *pSqlStmt)
1005 {
1006 char sqlStmt[DB_MAX_STATEMENT_LEN];
1007
1008 // Drop the view first
1009 sprintf(sqlStmt, "DROP VIEW %s", viewName);
1010 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
1011 {
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"))
1018 {
1019 DispNextError();
1020 DispAllErrors(henv, hdbc, hstmt);
1021 RollbackTrans();
1022 return(FALSE);
1023 }
1024 }
1025
1026 #ifdef _CONSOLE
1027 cout << endl << sqlStmt << endl;
1028 #endif
1029
1030 // Build the create view statement
1031 strcpy(sqlStmt, "CREATE VIEW ");
1032 strcat(sqlStmt, viewName);
1033
1034 if (strlen(colList))
1035 {
1036 strcat(sqlStmt, " (");
1037 strcat(sqlStmt, colList);
1038 strcat(sqlStmt, ")");
1039 }
1040
1041 strcat(sqlStmt, " AS ");
1042 strcat(sqlStmt, pSqlStmt);
1043
1044 #ifdef _CONSOLE
1045 cout << sqlStmt << endl;
1046 #endif
1047
1048 return(ExecSql(sqlStmt));
1049
1050 } // wxDB::CreateView()
1051
1052 /********** wxDB::ExecSql() **********/
1053 bool wxDB::ExecSql(char *pSqlStmt)
1054 {
1055 if (SQLExecDirect(hstmt, (UCHAR FAR *) pSqlStmt, SQL_NTS) == SQL_SUCCESS)
1056 return(TRUE);
1057 else
1058 {
1059 DispAllErrors(henv, hdbc, hstmt);
1060 return(FALSE);
1061 }
1062
1063 } // wxDB::ExecSql()
1064
1065 /********** wxDB::GetColumns() **********/
1066 /*
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.
1073 *
1074 * CcolInf *colInf = pDb->GetColumns(tableList);
1075 * if (colInf)
1076 * {
1077 * // Use the column inf
1078 * .......
1079 * // Destroy the memory
1080 * delete [] colInf;
1081 * }
1082 */
1083 CcolInf *wxDB::GetColumns(char *tableName[])
1084 {
1085 UINT noCols = 0;
1086 UINT colNo = 0;
1087 CcolInf *colInf = 0;
1088 RETCODE retcode;
1089 SDWORD cb;
1090 char tblName[DB_MAX_TABLE_NAME_LEN+1];
1091 char colName[DB_MAX_COLUMN_NAME_LEN+1];
1092 SWORD sqlDataType;
1093
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++)
1098 {
1099 if (pass == 2)
1100 {
1101 if (noCols == 0) // Probably a bogus table name(s)
1102 break;
1103 // Allocate n CcolInf objects to hold the column information
1104 colInf = new CcolInf[noCols+1];
1105 if (!colInf)
1106 break;
1107 // Mark the end of the array
1108 strcpy(colInf[noCols].tableName, "");
1109 strcpy(colInf[noCols].colName, "");
1110 colInf[noCols].sqlDataType = 0;
1111 }
1112 // Loop through each table name
1113 for (int tbl = 0; tableName[tbl]; tbl++)
1114 {
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);
1124 if (colInf)
1125 delete [] colInf;
1126 return(0);
1127 }
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)
1132 {
1133 if (pass == 1) // First pass, just add up the number of columns
1134 noCols++;
1135 else // Pass 2; Fill in the array of structures
1136 {
1137 if (colNo < noCols) // Some extra error checking to prevent memory overwrites
1138 {
1139 strcpy(colInf[colNo].tableName, tblName);
1140 strcpy(colInf[colNo].colName, colName);
1141 colInf[colNo].sqlDataType = sqlDataType;
1142 colNo++;
1143 }
1144 }
1145 }
1146 if (retcode != SQL_NO_DATA_FOUND)
1147 { // Error occured, abort
1148 DispAllErrors(henv, hdbc, hstmt);
1149 if (colInf)
1150 delete [] colInf;
1151 return(0);
1152 }
1153 }
1154 }
1155
1156 SQLFreeStmt(hstmt, SQL_CLOSE);
1157 return colInf;
1158
1159 } // wxDB::GetColumns()
1160
1161
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
1165 // on the table.
1166 bool wxDB::TableExists(char *tableName)
1167 {
1168 assert(tableName && strlen(tableName));
1169
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));
1178
1179 if (SQLFetch(hstmt) != SQL_SUCCESS)
1180 {
1181 SQLFreeStmt(hstmt, SQL_CLOSE);
1182 return(DispAllErrors(henv, hdbc, hstmt));
1183 }
1184
1185 SQLFreeStmt(hstmt, SQL_CLOSE);
1186 return(TRUE);
1187
1188 } // wxDB::TableExists()
1189
1190
1191 /********** GetDbConnection() **********/
1192 wxDB *GetDbConnection(DbStuff *pDbStuff)
1193 {
1194 DbList *pList;
1195
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)
1199 {
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
1203 {
1204 pList->Free = FALSE;
1205 return(pList->PtrDb);
1206 }
1207 }
1208
1209 // No available connections. A new connection must be made and
1210 // appended to the end of the linked list.
1211 if (PtrBegDbList)
1212 {
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;
1219 }
1220 else // Empty list
1221 {
1222 // Create the first node on the list
1223 pList = PtrBegDbList = new DbList;
1224 pList->PtrPrev = 0;
1225 }
1226
1227 // Initialize new node in the linked list
1228 pList->PtrNext = 0;
1229 pList->Free = FALSE;
1230 strcpy(pList->Dsn, pDbStuff->Dsn);
1231 pList->PtrDb = new wxDB(pDbStuff->Henv);
1232
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
1237 {
1238 if (pList->PtrPrev)
1239 pList->PtrPrev->PtrNext = 0;
1240 else
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
1246 return(0);
1247 }
1248
1249 } // GetDbConnection()
1250
1251 /********** FreeDbConnection() **********/
1252 bool FreeDbConnection(wxDB *pDb)
1253 {
1254 DbList *pList;
1255
1256 // Scan the linked list searching for the database connection
1257 for (pList = PtrBegDbList; pList; pList = pList->PtrNext)
1258 {
1259 if (pList->PtrDb == pDb) // Found it!!!
1260 return(pList->Free = TRUE);
1261 }
1262
1263 // Never found the database object, return failure
1264 return(FALSE);
1265
1266 } // FreeDbConnection()
1267
1268 /********** CloseDbConnections() **********/
1269 void CloseDbConnections(void)
1270 {
1271 DbList *pList, *pNext;
1272
1273 // Traverse the linked list closing database connections and freeing memory as I go.
1274 for (pList = PtrBegDbList; pList; pList = pNext)
1275 {
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
1281 }
1282
1283 // Mark the list as empty
1284 PtrBegDbList = 0;
1285
1286 } // CloseDbConnections()
1287
1288 /********** NumberDbConnectionsInUse() **********/
1289 int NumberDbConnectionsInUse(void)
1290 {
1291 DbList *pList;
1292 int cnt = 0;
1293
1294 // Scan the linked list counting db connections that are currently in use
1295 for (pList = PtrBegDbList; pList; pList = pList->PtrNext)
1296 {
1297 if (pList->Free == FALSE)
1298 cnt++;
1299 }
1300
1301 return(cnt);
1302
1303 } // NumberDbConnectionsInUse()
1304
1305 /********** GetDataSource() **********/
1306 bool GetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDescMax,
1307 UWORD direction)
1308 {
1309 SWORD cb;
1310
1311 if (SQLDataSources(henv, direction, (UCHAR FAR *) Dsn, DsnMax, &cb,
1312 (UCHAR FAR *) DsDesc, DsDescMax, &cb) == SQL_SUCCESS)
1313 return(TRUE);
1314 else
1315 return(FALSE);
1316
1317 } // GetDataSource()
1318
1319 #endif
1320 // USE_ODBC