]> git.saurik.com Git - wxWidgets.git/blob - src/common/db.cpp
Fixed compile errors.
[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 // Mods: Dec, 1998: Added support for SQL statement logging and database
9 // cataloging
10 // Created: 9.96
11 // RCS-ID: $Id$
12 // Copyright: (c) 1996 Remstar International, Inc.
13 // Licence: wxWindows licence, plus:
14 // Notice: This class library and its intellectual design are free of charge for use,
15 // modification, enhancement, debugging under the following conditions:
16 // 1) These classes may only be used as part of the implementation of a
17 // wxWindows-based application
18 // 2) All enhancements and bug fixes are to be submitted back to the wxWindows
19 // user groups free of all charges for use with the wxWindows library.
20 // 3) These classes may not be distributed as part of any other class library,
21 // DLL, text (written or electronic), other than a complete distribution of
22 // the wxWindows GUI development toolkit.
23 ///////////////////////////////////////////////////////////////////////////////
24
25 #ifdef __GNUG__
26 #pragma implementation "db.h"
27 #endif
28
29 /*
30 // SYNOPSIS START
31 // SYNOPSIS STOP
32 */
33
34 /*
35 #ifdef DBDEBUG_CONSOLE
36 #include <iostream.h>
37 #endif
38 */
39
40 #include "wx/wxprec.h"
41
42 #ifdef __BORLANDC__
43 #pragma hdrstop
44 #endif //__BORLANDC__
45
46 #ifndef WX_PRECOMP
47 #include <wx/wx.h>
48 #endif //WX_PRECOMP
49
50 #if wxUSE_ODBC
51
52 #include <stdio.h>
53 #include <string.h>
54 #include <assert.h>
55 #include <stdlib.h>
56 #include <ctype.h>
57 #include "wx/db.h"
58
59 DbList* WXDLLEXPORT PtrBegDbList = 0;
60
61 /********** wxDB Constructor **********/
62 wxDB::wxDB(HENV &aHenv)
63 {
64 int i;
65
66 fpSqlLog = 0; // Sql Log file pointer
67 sqlLogState = sqlLogOFF; // By default, logging is turned off
68
69 strcpy(sqlState,"");
70 strcpy(errorMsg,"");
71 nativeError = cbErrorMsg = 0;
72 for (i = 0; i < DB_MAX_ERROR_HISTORY; i++)
73 strcpy(errorList[i], "");
74
75 // Init typeInf structures
76 strcpy(typeInfVarchar.TypeName,"");
77 typeInfVarchar.FsqlType = 0;
78 typeInfVarchar.Precision = 0;
79 typeInfVarchar.CaseSensitive = 0;
80 typeInfVarchar.MaximumScale = 0;
81
82 strcpy(typeInfInteger.TypeName,"");
83 typeInfInteger.FsqlType = 0;
84 typeInfInteger.Precision = 0;
85 typeInfInteger.CaseSensitive = 0;
86 typeInfInteger.MaximumScale = 0;
87
88 strcpy(typeInfFloat.TypeName,"");
89 typeInfFloat.FsqlType = 0;
90 typeInfFloat.Precision = 0;
91 typeInfFloat.CaseSensitive = 0;
92 typeInfFloat.MaximumScale = 0;
93
94 strcpy(typeInfDate.TypeName,"");
95 typeInfDate.FsqlType = 0;
96 typeInfDate.Precision = 0;
97 typeInfDate.CaseSensitive = 0;
98 typeInfDate.MaximumScale = 0;
99
100 // Error reporting is turned OFF by default
101 silent = TRUE;
102
103 // Copy the HENV into the db class
104 henv = aHenv;
105
106 // Allocate a data source connection handle
107 if (SQLAllocConnect(henv, &hdbc) != SQL_SUCCESS)
108 DispAllErrors(henv);
109
110 // Initialize the db status flag
111 DB_STATUS = 0;
112
113 // Mark database as not open as of yet
114 dbIsOpen = FALSE;
115
116 } // wxDB::wxDB()
117
118 /********** wxDB::Open() **********/
119 bool wxDB::Open(char *Dsn, char *Uid, char *AuthStr)
120 {
121 assert(Dsn && strlen(Dsn));
122 dsn = Dsn;
123 uid = Uid;
124 authStr = AuthStr;
125
126 #ifndef FWD_ONLY_CURSORS
127
128 RETCODE retcode;
129
130 // Specify that the ODBC cursor library be used, if needed. This must be
131 // specified before the connection is made.
132 retcode = SQLSetConnectOption(hdbc, SQL_ODBC_CURSORS, SQL_CUR_USE_IF_NEEDED);
133
134 #ifdef DBDEBUG_CONSOLE
135 if (retcode == SQL_SUCCESS)
136 cout << "SQLSetConnectOption(CURSOR_LIB) successful" << endl;
137 else
138 cout << "SQLSetConnectOption(CURSOR_LIB) failed" << endl;
139 #endif
140
141 #endif
142
143 // Connect to the data source
144 if (SQLConnect(hdbc, (UCHAR FAR *) Dsn, SQL_NTS,
145 (UCHAR FAR *) Uid, SQL_NTS,
146 (UCHAR FAR *) AuthStr, SQL_NTS) != SQL_SUCCESS)
147 return(DispAllErrors(henv, hdbc));
148
149 // Mark database as open
150 dbIsOpen = TRUE;
151
152 // Allocate a statement handle for the database connection
153 if (SQLAllocStmt(hdbc, &hstmt) != SQL_SUCCESS)
154 return(DispAllErrors(henv, hdbc));
155
156 // Set Connection Options
157 if (! setConnectionOptions())
158 return(FALSE);
159
160 // Query the data source for inf. about itself
161 if (! getDbInfo())
162 return(FALSE);
163
164 // Query the data source regarding data type information
165
166 //
167 // The way I determined which SQL data types to use was by calling SQLGetInfo
168 // for all of the possible SQL data types to see which ones were supported. If
169 // a type is not supported, the SQLFetch() that's called from getDataTypeInfo()
170 // fails with SQL_NO_DATA_FOUND. This is ugly because I'm sure the three SQL data
171 // types I've selected below will not alway's be what we want. These are just
172 // what happened to work against an Oracle 7/Intersolv combination. The following is
173 // a complete list of the results I got back against the Oracle 7 database:
174 //
175 // SQL_BIGINT SQL_NO_DATA_FOUND
176 // SQL_BINARY SQL_NO_DATA_FOUND
177 // SQL_BIT SQL_NO_DATA_FOUND
178 // SQL_CHAR type name = 'CHAR', Precision = 255
179 // SQL_DATE SQL_NO_DATA_FOUND
180 // SQL_DECIMAL type name = 'NUMBER', Precision = 38
181 // SQL_DOUBLE type name = 'NUMBER', Precision = 15
182 // SQL_FLOAT SQL_NO_DATA_FOUND
183 // SQL_INTEGER SQL_NO_DATA_FOUND
184 // SQL_LONGVARBINARY type name = 'LONG RAW', Precision = 2 billion
185 // SQL_LONGVARCHAR type name = 'LONG', Precision = 2 billion
186 // SQL_NUMERIC SQL_NO_DATA_FOUND
187 // SQL_REAL SQL_NO_DATA_FOUND
188 // SQL_SMALLINT SQL_NO_DATA_FOUND
189 // SQL_TIME SQL_NO_DATA_FOUND
190 // SQL_TIMESTAMP type name = 'DATE', Precision = 19
191 // SQL_VARBINARY type name = 'RAW', Precision = 255
192 // SQL_VARCHAR type name = 'VARCHAR2', Precision = 2000
193 // =====================================================================
194 // Results from a Microsoft Access 7.0 db, using a driver from Microsoft
195 //
196 // SQL_VARCHAR type name = 'TEXT', Precision = 255
197 // SQL_TIMESTAMP type name = 'DATETIME'
198 // SQL_DECIMAL SQL_NO_DATA_FOUND
199 // SQL_NUMERIC type name = 'CURRENCY', Precision = 19
200 // SQL_FLOAT SQL_NO_DATA_FOUND
201 // SQL_REAL type name = 'SINGLE', Precision = 7
202 // SQL_DOUBLE type name = 'DOUBLE', Precision = 15
203 // SQL_INTEGER type name = 'LONG', Precision = 10
204
205 // VARCHAR = Variable length character string
206 if (! getDataTypeInfo(SQL_VARCHAR, typeInfVarchar))
207 if (! getDataTypeInfo(SQL_CHAR, typeInfVarchar))
208 return(FALSE);
209 else
210 typeInfVarchar.FsqlType = SQL_CHAR;
211 else
212 typeInfVarchar.FsqlType = SQL_VARCHAR;
213
214 // Float
215 if (! getDataTypeInfo(SQL_DOUBLE, typeInfFloat))
216 if (! getDataTypeInfo(SQL_REAL, typeInfFloat))
217 if (! getDataTypeInfo(SQL_FLOAT, typeInfFloat))
218 if (! getDataTypeInfo(SQL_DECIMAL, typeInfFloat))
219 if (! getDataTypeInfo(SQL_NUMERIC, typeInfFloat))
220 return(FALSE);
221 else
222 typeInfFloat.FsqlType = SQL_NUMERIC;
223 else
224 typeInfFloat.FsqlType = SQL_DECIMAL;
225 else
226 typeInfFloat.FsqlType = SQL_FLOAT;
227 else
228 typeInfFloat.FsqlType = SQL_REAL;
229 else
230 typeInfFloat.FsqlType = SQL_DOUBLE;
231
232 // Integer
233 if (! getDataTypeInfo(SQL_INTEGER, typeInfInteger))
234 // If SQL_INTEGER is not supported, use the floating point
235 // data type to store integers as well as floats
236 if (! getDataTypeInfo(typeInfFloat.FsqlType, typeInfInteger))
237 return(FALSE);
238 else
239 typeInfInteger.FsqlType = typeInfFloat.FsqlType;
240 else
241 typeInfInteger.FsqlType = SQL_INTEGER;
242
243 // Date/Time
244 if (! getDataTypeInfo(SQL_TIMESTAMP, typeInfDate))
245 return(FALSE);
246 else
247 typeInfDate.FsqlType = SQL_TIMESTAMP;
248
249 #ifdef DBDEBUG_CONSOLE
250 cout << "VARCHAR DATA TYPE: " << typeInfVarchar.TypeName << endl;
251 cout << "INTEGER DATA TYPE: " << typeInfInteger.TypeName << endl;
252 cout << "FLOAT DATA TYPE: " << typeInfFloat.TypeName << endl;
253 cout << "DATE DATA TYPE: " << typeInfDate.TypeName << endl;
254 cout << endl;
255 #endif
256
257 // Completed Successfully
258 return(TRUE);
259
260 } // wxDB::Open()
261
262 // The Intersolv/Oracle 7 driver was "Not Capable" of setting the login timeout.
263
264 /********** wxDB::setConnectionOptions() **********/
265 bool wxDB::setConnectionOptions(void)
266 {
267 SQLSetConnectOption(hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
268 SQLSetConnectOption(hdbc, SQL_OPT_TRACE, SQL_OPT_TRACE_OFF);
269
270 // Display the connection options to verify them
271 #ifdef DBDEBUG_CONSOLE
272 long l;
273 cout << ">>>>> CONNECTION OPTIONS <<<<<<" << endl;
274
275 if (SQLGetConnectOption(hdbc, SQL_AUTOCOMMIT, &l) != SQL_SUCCESS)
276 return(DispAllErrors(henv, hdbc));
277 cout << "AUTOCOMMIT: " << (l == SQL_AUTOCOMMIT_OFF ? "OFF" : "ON") << endl;
278
279 if (SQLGetConnectOption(hdbc, SQL_ODBC_CURSORS, &l) != SQL_SUCCESS)
280 return(DispAllErrors(henv, hdbc));
281 cout << "ODBC CURSORS: ";
282 switch(l)
283 {
284 case(SQL_CUR_USE_IF_NEEDED):
285 cout << "SQL_CUR_USE_IF_NEEDED";
286 break;
287 case(SQL_CUR_USE_ODBC):
288 cout << "SQL_CUR_USE_ODBC";
289 break;
290 case(SQL_CUR_USE_DRIVER):
291 cout << "SQL_CUR_USE_DRIVER";
292 break;
293 }
294 cout << endl;
295
296 if (SQLGetConnectOption(hdbc, SQL_OPT_TRACE, &l) != SQL_SUCCESS)
297 return(DispAllErrors(henv, hdbc));
298 cout << "TRACING: " << (l == SQL_OPT_TRACE_OFF ? "OFF" : "ON") << endl;
299
300 cout << endl;
301 #endif
302
303 // Completed Successfully
304 return(TRUE);
305
306 } // wxDB::setConnectionOptions()
307
308 /********** wxDB::getDbInfo() **********/
309 bool wxDB::getDbInfo(void)
310 {
311 SWORD cb;
312
313 if (SQLGetInfo(hdbc, SQL_SERVER_NAME, (UCHAR*) dbInf.serverName, 40, &cb) != SQL_SUCCESS)
314 return(DispAllErrors(henv, hdbc));
315
316 if (SQLGetInfo(hdbc, SQL_DATABASE_NAME, (UCHAR*) dbInf.databaseName, 128, &cb) != SQL_SUCCESS)
317 return(DispAllErrors(henv, hdbc));
318
319 if (SQLGetInfo(hdbc, SQL_DBMS_NAME, (UCHAR*) dbInf.dbmsName, 40, &cb) != SQL_SUCCESS)
320 return(DispAllErrors(henv, hdbc));
321
322 if (SQLGetInfo(hdbc, SQL_DBMS_VER, (UCHAR*) dbInf.dbmsVer, 40, &cb) != SQL_SUCCESS)
323 return(DispAllErrors(henv, hdbc));
324
325 if (SQLGetInfo(hdbc, SQL_ACTIVE_CONNECTIONS, (UCHAR*) &dbInf.maxConnections, sizeof(dbInf.maxConnections), &cb) != SQL_SUCCESS)
326 return(DispAllErrors(henv, hdbc));
327
328 if (SQLGetInfo(hdbc, SQL_ACTIVE_STATEMENTS, (UCHAR*) &dbInf.maxStmts, sizeof(dbInf.maxStmts), &cb) != SQL_SUCCESS)
329 return(DispAllErrors(henv, hdbc));
330
331 if (SQLGetInfo(hdbc, SQL_DRIVER_NAME, (UCHAR*) dbInf.driverName, 40, &cb) != SQL_SUCCESS)
332 return(DispAllErrors(henv, hdbc));
333
334 if (SQLGetInfo(hdbc, SQL_DRIVER_ODBC_VER, (UCHAR*) dbInf.odbcVer, 60, &cb) == SQL_ERROR)
335 return(DispAllErrors(henv, hdbc));
336
337 if (SQLGetInfo(hdbc, SQL_ODBC_VER, (UCHAR*) dbInf.drvMgrOdbcVer, 60, &cb) == SQL_ERROR)
338 return(DispAllErrors(henv, hdbc));
339
340 if (SQLGetInfo(hdbc, SQL_DRIVER_VER, (UCHAR*) dbInf.driverVer, 60, &cb) == SQL_ERROR)
341 return(DispAllErrors(henv, hdbc));
342
343 if (SQLGetInfo(hdbc, SQL_ODBC_API_CONFORMANCE, (UCHAR*) &dbInf.apiConfLvl, sizeof(dbInf.apiConfLvl), &cb) != SQL_SUCCESS)
344 return(DispAllErrors(henv, hdbc));
345
346 if (SQLGetInfo(hdbc, SQL_ODBC_SAG_CLI_CONFORMANCE, (UCHAR*) &dbInf.cliConfLvl, sizeof(dbInf.cliConfLvl), &cb) != SQL_SUCCESS)
347 return(DispAllErrors(henv, hdbc));
348
349 if (SQLGetInfo(hdbc, SQL_ODBC_SQL_CONFORMANCE, (UCHAR*) &dbInf.sqlConfLvl, sizeof(dbInf.sqlConfLvl), &cb) != SQL_SUCCESS)
350 return(DispAllErrors(henv, hdbc));
351
352 if (SQLGetInfo(hdbc, SQL_OUTER_JOINS, (UCHAR*) dbInf.outerJoins, 2, &cb) != SQL_SUCCESS)
353 return(DispAllErrors(henv, hdbc));
354
355 if (SQLGetInfo(hdbc, SQL_PROCEDURES, (UCHAR*) dbInf.procedureSupport, 2, &cb) != SQL_SUCCESS)
356 return(DispAllErrors(henv, hdbc));
357
358 if (SQLGetInfo(hdbc, SQL_CURSOR_COMMIT_BEHAVIOR, (UCHAR*) &dbInf.cursorCommitBehavior, sizeof(dbInf.cursorCommitBehavior), &cb) != SQL_SUCCESS)
359 return(DispAllErrors(henv, hdbc));
360
361 if (SQLGetInfo(hdbc, SQL_CURSOR_ROLLBACK_BEHAVIOR, (UCHAR*) &dbInf.cursorRollbackBehavior, sizeof(dbInf.cursorRollbackBehavior), &cb) != SQL_SUCCESS)
362 return(DispAllErrors(henv, hdbc));
363
364 if (SQLGetInfo(hdbc, SQL_NON_NULLABLE_COLUMNS, (UCHAR*) &dbInf.supportNotNullClause, sizeof(dbInf.supportNotNullClause), &cb) != SQL_SUCCESS)
365 return(DispAllErrors(henv, hdbc));
366
367 if (SQLGetInfo(hdbc, SQL_ODBC_SQL_OPT_IEF, (UCHAR*) dbInf.supportIEF, 2, &cb) != SQL_SUCCESS)
368 return(DispAllErrors(henv, hdbc));
369
370 if (SQLGetInfo(hdbc, SQL_DEFAULT_TXN_ISOLATION, (UCHAR*) &dbInf.txnIsolation, sizeof(dbInf.txnIsolation), &cb) != SQL_SUCCESS)
371 return(DispAllErrors(henv, hdbc));
372
373 if (SQLGetInfo(hdbc, SQL_TXN_ISOLATION_OPTION, (UCHAR*) &dbInf.txnIsolationOptions, sizeof(dbInf.txnIsolationOptions), &cb) != SQL_SUCCESS)
374 return(DispAllErrors(henv, hdbc));
375
376 if (SQLGetInfo(hdbc, SQL_FETCH_DIRECTION, (UCHAR*) &dbInf.fetchDirections, sizeof(dbInf.fetchDirections), &cb) != SQL_SUCCESS)
377 return(DispAllErrors(henv, hdbc));
378
379 if (SQLGetInfo(hdbc, SQL_LOCK_TYPES, (UCHAR*) &dbInf.lockTypes, sizeof(dbInf.lockTypes), &cb) != SQL_SUCCESS)
380 return(DispAllErrors(henv, hdbc));
381
382 if (SQLGetInfo(hdbc, SQL_POS_OPERATIONS, (UCHAR*) &dbInf.posOperations, sizeof(dbInf.posOperations), &cb) != SQL_SUCCESS)
383 return(DispAllErrors(henv, hdbc));
384
385 if (SQLGetInfo(hdbc, SQL_POSITIONED_STATEMENTS, (UCHAR*) &dbInf.posStmts, sizeof(dbInf.posStmts), &cb) != SQL_SUCCESS)
386 return(DispAllErrors(henv, hdbc));
387
388 if (SQLGetInfo(hdbc, SQL_SCROLL_CONCURRENCY, (UCHAR*) &dbInf.scrollConcurrency, sizeof(dbInf.scrollConcurrency), &cb) != SQL_SUCCESS)
389 return(DispAllErrors(henv, hdbc));
390
391 if (SQLGetInfo(hdbc, SQL_SCROLL_OPTIONS, (UCHAR*) &dbInf.scrollOptions, sizeof(dbInf.scrollOptions), &cb) != SQL_SUCCESS)
392 return(DispAllErrors(henv, hdbc));
393
394 if (SQLGetInfo(hdbc, SQL_STATIC_SENSITIVITY, (UCHAR*) &dbInf.staticSensitivity, sizeof(dbInf.staticSensitivity), &cb) != SQL_SUCCESS)
395 return(DispAllErrors(henv, hdbc));
396
397 if (SQLGetInfo(hdbc, SQL_TXN_CAPABLE, (UCHAR*) &dbInf.txnCapable, sizeof(dbInf.txnCapable), &cb) != SQL_SUCCESS)
398 return(DispAllErrors(henv, hdbc));
399
400 if (SQLGetInfo(hdbc, SQL_LOGIN_TIMEOUT, (UCHAR*) &dbInf.loginTimeout, sizeof(dbInf.loginTimeout), &cb) != SQL_SUCCESS)
401 return(DispAllErrors(henv, hdbc));
402
403 #ifdef DBDEBUG_CONSOLE
404 cout << ">>>>> DATA SOURCE INFORMATION <<<<<" << endl;
405 cout << "SERVER Name: " << dbInf.serverName << endl;
406 cout << "DBMS Name: " << dbInf.dbmsName << "; DBMS Version: " << dbInf.dbmsVer << endl;
407 cout << "ODBC Version: " << dbInf.odbcVer << "; Driver Version: " << dbInf.driverVer << endl;
408
409 cout << "API Conf. Level: ";
410 switch(dbInf.apiConfLvl)
411 {
412 case SQL_OAC_NONE: cout << "None"; break;
413 case SQL_OAC_LEVEL1: cout << "Level 1"; break;
414 case SQL_OAC_LEVEL2: cout << "Level 2"; break;
415 }
416 cout << endl;
417
418 cout << "SAG CLI Conf. Level: ";
419 switch(dbInf.cliConfLvl)
420 {
421 case SQL_OSCC_NOT_COMPLIANT: cout << "Not Compliant"; break;
422 case SQL_OSCC_COMPLIANT: cout << "Compliant"; break;
423 }
424 cout << endl;
425
426 cout << "SQL Conf. Level: ";
427 switch(dbInf.sqlConfLvl)
428 {
429 case SQL_OSC_MINIMUM: cout << "Minimum Grammer"; break;
430 case SQL_OSC_CORE: cout << "Core Grammer"; break;
431 case SQL_OSC_EXTENDED: cout << "Extended Grammer"; break;
432 }
433 cout << endl;
434
435 cout << "Max. Connections: " << dbInf.maxConnections << endl;
436 cout << "Outer Joins: " << dbInf.outerJoins << endl;
437 cout << "Support for Procedures: " << dbInf.procedureSupport << endl;
438
439 cout << "Cursor COMMIT Behavior: ";
440 switch(dbInf.cursorCommitBehavior)
441 {
442 case SQL_CB_DELETE: cout << "Delete cursors"; break;
443 case SQL_CB_CLOSE: cout << "Close cursors"; break;
444 case SQL_CB_PRESERVE: cout << "Preserve cursors"; break;
445 }
446 cout << endl;
447
448 cout << "Cursor ROLLBACK Behavior: ";
449 switch(dbInf.cursorRollbackBehavior)
450 {
451 case SQL_CB_DELETE: cout << "Delete cursors"; break;
452 case SQL_CB_CLOSE: cout << "Close cursors"; break;
453 case SQL_CB_PRESERVE: cout << "Preserve cursors"; break;
454 }
455 cout << endl;
456
457 cout << "Support NOT NULL clause: ";
458 switch(dbInf.supportNotNullClause)
459 {
460 case SQL_NNC_NULL: cout << "No"; break;
461 case SQL_NNC_NON_NULL: cout << "Yes"; break;
462 }
463 cout << endl;
464
465 cout << "Support IEF (Ref. Integrity): " << dbInf.supportIEF << endl;
466 cout << "Login Timeout: " << dbInf.loginTimeout << endl;
467
468 cout << endl << endl << "more ..." << endl;
469 getchar();
470
471 cout << "Default Transaction Isolation: ";
472 switch(dbInf.txnIsolation)
473 {
474 case SQL_TXN_READ_UNCOMMITTED: cout << "Read Uncommitted"; break;
475 case SQL_TXN_READ_COMMITTED: cout << "Read Committed"; break;
476 case SQL_TXN_REPEATABLE_READ: cout << "Repeatable Read"; break;
477 case SQL_TXN_SERIALIZABLE: cout << "Serializable"; break;
478 #ifdef ODBC_V20
479 case SQL_TXN_VERSIONING: cout << "Versioning"; break;
480 #endif
481 }
482 cout << endl;
483
484 cout << "Transaction Isolation Options: ";
485 if (dbInf.txnIsolationOptions & SQL_TXN_READ_UNCOMMITTED)
486 cout << "Read Uncommitted, ";
487 if (dbInf.txnIsolationOptions & SQL_TXN_READ_COMMITTED)
488 cout << "Read Committed, ";
489 if (dbInf.txnIsolationOptions & SQL_TXN_REPEATABLE_READ)
490 cout << "Repeatable Read, ";
491 if (dbInf.txnIsolationOptions & SQL_TXN_SERIALIZABLE)
492 cout << "Serializable, ";
493 #ifdef ODBC_V20
494 if (dbInf.txnIsolationOptions & SQL_TXN_VERSIONING)
495 cout << "Versioning";
496 #endif
497 cout << endl;
498
499 cout << "Fetch Directions Supported:" << endl << " ";
500 if (dbInf.fetchDirections & SQL_FD_FETCH_NEXT)
501 cout << "Next, ";
502 if (dbInf.fetchDirections & SQL_FD_FETCH_PRIOR)
503 cout << "Prev, ";
504 if (dbInf.fetchDirections & SQL_FD_FETCH_FIRST)
505 cout << "First, ";
506 if (dbInf.fetchDirections & SQL_FD_FETCH_LAST)
507 cout << "Last, ";
508 if (dbInf.fetchDirections & SQL_FD_FETCH_ABSOLUTE)
509 cout << "Absolute, ";
510 if (dbInf.fetchDirections & SQL_FD_FETCH_RELATIVE)
511 cout << "Relative, ";
512 #ifdef ODBC_V20
513 if (dbInf.fetchDirections & SQL_FD_FETCH_RESUME)
514 cout << "Resume, ";
515 #endif
516 if (dbInf.fetchDirections & SQL_FD_FETCH_BOOKMARK)
517 cout << "Bookmark";
518 cout << endl;
519
520 cout << "Lock Types Supported (SQLSetPos): ";
521 if (dbInf.lockTypes & SQL_LCK_NO_CHANGE)
522 cout << "No Change, ";
523 if (dbInf.lockTypes & SQL_LCK_EXCLUSIVE)
524 cout << "Exclusive, ";
525 if (dbInf.lockTypes & SQL_LCK_UNLOCK)
526 cout << "UnLock";
527 cout << endl;
528
529 cout << "Position Operations Supported (SQLSetPos): ";
530 if (dbInf.posOperations & SQL_POS_POSITION)
531 cout << "Position, ";
532 if (dbInf.posOperations & SQL_POS_REFRESH)
533 cout << "Refresh, ";
534 if (dbInf.posOperations & SQL_POS_UPDATE)
535 cout << "Upd, ";
536 if (dbInf.posOperations & SQL_POS_DELETE)
537 cout << "Del, ";
538 if (dbInf.posOperations & SQL_POS_ADD)
539 cout << "Add";
540 cout << endl;
541
542 cout << "Positioned Statements Supported: ";
543 if (dbInf.posStmts & SQL_PS_POSITIONED_DELETE)
544 cout << "Pos delete, ";
545 if (dbInf.posStmts & SQL_PS_POSITIONED_UPDATE)
546 cout << "Pos update, ";
547 if (dbInf.posStmts & SQL_PS_SELECT_FOR_UPDATE)
548 cout << "Select for update";
549 cout << endl;
550
551 cout << "Scroll Concurrency: ";
552 if (dbInf.scrollConcurrency & SQL_SCCO_READ_ONLY)
553 cout << "Read Only, ";
554 if (dbInf.scrollConcurrency & SQL_SCCO_LOCK)
555 cout << "Lock, ";
556 if (dbInf.scrollConcurrency & SQL_SCCO_OPT_ROWVER)
557 cout << "Opt. Rowver, ";
558 if (dbInf.scrollConcurrency & SQL_SCCO_OPT_VALUES)
559 cout << "Opt. Values";
560 cout << endl;
561
562 cout << "Scroll Options: ";
563 if (dbInf.scrollOptions & SQL_SO_FORWARD_ONLY)
564 cout << "Fwd Only, ";
565 if (dbInf.scrollOptions & SQL_SO_STATIC)
566 cout << "Static, ";
567 if (dbInf.scrollOptions & SQL_SO_KEYSET_DRIVEN)
568 cout << "Keyset Driven, ";
569 if (dbInf.scrollOptions & SQL_SO_DYNAMIC)
570 cout << "Dynamic, ";
571 if (dbInf.scrollOptions & SQL_SO_MIXED)
572 cout << "Mixed";
573 cout << endl;
574
575 cout << "Static Sensitivity: ";
576 if (dbInf.staticSensitivity & SQL_SS_ADDITIONS)
577 cout << "Additions, ";
578 if (dbInf.staticSensitivity & SQL_SS_DELETIONS)
579 cout << "Deletions, ";
580 if (dbInf.staticSensitivity & SQL_SS_UPDATES)
581 cout << "Updates";
582 cout << endl;
583
584 cout << "Transaction Capable?: ";
585 switch(dbInf.txnCapable)
586 {
587 case SQL_TC_NONE: cout << "No"; break;
588 case SQL_TC_DML: cout << "DML Only"; break;
589 case SQL_TC_DDL_COMMIT: cout << "DDL Commit"; break;
590 case SQL_TC_DDL_IGNORE: cout << "DDL Ignore"; break;
591 case SQL_TC_ALL: cout << "DDL & DML"; break;
592 }
593 cout << endl;
594
595 cout << endl;
596
597 #endif
598
599 // Completed Successfully
600 return(TRUE);
601
602 } // wxDB::getDbInfo()
603
604 /********** wxDB::getDataTypeInfo() **********/
605 bool wxDB::getDataTypeInfo(SWORD fSqlType, SqlTypeInfo &structSQLTypeInfo)
606 {
607 // fSqlType will be something like SQL_VARCHAR. This parameter determines
608 // the data type inf. is gathered for.
609 //
610 // SqlTypeInfo is a structure that is filled in with data type information,
611
612 RETCODE retcode;
613 SDWORD cbRet;
614
615 // Get information about the data type specified
616 if (SQLGetTypeInfo(hstmt, fSqlType) != SQL_SUCCESS)
617 return(DispAllErrors(henv, hdbc, hstmt));
618 // Fetch the record
619 if ((retcode = SQLFetch(hstmt)) != SQL_SUCCESS)
620 {
621 #ifdef DBDEBUG_CONSOLE
622 if (retcode == SQL_NO_DATA_FOUND)
623 cout << "SQL_NO_DATA_FOUND fetching inf. about data type." << endl;
624 #endif
625 DispAllErrors(henv, hdbc, hstmt);
626 SQLFreeStmt(hstmt, SQL_CLOSE);
627 return(FALSE);
628 }
629 // Obtain columns from the record
630 if (SQLGetData(hstmt, 1, SQL_C_CHAR, (UCHAR*) structSQLTypeInfo.TypeName, DB_TYPE_NAME_LEN, &cbRet) != SQL_SUCCESS)
631 return(DispAllErrors(henv, hdbc, hstmt));
632 if (SQLGetData(hstmt, 3, SQL_C_LONG, (UCHAR*) &structSQLTypeInfo.Precision, 0, &cbRet) != SQL_SUCCESS)
633 return(DispAllErrors(henv, hdbc, hstmt));
634 if (SQLGetData(hstmt, 8, SQL_C_SHORT, (UCHAR*) &structSQLTypeInfo.CaseSensitive, 0, &cbRet) != SQL_SUCCESS)
635 return(DispAllErrors(henv, hdbc, hstmt));
636 // if (SQLGetData(hstmt, 14, SQL_C_SHORT, (UCHAR*) &structSQLTypeInfo.MinimumScale, 0, &cbRet) != SQL_SUCCESS)
637 // return(DispAllErrors(henv, hdbc, hstmt));
638 if (SQLGetData(hstmt, 15, SQL_C_SHORT, (UCHAR*) &structSQLTypeInfo.MaximumScale, 0, &cbRet) != SQL_SUCCESS)
639 return(DispAllErrors(henv, hdbc, hstmt));
640
641 if (structSQLTypeInfo.MaximumScale < 0)
642 structSQLTypeInfo.MaximumScale = 0;
643
644 // Close the statement handle which closes open cursors
645 if (SQLFreeStmt(hstmt, SQL_CLOSE) != SQL_SUCCESS)
646 return(DispAllErrors(henv, hdbc, hstmt));
647
648 // Completed Successfully
649 return(TRUE);
650
651 } // wxDB::getDataTypeInfo()
652
653 /********** wxDB::Close() **********/
654 void wxDB::Close(void)
655 {
656 // Close the Sql Log file
657 if (fpSqlLog)
658 {
659 fclose(fpSqlLog);
660 fpSqlLog = 0; //glt
661 }
662
663 // Free statement handle
664 if (dbIsOpen)
665 {
666 if (SQLFreeStmt(hstmt, SQL_DROP) != SQL_SUCCESS)
667 DispAllErrors(henv, hdbc);
668 }
669
670 // Disconnect from the datasource
671 if (SQLDisconnect(hdbc) != SQL_SUCCESS)
672 DispAllErrors(henv, hdbc);
673
674 // Free the connection to the datasource
675 if (SQLFreeConnect(hdbc) != SQL_SUCCESS)
676 DispAllErrors(henv, hdbc);
677
678 } // wxDB::Close()
679
680 /********** wxDB::CommitTrans() **********/
681 bool wxDB::CommitTrans(void)
682 {
683 // Commit the transaction
684 if (SQLTransact(henv, hdbc, SQL_COMMIT) != SQL_SUCCESS)
685 return(DispAllErrors(henv, hdbc));
686
687 // Completed successfully
688 return(TRUE);
689
690 } // wxDB::CommitTrans()
691
692 /********** wxDB::RollbackTrans() **********/
693 bool wxDB::RollbackTrans(void)
694 {
695 // Rollback the transaction
696 if (SQLTransact(henv, hdbc, SQL_ROLLBACK) != SQL_SUCCESS)
697 return(DispAllErrors(henv, hdbc));
698
699 // Completed successfully
700 return(TRUE);
701
702 } // wxDB::RollbackTrans()
703
704 /********** wxDB::DispAllErrors() **********/
705 bool wxDB::DispAllErrors(HENV aHenv, HDBC aHdbc, HSTMT aHstmt)
706 {
707 char odbcErrMsg[DB_MAX_ERROR_MSG_LEN];
708
709 while (SQLError(aHenv, aHdbc, aHstmt, (UCHAR FAR *) sqlState, &nativeError, (UCHAR FAR *) errorMsg, SQL_MAX_MESSAGE_LENGTH - 1, &cbErrorMsg) == SQL_SUCCESS)
710 {
711 sprintf(odbcErrMsg, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState, nativeError, errorMsg);
712 logError(odbcErrMsg, sqlState);
713 if (!silent)
714 {
715 #ifdef DBDEBUG_CONSOLE
716 // When run in console mode, use standard out to display errors.
717 cout << odbcErrMsg << endl;
718 cout << "Press any key to continue..." << endl;
719 getchar();
720 #endif
721 }
722 }
723
724 return(FALSE); // This function alway's returns false.
725
726 } // wxDB::DispAllErrors()
727
728 /********** wxDB::GetNextError() **********/
729 bool wxDB::GetNextError(HENV aHenv, HDBC aHdbc, HSTMT aHstmt)
730 {
731 if (SQLError(aHenv, aHdbc, aHstmt, (UCHAR FAR *) sqlState, &nativeError, (UCHAR FAR *) errorMsg, SQL_MAX_MESSAGE_LENGTH - 1, &cbErrorMsg) == SQL_SUCCESS)
732 return(TRUE);
733 else
734 return(FALSE);
735
736 } // wxDB::GetNextError()
737
738 /********** wxDB::DispNextError() **********/
739 void wxDB::DispNextError(void)
740 {
741 char odbcErrMsg[DB_MAX_ERROR_MSG_LEN];
742
743 sprintf(odbcErrMsg, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState, nativeError, errorMsg);
744 logError(odbcErrMsg, sqlState);
745
746 if (silent)
747 return;
748
749 #ifdef DBDEBUG_CONSOLE
750 // When run in console mode, use standard out to display errors.
751 cout << odbcErrMsg << endl;
752 cout << "Press any key to continue..." << endl;
753 getchar();
754 #endif
755
756 } // wxDB::DispNextError()
757
758 /********** wxDB::logError() **********/
759 void wxDB::logError(char *errMsg, char *SQLState)
760 {
761 assert(errMsg && strlen(errMsg));
762
763 static int pLast = -1;
764 int dbStatus;
765
766 if (++pLast == DB_MAX_ERROR_HISTORY)
767 {
768 for (int i = 0; i < DB_MAX_ERROR_HISTORY; i++)
769 strcpy(errorList[i], errorList[i+1]);
770 pLast--;
771 }
772
773 strcpy(errorList[pLast], errMsg);
774
775 if (SQLState && strlen(SQLState))
776 if ((dbStatus = TranslateSqlState(SQLState)) != DB_ERR_FUNCTION_SEQUENCE_ERROR)
777 DB_STATUS = dbStatus;
778
779 } // wxDB::logError()
780
781 /**********wxDB::TranslateSqlState() **********/
782 int wxDB::TranslateSqlState(char *SQLState)
783 {
784 if (!strcmp(SQLState, "01000"))
785 return(DB_ERR_GENERAL_WARNING);
786 if (!strcmp(SQLState, "01002"))
787 return(DB_ERR_DISCONNECT_ERROR);
788 if (!strcmp(SQLState, "01004"))
789 return(DB_ERR_DATA_TRUNCATED);
790 if (!strcmp(SQLState, "01006"))
791 return(DB_ERR_PRIV_NOT_REVOKED);
792 if (!strcmp(SQLState, "01S00"))
793 return(DB_ERR_INVALID_CONN_STR_ATTR);
794 if (!strcmp(SQLState, "01S01"))
795 return(DB_ERR_ERROR_IN_ROW);
796 if (!strcmp(SQLState, "01S02"))
797 return(DB_ERR_OPTION_VALUE_CHANGED);
798 if (!strcmp(SQLState, "01S03"))
799 return(DB_ERR_NO_ROWS_UPD_OR_DEL);
800 if (!strcmp(SQLState, "01S04"))
801 return(DB_ERR_MULTI_ROWS_UPD_OR_DEL);
802 if (!strcmp(SQLState, "07001"))
803 return(DB_ERR_WRONG_NO_OF_PARAMS);
804 if (!strcmp(SQLState, "07006"))
805 return(DB_ERR_DATA_TYPE_ATTR_VIOL);
806 if (!strcmp(SQLState, "08001"))
807 return(DB_ERR_UNABLE_TO_CONNECT);
808 if (!strcmp(SQLState, "08002"))
809 return(DB_ERR_CONNECTION_IN_USE);
810 if (!strcmp(SQLState, "08003"))
811 return(DB_ERR_CONNECTION_NOT_OPEN);
812 if (!strcmp(SQLState, "08004"))
813 return(DB_ERR_REJECTED_CONNECTION);
814 if (!strcmp(SQLState, "08007"))
815 return(DB_ERR_CONN_FAIL_IN_TRANS);
816 if (!strcmp(SQLState, "08S01"))
817 return(DB_ERR_COMM_LINK_FAILURE);
818 if (!strcmp(SQLState, "21S01"))
819 return(DB_ERR_INSERT_VALUE_LIST_MISMATCH);
820 if (!strcmp(SQLState, "21S02"))
821 return(DB_ERR_DERIVED_TABLE_MISMATCH);
822 if (!strcmp(SQLState, "22001"))
823 return(DB_ERR_STRING_RIGHT_TRUNC);
824 if (!strcmp(SQLState, "22003"))
825 return(DB_ERR_NUMERIC_VALUE_OUT_OF_RNG);
826 if (!strcmp(SQLState, "22005"))
827 return(DB_ERR_ERROR_IN_ASSIGNMENT);
828 if (!strcmp(SQLState, "22008"))
829 return(DB_ERR_DATETIME_FLD_OVERFLOW);
830 if (!strcmp(SQLState, "22012"))
831 return(DB_ERR_DIVIDE_BY_ZERO);
832 if (!strcmp(SQLState, "22026"))
833 return(DB_ERR_STR_DATA_LENGTH_MISMATCH);
834 if (!strcmp(SQLState, "23000"))
835 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL);
836 if (!strcmp(SQLState, "24000"))
837 return(DB_ERR_INVALID_CURSOR_STATE);
838 if (!strcmp(SQLState, "25000"))
839 return(DB_ERR_INVALID_TRANS_STATE);
840 if (!strcmp(SQLState, "28000"))
841 return(DB_ERR_INVALID_AUTH_SPEC);
842 if (!strcmp(SQLState, "34000"))
843 return(DB_ERR_INVALID_CURSOR_NAME);
844 if (!strcmp(SQLState, "37000"))
845 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL);
846 if (!strcmp(SQLState, "3C000"))
847 return(DB_ERR_DUPLICATE_CURSOR_NAME);
848 if (!strcmp(SQLState, "40001"))
849 return(DB_ERR_SERIALIZATION_FAILURE);
850 if (!strcmp(SQLState, "42000"))
851 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2);
852 if (!strcmp(SQLState, "70100"))
853 return(DB_ERR_OPERATION_ABORTED);
854 if (!strcmp(SQLState, "IM001"))
855 return(DB_ERR_UNSUPPORTED_FUNCTION);
856 if (!strcmp(SQLState, "IM002"))
857 return(DB_ERR_NO_DATA_SOURCE);
858 if (!strcmp(SQLState, "IM003"))
859 return(DB_ERR_DRIVER_LOAD_ERROR);
860 if (!strcmp(SQLState, "IM004"))
861 return(DB_ERR_SQLALLOCENV_FAILED);
862 if (!strcmp(SQLState, "IM005"))
863 return(DB_ERR_SQLALLOCCONNECT_FAILED);
864 if (!strcmp(SQLState, "IM006"))
865 return(DB_ERR_SQLSETCONNECTOPTION_FAILED);
866 if (!strcmp(SQLState, "IM007"))
867 return(DB_ERR_NO_DATA_SOURCE_DLG_PROHIB);
868 if (!strcmp(SQLState, "IM008"))
869 return(DB_ERR_DIALOG_FAILED);
870 if (!strcmp(SQLState, "IM009"))
871 return(DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL);
872 if (!strcmp(SQLState, "IM010"))
873 return(DB_ERR_DATA_SOURCE_NAME_TOO_LONG);
874 if (!strcmp(SQLState, "IM011"))
875 return(DB_ERR_DRIVER_NAME_TOO_LONG);
876 if (!strcmp(SQLState, "IM012"))
877 return(DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR);
878 if (!strcmp(SQLState, "IM013"))
879 return(DB_ERR_TRACE_FILE_ERROR);
880 if (!strcmp(SQLState, "S0001"))
881 return(DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS);
882 if (!strcmp(SQLState, "S0002"))
883 return(DB_ERR_TABLE_NOT_FOUND);
884 if (!strcmp(SQLState, "S0011"))
885 return(DB_ERR_INDEX_ALREADY_EXISTS);
886 if (!strcmp(SQLState, "S0012"))
887 return(DB_ERR_INDEX_NOT_FOUND);
888 if (!strcmp(SQLState, "S0021"))
889 return(DB_ERR_COLUMN_ALREADY_EXISTS);
890 if (!strcmp(SQLState, "S0022"))
891 return(DB_ERR_COLUMN_NOT_FOUND);
892 if (!strcmp(SQLState, "S0023"))
893 return(DB_ERR_NO_DEFAULT_FOR_COLUMN);
894 if (!strcmp(SQLState, "S1000"))
895 return(DB_ERR_GENERAL_ERROR);
896 if (!strcmp(SQLState, "S1001"))
897 return(DB_ERR_MEMORY_ALLOCATION_FAILURE);
898 if (!strcmp(SQLState, "S1002"))
899 return(DB_ERR_INVALID_COLUMN_NUMBER);
900 if (!strcmp(SQLState, "S1003"))
901 return(DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE);
902 if (!strcmp(SQLState, "S1004"))
903 return(DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE);
904 if (!strcmp(SQLState, "S1008"))
905 return(DB_ERR_OPERATION_CANCELLED);
906 if (!strcmp(SQLState, "S1009"))
907 return(DB_ERR_INVALID_ARGUMENT_VALUE);
908 if (!strcmp(SQLState, "S1010"))
909 return(DB_ERR_FUNCTION_SEQUENCE_ERROR);
910 if (!strcmp(SQLState, "S1011"))
911 return(DB_ERR_OPERATION_INVALID_AT_THIS_TIME);
912 if (!strcmp(SQLState, "S1012"))
913 return(DB_ERR_INVALID_TRANS_OPERATION_CODE);
914 if (!strcmp(SQLState, "S1015"))
915 return(DB_ERR_NO_CURSOR_NAME_AVAIL);
916 if (!strcmp(SQLState, "S1090"))
917 return(DB_ERR_INVALID_STR_OR_BUF_LEN);
918 if (!strcmp(SQLState, "S1091"))
919 return(DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE);
920 if (!strcmp(SQLState, "S1092"))
921 return(DB_ERR_OPTION_TYPE_OUT_OF_RANGE);
922 if (!strcmp(SQLState, "S1093"))
923 return(DB_ERR_INVALID_PARAM_NO);
924 if (!strcmp(SQLState, "S1094"))
925 return(DB_ERR_INVALID_SCALE_VALUE);
926 if (!strcmp(SQLState, "S1095"))
927 return(DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE);
928 if (!strcmp(SQLState, "S1096"))
929 return(DB_ERR_INF_TYPE_OUT_OF_RANGE);
930 if (!strcmp(SQLState, "S1097"))
931 return(DB_ERR_COLUMN_TYPE_OUT_OF_RANGE);
932 if (!strcmp(SQLState, "S1098"))
933 return(DB_ERR_SCOPE_TYPE_OUT_OF_RANGE);
934 if (!strcmp(SQLState, "S1099"))
935 return(DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE);
936 if (!strcmp(SQLState, "S1100"))
937 return(DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE);
938 if (!strcmp(SQLState, "S1101"))
939 return(DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE);
940 if (!strcmp(SQLState, "S1103"))
941 return(DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE);
942 if (!strcmp(SQLState, "S1104"))
943 return(DB_ERR_INVALID_PRECISION_VALUE);
944 if (!strcmp(SQLState, "S1105"))
945 return(DB_ERR_INVALID_PARAM_TYPE);
946 if (!strcmp(SQLState, "S1106"))
947 return(DB_ERR_FETCH_TYPE_OUT_OF_RANGE);
948 if (!strcmp(SQLState, "S1107"))
949 return(DB_ERR_ROW_VALUE_OUT_OF_RANGE);
950 if (!strcmp(SQLState, "S1108"))
951 return(DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE);
952 if (!strcmp(SQLState, "S1109"))
953 return(DB_ERR_INVALID_CURSOR_POSITION);
954 if (!strcmp(SQLState, "S1110"))
955 return(DB_ERR_INVALID_DRIVER_COMPLETION);
956 if (!strcmp(SQLState, "S1111"))
957 return(DB_ERR_INVALID_BOOKMARK_VALUE);
958 if (!strcmp(SQLState, "S1C00"))
959 return(DB_ERR_DRIVER_NOT_CAPABLE);
960 if (!strcmp(SQLState, "S1T00"))
961 return(DB_ERR_TIMEOUT_EXPIRED);
962
963 // No match
964 return(0);
965
966 } // wxDB::TranslateSqlState()
967
968 /********** wxDB::Grant() **********/
969 bool wxDB::Grant(int privileges, char *tableName, char *userList)
970 {
971 char sqlStmt[DB_MAX_STATEMENT_LEN];
972
973 // Build the grant statement
974 strcpy(sqlStmt, "GRANT ");
975 if (privileges == DB_GRANT_ALL)
976 strcat(sqlStmt, "ALL");
977 else
978 {
979 int c = 0;
980 if (privileges & DB_GRANT_SELECT)
981 {
982 strcat(sqlStmt, "SELECT");
983 c++;
984 }
985 if (privileges & DB_GRANT_INSERT)
986 {
987 if (c++)
988 strcat(sqlStmt, ", ");
989 strcat(sqlStmt, "INSERT");
990 }
991 if (privileges & DB_GRANT_UPDATE)
992 {
993 if (c++)
994 strcat(sqlStmt, ", ");
995 strcat(sqlStmt, "UPDATE");
996 }
997 if (privileges & DB_GRANT_DELETE)
998 {
999 if (c++)
1000 strcat(sqlStmt, ", ");
1001 strcat(sqlStmt, "DELETE");
1002 }
1003 }
1004
1005 strcat(sqlStmt, " ON ");
1006 strcat(sqlStmt, tableName);
1007 strcat(sqlStmt, " TO ");
1008 strcat(sqlStmt, userList);
1009
1010 #ifdef DBDEBUG_CONSOLE
1011 cout << endl << sqlStmt << endl;
1012 #endif
1013
1014 WriteSqlLog(sqlStmt);
1015
1016 return(ExecSql(sqlStmt));
1017
1018 } // wxDB::Grant()
1019
1020 /********** wxDB::CreateView() **********/
1021 bool wxDB::CreateView(char *viewName, char *colList, char *pSqlStmt)
1022 {
1023 char sqlStmt[DB_MAX_STATEMENT_LEN];
1024
1025 // Drop the view first
1026 sprintf(sqlStmt, "DROP VIEW %s", viewName);
1027 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
1028 {
1029 // Check for sqlState = S0002, "Table or view not found".
1030 // Ignore this error, bomb out on any other error.
1031 // SQL Sybase Anwhere v5.5 returns an access violation error here
1032 // (sqlstate = 42000) rather than an S0002.
1033 GetNextError(henv, hdbc, hstmt);
1034 if (strcmp(sqlState, "S0002") && strcmp(sqlState, "42000"))
1035 {
1036 DispNextError();
1037 DispAllErrors(henv, hdbc, hstmt);
1038 RollbackTrans();
1039 return(FALSE);
1040 }
1041 }
1042
1043 WriteSqlLog(sqlStmt);
1044
1045 #ifdef DBDEBUG_CONSOLE
1046 cout << endl << sqlStmt << endl;
1047 #endif
1048
1049 // Build the create view statement
1050 strcpy(sqlStmt, "CREATE VIEW ");
1051 strcat(sqlStmt, viewName);
1052
1053 if (strlen(colList))
1054 {
1055 strcat(sqlStmt, " (");
1056 strcat(sqlStmt, colList);
1057 strcat(sqlStmt, ")");
1058 }
1059
1060 strcat(sqlStmt, " AS ");
1061 strcat(sqlStmt, pSqlStmt);
1062
1063 WriteSqlLog(sqlStmt);
1064
1065 #ifdef DBDEBUG_CONSOLE
1066 cout << sqlStmt << endl;
1067 #endif
1068
1069 return(ExecSql(sqlStmt));
1070
1071 } // wxDB::CreateView()
1072
1073 /********** wxDB::ExecSql() **********/
1074 bool wxDB::ExecSql(char *pSqlStmt)
1075 {
1076 if (SQLExecDirect(hstmt, (UCHAR FAR *) pSqlStmt, SQL_NTS) == SQL_SUCCESS)
1077 return(TRUE);
1078 else
1079 {
1080 DispAllErrors(henv, hdbc, hstmt);
1081 return(FALSE);
1082 }
1083
1084 } // wxDB::ExecSql()
1085
1086 /********** wxDB::GetColumns() **********/
1087 /*
1088 * 1) The last array element of the tableName[] argument must be zero (null).
1089 * This is how the end of the array is detected.
1090 * 2) This function returns an array of CcolInf structures. If no columns
1091 * were found, or an error occured, this pointer will be zero (null). THE
1092 * CALLING FUNCTION IS RESPONSIBLE FOR DELETING THE MEMORY RETURNED WHEN IT
1093 * IS FINISHED WITH IT. i.e.
1094 *
1095 * CcolInf *colInf = pDb->GetColumns(tableList);
1096 * if (colInf)
1097 * {
1098 * // Use the column inf
1099 * .......
1100 * // Destroy the memory
1101 * delete [] colInf;
1102 * }
1103 */
1104 CcolInf *wxDB::GetColumns(char *tableName[])
1105 {
1106 UINT noCols = 0;
1107 UINT colNo = 0;
1108 CcolInf *colInf = 0;
1109 RETCODE retcode;
1110 SDWORD cb;
1111 char tblName[DB_MAX_TABLE_NAME_LEN+1];
1112 char colName[DB_MAX_COLUMN_NAME_LEN+1];
1113 SWORD sqlDataType;
1114
1115 // Pass 1 - Determine how many columns there are.
1116 // Pass 2 - Allocate the CcolInf array and fill in
1117 // the array with the column information.
1118 for (int pass = 1; pass <= 2; pass++)
1119 {
1120 if (pass == 2)
1121 {
1122 if (noCols == 0) // Probably a bogus table name(s)
1123 break;
1124 // Allocate n CcolInf objects to hold the column information
1125 colInf = new CcolInf[noCols+1];
1126 if (!colInf)
1127 break;
1128 // Mark the end of the array
1129 strcpy(colInf[noCols].tableName, "");
1130 strcpy(colInf[noCols].colName, "");
1131 colInf[noCols].sqlDataType = 0;
1132 }
1133 // Loop through each table name
1134 for (int tbl = 0; tableName[tbl]; tbl++)
1135 {
1136 SQLFreeStmt(hstmt, SQL_CLOSE);
1137 retcode = SQLColumns(hstmt,
1138 NULL, 0, // All qualifiers
1139 NULL, 0, // All owners
1140 (UCHAR *) tableName[tbl], SQL_NTS,
1141 NULL, 0); // All columns
1142 if (retcode != SQL_SUCCESS)
1143 { // Error occured, abort
1144 DispAllErrors(henv, hdbc, hstmt);
1145 if (colInf)
1146 delete [] colInf;
1147 return(0);
1148 }
1149 SQLBindCol(hstmt, 3, SQL_C_CHAR, (UCHAR*) tblName, DB_MAX_TABLE_NAME_LEN+1, &cb);
1150 SQLBindCol(hstmt, 4, SQL_C_CHAR, (UCHAR*) colName, DB_MAX_COLUMN_NAME_LEN+1, &cb);
1151 SQLBindCol(hstmt, 5, SQL_C_SSHORT, (UCHAR*) &sqlDataType, 0, &cb);
1152 while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS)
1153 {
1154 if (pass == 1) // First pass, just add up the number of columns
1155 noCols++;
1156 else // Pass 2; Fill in the array of structures
1157 {
1158 if (colNo < noCols) // Some extra error checking to prevent memory overwrites
1159 {
1160 strcpy(colInf[colNo].tableName, tblName);
1161 strcpy(colInf[colNo].colName, colName);
1162 colInf[colNo].sqlDataType = sqlDataType;
1163 colNo++;
1164 }
1165 }
1166 }
1167 if (retcode != SQL_NO_DATA_FOUND)
1168 { // Error occured, abort
1169 DispAllErrors(henv, hdbc, hstmt);
1170 if (colInf)
1171 delete [] colInf;
1172 return(0);
1173 }
1174 }
1175 }
1176
1177 SQLFreeStmt(hstmt, SQL_CLOSE);
1178 return colInf;
1179
1180 } // wxDB::GetColumns()
1181
1182
1183 /********** wxDB::Catalog() **********/
1184 bool wxDB::Catalog(char *userID, char *fileName)
1185 {
1186 assert(userID && strlen(userID));
1187 assert(fileName && strlen(fileName));
1188
1189 RETCODE retcode;
1190 SDWORD cb;
1191 char tblName[DB_MAX_TABLE_NAME_LEN+1];
1192 char tblNameSave[DB_MAX_TABLE_NAME_LEN+1];
1193 char colName[DB_MAX_COLUMN_NAME_LEN+1];
1194 SWORD sqlDataType;
1195 char typeName[16];
1196 SWORD precision, length;
1197
1198 FILE *fp = fopen(fileName,"wt");
1199 if (fp == NULL)
1200 return(FALSE);
1201
1202 SQLFreeStmt(hstmt, SQL_CLOSE);
1203
1204 int i = 0;
1205 char userIdUC[81];
1206 for (char *p = userID; *p; p++)
1207 userIdUC[i++] = toupper(*p);
1208 userIdUC[i] = 0;
1209
1210 retcode = SQLColumns(hstmt,
1211 NULL, 0, // All qualifiers
1212 (UCHAR *) userIdUC, SQL_NTS, // User specified
1213 NULL, 0, // All tables
1214 NULL, 0); // All columns
1215 if (retcode != SQL_SUCCESS)
1216 {
1217 DispAllErrors(henv, hdbc, hstmt);
1218 fclose(fp);
1219 return(FALSE);
1220 }
1221
1222 SQLBindCol(hstmt, 3, SQL_C_CHAR, (UCHAR*) tblName, DB_MAX_TABLE_NAME_LEN+1, &cb);
1223 SQLBindCol(hstmt, 4, SQL_C_CHAR, (UCHAR*) colName, DB_MAX_COLUMN_NAME_LEN+1, &cb);
1224 SQLBindCol(hstmt, 5, SQL_C_SSHORT, (UCHAR*) &sqlDataType, 0, &cb);
1225 SQLBindCol(hstmt, 6, SQL_C_CHAR, (UCHAR*) typeName, 16, &cb);
1226 SQLBindCol(hstmt, 7, SQL_C_SSHORT, (UCHAR*) &precision, 0, &cb);
1227 SQLBindCol(hstmt, 8, SQL_C_SSHORT, (UCHAR*) &length, 0, &cb);
1228
1229 char outStr[256];
1230 strcpy(tblNameSave,"");
1231 int cnt = 0;
1232
1233 while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS)
1234 {
1235 if (strcmp(tblName,tblNameSave))
1236 {
1237 if (cnt)
1238 fputs("\n", fp);
1239 fputs("================================ ", fp);
1240 fputs("================================ ", fp);
1241 fputs("===================== ", fp);
1242 fputs("========= ", fp);
1243 fputs("=========\n", fp);
1244 sprintf(outStr, "%-32s %-32s %-21s %9s %9s\n",
1245 "TABLE NAME", "COLUMN NAME", "DATA TYPE", "PRECISION", "LENGTH");
1246 fputs(outStr, fp);
1247 fputs("================================ ", fp);
1248 fputs("================================ ", fp);
1249 fputs("===================== ", fp);
1250 fputs("========= ", fp);
1251 fputs("=========\n", fp);
1252 strcpy(tblNameSave,tblName);
1253 }
1254 sprintf(outStr, "%-32s %-32s (%04d)%-15s %9d %9d\n",
1255 tblName, colName, sqlDataType, typeName, precision, length);
1256 if (fputs(outStr, fp) == EOF)
1257 {
1258 fclose(fp);
1259 return(FALSE);
1260 }
1261 cnt++;
1262 }
1263
1264 if (retcode != SQL_NO_DATA_FOUND)
1265 {
1266 DispAllErrors(henv, hdbc, hstmt);
1267 fclose(fp);
1268 return(FALSE);
1269 }
1270
1271 SQLFreeStmt(hstmt, SQL_CLOSE);
1272 fclose(fp);
1273 return(TRUE);
1274
1275 } // wxDB::Catalog()
1276
1277
1278 // Table name can refer to a table, view, alias or synonym. Returns true
1279 // if the object exists in the database. This function does not indicate
1280 // whether or not the user has privleges to query or perform other functions
1281 // on the table.
1282 bool wxDB::TableExists(char *tableName)
1283 {
1284 assert(tableName && strlen(tableName));
1285
1286 SQLFreeStmt(hstmt, SQL_CLOSE);
1287 RETCODE retcode = SQLTables(hstmt,
1288 NULL, 0, // All qualifiers
1289 NULL, 0, // All owners
1290 (UCHAR FAR *)tableName, SQL_NTS,
1291 NULL, 0); // All table types
1292 if (retcode != SQL_SUCCESS)
1293 return(DispAllErrors(henv, hdbc, hstmt));
1294
1295 if (SQLFetch(hstmt) != SQL_SUCCESS)
1296 {
1297 SQLFreeStmt(hstmt, SQL_CLOSE);
1298 return(DispAllErrors(henv, hdbc, hstmt));
1299 }
1300
1301 SQLFreeStmt(hstmt, SQL_CLOSE);
1302 return(TRUE);
1303
1304 } // wxDB::TableExists()
1305
1306
1307 /********** wxDB::SqlLog() **********/
1308 bool wxDB::SqlLog(enum sqlLog state, char *filename, bool append)
1309 {
1310 assert(state == sqlLogON || state == sqlLogOFF);
1311 assert(state == sqlLogOFF || filename);
1312
1313 if (state == sqlLogON)
1314 {
1315 if (fpSqlLog == 0)
1316 {
1317 fpSqlLog = fopen(filename, (append ? "at" : "wt"));
1318 if (fpSqlLog == NULL)
1319 return(FALSE);
1320 }
1321 }
1322 else // sqlLogOFF
1323 {
1324 if (fpSqlLog)
1325 {
1326 if (fclose(fpSqlLog))
1327 return(FALSE);
1328 fpSqlLog = 0;
1329 }
1330 }
1331
1332 sqlLogState = state;
1333 return(TRUE);
1334
1335 } // wxDB::SqlLog()
1336
1337
1338 /********** wxDB::WriteSqlLog() **********/
1339 bool wxDB::WriteSqlLog(char *logMsg)
1340 {
1341 assert(logMsg);
1342
1343 if (fpSqlLog == 0 || sqlLogState == sqlLogOFF)
1344 return(FALSE);
1345
1346 if (fputs("\n", fpSqlLog) == EOF) return(FALSE);
1347 if (fputs(logMsg, fpSqlLog) == EOF) return(FALSE);
1348 if (fputs("\n", fpSqlLog) == EOF) return(FALSE);
1349
1350 return(TRUE);
1351
1352 } // wxDB::WriteSqlLog()
1353
1354
1355 /********** GetDbConnection() **********/
1356 wxDB* WXDLLEXPORT GetDbConnection(DbStuff *pDbStuff)
1357 {
1358 DbList *pList;
1359
1360 // Scan the linked list searching for an available database connection
1361 // that's already been opened but is currently not in use.
1362 for (pList = PtrBegDbList; pList; pList = pList->PtrNext)
1363 {
1364 // The database connection must be for the same datasource
1365 // name and must currently not be in use.
1366 if (pList->Free && (! strcmp(pDbStuff->Dsn, pList->Dsn))) // Found a free connection
1367 {
1368 pList->Free = FALSE;
1369 return(pList->PtrDb);
1370 }
1371 }
1372
1373 // No available connections. A new connection must be made and
1374 // appended to the end of the linked list.
1375 if (PtrBegDbList)
1376 {
1377 // Find the end of the list
1378 for (pList = PtrBegDbList; pList->PtrNext; pList = pList->PtrNext);
1379 // Append a new list item
1380 pList->PtrNext = new DbList;
1381 pList->PtrNext->PtrPrev = pList;
1382 pList = pList->PtrNext;
1383 }
1384 else // Empty list
1385 {
1386 // Create the first node on the list
1387 pList = PtrBegDbList = new DbList;
1388 pList->PtrPrev = 0;
1389 }
1390
1391 // Initialize new node in the linked list
1392 pList->PtrNext = 0;
1393 pList->Free = FALSE;
1394 strcpy(pList->Dsn, pDbStuff->Dsn);
1395 pList->PtrDb = new wxDB(pDbStuff->Henv);
1396
1397 // Connect to the datasource
1398 if (pList->PtrDb->Open(pDbStuff->Dsn, pDbStuff->Uid, pDbStuff->AuthStr))
1399 return(pList->PtrDb);
1400 else // Unable to connect, destroy list item
1401 {
1402 if (pList->PtrPrev)
1403 pList->PtrPrev->PtrNext = 0;
1404 else
1405 PtrBegDbList = 0; // Empty list again
1406 pList->PtrDb->CommitTrans(); // Commit any open transactions on wxDB object
1407 pList->PtrDb->Close(); // Close the wxDB object
1408 delete pList->PtrDb; // Deletes the wxDB object
1409 delete pList; // Deletes the linked list object
1410 return(0);
1411 }
1412
1413 } // GetDbConnection()
1414
1415 /********** FreeDbConnection() **********/
1416 bool WXDLLEXPORT FreeDbConnection(wxDB *pDb)
1417 {
1418 DbList *pList;
1419
1420 // Scan the linked list searching for the database connection
1421 for (pList = PtrBegDbList; pList; pList = pList->PtrNext)
1422 {
1423 if (pList->PtrDb == pDb) // Found it!!!
1424 return(pList->Free = TRUE);
1425 }
1426
1427 // Never found the database object, return failure
1428 return(FALSE);
1429
1430 } // FreeDbConnection()
1431
1432 /********** CloseDbConnections() **********/
1433 void WXDLLEXPORT CloseDbConnections(void)
1434 {
1435 DbList *pList, *pNext;
1436
1437 // Traverse the linked list closing database connections and freeing memory as I go.
1438 for (pList = PtrBegDbList; pList; pList = pNext)
1439 {
1440 pNext = pList->PtrNext; // Save the pointer to next
1441 pList->PtrDb->CommitTrans(); // Commit any open transactions on wxDB object
1442 pList->PtrDb->Close(); // Close the wxDB object
1443 delete pList->PtrDb; // Deletes the wxDB object
1444 delete pList; // Deletes the linked list object
1445 }
1446
1447 // Mark the list as empty
1448 PtrBegDbList = 0;
1449
1450 } // CloseDbConnections()
1451
1452 /********** NumberDbConnectionsInUse() **********/
1453 int WXDLLEXPORT NumberDbConnectionsInUse(void)
1454 {
1455 DbList *pList;
1456 int cnt = 0;
1457
1458 // Scan the linked list counting db connections that are currently in use
1459 for (pList = PtrBegDbList; pList; pList = pList->PtrNext)
1460 {
1461 if (pList->Free == FALSE)
1462 cnt++;
1463 }
1464
1465 return(cnt);
1466
1467 } // NumberDbConnectionsInUse()
1468
1469 /********** GetDataSource() **********/
1470 bool GetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDescMax,
1471 UWORD direction)
1472 {
1473 SWORD cb;
1474
1475 if (SQLDataSources(henv, direction, (UCHAR FAR *) Dsn, DsnMax, &cb,
1476 (UCHAR FAR *) DsDesc, DsDescMax, &cb) == SQL_SUCCESS)
1477 return(TRUE);
1478 else
1479 return(FALSE);
1480
1481 } // GetDataSource()
1482
1483 #endif
1484 // wxUSE_ODBC
1485