]> git.saurik.com Git - wxWidgets.git/blame - src/common/db.cpp
Committing in .
[wxWidgets.git] / src / common / db.cpp
CommitLineData
108106cf
JS
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:
a2115c88
GT
8// Mods: Dec, 1998:
9// -Added support for SQL statement logging and database cataloging
10// Mods: April, 1999
11// -Added QUERY_ONLY mode support to reduce default number of cursors
12// -Added additional SQL logging code
13// -Added DEBUG-ONLY tracking of wxTable objects to detect orphaned DB connections
14// -Set ODBC option to only read committed writes to the DB so all
15// databases operate the same in that respect
108106cf
JS
16// Created: 9.96
17// RCS-ID: $Id$
18// Copyright: (c) 1996 Remstar International, Inc.
19// Licence: wxWindows licence, plus:
a2115c88 20// Notice: This class library and its intellectual design are free of charge for use,
108106cf
JS
21// modification, enhancement, debugging under the following conditions:
22// 1) These classes may only be used as part of the implementation of a
23// wxWindows-based application
24// 2) All enhancements and bug fixes are to be submitted back to the wxWindows
25// user groups free of all charges for use with the wxWindows library.
26// 3) These classes may not be distributed as part of any other class library,
27// DLL, text (written or electronic), other than a complete distribution of
28// the wxWindows GUI development toolkit.
29///////////////////////////////////////////////////////////////////////////////
30
31/*
32// SYNOPSIS START
33// SYNOPSIS STOP
34*/
35
01dba85a
JS
36#include "wx/wxprec.h"
37
a2115c88
GT
38// Use this line for wxWindows v1.x
39//#include "wx_ver.h"
40// Use this line for wxWindows v2.x
41#include "wx/version.h"
a2115c88
GT
42
43#if wxMAJOR_VERSION == 2
44 #ifdef __GNUG__
45 #pragma implementation "db.h"
46 #endif
47#endif
48
1fc5dd6f 49#ifdef DBDEBUG_CONSOLE
108106cf
JS
50 #include <iostream.h>
51#endif
108106cf
JS
52
53#ifdef __BORLANDC__
a2115c88 54 #pragma hdrstop
108106cf
JS
55#endif //__BORLANDC__
56
a2115c88 57#if wxMAJOR_VERSION == 2
9199e66f
RR
58 #ifndef WX_PRECOMP
59 #include "wx/string.h"
60 #include "wx/object.h"
61 #include "wx/list.h"
62 #include "wx/utils.h"
63 #include "wx/msgdlg.h"
64 #endif
3351164d 65 #include "wx/filefn.h"
aa33452c 66 #include "wx/wxchar.h"
a2115c88
GT
67#endif
68
69#if wxMAJOR_VERSION == 1
70# if defined(wx_msw) || defined(wx_x)
71# ifdef WX_PRECOMP
72# include "wx_prec.h"
73# else
74# include "wx.h"
75# endif
76# endif
77# define wxUSE_ODBC 1
78#endif
108106cf 79
47d67540 80#if wxUSE_ODBC
108106cf 81
108106cf
JS
82#include <stdio.h>
83#include <string.h>
84#include <assert.h>
7e616b10
RR
85#include <stdlib.h>
86#include <ctype.h>
a2115c88
GT
87#if wxMAJOR_VERSION == 1
88 #include "db.h"
89#elif wxMAJOR_VERSION == 2
90 #include "wx/db.h"
91#endif
108106cf 92
a497618a 93DbList WXDLLEXPORT *PtrBegDbList = 0;
108106cf 94
e041ce57 95#ifdef __WXDEBUG__
a2115c88
GT
96 extern wxList TablesInUse;
97#endif
98
e7549107
SC
99
100#ifdef __MWERKS__
101#ifdef __WXMSW__
102#define stricmp _stricmp
103#define strnicmp _strnicmp
104#else //__WXMSW_
105int strcasecmp(const char *str_1, const char *str_2) ;
106int strncasecmp(const char *str_1, const char *str_2, size_t maxchar) ;
107#define stricmp strcasecmp
108#define strnicmp strncasecmp
109#endif
110#endif //__MWERKS__
111
a2115c88
GT
112// SQL Log defaults to be used by GetDbConnection
113enum sqlLog SQLLOGstate = sqlLogOFF;
114
115char SQLLOGfn[DB_PATH_MAX+1] = "sqllog.txt";
116
117// The wxDB::errorList is copied to this variable when the wxDB object
118// is closed. This way, the error list is still available after the
119// database object is closed. This is necessary if the database
120// connection fails so the calling application can show the operator
121// why the connection failed. Note: as each wxDB object is closed, it
122// will overwrite the errors of the previously destroyed wxDB object in
123// this variable.
124char DBerrorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN];
125
108106cf
JS
126/********** wxDB Constructor **********/
127wxDB::wxDB(HENV &aHenv)
128{
129 int i;
1fc5dd6f
JS
130
131 fpSqlLog = 0; // Sql Log file pointer
132 sqlLogState = sqlLogOFF; // By default, logging is turned off
a2115c88 133 nTables = 0;
108106cf
JS
134
135 strcpy(sqlState,"");
136 strcpy(errorMsg,"");
137 nativeError = cbErrorMsg = 0;
138 for (i = 0; i < DB_MAX_ERROR_HISTORY; i++)
139 strcpy(errorList[i], "");
140
141 // Init typeInf structures
142 strcpy(typeInfVarchar.TypeName,"");
143 typeInfVarchar.FsqlType = 0;
144 typeInfVarchar.Precision = 0;
145 typeInfVarchar.CaseSensitive = 0;
146 typeInfVarchar.MaximumScale = 0;
147
148 strcpy(typeInfInteger.TypeName,"");
149 typeInfInteger.FsqlType = 0;
150 typeInfInteger.Precision = 0;
151 typeInfInteger.CaseSensitive = 0;
152 typeInfInteger.MaximumScale = 0;
153
154 strcpy(typeInfFloat.TypeName,"");
155 typeInfFloat.FsqlType = 0;
156 typeInfFloat.Precision = 0;
157 typeInfFloat.CaseSensitive = 0;
158 typeInfFloat.MaximumScale = 0;
159
160 strcpy(typeInfDate.TypeName,"");
161 typeInfDate.FsqlType = 0;
162 typeInfDate.Precision = 0;
163 typeInfDate.CaseSensitive = 0;
164 typeInfDate.MaximumScale = 0;
165
166 // Error reporting is turned OFF by default
167 silent = TRUE;
168
169 // Copy the HENV into the db class
170 henv = aHenv;
171
172 // Allocate a data source connection handle
173 if (SQLAllocConnect(henv, &hdbc) != SQL_SUCCESS)
174 DispAllErrors(henv);
175
176 // Initialize the db status flag
177 DB_STATUS = 0;
178
179 // Mark database as not open as of yet
180 dbIsOpen = FALSE;
181
182} // wxDB::wxDB()
183
184/********** wxDB::Open() **********/
185bool wxDB::Open(char *Dsn, char *Uid, char *AuthStr)
186{
1fc5dd6f 187 assert(Dsn && strlen(Dsn));
108106cf
JS
188 dsn = Dsn;
189 uid = Uid;
190 authStr = AuthStr;
191
108106cf
JS
192 RETCODE retcode;
193
ac99a98f 194#if !wxODBC_FWD_ONLY_CURSORS
a2115c88 195
108106cf
JS
196 // Specify that the ODBC cursor library be used, if needed. This must be
197 // specified before the connection is made.
198 retcode = SQLSetConnectOption(hdbc, SQL_ODBC_CURSORS, SQL_CUR_USE_IF_NEEDED);
199
1fc5dd6f 200 #ifdef DBDEBUG_CONSOLE
108106cf
JS
201 if (retcode == SQL_SUCCESS)
202 cout << "SQLSetConnectOption(CURSOR_LIB) successful" << endl;
203 else
204 cout << "SQLSetConnectOption(CURSOR_LIB) failed" << endl;
205 #endif
206
207#endif
208
209 // Connect to the data source
a2115c88
GT
210 retcode = SQLConnect(hdbc, (UCHAR FAR *) Dsn, SQL_NTS,
211 (UCHAR FAR *) Uid, SQL_NTS,
212 (UCHAR FAR *) AuthStr,SQL_NTS);
213 if (retcode == SQL_SUCCESS_WITH_INFO)
214 DispAllErrors(henv, hdbc);
215 else if (retcode != SQL_SUCCESS)
108106cf
JS
216 return(DispAllErrors(henv, hdbc));
217
a2115c88
GT
218/*
219 If using Intersolv branded ODBC drivers, this is the place where you would substitute
220 your branded driver license information
221
222 SQLSetConnectOption(hdbc, 1041, (UDWORD) "");
223 SQLSetConnectOption(hdbc, 1042, (UDWORD) "");
224*/
108106cf
JS
225 // Mark database as open
226 dbIsOpen = TRUE;
227
228 // Allocate a statement handle for the database connection
229 if (SQLAllocStmt(hdbc, &hstmt) != SQL_SUCCESS)
230 return(DispAllErrors(henv, hdbc));
231
232 // Set Connection Options
233 if (! setConnectionOptions())
234 return(FALSE);
235
236 // Query the data source for inf. about itself
237 if (! getDbInfo())
238 return(FALSE);
239
240 // Query the data source regarding data type information
241
242 //
243 // The way I determined which SQL data types to use was by calling SQLGetInfo
244 // for all of the possible SQL data types to see which ones were supported. If
245 // a type is not supported, the SQLFetch() that's called from getDataTypeInfo()
246 // fails with SQL_NO_DATA_FOUND. This is ugly because I'm sure the three SQL data
247 // types I've selected below will not alway's be what we want. These are just
248 // what happened to work against an Oracle 7/Intersolv combination. The following is
249 // a complete list of the results I got back against the Oracle 7 database:
250 //
251 // SQL_BIGINT SQL_NO_DATA_FOUND
252 // SQL_BINARY SQL_NO_DATA_FOUND
253 // SQL_BIT SQL_NO_DATA_FOUND
254 // SQL_CHAR type name = 'CHAR', Precision = 255
255 // SQL_DATE SQL_NO_DATA_FOUND
256 // SQL_DECIMAL type name = 'NUMBER', Precision = 38
257 // SQL_DOUBLE type name = 'NUMBER', Precision = 15
258 // SQL_FLOAT SQL_NO_DATA_FOUND
259 // SQL_INTEGER SQL_NO_DATA_FOUND
260 // SQL_LONGVARBINARY type name = 'LONG RAW', Precision = 2 billion
261 // SQL_LONGVARCHAR type name = 'LONG', Precision = 2 billion
262 // SQL_NUMERIC SQL_NO_DATA_FOUND
263 // SQL_REAL SQL_NO_DATA_FOUND
264 // SQL_SMALLINT SQL_NO_DATA_FOUND
265 // SQL_TIME SQL_NO_DATA_FOUND
266 // SQL_TIMESTAMP type name = 'DATE', Precision = 19
267 // SQL_VARBINARY type name = 'RAW', Precision = 255
268 // SQL_VARCHAR type name = 'VARCHAR2', Precision = 2000
269 // =====================================================================
270 // Results from a Microsoft Access 7.0 db, using a driver from Microsoft
271 //
a2115c88 272 // SQL_VARCHAR type name = 'TEXT', Precision = 255
108106cf
JS
273 // SQL_TIMESTAMP type name = 'DATETIME'
274 // SQL_DECIMAL SQL_NO_DATA_FOUND
275 // SQL_NUMERIC type name = 'CURRENCY', Precision = 19
276 // SQL_FLOAT SQL_NO_DATA_FOUND
277 // SQL_REAL type name = 'SINGLE', Precision = 7
278 // SQL_DOUBLE type name = 'DOUBLE', Precision = 15
279 // SQL_INTEGER type name = 'LONG', Precision = 10
280
281 // VARCHAR = Variable length character string
282 if (! getDataTypeInfo(SQL_VARCHAR, typeInfVarchar))
283 if (! getDataTypeInfo(SQL_CHAR, typeInfVarchar))
284 return(FALSE);
285 else
286 typeInfVarchar.FsqlType = SQL_CHAR;
287 else
288 typeInfVarchar.FsqlType = SQL_VARCHAR;
289
290 // Float
291 if (! getDataTypeInfo(SQL_DOUBLE, typeInfFloat))
292 if (! getDataTypeInfo(SQL_REAL, typeInfFloat))
293 if (! getDataTypeInfo(SQL_FLOAT, typeInfFloat))
294 if (! getDataTypeInfo(SQL_DECIMAL, typeInfFloat))
295 if (! getDataTypeInfo(SQL_NUMERIC, typeInfFloat))
296 return(FALSE);
297 else
298 typeInfFloat.FsqlType = SQL_NUMERIC;
299 else
300 typeInfFloat.FsqlType = SQL_DECIMAL;
301 else
302 typeInfFloat.FsqlType = SQL_FLOAT;
303 else
304 typeInfFloat.FsqlType = SQL_REAL;
305 else
306 typeInfFloat.FsqlType = SQL_DOUBLE;
307
308 // Integer
309 if (! getDataTypeInfo(SQL_INTEGER, typeInfInteger))
310 // If SQL_INTEGER is not supported, use the floating point
311 // data type to store integers as well as floats
312 if (! getDataTypeInfo(typeInfFloat.FsqlType, typeInfInteger))
313 return(FALSE);
314 else
315 typeInfInteger.FsqlType = typeInfFloat.FsqlType;
316 else
317 typeInfInteger.FsqlType = SQL_INTEGER;
318
319 // Date/Time
a2115c88
GT
320 if (Dbms() != dbmsDBASE)
321 {
322 if (! getDataTypeInfo(SQL_TIMESTAMP, typeInfDate))
323 return(FALSE);
324 else
325 typeInfDate.FsqlType = SQL_TIMESTAMP;
326 }
108106cf 327 else
a2115c88
GT
328 {
329 if (! getDataTypeInfo(SQL_DATE, typeInfDate))
330 return(FALSE);
331 else
332 typeInfDate.FsqlType = SQL_DATE;
333 }
108106cf 334
1fc5dd6f 335#ifdef DBDEBUG_CONSOLE
108106cf
JS
336 cout << "VARCHAR DATA TYPE: " << typeInfVarchar.TypeName << endl;
337 cout << "INTEGER DATA TYPE: " << typeInfInteger.TypeName << endl;
338 cout << "FLOAT DATA TYPE: " << typeInfFloat.TypeName << endl;
339 cout << "DATE DATA TYPE: " << typeInfDate.TypeName << endl;
340 cout << endl;
341#endif
342
343 // Completed Successfully
344 return(TRUE);
345
346} // wxDB::Open()
347
348// The Intersolv/Oracle 7 driver was "Not Capable" of setting the login timeout.
349
350/********** wxDB::setConnectionOptions() **********/
351bool wxDB::setConnectionOptions(void)
352{
353 SQLSetConnectOption(hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
354 SQLSetConnectOption(hdbc, SQL_OPT_TRACE, SQL_OPT_TRACE_OFF);
355
356 // Display the connection options to verify them
1fc5dd6f 357#ifdef DBDEBUG_CONSOLE
108106cf
JS
358 long l;
359 cout << ">>>>> CONNECTION OPTIONS <<<<<<" << endl;
360
361 if (SQLGetConnectOption(hdbc, SQL_AUTOCOMMIT, &l) != SQL_SUCCESS)
362 return(DispAllErrors(henv, hdbc));
363 cout << "AUTOCOMMIT: " << (l == SQL_AUTOCOMMIT_OFF ? "OFF" : "ON") << endl;
364
365 if (SQLGetConnectOption(hdbc, SQL_ODBC_CURSORS, &l) != SQL_SUCCESS)
366 return(DispAllErrors(henv, hdbc));
367 cout << "ODBC CURSORS: ";
368 switch(l)
369 {
370 case(SQL_CUR_USE_IF_NEEDED):
371 cout << "SQL_CUR_USE_IF_NEEDED";
372 break;
373 case(SQL_CUR_USE_ODBC):
374 cout << "SQL_CUR_USE_ODBC";
375 break;
376 case(SQL_CUR_USE_DRIVER):
377 cout << "SQL_CUR_USE_DRIVER";
378 break;
379 }
380 cout << endl;
381
382 if (SQLGetConnectOption(hdbc, SQL_OPT_TRACE, &l) != SQL_SUCCESS)
383 return(DispAllErrors(henv, hdbc));
384 cout << "TRACING: " << (l == SQL_OPT_TRACE_OFF ? "OFF" : "ON") << endl;
385
386 cout << endl;
387#endif
388
389 // Completed Successfully
390 return(TRUE);
391
392} // wxDB::setConnectionOptions()
393
394/********** wxDB::getDbInfo() **********/
395bool wxDB::getDbInfo(void)
396{
397 SWORD cb;
a2115c88 398 RETCODE retcode;
108106cf 399
a2115c88 400 if (SQLGetInfo(hdbc, SQL_SERVER_NAME, (UCHAR*) dbInf.serverName, 80, &cb) != SQL_SUCCESS)
108106cf
JS
401 return(DispAllErrors(henv, hdbc));
402
7e616b10 403 if (SQLGetInfo(hdbc, SQL_DATABASE_NAME, (UCHAR*) dbInf.databaseName, 128, &cb) != SQL_SUCCESS)
108106cf
JS
404 return(DispAllErrors(henv, hdbc));
405
7e616b10 406 if (SQLGetInfo(hdbc, SQL_DBMS_NAME, (UCHAR*) dbInf.dbmsName, 40, &cb) != SQL_SUCCESS)
108106cf
JS
407 return(DispAllErrors(henv, hdbc));
408
a2115c88
GT
409 // 16-Mar-1999
410 // After upgrading to MSVC6, the original 20 char buffer below was insufficient,
411 // causing database connectivity to fail in some cases.
412 retcode = SQLGetInfo(hdbc, SQL_DBMS_VER, (UCHAR*) dbInf.dbmsVer, 64, &cb);
413 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO )
108106cf
JS
414 return(DispAllErrors(henv, hdbc));
415
7e616b10 416 if (SQLGetInfo(hdbc, SQL_ACTIVE_CONNECTIONS, (UCHAR*) &dbInf.maxConnections, sizeof(dbInf.maxConnections), &cb) != SQL_SUCCESS)
108106cf
JS
417 return(DispAllErrors(henv, hdbc));
418
7e616b10 419 if (SQLGetInfo(hdbc, SQL_ACTIVE_STATEMENTS, (UCHAR*) &dbInf.maxStmts, sizeof(dbInf.maxStmts), &cb) != SQL_SUCCESS)
108106cf
JS
420 return(DispAllErrors(henv, hdbc));
421
7e616b10 422 if (SQLGetInfo(hdbc, SQL_DRIVER_NAME, (UCHAR*) dbInf.driverName, 40, &cb) != SQL_SUCCESS)
108106cf
JS
423 return(DispAllErrors(henv, hdbc));
424
1acd7ba6 425 if (SQLGetInfo(hdbc, SQL_DRIVER_ODBC_VER, (UCHAR*) dbInf.odbcVer, 60, &cb) == SQL_ERROR)
108106cf
JS
426 return(DispAllErrors(henv, hdbc));
427
a2115c88
GT
428 retcode = SQLGetInfo(hdbc, SQL_ODBC_VER, (UCHAR*) dbInf.drvMgrOdbcVer, 60, &cb);
429 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
108106cf
JS
430 return(DispAllErrors(henv, hdbc));
431
1acd7ba6 432 if (SQLGetInfo(hdbc, SQL_DRIVER_VER, (UCHAR*) dbInf.driverVer, 60, &cb) == SQL_ERROR)
108106cf
JS
433 return(DispAllErrors(henv, hdbc));
434
7e616b10 435 if (SQLGetInfo(hdbc, SQL_ODBC_API_CONFORMANCE, (UCHAR*) &dbInf.apiConfLvl, sizeof(dbInf.apiConfLvl), &cb) != SQL_SUCCESS)
108106cf
JS
436 return(DispAllErrors(henv, hdbc));
437
7e616b10 438 if (SQLGetInfo(hdbc, SQL_ODBC_SAG_CLI_CONFORMANCE, (UCHAR*) &dbInf.cliConfLvl, sizeof(dbInf.cliConfLvl), &cb) != SQL_SUCCESS)
108106cf
JS
439 return(DispAllErrors(henv, hdbc));
440
7e616b10 441 if (SQLGetInfo(hdbc, SQL_ODBC_SQL_CONFORMANCE, (UCHAR*) &dbInf.sqlConfLvl, sizeof(dbInf.sqlConfLvl), &cb) != SQL_SUCCESS)
108106cf
JS
442 return(DispAllErrors(henv, hdbc));
443
7e616b10 444 if (SQLGetInfo(hdbc, SQL_OUTER_JOINS, (UCHAR*) dbInf.outerJoins, 2, &cb) != SQL_SUCCESS)
108106cf
JS
445 return(DispAllErrors(henv, hdbc));
446
7e616b10 447 if (SQLGetInfo(hdbc, SQL_PROCEDURES, (UCHAR*) dbInf.procedureSupport, 2, &cb) != SQL_SUCCESS)
108106cf
JS
448 return(DispAllErrors(henv, hdbc));
449
7e616b10 450 if (SQLGetInfo(hdbc, SQL_CURSOR_COMMIT_BEHAVIOR, (UCHAR*) &dbInf.cursorCommitBehavior, sizeof(dbInf.cursorCommitBehavior), &cb) != SQL_SUCCESS)
108106cf
JS
451 return(DispAllErrors(henv, hdbc));
452
7e616b10 453 if (SQLGetInfo(hdbc, SQL_CURSOR_ROLLBACK_BEHAVIOR, (UCHAR*) &dbInf.cursorRollbackBehavior, sizeof(dbInf.cursorRollbackBehavior), &cb) != SQL_SUCCESS)
108106cf
JS
454 return(DispAllErrors(henv, hdbc));
455
7e616b10 456 if (SQLGetInfo(hdbc, SQL_NON_NULLABLE_COLUMNS, (UCHAR*) &dbInf.supportNotNullClause, sizeof(dbInf.supportNotNullClause), &cb) != SQL_SUCCESS)
108106cf
JS
457 return(DispAllErrors(henv, hdbc));
458
7e616b10 459 if (SQLGetInfo(hdbc, SQL_ODBC_SQL_OPT_IEF, (UCHAR*) dbInf.supportIEF, 2, &cb) != SQL_SUCCESS)
108106cf
JS
460 return(DispAllErrors(henv, hdbc));
461
7e616b10 462 if (SQLGetInfo(hdbc, SQL_DEFAULT_TXN_ISOLATION, (UCHAR*) &dbInf.txnIsolation, sizeof(dbInf.txnIsolation), &cb) != SQL_SUCCESS)
108106cf
JS
463 return(DispAllErrors(henv, hdbc));
464
7e616b10 465 if (SQLGetInfo(hdbc, SQL_TXN_ISOLATION_OPTION, (UCHAR*) &dbInf.txnIsolationOptions, sizeof(dbInf.txnIsolationOptions), &cb) != SQL_SUCCESS)
108106cf
JS
466 return(DispAllErrors(henv, hdbc));
467
7e616b10 468 if (SQLGetInfo(hdbc, SQL_FETCH_DIRECTION, (UCHAR*) &dbInf.fetchDirections, sizeof(dbInf.fetchDirections), &cb) != SQL_SUCCESS)
108106cf
JS
469 return(DispAllErrors(henv, hdbc));
470
7e616b10 471 if (SQLGetInfo(hdbc, SQL_LOCK_TYPES, (UCHAR*) &dbInf.lockTypes, sizeof(dbInf.lockTypes), &cb) != SQL_SUCCESS)
108106cf
JS
472 return(DispAllErrors(henv, hdbc));
473
7e616b10 474 if (SQLGetInfo(hdbc, SQL_POS_OPERATIONS, (UCHAR*) &dbInf.posOperations, sizeof(dbInf.posOperations), &cb) != SQL_SUCCESS)
108106cf
JS
475 return(DispAllErrors(henv, hdbc));
476
7e616b10 477 if (SQLGetInfo(hdbc, SQL_POSITIONED_STATEMENTS, (UCHAR*) &dbInf.posStmts, sizeof(dbInf.posStmts), &cb) != SQL_SUCCESS)
108106cf
JS
478 return(DispAllErrors(henv, hdbc));
479
7e616b10 480 if (SQLGetInfo(hdbc, SQL_SCROLL_CONCURRENCY, (UCHAR*) &dbInf.scrollConcurrency, sizeof(dbInf.scrollConcurrency), &cb) != SQL_SUCCESS)
108106cf
JS
481 return(DispAllErrors(henv, hdbc));
482
7e616b10 483 if (SQLGetInfo(hdbc, SQL_SCROLL_OPTIONS, (UCHAR*) &dbInf.scrollOptions, sizeof(dbInf.scrollOptions), &cb) != SQL_SUCCESS)
108106cf
JS
484 return(DispAllErrors(henv, hdbc));
485
7e616b10 486 if (SQLGetInfo(hdbc, SQL_STATIC_SENSITIVITY, (UCHAR*) &dbInf.staticSensitivity, sizeof(dbInf.staticSensitivity), &cb) != SQL_SUCCESS)
108106cf
JS
487 return(DispAllErrors(henv, hdbc));
488
7e616b10 489 if (SQLGetInfo(hdbc, SQL_TXN_CAPABLE, (UCHAR*) &dbInf.txnCapable, sizeof(dbInf.txnCapable), &cb) != SQL_SUCCESS)
108106cf
JS
490 return(DispAllErrors(henv, hdbc));
491
7e616b10 492 if (SQLGetInfo(hdbc, SQL_LOGIN_TIMEOUT, (UCHAR*) &dbInf.loginTimeout, sizeof(dbInf.loginTimeout), &cb) != SQL_SUCCESS)
108106cf
JS
493 return(DispAllErrors(henv, hdbc));
494
1fc5dd6f 495#ifdef DBDEBUG_CONSOLE
108106cf
JS
496 cout << ">>>>> DATA SOURCE INFORMATION <<<<<" << endl;
497 cout << "SERVER Name: " << dbInf.serverName << endl;
498 cout << "DBMS Name: " << dbInf.dbmsName << "; DBMS Version: " << dbInf.dbmsVer << endl;
499 cout << "ODBC Version: " << dbInf.odbcVer << "; Driver Version: " << dbInf.driverVer << endl;
500
501 cout << "API Conf. Level: ";
502 switch(dbInf.apiConfLvl)
503 {
504 case SQL_OAC_NONE: cout << "None"; break;
505 case SQL_OAC_LEVEL1: cout << "Level 1"; break;
506 case SQL_OAC_LEVEL2: cout << "Level 2"; break;
507 }
508 cout << endl;
509
510 cout << "SAG CLI Conf. Level: ";
511 switch(dbInf.cliConfLvl)
512 {
513 case SQL_OSCC_NOT_COMPLIANT: cout << "Not Compliant"; break;
514 case SQL_OSCC_COMPLIANT: cout << "Compliant"; break;
515 }
516 cout << endl;
517
518 cout << "SQL Conf. Level: ";
519 switch(dbInf.sqlConfLvl)
520 {
521 case SQL_OSC_MINIMUM: cout << "Minimum Grammer"; break;
522 case SQL_OSC_CORE: cout << "Core Grammer"; break;
523 case SQL_OSC_EXTENDED: cout << "Extended Grammer"; break;
524 }
525 cout << endl;
526
527 cout << "Max. Connections: " << dbInf.maxConnections << endl;
528 cout << "Outer Joins: " << dbInf.outerJoins << endl;
529 cout << "Support for Procedures: " << dbInf.procedureSupport << endl;
530
531 cout << "Cursor COMMIT Behavior: ";
532 switch(dbInf.cursorCommitBehavior)
533 {
534 case SQL_CB_DELETE: cout << "Delete cursors"; break;
535 case SQL_CB_CLOSE: cout << "Close cursors"; break;
536 case SQL_CB_PRESERVE: cout << "Preserve cursors"; break;
537 }
538 cout << endl;
539
540 cout << "Cursor ROLLBACK Behavior: ";
541 switch(dbInf.cursorRollbackBehavior)
542 {
543 case SQL_CB_DELETE: cout << "Delete cursors"; break;
544 case SQL_CB_CLOSE: cout << "Close cursors"; break;
545 case SQL_CB_PRESERVE: cout << "Preserve cursors"; break;
546 }
547 cout << endl;
548
549 cout << "Support NOT NULL clause: ";
550 switch(dbInf.supportNotNullClause)
551 {
552 case SQL_NNC_NULL: cout << "No"; break;
553 case SQL_NNC_NON_NULL: cout << "Yes"; break;
554 }
555 cout << endl;
556
557 cout << "Support IEF (Ref. Integrity): " << dbInf.supportIEF << endl;
558 cout << "Login Timeout: " << dbInf.loginTimeout << endl;
559
560 cout << endl << endl << "more ..." << endl;
561 getchar();
562
563 cout << "Default Transaction Isolation: ";
564 switch(dbInf.txnIsolation)
565 {
566 case SQL_TXN_READ_UNCOMMITTED: cout << "Read Uncommitted"; break;
567 case SQL_TXN_READ_COMMITTED: cout << "Read Committed"; break;
568 case SQL_TXN_REPEATABLE_READ: cout << "Repeatable Read"; break;
569 case SQL_TXN_SERIALIZABLE: cout << "Serializable"; break;
570#ifdef ODBC_V20
571 case SQL_TXN_VERSIONING: cout << "Versioning"; break;
572#endif
573 }
574 cout << endl;
575
576 cout << "Transaction Isolation Options: ";
577 if (dbInf.txnIsolationOptions & SQL_TXN_READ_UNCOMMITTED)
578 cout << "Read Uncommitted, ";
579 if (dbInf.txnIsolationOptions & SQL_TXN_READ_COMMITTED)
580 cout << "Read Committed, ";
581 if (dbInf.txnIsolationOptions & SQL_TXN_REPEATABLE_READ)
582 cout << "Repeatable Read, ";
583 if (dbInf.txnIsolationOptions & SQL_TXN_SERIALIZABLE)
584 cout << "Serializable, ";
585#ifdef ODBC_V20
586 if (dbInf.txnIsolationOptions & SQL_TXN_VERSIONING)
587 cout << "Versioning";
588#endif
589 cout << endl;
590
591 cout << "Fetch Directions Supported:" << endl << " ";
592 if (dbInf.fetchDirections & SQL_FD_FETCH_NEXT)
593 cout << "Next, ";
594 if (dbInf.fetchDirections & SQL_FD_FETCH_PRIOR)
595 cout << "Prev, ";
596 if (dbInf.fetchDirections & SQL_FD_FETCH_FIRST)
597 cout << "First, ";
598 if (dbInf.fetchDirections & SQL_FD_FETCH_LAST)
599 cout << "Last, ";
600 if (dbInf.fetchDirections & SQL_FD_FETCH_ABSOLUTE)
601 cout << "Absolute, ";
602 if (dbInf.fetchDirections & SQL_FD_FETCH_RELATIVE)
603 cout << "Relative, ";
604#ifdef ODBC_V20
605 if (dbInf.fetchDirections & SQL_FD_FETCH_RESUME)
606 cout << "Resume, ";
607#endif
608 if (dbInf.fetchDirections & SQL_FD_FETCH_BOOKMARK)
609 cout << "Bookmark";
610 cout << endl;
611
612 cout << "Lock Types Supported (SQLSetPos): ";
613 if (dbInf.lockTypes & SQL_LCK_NO_CHANGE)
614 cout << "No Change, ";
615 if (dbInf.lockTypes & SQL_LCK_EXCLUSIVE)
616 cout << "Exclusive, ";
617 if (dbInf.lockTypes & SQL_LCK_UNLOCK)
618 cout << "UnLock";
619 cout << endl;
620
621 cout << "Position Operations Supported (SQLSetPos): ";
622 if (dbInf.posOperations & SQL_POS_POSITION)
623 cout << "Position, ";
624 if (dbInf.posOperations & SQL_POS_REFRESH)
625 cout << "Refresh, ";
626 if (dbInf.posOperations & SQL_POS_UPDATE)
627 cout << "Upd, ";
628 if (dbInf.posOperations & SQL_POS_DELETE)
629 cout << "Del, ";
630 if (dbInf.posOperations & SQL_POS_ADD)
631 cout << "Add";
632 cout << endl;
633
634 cout << "Positioned Statements Supported: ";
635 if (dbInf.posStmts & SQL_PS_POSITIONED_DELETE)
636 cout << "Pos delete, ";
637 if (dbInf.posStmts & SQL_PS_POSITIONED_UPDATE)
638 cout << "Pos update, ";
639 if (dbInf.posStmts & SQL_PS_SELECT_FOR_UPDATE)
640 cout << "Select for update";
641 cout << endl;
642
643 cout << "Scroll Concurrency: ";
644 if (dbInf.scrollConcurrency & SQL_SCCO_READ_ONLY)
645 cout << "Read Only, ";
646 if (dbInf.scrollConcurrency & SQL_SCCO_LOCK)
647 cout << "Lock, ";
648 if (dbInf.scrollConcurrency & SQL_SCCO_OPT_ROWVER)
649 cout << "Opt. Rowver, ";
650 if (dbInf.scrollConcurrency & SQL_SCCO_OPT_VALUES)
651 cout << "Opt. Values";
652 cout << endl;
653
654 cout << "Scroll Options: ";
655 if (dbInf.scrollOptions & SQL_SO_FORWARD_ONLY)
656 cout << "Fwd Only, ";
657 if (dbInf.scrollOptions & SQL_SO_STATIC)
658 cout << "Static, ";
659 if (dbInf.scrollOptions & SQL_SO_KEYSET_DRIVEN)
660 cout << "Keyset Driven, ";
661 if (dbInf.scrollOptions & SQL_SO_DYNAMIC)
662 cout << "Dynamic, ";
663 if (dbInf.scrollOptions & SQL_SO_MIXED)
664 cout << "Mixed";
665 cout << endl;
666
667 cout << "Static Sensitivity: ";
668 if (dbInf.staticSensitivity & SQL_SS_ADDITIONS)
669 cout << "Additions, ";
670 if (dbInf.staticSensitivity & SQL_SS_DELETIONS)
671 cout << "Deletions, ";
672 if (dbInf.staticSensitivity & SQL_SS_UPDATES)
673 cout << "Updates";
674 cout << endl;
675
676 cout << "Transaction Capable?: ";
677 switch(dbInf.txnCapable)
678 {
679 case SQL_TC_NONE: cout << "No"; break;
680 case SQL_TC_DML: cout << "DML Only"; break;
681 case SQL_TC_DDL_COMMIT: cout << "DDL Commit"; break;
682 case SQL_TC_DDL_IGNORE: cout << "DDL Ignore"; break;
683 case SQL_TC_ALL: cout << "DDL & DML"; break;
684 }
685 cout << endl;
686
687 cout << endl;
688
689#endif
690
691 // Completed Successfully
692 return(TRUE);
693
694} // wxDB::getDbInfo()
695
696/********** wxDB::getDataTypeInfo() **********/
697bool wxDB::getDataTypeInfo(SWORD fSqlType, SqlTypeInfo &structSQLTypeInfo)
698{
699 // fSqlType will be something like SQL_VARCHAR. This parameter determines
700 // the data type inf. is gathered for.
701 //
702 // SqlTypeInfo is a structure that is filled in with data type information,
703
704 RETCODE retcode;
705 SDWORD cbRet;
706
707 // Get information about the data type specified
708 if (SQLGetTypeInfo(hstmt, fSqlType) != SQL_SUCCESS)
709 return(DispAllErrors(henv, hdbc, hstmt));
710 // Fetch the record
711 if ((retcode = SQLFetch(hstmt)) != SQL_SUCCESS)
712 {
1fc5dd6f 713#ifdef DBDEBUG_CONSOLE
108106cf
JS
714 if (retcode == SQL_NO_DATA_FOUND)
715 cout << "SQL_NO_DATA_FOUND fetching inf. about data type." << endl;
716#endif
717 DispAllErrors(henv, hdbc, hstmt);
718 SQLFreeStmt(hstmt, SQL_CLOSE);
719 return(FALSE);
720 }
721 // Obtain columns from the record
7e616b10 722 if (SQLGetData(hstmt, 1, SQL_C_CHAR, (UCHAR*) structSQLTypeInfo.TypeName, DB_TYPE_NAME_LEN, &cbRet) != SQL_SUCCESS)
108106cf 723 return(DispAllErrors(henv, hdbc, hstmt));
7e616b10 724 if (SQLGetData(hstmt, 3, SQL_C_LONG, (UCHAR*) &structSQLTypeInfo.Precision, 0, &cbRet) != SQL_SUCCESS)
108106cf 725 return(DispAllErrors(henv, hdbc, hstmt));
7e616b10 726 if (SQLGetData(hstmt, 8, SQL_C_SHORT, (UCHAR*) &structSQLTypeInfo.CaseSensitive, 0, &cbRet) != SQL_SUCCESS)
108106cf 727 return(DispAllErrors(henv, hdbc, hstmt));
7e616b10 728// if (SQLGetData(hstmt, 14, SQL_C_SHORT, (UCHAR*) &structSQLTypeInfo.MinimumScale, 0, &cbRet) != SQL_SUCCESS)
108106cf 729// return(DispAllErrors(henv, hdbc, hstmt));
a2115c88
GT
730
731//#ifdef __UNIX__ // BJO : IODBC knows about 5, not 15...
732// if (SQLGetData(hstmt, 5, SQL_C_SHORT,(UCHAR*) &structSQLTypeInfo.MaximumScale, 0, &cbRet) != SQL_SUCCESS)
733// return(DispAllErrors(henv, hdbc, hstmt));
734//#else
735 if (SQLGetData(hstmt, 15, SQL_C_SHORT,(UCHAR*) &structSQLTypeInfo.MaximumScale, 0, &cbRet) != SQL_SUCCESS)
108106cf 736 return(DispAllErrors(henv, hdbc, hstmt));
a2115c88 737//#endif
108106cf
JS
738
739 if (structSQLTypeInfo.MaximumScale < 0)
740 structSQLTypeInfo.MaximumScale = 0;
741
742 // Close the statement handle which closes open cursors
743 if (SQLFreeStmt(hstmt, SQL_CLOSE) != SQL_SUCCESS)
744 return(DispAllErrors(henv, hdbc, hstmt));
745
746 // Completed Successfully
747 return(TRUE);
748
749} // wxDB::getDataTypeInfo()
750
751/********** wxDB::Close() **********/
752void wxDB::Close(void)
753{
1fc5dd6f
JS
754 // Close the Sql Log file
755 if (fpSqlLog)
756 {
757 fclose(fpSqlLog);
a2115c88 758 fpSqlLog = 0;
1fc5dd6f
JS
759 }
760
108106cf
JS
761 // Free statement handle
762 if (dbIsOpen)
763 {
764 if (SQLFreeStmt(hstmt, SQL_DROP) != SQL_SUCCESS)
765 DispAllErrors(henv, hdbc);
766 }
767
768 // Disconnect from the datasource
769 if (SQLDisconnect(hdbc) != SQL_SUCCESS)
770 DispAllErrors(henv, hdbc);
771
772 // Free the connection to the datasource
773 if (SQLFreeConnect(hdbc) != SQL_SUCCESS)
774 DispAllErrors(henv, hdbc);
775
a2115c88
GT
776 // There should be zero Ctable objects still connected to this db object
777 assert(nTables == 0);
778
e041ce57 779#ifdef __WXDEBUG__
a2115c88
GT
780 CstructTablesInUse *tiu;
781 wxNode *pNode;
782 pNode = TablesInUse.First();
783 char s[80];
784 char s2[80];
785 while (pNode)
786 {
787 tiu = (CstructTablesInUse *)pNode->Data();
788 if (tiu->pDb == this)
789 {
12234741
VZ
790 sprintf(s, "(%-20s) tableID:[%6lu] pDb:[%p]", tiu->tableName,tiu->tableID,tiu->pDb);
791 sprintf(s2,"Orphaned found using pDb:[%p]",this);
a2115c88
GT
792 wxMessageBox (s,s2);
793 }
794 pNode = pNode->Next();
795 }
796#endif
797
798 // Copy the error messages to a global variable
9c40a88d
GT
799 int i;
800 for (i = 0; i < DB_MAX_ERROR_HISTORY; i++)
a2115c88
GT
801 strcpy(DBerrorList[i],errorList[i]);
802
108106cf
JS
803} // wxDB::Close()
804
805/********** wxDB::CommitTrans() **********/
806bool wxDB::CommitTrans(void)
807{
a2115c88
GT
808 if (this)
809 {
810 // Commit the transaction
811 if (SQLTransact(henv, hdbc, SQL_COMMIT) != SQL_SUCCESS)
812 return(DispAllErrors(henv, hdbc));
813 }
108106cf
JS
814
815 // Completed successfully
816 return(TRUE);
817
818} // wxDB::CommitTrans()
819
820/********** wxDB::RollbackTrans() **********/
821bool wxDB::RollbackTrans(void)
822{
823 // Rollback the transaction
824 if (SQLTransact(henv, hdbc, SQL_ROLLBACK) != SQL_SUCCESS)
825 return(DispAllErrors(henv, hdbc));
826
827 // Completed successfully
828 return(TRUE);
829
830} // wxDB::RollbackTrans()
831
832/********** wxDB::DispAllErrors() **********/
833bool wxDB::DispAllErrors(HENV aHenv, HDBC aHdbc, HSTMT aHstmt)
834{
835 char odbcErrMsg[DB_MAX_ERROR_MSG_LEN];
836
837 while (SQLError(aHenv, aHdbc, aHstmt, (UCHAR FAR *) sqlState, &nativeError, (UCHAR FAR *) errorMsg, SQL_MAX_MESSAGE_LENGTH - 1, &cbErrorMsg) == SQL_SUCCESS)
838 {
839 sprintf(odbcErrMsg, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState, nativeError, errorMsg);
840 logError(odbcErrMsg, sqlState);
841 if (!silent)
842 {
1fc5dd6f 843#ifdef DBDEBUG_CONSOLE
108106cf
JS
844 // When run in console mode, use standard out to display errors.
845 cout << odbcErrMsg << endl;
846 cout << "Press any key to continue..." << endl;
847 getchar();
848#endif
849 }
a2115c88
GT
850
851#ifdef __WXDEBUG__
852 wxMessageBox(odbcErrMsg);
853#endif
108106cf
JS
854 }
855
a2115c88 856 return(FALSE); // This function always returns false.
108106cf
JS
857
858} // wxDB::DispAllErrors()
859
860/********** wxDB::GetNextError() **********/
861bool wxDB::GetNextError(HENV aHenv, HDBC aHdbc, HSTMT aHstmt)
862{
863 if (SQLError(aHenv, aHdbc, aHstmt, (UCHAR FAR *) sqlState, &nativeError, (UCHAR FAR *) errorMsg, SQL_MAX_MESSAGE_LENGTH - 1, &cbErrorMsg) == SQL_SUCCESS)
864 return(TRUE);
865 else
866 return(FALSE);
867
868} // wxDB::GetNextError()
869
870/********** wxDB::DispNextError() **********/
871void wxDB::DispNextError(void)
872{
873 char odbcErrMsg[DB_MAX_ERROR_MSG_LEN];
874
875 sprintf(odbcErrMsg, "SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState, nativeError, errorMsg);
876 logError(odbcErrMsg, sqlState);
877
878 if (silent)
879 return;
880
1fc5dd6f 881#ifdef DBDEBUG_CONSOLE
108106cf
JS
882 // When run in console mode, use standard out to display errors.
883 cout << odbcErrMsg << endl;
884 cout << "Press any key to continue..." << endl;
885 getchar();
886#endif
887
888} // wxDB::DispNextError()
889
890/********** wxDB::logError() **********/
891void wxDB::logError(char *errMsg, char *SQLState)
892{
893 assert(errMsg && strlen(errMsg));
894
895 static int pLast = -1;
896 int dbStatus;
897
898 if (++pLast == DB_MAX_ERROR_HISTORY)
899 {
a2115c88
GT
900 int i;
901 for (i = 0; i < DB_MAX_ERROR_HISTORY; i++)
108106cf
JS
902 strcpy(errorList[i], errorList[i+1]);
903 pLast--;
904 }
905
906 strcpy(errorList[pLast], errMsg);
907
908 if (SQLState && strlen(SQLState))
909 if ((dbStatus = TranslateSqlState(SQLState)) != DB_ERR_FUNCTION_SEQUENCE_ERROR)
910 DB_STATUS = dbStatus;
911
a2115c88
GT
912 // Add the errmsg to the sql log
913 WriteSqlLog(errMsg);
914
108106cf
JS
915} // wxDB::logError()
916
917/**********wxDB::TranslateSqlState() **********/
918int wxDB::TranslateSqlState(char *SQLState)
919{
9199e66f 920 if (!wxStrcmp(SQLState, "01000"))
108106cf 921 return(DB_ERR_GENERAL_WARNING);
9199e66f 922 if (!wxStrcmp(SQLState, "01002"))
108106cf 923 return(DB_ERR_DISCONNECT_ERROR);
9199e66f 924 if (!wxStrcmp(SQLState, "01004"))
108106cf 925 return(DB_ERR_DATA_TRUNCATED);
9199e66f 926 if (!wxStrcmp(SQLState, "01006"))
108106cf 927 return(DB_ERR_PRIV_NOT_REVOKED);
9199e66f 928 if (!wxStrcmp(SQLState, "01S00"))
108106cf 929 return(DB_ERR_INVALID_CONN_STR_ATTR);
9199e66f 930 if (!wxStrcmp(SQLState, "01S01"))
108106cf 931 return(DB_ERR_ERROR_IN_ROW);
9199e66f 932 if (!wxStrcmp(SQLState, "01S02"))
108106cf 933 return(DB_ERR_OPTION_VALUE_CHANGED);
9199e66f 934 if (!wxStrcmp(SQLState, "01S03"))
108106cf 935 return(DB_ERR_NO_ROWS_UPD_OR_DEL);
9199e66f 936 if (!wxStrcmp(SQLState, "01S04"))
108106cf 937 return(DB_ERR_MULTI_ROWS_UPD_OR_DEL);
9199e66f 938 if (!wxStrcmp(SQLState, "07001"))
108106cf 939 return(DB_ERR_WRONG_NO_OF_PARAMS);
9199e66f 940 if (!wxStrcmp(SQLState, "07006"))
108106cf 941 return(DB_ERR_DATA_TYPE_ATTR_VIOL);
9199e66f 942 if (!wxStrcmp(SQLState, "08001"))
108106cf 943 return(DB_ERR_UNABLE_TO_CONNECT);
9199e66f 944 if (!wxStrcmp(SQLState, "08002"))
108106cf 945 return(DB_ERR_CONNECTION_IN_USE);
9199e66f 946 if (!wxStrcmp(SQLState, "08003"))
108106cf 947 return(DB_ERR_CONNECTION_NOT_OPEN);
9199e66f 948 if (!wxStrcmp(SQLState, "08004"))
108106cf 949 return(DB_ERR_REJECTED_CONNECTION);
9199e66f 950 if (!wxStrcmp(SQLState, "08007"))
108106cf 951 return(DB_ERR_CONN_FAIL_IN_TRANS);
9199e66f 952 if (!wxStrcmp(SQLState, "08S01"))
108106cf 953 return(DB_ERR_COMM_LINK_FAILURE);
9199e66f 954 if (!wxStrcmp(SQLState, "21S01"))
108106cf 955 return(DB_ERR_INSERT_VALUE_LIST_MISMATCH);
9199e66f 956 if (!wxStrcmp(SQLState, "21S02"))
108106cf 957 return(DB_ERR_DERIVED_TABLE_MISMATCH);
9199e66f 958 if (!wxStrcmp(SQLState, "22001"))
108106cf 959 return(DB_ERR_STRING_RIGHT_TRUNC);
9199e66f 960 if (!wxStrcmp(SQLState, "22003"))
108106cf 961 return(DB_ERR_NUMERIC_VALUE_OUT_OF_RNG);
9199e66f 962 if (!wxStrcmp(SQLState, "22005"))
108106cf 963 return(DB_ERR_ERROR_IN_ASSIGNMENT);
9199e66f 964 if (!wxStrcmp(SQLState, "22008"))
108106cf 965 return(DB_ERR_DATETIME_FLD_OVERFLOW);
9199e66f 966 if (!wxStrcmp(SQLState, "22012"))
108106cf 967 return(DB_ERR_DIVIDE_BY_ZERO);
9199e66f 968 if (!wxStrcmp(SQLState, "22026"))
108106cf 969 return(DB_ERR_STR_DATA_LENGTH_MISMATCH);
9199e66f 970 if (!wxStrcmp(SQLState, "23000"))
108106cf 971 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL);
9199e66f 972 if (!wxStrcmp(SQLState, "24000"))
108106cf 973 return(DB_ERR_INVALID_CURSOR_STATE);
9199e66f 974 if (!wxStrcmp(SQLState, "25000"))
108106cf 975 return(DB_ERR_INVALID_TRANS_STATE);
9199e66f 976 if (!wxStrcmp(SQLState, "28000"))
108106cf 977 return(DB_ERR_INVALID_AUTH_SPEC);
9199e66f 978 if (!wxStrcmp(SQLState, "34000"))
108106cf 979 return(DB_ERR_INVALID_CURSOR_NAME);
9199e66f 980 if (!wxStrcmp(SQLState, "37000"))
108106cf 981 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL);
9199e66f 982 if (!wxStrcmp(SQLState, "3C000"))
108106cf 983 return(DB_ERR_DUPLICATE_CURSOR_NAME);
9199e66f 984 if (!wxStrcmp(SQLState, "40001"))
108106cf 985 return(DB_ERR_SERIALIZATION_FAILURE);
9199e66f 986 if (!wxStrcmp(SQLState, "42000"))
108106cf 987 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2);
9199e66f 988 if (!wxStrcmp(SQLState, "70100"))
108106cf 989 return(DB_ERR_OPERATION_ABORTED);
9199e66f 990 if (!wxStrcmp(SQLState, "IM001"))
108106cf 991 return(DB_ERR_UNSUPPORTED_FUNCTION);
9199e66f 992 if (!wxStrcmp(SQLState, "IM002"))
108106cf 993 return(DB_ERR_NO_DATA_SOURCE);
9199e66f 994 if (!wxStrcmp(SQLState, "IM003"))
108106cf 995 return(DB_ERR_DRIVER_LOAD_ERROR);
9199e66f 996 if (!wxStrcmp(SQLState, "IM004"))
108106cf 997 return(DB_ERR_SQLALLOCENV_FAILED);
9199e66f 998 if (!wxStrcmp(SQLState, "IM005"))
108106cf 999 return(DB_ERR_SQLALLOCCONNECT_FAILED);
9199e66f 1000 if (!wxStrcmp(SQLState, "IM006"))
108106cf 1001 return(DB_ERR_SQLSETCONNECTOPTION_FAILED);
9199e66f 1002 if (!wxStrcmp(SQLState, "IM007"))
108106cf 1003 return(DB_ERR_NO_DATA_SOURCE_DLG_PROHIB);
9199e66f 1004 if (!wxStrcmp(SQLState, "IM008"))
108106cf 1005 return(DB_ERR_DIALOG_FAILED);
9199e66f 1006 if (!wxStrcmp(SQLState, "IM009"))
108106cf 1007 return(DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL);
9199e66f 1008 if (!wxStrcmp(SQLState, "IM010"))
108106cf 1009 return(DB_ERR_DATA_SOURCE_NAME_TOO_LONG);
9199e66f 1010 if (!wxStrcmp(SQLState, "IM011"))
108106cf 1011 return(DB_ERR_DRIVER_NAME_TOO_LONG);
9199e66f 1012 if (!wxStrcmp(SQLState, "IM012"))
108106cf 1013 return(DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR);
9199e66f 1014 if (!wxStrcmp(SQLState, "IM013"))
108106cf 1015 return(DB_ERR_TRACE_FILE_ERROR);
9199e66f 1016 if (!wxStrcmp(SQLState, "S0001"))
108106cf 1017 return(DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS);
9199e66f 1018 if (!wxStrcmp(SQLState, "S0002"))
108106cf 1019 return(DB_ERR_TABLE_NOT_FOUND);
9199e66f 1020 if (!wxStrcmp(SQLState, "S0011"))
108106cf 1021 return(DB_ERR_INDEX_ALREADY_EXISTS);
9199e66f 1022 if (!wxStrcmp(SQLState, "S0012"))
108106cf 1023 return(DB_ERR_INDEX_NOT_FOUND);
9199e66f 1024 if (!wxStrcmp(SQLState, "S0021"))
108106cf 1025 return(DB_ERR_COLUMN_ALREADY_EXISTS);
9199e66f 1026 if (!wxStrcmp(SQLState, "S0022"))
108106cf 1027 return(DB_ERR_COLUMN_NOT_FOUND);
9199e66f 1028 if (!wxStrcmp(SQLState, "S0023"))
108106cf 1029 return(DB_ERR_NO_DEFAULT_FOR_COLUMN);
9199e66f 1030 if (!wxStrcmp(SQLState, "S1000"))
108106cf 1031 return(DB_ERR_GENERAL_ERROR);
9199e66f 1032 if (!wxStrcmp(SQLState, "S1001"))
108106cf 1033 return(DB_ERR_MEMORY_ALLOCATION_FAILURE);
9199e66f 1034 if (!wxStrcmp(SQLState, "S1002"))
108106cf 1035 return(DB_ERR_INVALID_COLUMN_NUMBER);
9199e66f 1036 if (!wxStrcmp(SQLState, "S1003"))
108106cf 1037 return(DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE);
9199e66f 1038 if (!wxStrcmp(SQLState, "S1004"))
108106cf 1039 return(DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE);
9199e66f 1040 if (!wxStrcmp(SQLState, "S1008"))
108106cf 1041 return(DB_ERR_OPERATION_CANCELLED);
9199e66f 1042 if (!wxStrcmp(SQLState, "S1009"))
108106cf 1043 return(DB_ERR_INVALID_ARGUMENT_VALUE);
9199e66f 1044 if (!wxStrcmp(SQLState, "S1010"))
108106cf 1045 return(DB_ERR_FUNCTION_SEQUENCE_ERROR);
9199e66f 1046 if (!wxStrcmp(SQLState, "S1011"))
108106cf 1047 return(DB_ERR_OPERATION_INVALID_AT_THIS_TIME);
9199e66f 1048 if (!wxStrcmp(SQLState, "S1012"))
108106cf 1049 return(DB_ERR_INVALID_TRANS_OPERATION_CODE);
9199e66f 1050 if (!wxStrcmp(SQLState, "S1015"))
108106cf 1051 return(DB_ERR_NO_CURSOR_NAME_AVAIL);
9199e66f 1052 if (!wxStrcmp(SQLState, "S1090"))
108106cf 1053 return(DB_ERR_INVALID_STR_OR_BUF_LEN);
9199e66f 1054 if (!wxStrcmp(SQLState, "S1091"))
108106cf 1055 return(DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE);
9199e66f 1056 if (!wxStrcmp(SQLState, "S1092"))
108106cf 1057 return(DB_ERR_OPTION_TYPE_OUT_OF_RANGE);
9199e66f 1058 if (!wxStrcmp(SQLState, "S1093"))
108106cf 1059 return(DB_ERR_INVALID_PARAM_NO);
9199e66f 1060 if (!wxStrcmp(SQLState, "S1094"))
108106cf 1061 return(DB_ERR_INVALID_SCALE_VALUE);
9199e66f 1062 if (!wxStrcmp(SQLState, "S1095"))
108106cf 1063 return(DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE);
9199e66f 1064 if (!wxStrcmp(SQLState, "S1096"))
108106cf 1065 return(DB_ERR_INF_TYPE_OUT_OF_RANGE);
9199e66f 1066 if (!wxStrcmp(SQLState, "S1097"))
108106cf 1067 return(DB_ERR_COLUMN_TYPE_OUT_OF_RANGE);
9199e66f 1068 if (!wxStrcmp(SQLState, "S1098"))
108106cf 1069 return(DB_ERR_SCOPE_TYPE_OUT_OF_RANGE);
9199e66f 1070 if (!wxStrcmp(SQLState, "S1099"))
108106cf 1071 return(DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE);
9199e66f 1072 if (!wxStrcmp(SQLState, "S1100"))
108106cf 1073 return(DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE);
9199e66f 1074 if (!wxStrcmp(SQLState, "S1101"))
108106cf 1075 return(DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE);
9199e66f 1076 if (!wxStrcmp(SQLState, "S1103"))
108106cf 1077 return(DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE);
9199e66f 1078 if (!wxStrcmp(SQLState, "S1104"))
108106cf 1079 return(DB_ERR_INVALID_PRECISION_VALUE);
9199e66f 1080 if (!wxStrcmp(SQLState, "S1105"))
108106cf 1081 return(DB_ERR_INVALID_PARAM_TYPE);
9199e66f 1082 if (!wxStrcmp(SQLState, "S1106"))
108106cf 1083 return(DB_ERR_FETCH_TYPE_OUT_OF_RANGE);
9199e66f 1084 if (!wxStrcmp(SQLState, "S1107"))
108106cf 1085 return(DB_ERR_ROW_VALUE_OUT_OF_RANGE);
9199e66f 1086 if (!wxStrcmp(SQLState, "S1108"))
108106cf 1087 return(DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE);
9199e66f 1088 if (!wxStrcmp(SQLState, "S1109"))
108106cf 1089 return(DB_ERR_INVALID_CURSOR_POSITION);
9199e66f 1090 if (!wxStrcmp(SQLState, "S1110"))
108106cf 1091 return(DB_ERR_INVALID_DRIVER_COMPLETION);
9199e66f 1092 if (!wxStrcmp(SQLState, "S1111"))
108106cf 1093 return(DB_ERR_INVALID_BOOKMARK_VALUE);
9199e66f 1094 if (!wxStrcmp(SQLState, "S1C00"))
108106cf 1095 return(DB_ERR_DRIVER_NOT_CAPABLE);
9199e66f 1096 if (!wxStrcmp(SQLState, "S1T00"))
108106cf
JS
1097 return(DB_ERR_TIMEOUT_EXPIRED);
1098
1099 // No match
1100 return(0);
1101
1102} // wxDB::TranslateSqlState()
1103
1104/********** wxDB::Grant() **********/
1105bool wxDB::Grant(int privileges, char *tableName, char *userList)
1106{
1107 char sqlStmt[DB_MAX_STATEMENT_LEN];
1108
1109 // Build the grant statement
1110 strcpy(sqlStmt, "GRANT ");
1111 if (privileges == DB_GRANT_ALL)
1112 strcat(sqlStmt, "ALL");
1113 else
1114 {
1115 int c = 0;
1116 if (privileges & DB_GRANT_SELECT)
1117 {
a2115c88 1118 strcat(sqlStmt, "SELECT");
108106cf
JS
1119 c++;
1120 }
1121 if (privileges & DB_GRANT_INSERT)
1122 {
1123 if (c++)
1124 strcat(sqlStmt, ", ");
a2115c88 1125 strcat(sqlStmt, "INSERT");
108106cf
JS
1126 }
1127 if (privileges & DB_GRANT_UPDATE)
1128 {
1129 if (c++)
1130 strcat(sqlStmt, ", ");
1131 strcat(sqlStmt, "UPDATE");
1132 }
1133 if (privileges & DB_GRANT_DELETE)
1134 {
1135 if (c++)
1136 strcat(sqlStmt, ", ");
1137 strcat(sqlStmt, "DELETE");
1138 }
1139 }
1140
1141 strcat(sqlStmt, " ON ");
1142 strcat(sqlStmt, tableName);
1143 strcat(sqlStmt, " TO ");
1144 strcat(sqlStmt, userList);
1145
1fc5dd6f 1146#ifdef DBDEBUG_CONSOLE
108106cf
JS
1147 cout << endl << sqlStmt << endl;
1148#endif
1149
1fc5dd6f
JS
1150 WriteSqlLog(sqlStmt);
1151
108106cf
JS
1152 return(ExecSql(sqlStmt));
1153
1154} // wxDB::Grant()
1155
1156/********** wxDB::CreateView() **********/
a2115c88 1157bool wxDB::CreateView(char *viewName, char *colList, char *pSqlStmt, bool attemptDrop)
108106cf
JS
1158{
1159 char sqlStmt[DB_MAX_STATEMENT_LEN];
1160
1161 // Drop the view first
a2115c88
GT
1162 if (attemptDrop && !DropView(viewName))
1163 return FALSE;
108106cf
JS
1164
1165 // Build the create view statement
1166 strcpy(sqlStmt, "CREATE VIEW ");
1167 strcat(sqlStmt, viewName);
1168
1169 if (strlen(colList))
1170 {
1171 strcat(sqlStmt, " (");
1172 strcat(sqlStmt, colList);
1173 strcat(sqlStmt, ")");
1174 }
1175
1176 strcat(sqlStmt, " AS ");
1177 strcat(sqlStmt, pSqlStmt);
1178
1fc5dd6f
JS
1179 WriteSqlLog(sqlStmt);
1180
1181#ifdef DBDEBUG_CONSOLE
108106cf
JS
1182 cout << sqlStmt << endl;
1183#endif
1184
1185 return(ExecSql(sqlStmt));
1186
1187} // wxDB::CreateView()
1188
a2115c88
GT
1189/********** wxDB::DropView() **********/
1190bool wxDB::DropView(char *viewName)
1191{
1192 // NOTE: This function returns TRUE if the View does not exist, but
1193 // only for identified databases. Code will need to be added
1194 // below for any other databases when those databases are defined
1195 // to handle this situation consistently
1196
1197 char sqlStmt[DB_MAX_STATEMENT_LEN];
1198
1199 sprintf(sqlStmt, "DROP VIEW %s", viewName);
1200
1201 WriteSqlLog(sqlStmt);
1202
1203#ifdef DBDEBUG_CONSOLE
1204 cout << endl << sqlStmt << endl;
1205#endif
1206
1207 if (SQLExecDirect(hstmt, (UCHAR FAR *) sqlStmt, SQL_NTS) != SQL_SUCCESS)
1208 {
1209 // Check for "Base table not found" error and ignore
1210 GetNextError(henv, hdbc, hstmt);
9199e66f 1211 if (wxStrcmp(sqlState,"S0002")) // "Base table not found"
a2115c88
GT
1212 {
1213 // Check for product specific error codes
9199e66f 1214 if (!((Dbms() == dbmsSYBASE_ASA && !wxStrcmp(sqlState,"42000")))) // 5.x (and lower?)
a2115c88
GT
1215 {
1216 DispNextError();
1217 DispAllErrors(henv, hdbc, hstmt);
1218 RollbackTrans();
1219 return(FALSE);
1220 }
1221 }
1222 }
1223
1224 // Commit the transaction
1225 if (! CommitTrans())
1226 return(FALSE);
1227
1228 return TRUE;
1229
1230} // wxDB::DropView()
1231
1232
108106cf
JS
1233/********** wxDB::ExecSql() **********/
1234bool wxDB::ExecSql(char *pSqlStmt)
1235{
a2115c88 1236 SQLFreeStmt(hstmt, SQL_CLOSE);
108106cf
JS
1237 if (SQLExecDirect(hstmt, (UCHAR FAR *) pSqlStmt, SQL_NTS) == SQL_SUCCESS)
1238 return(TRUE);
1239 else
1240 {
1241 DispAllErrors(henv, hdbc, hstmt);
1242 return(FALSE);
1243 }
1244
1245} // wxDB::ExecSql()
1246
a2115c88
GT
1247/********** wxDB::GetNext() **********/
1248bool wxDB::GetNext(void)
1249{
1250 if (SQLFetch(hstmt) == SQL_SUCCESS)
1251 return(TRUE);
1252 else
1253 {
1254 DispAllErrors(henv, hdbc, hstmt);
1255 return(FALSE);
1256 }
1257
1258} // wxDB::GetNext()
1259
1260/********** wxDB::GetData() **********/
1261bool wxDB::GetData(UWORD colNo, SWORD cType, PTR pData, SDWORD maxLen, SDWORD FAR *cbReturned)
1262{
1263 assert(pData);
1264 assert(cbReturned);
1265
1266 if (SQLGetData(hstmt, colNo, cType, pData, maxLen, cbReturned) == SQL_SUCCESS)
1267 return(TRUE);
1268 else
1269 {
1270 DispAllErrors(henv, hdbc, hstmt);
1271 return(FALSE);
1272 }
1273
1274} // wxDB::GetData()
1275
108106cf
JS
1276/********** wxDB::GetColumns() **********/
1277/*
1278 * 1) The last array element of the tableName[] argument must be zero (null).
1279 * This is how the end of the array is detected.
1280 * 2) This function returns an array of CcolInf structures. If no columns
1281 * were found, or an error occured, this pointer will be zero (null). THE
1282 * CALLING FUNCTION IS RESPONSIBLE FOR DELETING THE MEMORY RETURNED WHEN IT
1283 * IS FINISHED WITH IT. i.e.
1284 *
a2115c88 1285 * CcolInf *colInf = pDb->GetColumns(tableList, userID);
108106cf
JS
1286 * if (colInf)
1287 * {
1288 * // Use the column inf
1289 * .......
1290 * // Destroy the memory
1291 * delete [] colInf;
1292 * }
1293 */
a2115c88 1294CcolInf *wxDB::GetColumns(char *tableName[], char *userID)
108106cf
JS
1295{
1296 UINT noCols = 0;
1297 UINT colNo = 0;
1298 CcolInf *colInf = 0;
1299 RETCODE retcode;
1300 SDWORD cb;
1301 char tblName[DB_MAX_TABLE_NAME_LEN+1];
1302 char colName[DB_MAX_COLUMN_NAME_LEN+1];
1303 SWORD sqlDataType;
a2115c88
GT
1304 char userIdUC[80+1];
1305 char tableNameUC[DB_MAX_TABLE_NAME_LEN+1];
1306
1307 if (!userID || !strlen(userID))
1308 userID = uid;
1309
1310 // dBase does not use user names, and some drivers fail if you try to pass one
1311 if (Dbms() == dbmsDBASE)
1312 userID = "";
1313
1314 // Oracle user names may only be in uppercase, so force
1315 // the name to uppercase
1316 if (Dbms() == dbmsORACLE)
1317 {
1318 int i = 0;
1319 for (char *p = userID; *p; p++)
1320 userIdUC[i++] = toupper(*p);
1321 userIdUC[i] = 0;
1322 userID = userIdUC;
1323 }
108106cf
JS
1324
1325 // Pass 1 - Determine how many columns there are.
1326 // Pass 2 - Allocate the CcolInf array and fill in
1327 // the array with the column information.
a2115c88
GT
1328 int pass;
1329 for (pass = 1; pass <= 2; pass++)
108106cf
JS
1330 {
1331 if (pass == 2)
1332 {
1333 if (noCols == 0) // Probably a bogus table name(s)
1334 break;
1335 // Allocate n CcolInf objects to hold the column information
1336 colInf = new CcolInf[noCols+1];
1337 if (!colInf)
1338 break;
1339 // Mark the end of the array
1340 strcpy(colInf[noCols].tableName, "");
1341 strcpy(colInf[noCols].colName, "");
1342 colInf[noCols].sqlDataType = 0;
1343 }
1344 // Loop through each table name
a2115c88
GT
1345 int tbl;
1346 for (tbl = 0; tableName[tbl]; tbl++)
108106cf 1347 {
a2115c88
GT
1348 // Oracle table names are uppercase only, so force
1349 // the name to uppercase just in case programmer forgot to do this
1350 if (Dbms() == dbmsORACLE)
1351 {
1352 int i = 0;
1353 for (char *p = tableName[tbl]; *p; p++)
1354 tableNameUC[i++] = toupper(*p);
1355 tableNameUC[i] = 0;
1356 }
1357 else
1358 sprintf(tableNameUC,tableName[tbl]);
1359
108106cf 1360 SQLFreeStmt(hstmt, SQL_CLOSE);
a2115c88
GT
1361
1362 // MySQL and Access cannot accept a user name when looking up column names, so we
1363 // use the call below that leaves out the user name
9199e66f 1364 if (wxStrcmp(userID,"") &&
a2115c88
GT
1365 Dbms() != dbmsMY_SQL &&
1366 Dbms() != dbmsACCESS)
1367 {
1368 retcode = SQLColumns(hstmt,
1369 NULL, 0, // All qualifiers
1370 (UCHAR *) userID, SQL_NTS, // Owner
1371 (UCHAR *) tableNameUC, SQL_NTS,
1372 NULL, 0); // All columns
1373 }
1374 else
1375 {
1376 retcode = SQLColumns(hstmt,
1377 NULL, 0, // All qualifiers
1378 NULL, 0, // Owner
1379 (UCHAR *) tableNameUC, SQL_NTS,
1380 NULL, 0); // All columns
1381 }
108106cf
JS
1382 if (retcode != SQL_SUCCESS)
1383 { // Error occured, abort
1384 DispAllErrors(henv, hdbc, hstmt);
1385 if (colInf)
1386 delete [] colInf;
a2115c88
GT
1387 SQLFreeStmt(hstmt, SQL_UNBIND);
1388 SQLFreeStmt(hstmt, SQL_CLOSE);
108106cf
JS
1389 return(0);
1390 }
7e616b10
RR
1391 SQLBindCol(hstmt, 3, SQL_C_CHAR, (UCHAR*) tblName, DB_MAX_TABLE_NAME_LEN+1, &cb);
1392 SQLBindCol(hstmt, 4, SQL_C_CHAR, (UCHAR*) colName, DB_MAX_COLUMN_NAME_LEN+1, &cb);
1393 SQLBindCol(hstmt, 5, SQL_C_SSHORT, (UCHAR*) &sqlDataType, 0, &cb);
108106cf
JS
1394 while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS)
1395 {
1396 if (pass == 1) // First pass, just add up the number of columns
1397 noCols++;
1398 else // Pass 2; Fill in the array of structures
1399 {
1400 if (colNo < noCols) // Some extra error checking to prevent memory overwrites
1401 {
1402 strcpy(colInf[colNo].tableName, tblName);
1403 strcpy(colInf[colNo].colName, colName);
1404 colInf[colNo].sqlDataType = sqlDataType;
1405 colNo++;
1406 }
1407 }
1408 }
1409 if (retcode != SQL_NO_DATA_FOUND)
1410 { // Error occured, abort
1411 DispAllErrors(henv, hdbc, hstmt);
1412 if (colInf)
1413 delete [] colInf;
a2115c88
GT
1414 SQLFreeStmt(hstmt, SQL_UNBIND);
1415 SQLFreeStmt(hstmt, SQL_CLOSE);
108106cf
JS
1416 return(0);
1417 }
1418 }
1419 }
1420
a2115c88 1421 SQLFreeStmt(hstmt, SQL_UNBIND);
108106cf
JS
1422 SQLFreeStmt(hstmt, SQL_CLOSE);
1423 return colInf;
1424
1425} // wxDB::GetColumns()
1426
1427
1fc5dd6f
JS
1428/********** wxDB::Catalog() **********/
1429bool wxDB::Catalog(char *userID, char *fileName)
1430{
1fc5dd6f
JS
1431 assert(fileName && strlen(fileName));
1432
1433 RETCODE retcode;
1434 SDWORD cb;
1435 char tblName[DB_MAX_TABLE_NAME_LEN+1];
1436 char tblNameSave[DB_MAX_TABLE_NAME_LEN+1];
1437 char colName[DB_MAX_COLUMN_NAME_LEN+1];
1438 SWORD sqlDataType;
a2115c88 1439 char typeName[30+1];
1fc5dd6f
JS
1440 SWORD precision, length;
1441
1442 FILE *fp = fopen(fileName,"wt");
1443 if (fp == NULL)
1444 return(FALSE);
1445
1446 SQLFreeStmt(hstmt, SQL_CLOSE);
1447
a2115c88
GT
1448 if (!userID || !strlen(userID))
1449 userID = uid;
1450
1451 char userIdUC[80+1];
1452 // Oracle user names may only be in uppercase, so force
1453 // the name to uppercase
1454 if (Dbms() == dbmsORACLE)
1455 {
1456 int i = 0;
1457 for (char *p = userID; *p; p++)
1458 userIdUC[i++] = toupper(*p);
1459 userIdUC[i] = 0;
1460 userID = userIdUC;
1461 }
1462
9199e66f 1463 if (wxStrcmp(userID,""))
a2115c88
GT
1464 {
1465 retcode = SQLColumns(hstmt,
1466 NULL, 0, // All qualifiers
1467 (UCHAR *) userID, SQL_NTS, // User specified
1468 NULL, 0, // All tables
1469 NULL, 0); // All columns
1470 }
1471 else
1472 {
1473 retcode = SQLColumns(hstmt,
1474 NULL, 0, // All qualifiers
1475 NULL, 0, // User specified
1476 NULL, 0, // All tables
1477 NULL, 0); // All columns
1478 }
1fc5dd6f
JS
1479 if (retcode != SQL_SUCCESS)
1480 {
1481 DispAllErrors(henv, hdbc, hstmt);
1482 fclose(fp);
1483 return(FALSE);
1484 }
1485
a2115c88
GT
1486 SQLBindCol(hstmt, 3, SQL_C_CHAR, (UCHAR*) tblName, DB_MAX_TABLE_NAME_LEN+1, &cb);
1487 SQLBindCol(hstmt, 4, SQL_C_CHAR, (UCHAR*) colName, DB_MAX_COLUMN_NAME_LEN+1, &cb);
7e616b10 1488 SQLBindCol(hstmt, 5, SQL_C_SSHORT, (UCHAR*) &sqlDataType, 0, &cb);
a2115c88 1489 SQLBindCol(hstmt, 6, SQL_C_CHAR, (UCHAR*) typeName, sizeof(typeName), &cb);
7e616b10
RR
1490 SQLBindCol(hstmt, 7, SQL_C_SSHORT, (UCHAR*) &precision, 0, &cb);
1491 SQLBindCol(hstmt, 8, SQL_C_SSHORT, (UCHAR*) &length, 0, &cb);
1fc5dd6f
JS
1492
1493 char outStr[256];
1494 strcpy(tblNameSave,"");
1495 int cnt = 0;
1496
1497 while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS)
1498 {
9199e66f 1499 if (wxStrcmp(tblName,tblNameSave))
1fc5dd6f
JS
1500 {
1501 if (cnt)
1502 fputs("\n", fp);
1503 fputs("================================ ", fp);
1504 fputs("================================ ", fp);
1505 fputs("===================== ", fp);
1506 fputs("========= ", fp);
1507 fputs("=========\n", fp);
1508 sprintf(outStr, "%-32s %-32s %-21s %9s %9s\n",
1509 "TABLE NAME", "COLUMN NAME", "DATA TYPE", "PRECISION", "LENGTH");
1510 fputs(outStr, fp);
1511 fputs("================================ ", fp);
1512 fputs("================================ ", fp);
1513 fputs("===================== ", fp);
1514 fputs("========= ", fp);
1515 fputs("=========\n", fp);
1516 strcpy(tblNameSave,tblName);
1517 }
1518 sprintf(outStr, "%-32s %-32s (%04d)%-15s %9d %9d\n",
1519 tblName, colName, sqlDataType, typeName, precision, length);
1520 if (fputs(outStr, fp) == EOF)
1521 {
a2115c88
GT
1522 SQLFreeStmt(hstmt, SQL_UNBIND);
1523 SQLFreeStmt(hstmt, SQL_CLOSE);
1fc5dd6f
JS
1524 fclose(fp);
1525 return(FALSE);
1526 }
1527 cnt++;
1528 }
1529
1530 if (retcode != SQL_NO_DATA_FOUND)
1fc5dd6f 1531 DispAllErrors(henv, hdbc, hstmt);
1fc5dd6f 1532
a2115c88 1533 SQLFreeStmt(hstmt, SQL_UNBIND);
1fc5dd6f 1534 SQLFreeStmt(hstmt, SQL_CLOSE);
a2115c88 1535
1fc5dd6f 1536 fclose(fp);
a2115c88 1537 return(retcode == SQL_NO_DATA_FOUND);
1fc5dd6f
JS
1538
1539} // wxDB::Catalog()
1540
1541
108106cf
JS
1542// Table name can refer to a table, view, alias or synonym. Returns true
1543// if the object exists in the database. This function does not indicate
1544// whether or not the user has privleges to query or perform other functions
1545// on the table.
a2115c88 1546bool wxDB::TableExists(char *tableName, char *userID, char *tablePath)
108106cf
JS
1547{
1548 assert(tableName && strlen(tableName));
1549
a2115c88
GT
1550 if (Dbms() == dbmsDBASE)
1551 {
1552 wxString dbName;
1553 if (tablePath && strlen(tablePath))
1554 dbName.sprintf("%s/%s.dbf",tablePath,tableName);
1555 else
1556 dbName.sprintf("%s.dbf",tableName);
1557 bool glt;
1558 glt = wxFileExists(dbName.GetData());
1559 return glt;
1560 }
1561
1562 if (!userID || !strlen(userID))
1563 userID = uid;
1564
1565 char userIdUC[80+1];
1566 // Oracle user names may only be in uppercase, so force
1567 // the name to uppercase
1568 if (Dbms() == dbmsORACLE)
1569 {
1570 int i = 0;
1571 for (char *p = userID; *p; p++)
1572 userIdUC[i++] = toupper(*p);
1573 userIdUC[i] = 0;
1574 userID = userIdUC;
1575 }
1576
1577 char tableNameUC[DB_MAX_TABLE_NAME_LEN+1];
1578 // Oracle table names are uppercase only, so force
1579 // the name to uppercase just in case programmer forgot to do this
1580 if (Dbms() == dbmsORACLE)
1581 {
1582 int i = 0;
1583 for (char *p = tableName; *p; p++)
1584 tableNameUC[i++] = toupper(*p);
1585 tableNameUC[i] = 0;
1586 }
1587 else
1588 sprintf(tableNameUC,tableName);
1589
108106cf 1590 SQLFreeStmt(hstmt, SQL_CLOSE);
a2115c88
GT
1591 RETCODE retcode;
1592
1593 // MySQL and Access cannot accept a user name when looking up table names, so we
1594 // use the call below that leaves out the user name
9199e66f 1595 if (wxStrcmp(userID,"") &&
a2115c88
GT
1596 Dbms() != dbmsMY_SQL &&
1597 Dbms() != dbmsACCESS)
1598 {
1599 retcode = SQLTables(hstmt,
1600 NULL, 0, // All qualifiers
1601 (UCHAR *) userID, SQL_NTS, // All owners
1602 (UCHAR FAR *)tableNameUC, SQL_NTS,
1603 NULL, 0); // All table types
1604 }
1605 else
1606 {
1607 retcode = SQLTables(hstmt,
1608 NULL, 0, // All qualifiers
1609 NULL, 0, // All owners
1610 (UCHAR FAR *)tableNameUC, SQL_NTS,
1611 NULL, 0); // All table types
1612 }
108106cf
JS
1613 if (retcode != SQL_SUCCESS)
1614 return(DispAllErrors(henv, hdbc, hstmt));
1615
a2115c88
GT
1616 retcode = SQLFetch(hstmt);
1617 if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
108106cf
JS
1618 {
1619 SQLFreeStmt(hstmt, SQL_CLOSE);
1620 return(DispAllErrors(henv, hdbc, hstmt));
1621 }
1622
1623 SQLFreeStmt(hstmt, SQL_CLOSE);
1624 return(TRUE);
1625
1626} // wxDB::TableExists()
1627
1628
1fc5dd6f
JS
1629/********** wxDB::SqlLog() **********/
1630bool wxDB::SqlLog(enum sqlLog state, char *filename, bool append)
1631{
1632 assert(state == sqlLogON || state == sqlLogOFF);
1633 assert(state == sqlLogOFF || filename);
1634
1635 if (state == sqlLogON)
1636 {
1637 if (fpSqlLog == 0)
1638 {
1639 fpSqlLog = fopen(filename, (append ? "at" : "wt"));
1640 if (fpSqlLog == NULL)
1641 return(FALSE);
1642 }
1643 }
1644 else // sqlLogOFF
1645 {
1646 if (fpSqlLog)
1647 {
1648 if (fclose(fpSqlLog))
1649 return(FALSE);
1650 fpSqlLog = 0;
1651 }
1652 }
1653
1654 sqlLogState = state;
1655 return(TRUE);
1656
1657} // wxDB::SqlLog()
1658
1659
1660/********** wxDB::WriteSqlLog() **********/
1661bool wxDB::WriteSqlLog(char *logMsg)
1662{
1663 assert(logMsg);
1664
1665 if (fpSqlLog == 0 || sqlLogState == sqlLogOFF)
1666 return(FALSE);
1667
1668 if (fputs("\n", fpSqlLog) == EOF) return(FALSE);
1669 if (fputs(logMsg, fpSqlLog) == EOF) return(FALSE);
1670 if (fputs("\n", fpSqlLog) == EOF) return(FALSE);
1671
1672 return(TRUE);
1673
1674} // wxDB::WriteSqlLog()
1675
1676
a2115c88
GT
1677/********** wxDB::Dbms() **********/
1678/*
1679 * Be aware that not all database engines use the exact same syntax, and not
1680 * every ODBC compliant database is compliant to the same level of compliancy.
1681 * Some manufacturers support the minimum Level 1 compliancy, and others up
1682 * through Level 3. Others support subsets of features for levels above 1.
1683 *
1684 * If you find an inconsistency between the wxDB class and a specific database
1685 * engine, and an identifier to this section, and special handle the database in
1686 * the area where behavior is non-conforming with the other databases.
1687 *
1688 *
1689 * NOTES ABOUT ISSUES SPECIFIC TO EACH DATABASE ENGINE
1690 * ---------------------------------------------------
1691 *
1692 * ORACLE
1693 * - Currently the only database supported by the class to support VIEWS
1694 *
1695 * DBASE
1696 * - Does not support the SQL_TIMESTAMP structure
1697 * - Supports only one cursor and one connect (apparently? with Microsoft driver only?)
1698 * - Does not automatically create the primary index if the 'keyField' param of SetColDef
1699 * is TRUE. The user must create ALL indexes from their program.
1700 * - Table names can only be 8 characters long
1701 * - Column names can only be 10 characters long
1702 *
1703 * SYBASE (all)
1704 * - To lock a record during QUERY functions, the reserved word 'HOLDLOCK' must be added
1705 * after every table name involved in the query/join if that tables matching record(s)
1706 * are to be locked
1707 * - Ignores the keywords 'FOR UPDATE'. Use the HOLDLOCK functionality described above
1708 *
1709 * SYBASE (Enterprise)
1710 * - If a column is part of the Primary Key, the column cannot be NULL
1711 *
1712 * MY_SQL
1713 * - If a column is part of the Primary Key, the column cannot be NULL
1714 * - Cannot support selecting for update [::CanSelectForUpdate()]. Always returns FALSE
1715 *
1716 * POSTGRES
1717 * - Does not support the keywords 'ASC' or 'DESC' as of release v6.5.0
1718 *
1719 *
1720 */
1721DBMS wxDB::Dbms(void)
1722{
aa33452c
GT
1723 wxChar baseName[20];
1724
1725 wxStrncpy(baseName,dbInf.dbmsName,6);
1726 baseName[6] = 0;
1727// if (!wxStrnicmp(dbInf.dbmsName,"Oracle",6))
1728 if (!wxStricmp(baseName,"Oracle"))
a2115c88 1729 return(dbmsORACLE);
9199e66f 1730 if (!wxStricmp(dbInf.dbmsName,"Adaptive Server Anywhere"))
a2115c88 1731 return(dbmsSYBASE_ASA);
9199e66f 1732 if (!wxStricmp(dbInf.dbmsName,"SQL Server")) // Sybase Adaptive Server Enterprise
a2115c88 1733 return(dbmsSYBASE_ASE);
9199e66f 1734 if (!wxStricmp(dbInf.dbmsName,"Microsoft SQL Server"))
a2115c88 1735 return(dbmsMS_SQL_SERVER);
9199e66f 1736 if (!wxStricmp(dbInf.dbmsName,"MySQL"))
a2115c88 1737 return(dbmsMY_SQL);
9199e66f 1738 if (!wxStricmp(dbInf.dbmsName,"PostgreSQL")) // v6.5.0
a2115c88 1739 return(dbmsPOSTGRES);
9199e66f 1740 if (!wxStricmp(dbInf.dbmsName,"ACCESS"))
a2115c88 1741 return(dbmsACCESS);
aa33452c
GT
1742 wxStrncpy(baseName,dbInf.dbmsName,5);
1743 baseName[5] = 0;
1744// if (!wxStrnicmp(dbInf.dbmsName,"DBASE",5))
1745 if (!wxStricmp(baseName,"DBASE"))
a2115c88
GT
1746 return(dbmsDBASE);
1747 return(dbmsUNIDENTIFIED);
1748
1749} // wxDB::Dbms()
1750
1751
108106cf 1752/********** GetDbConnection() **********/
a497618a 1753wxDB WXDLLEXPORT *GetDbConnection(DbStuff *pDbStuff)
108106cf
JS
1754{
1755 DbList *pList;
1756
1757 // Scan the linked list searching for an available database connection
1758 // that's already been opened but is currently not in use.
1759 for (pList = PtrBegDbList; pList; pList = pList->PtrNext)
1760 {
1761 // The database connection must be for the same datasource
1762 // name and must currently not be in use.
9199e66f 1763 if (pList->Free && (! wxStrcmp(pDbStuff->Dsn, pList->Dsn))) // Found a free connection
108106cf
JS
1764 {
1765 pList->Free = FALSE;
1766 return(pList->PtrDb);
1767 }
1768 }
1769
1770 // No available connections. A new connection must be made and
1771 // appended to the end of the linked list.
1772 if (PtrBegDbList)
1773 {
1774 // Find the end of the list
1775 for (pList = PtrBegDbList; pList->PtrNext; pList = pList->PtrNext);
1776 // Append a new list item
1777 pList->PtrNext = new DbList;
1778 pList->PtrNext->PtrPrev = pList;
1779 pList = pList->PtrNext;
1780 }
1781 else // Empty list
1782 {
1783 // Create the first node on the list
1784 pList = PtrBegDbList = new DbList;
1785 pList->PtrPrev = 0;
1786 }
1787
1788 // Initialize new node in the linked list
1789 pList->PtrNext = 0;
1790 pList->Free = FALSE;
1791 strcpy(pList->Dsn, pDbStuff->Dsn);
1792 pList->PtrDb = new wxDB(pDbStuff->Henv);
1793
1794 // Connect to the datasource
1795 if (pList->PtrDb->Open(pDbStuff->Dsn, pDbStuff->Uid, pDbStuff->AuthStr))
a2115c88
GT
1796 {
1797 pList->PtrDb->SqlLog(SQLLOGstate,SQLLOGfn,TRUE);
108106cf 1798 return(pList->PtrDb);
a2115c88 1799 }
108106cf
JS
1800 else // Unable to connect, destroy list item
1801 {
1802 if (pList->PtrPrev)
1803 pList->PtrPrev->PtrNext = 0;
1804 else
1805 PtrBegDbList = 0; // Empty list again
1806 pList->PtrDb->CommitTrans(); // Commit any open transactions on wxDB object
1807 pList->PtrDb->Close(); // Close the wxDB object
1808 delete pList->PtrDb; // Deletes the wxDB object
1809 delete pList; // Deletes the linked list object
1810 return(0);
1811 }
1812
1813} // GetDbConnection()
1814
1815/********** FreeDbConnection() **********/
a1218415 1816bool WXDLLEXPORT FreeDbConnection(wxDB *pDb)
108106cf
JS
1817{
1818 DbList *pList;
1819
1820 // Scan the linked list searching for the database connection
1821 for (pList = PtrBegDbList; pList; pList = pList->PtrNext)
1822 {
1823 if (pList->PtrDb == pDb) // Found it!!!
1824 return(pList->Free = TRUE);
1825 }
1826
1827 // Never found the database object, return failure
1828 return(FALSE);
1829
1830} // FreeDbConnection()
1831
1832/********** CloseDbConnections() **********/
a1218415 1833void WXDLLEXPORT CloseDbConnections(void)
108106cf
JS
1834{
1835 DbList *pList, *pNext;
1836
1837 // Traverse the linked list closing database connections and freeing memory as I go.
1838 for (pList = PtrBegDbList; pList; pList = pNext)
1839 {
1840 pNext = pList->PtrNext; // Save the pointer to next
1841 pList->PtrDb->CommitTrans(); // Commit any open transactions on wxDB object
1842 pList->PtrDb->Close(); // Close the wxDB object
1843 delete pList->PtrDb; // Deletes the wxDB object
1844 delete pList; // Deletes the linked list object
1845 }
1846
1847 // Mark the list as empty
1848 PtrBegDbList = 0;
1849
1850} // CloseDbConnections()
1851
1852/********** NumberDbConnectionsInUse() **********/
a1218415 1853int WXDLLEXPORT NumberDbConnectionsInUse(void)
108106cf
JS
1854{
1855 DbList *pList;
1856 int cnt = 0;
1857
1858 // Scan the linked list counting db connections that are currently in use
1859 for (pList = PtrBegDbList; pList; pList = pList->PtrNext)
1860 {
1861 if (pList->Free == FALSE)
1862 cnt++;
1863 }
1864
1865 return(cnt);
1866
1867} // NumberDbConnectionsInUse()
1868
a2115c88
GT
1869/********** SqlLog() **********/
1870bool SqlLog(enum sqlLog state, char *filename)
1871{
1872 bool append = FALSE;
1873 DbList *pList;
1874
1875 for (pList = PtrBegDbList; pList; pList = pList->PtrNext)
1876 {
1877 if (!pList->PtrDb->SqlLog(state,filename,append))
1878 return(FALSE);
1879 append = TRUE;
1880 }
1881
1882 SQLLOGstate = state;
1883 strcpy(SQLLOGfn,filename);
1884
1885 return(TRUE);
1886
1887} // SqlLog()
1888
108106cf
JS
1889/********** GetDataSource() **********/
1890bool GetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDescMax,
1891 UWORD direction)
1892{
1893 SWORD cb;
1894
1895 if (SQLDataSources(henv, direction, (UCHAR FAR *) Dsn, DsnMax, &cb,
1896 (UCHAR FAR *) DsDesc, DsDescMax, &cb) == SQL_SUCCESS)
1897 return(TRUE);
1898 else
1899 return(FALSE);
1900
1901} // GetDataSource()
1902
1903#endif
1fc5dd6f
JS
1904 // wxUSE_ODBC
1905