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