1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implementation of the wxDB class. The wxDB class represents a connection
4 // to an ODBC data source. The wxDB class allows operations on the data
5 // source such as opening and closing the data source.
7 // Modified by: George Tasker
9 // -Added support for SQL statement logging and database cataloging
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
18 // Copyright: (c) 1996 Remstar International, Inc.
19 // Licence: wxWindows licence, plus:
20 // Notice: This class library and its intellectual design are free of charge for use,
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 ///////////////////////////////////////////////////////////////////////////////
36 #include "wx/wxprec.h"
38 // Use this line for wxWindows v1.x
40 // Use this line for wxWindows v2.x
41 #include "wx/version.h"
43 #if wxMAJOR_VERSION == 2
45 #pragma implementation "db.h"
49 #ifdef DBDEBUG_CONSOLE
50 #include "wx/ioswrap.h"
57 #if wxMAJOR_VERSION == 2
59 #include "wx/string.h"
60 #include "wx/object.h"
63 #include "wx/msgdlg.h"
66 #include "wx/filefn.h"
67 #include "wx/wxchar.h"
70 #if wxMAJOR_VERSION == 1
71 # if defined(wx_msw) || defined(wx_x)
89 #if wxMAJOR_VERSION == 1
91 #elif wxMAJOR_VERSION == 2
95 WXDLLEXPORT_DATA(wxDbList
*) PtrBegDbList
= 0;
97 char const *SQL_LOG_FILENAME
= "sqllog.txt";
98 char const *SQL_CATALOG_FILENAME
= "catalog.txt";
101 extern wxList TablesInUse
;
104 // SQL Log defaults to be used by GetDbConnection
105 wxSqlLogState SQLLOGstate
= sqlLogOFF
;
107 //char SQLLOGfn[DB_PATH_MAX+1] = SQL_LOG_FILENAME;
108 char *SQLLOGfn
= (char*) SQL_LOG_FILENAME
;
110 // The wxDB::errorList is copied to this variable when the wxDB object
111 // is closed. This way, the error list is still available after the
112 // database object is closed. This is necessary if the database
113 // connection fails so the calling application can show the operator
114 // why the connection failed. Note: as each wxDB object is closed, it
115 // will overwrite the errors of the previously destroyed wxDB object in
117 char DBerrorList
[DB_MAX_ERROR_HISTORY
][DB_MAX_ERROR_MSG_LEN
];
120 /********** wxColFor Constructor **********/
123 i_Nation
= 0; // 0=EU, 1=UK, 2=International, 3=US
132 Format(1,DB_DATA_TYPE_VARCHAR
,0,0,0); // the Function that does the work
133 } // wxColFor::wxColFor()
136 wxColFor::~wxColFor()
138 } // wxColFor::~wxColFor()
141 int wxColFor::Format(int Nation
,int dbDataType
,SWORD sqlDataType
,short columnSize
,short decimalDigits
)
143 // ----------------------------------------------------------------------------------------
144 // -- 19991224 : mj10777@gmx.net : Create
145 // There is still a lot of work to do here, but it is a start
146 // It handles all the basic data-types that I have run into up to now
147 // The main work will have be with Dates and float Formatting (US 1,000.00 ; EU 1.000,00)
148 // There are wxWindow plans for locale support and the new wxDateTime.
149 // - if they define some constants (wxEUROPEAN) that can be gloably used,
150 // they should be used here.
151 // ----------------------------------------------------------------------------------------
152 // There should also be a Function to scan in a string to fill the variable
153 // ----------------------------------------------------------------------------------------
155 i_Nation
= Nation
; // 0 = timestamp , 1=EU, 2=UK, 3=International, 4=US
156 i_dbDataType
= dbDataType
;
157 i_sqlDataType
= sqlDataType
;
158 s_Field
.Printf(wxT("%s%d"),s_Menge
[1].c_str(),i_Menge
[1]); // OK for VARCHAR, INTEGER and FLOAT
159 if (i_dbDataType
== 0) // Filter unsupported dbDataTypes
161 if ((i_sqlDataType
== SQL_VARCHAR
) || (i_sqlDataType
== SQL_LONGVARCHAR
))
162 i_dbDataType
= DB_DATA_TYPE_VARCHAR
;
163 if (i_sqlDataType
== SQL_C_DATE
)
164 i_dbDataType
= DB_DATA_TYPE_DATE
;
165 if (i_sqlDataType
== SQL_C_BIT
)
166 i_dbDataType
= DB_DATA_TYPE_INTEGER
;
167 if (i_sqlDataType
== SQL_NUMERIC
)
168 i_dbDataType
= DB_DATA_TYPE_VARCHAR
;
169 if (i_sqlDataType
== SQL_REAL
)
170 i_dbDataType
= DB_DATA_TYPE_FLOAT
;
172 if ((i_dbDataType
== DB_DATA_TYPE_INTEGER
) && (i_sqlDataType
== SQL_C_DOUBLE
))
174 i_dbDataType
= DB_DATA_TYPE_FLOAT
;
176 switch(i_dbDataType
) // -A-> Still a lot of proper formatting to do
178 case DB_DATA_TYPE_VARCHAR
:
181 case DB_DATA_TYPE_INTEGER
:
184 case DB_DATA_TYPE_FLOAT
:
185 if (decimalDigits
== 0)
188 Temp0
.Printf(wxT("%s%d.%d"),Temp0
.c_str(),columnSize
,decimalDigits
);
189 s_Field
.Printf(wxT("%sf"),Temp0
.c_str()); //
191 case DB_DATA_TYPE_DATE
:
192 if (i_Nation
== 0) // timestamp YYYY-MM-DD HH:MM:SS.SSS (tested for SYBASE)
194 s_Field
= "%04d-%02d-%02d %02d:%02d:%02d.%03d";
196 if (i_Nation
== 1) // European DD.MM.YYYY HH:MM:SS.SSS
198 s_Field
= "%02d.%02d.%04d %02d:%02d:%02d.%03d";
200 if (i_Nation
== 2) // UK DD/MM/YYYY HH:MM:SS.SSS
202 s_Field
= "%02d/%02d/%04d %02d:%02d:%02d.%03d";
204 if (i_Nation
== 3) // International YYYY-MM-DD HH:MM:SS.SSS
206 s_Field
= "%04d-%02d-%02d %02d:%02d:%02d.%03d";
208 if (i_Nation
== 4) // US MM/DD/YYYY HH:MM:SS.SSS
210 s_Field
= "%02d/%02d/%04d %02d:%02d:%02d.%03d";
214 s_Field
.Printf(wxT("-E-> unknown Format(%d)-sql(%d)"),dbDataType
,sqlDataType
); //
218 } // wxColFor::Format()
221 /********** wxDB Constructor **********/
222 wxDB::wxDB(HENV
&aHenv
, bool FwdOnlyCursors
)
226 fpSqlLog
= 0; // Sql Log file pointer
227 sqlLogState
= sqlLogOFF
; // By default, logging is turned off
230 wxStrcpy(sqlState
,wxT(""));
231 wxStrcpy(errorMsg
,wxT(""));
232 nativeError
= cbErrorMsg
= 0;
233 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
234 wxStrcpy(errorList
[i
], wxT(""));
236 // Init typeInf structures
237 wxStrcpy(typeInfVarchar
.TypeName
,wxT(""));
238 typeInfVarchar
.FsqlType
= 0;
239 typeInfVarchar
.Precision
= 0;
240 typeInfVarchar
.CaseSensitive
= 0;
241 typeInfVarchar
.MaximumScale
= 0;
243 wxStrcpy(typeInfInteger
.TypeName
,wxT(""));
244 typeInfInteger
.FsqlType
= 0;
245 typeInfInteger
.Precision
= 0;
246 typeInfInteger
.CaseSensitive
= 0;
247 typeInfInteger
.MaximumScale
= 0;
249 wxStrcpy(typeInfFloat
.TypeName
,wxT(""));
250 typeInfFloat
.FsqlType
= 0;
251 typeInfFloat
.Precision
= 0;
252 typeInfFloat
.CaseSensitive
= 0;
253 typeInfFloat
.MaximumScale
= 0;
255 wxStrcpy(typeInfDate
.TypeName
,wxT(""));
256 typeInfDate
.FsqlType
= 0;
257 typeInfDate
.Precision
= 0;
258 typeInfDate
.CaseSensitive
= 0;
259 typeInfDate
.MaximumScale
= 0;
261 // Error reporting is turned OFF by default
264 // Copy the HENV into the db class
266 fwdOnlyCursors
= FwdOnlyCursors
;
268 // Allocate a data source connection handle
269 if (SQLAllocConnect(henv
, &hdbc
) != SQL_SUCCESS
)
272 // Initialize the db status flag
275 // Mark database as not open as of yet
281 /********** wxDB::Open() **********/
282 bool wxDB::Open(char *Dsn
, char *Uid
, char *AuthStr
)
284 assert(Dsn
&& wxStrlen(Dsn
));
291 if (!FwdOnlyCursors())
293 // Specify that the ODBC cursor library be used, if needed. This must be
294 // specified before the connection is made.
295 retcode
= SQLSetConnectOption(hdbc
, SQL_ODBC_CURSORS
, SQL_CUR_USE_IF_NEEDED
);
297 #ifdef DBDEBUG_CONSOLE
298 if (retcode
== SQL_SUCCESS
)
299 cout
<< "SQLSetConnectOption(CURSOR_LIB) successful" << endl
;
301 cout
<< "SQLSetConnectOption(CURSOR_LIB) failed" << endl
;
305 // Connect to the data source
306 retcode
= SQLConnect(hdbc
, (UCHAR FAR
*) Dsn
, SQL_NTS
,
307 (UCHAR FAR
*) Uid
, SQL_NTS
,
308 (UCHAR FAR
*) AuthStr
,SQL_NTS
);
309 if (retcode
== SQL_SUCCESS_WITH_INFO
)
310 DispAllErrors(henv
, hdbc
);
311 else if (retcode
!= SQL_SUCCESS
)
312 return(DispAllErrors(henv
, hdbc
));
315 If using Intersolv branded ODBC drivers, this is the place where you would substitute
316 your branded driver license information
318 SQLSetConnectOption(hdbc, 1041, (UDWORD) "");
319 SQLSetConnectOption(hdbc, 1042, (UDWORD) "");
322 // Mark database as open
325 // Allocate a statement handle for the database connection
326 if (SQLAllocStmt(hdbc
, &hstmt
) != SQL_SUCCESS
)
327 return(DispAllErrors(henv
, hdbc
));
329 // Set Connection Options
330 if (! setConnectionOptions())
333 // Query the data source for inf. about itself
337 // Query the data source regarding data type information
340 // The way I determined which SQL data types to use was by calling SQLGetInfo
341 // for all of the possible SQL data types to see which ones were supported. If
342 // a type is not supported, the SQLFetch() that's called from getDataTypeInfo()
343 // fails with SQL_NO_DATA_FOUND. This is ugly because I'm sure the three SQL data
344 // types I've selected below will not alway's be what we want. These are just
345 // what happened to work against an Oracle 7/Intersolv combination. The following is
346 // a complete list of the results I got back against the Oracle 7 database:
348 // SQL_BIGINT SQL_NO_DATA_FOUND
349 // SQL_BINARY SQL_NO_DATA_FOUND
350 // SQL_BIT SQL_NO_DATA_FOUND
351 // SQL_CHAR type name = 'CHAR', Precision = 255
352 // SQL_DATE SQL_NO_DATA_FOUND
353 // SQL_DECIMAL type name = 'NUMBER', Precision = 38
354 // SQL_DOUBLE type name = 'NUMBER', Precision = 15
355 // SQL_FLOAT SQL_NO_DATA_FOUND
356 // SQL_INTEGER SQL_NO_DATA_FOUND
357 // SQL_LONGVARBINARY type name = 'LONG RAW', Precision = 2 billion
358 // SQL_LONGVARCHAR type name = 'LONG', Precision = 2 billion
359 // SQL_NUMERIC SQL_NO_DATA_FOUND
360 // SQL_REAL SQL_NO_DATA_FOUND
361 // SQL_SMALLINT SQL_NO_DATA_FOUND
362 // SQL_TIME SQL_NO_DATA_FOUND
363 // SQL_TIMESTAMP type name = 'DATE', Precision = 19
364 // SQL_VARBINARY type name = 'RAW', Precision = 255
365 // SQL_VARCHAR type name = 'VARCHAR2', Precision = 2000
366 // =====================================================================
367 // Results from a Microsoft Access 7.0 db, using a driver from Microsoft
369 // SQL_VARCHAR type name = 'TEXT', Precision = 255
370 // SQL_TIMESTAMP type name = 'DATETIME'
371 // SQL_DECIMAL SQL_NO_DATA_FOUND
372 // SQL_NUMERIC type name = 'CURRENCY', Precision = 19
373 // SQL_FLOAT SQL_NO_DATA_FOUND
374 // SQL_REAL type name = 'SINGLE', Precision = 7
375 // SQL_DOUBLE type name = 'DOUBLE', Precision = 15
376 // SQL_INTEGER type name = 'LONG', Precision = 10
378 // VARCHAR = Variable length character string
379 if (! getDataTypeInfo(SQL_VARCHAR
, typeInfVarchar
))
380 if (! getDataTypeInfo(SQL_CHAR
, typeInfVarchar
))
383 typeInfVarchar
.FsqlType
= SQL_CHAR
;
385 typeInfVarchar
.FsqlType
= SQL_VARCHAR
;
388 if (! getDataTypeInfo(SQL_DOUBLE
, typeInfFloat
))
389 if (! getDataTypeInfo(SQL_REAL
, typeInfFloat
))
390 if (! getDataTypeInfo(SQL_FLOAT
, typeInfFloat
))
391 if (! getDataTypeInfo(SQL_DECIMAL
, typeInfFloat
))
392 if (! getDataTypeInfo(SQL_NUMERIC
, typeInfFloat
))
395 typeInfFloat
.FsqlType
= SQL_NUMERIC
;
397 typeInfFloat
.FsqlType
= SQL_DECIMAL
;
399 typeInfFloat
.FsqlType
= SQL_FLOAT
;
401 typeInfFloat
.FsqlType
= SQL_REAL
;
403 typeInfFloat
.FsqlType
= SQL_DOUBLE
;
406 if (! getDataTypeInfo(SQL_INTEGER
, typeInfInteger
))
407 // If SQL_INTEGER is not supported, use the floating point
408 // data type to store integers as well as floats
409 if (! getDataTypeInfo(typeInfFloat
.FsqlType
, typeInfInteger
))
412 typeInfInteger
.FsqlType
= typeInfFloat
.FsqlType
;
414 typeInfInteger
.FsqlType
= SQL_INTEGER
;
417 if (Dbms() != dbmsDBASE
)
419 if (! getDataTypeInfo(SQL_TIMESTAMP
, typeInfDate
))
422 typeInfDate
.FsqlType
= SQL_TIMESTAMP
;
426 if (! getDataTypeInfo(SQL_DATE
, typeInfDate
))
429 typeInfDate
.FsqlType
= SQL_DATE
;
432 #ifdef DBDEBUG_CONSOLE
433 cout
<< "VARCHAR DATA TYPE: " << typeInfVarchar
.TypeName
<< endl
;
434 cout
<< "INTEGER DATA TYPE: " << typeInfInteger
.TypeName
<< endl
;
435 cout
<< "FLOAT DATA TYPE: " << typeInfFloat
.TypeName
<< endl
;
436 cout
<< "DATE DATA TYPE: " << typeInfDate
.TypeName
<< endl
;
440 // Completed Successfully
446 /********** wxDB::setConnectionOptions() **********/
447 bool wxDB::setConnectionOptions(void)
449 * NOTE: The Intersolv/Oracle 7 driver was "Not Capable" of setting the login timeout.
452 SQLSetConnectOption(hdbc
, SQL_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
);
453 SQLSetConnectOption(hdbc
, SQL_OPT_TRACE
, SQL_OPT_TRACE_OFF
);
455 // Display the connection options to verify them
456 #ifdef DBDEBUG_CONSOLE
458 cout
<< "****** CONNECTION OPTIONS ******" << endl
;
460 if (SQLGetConnectOption(hdbc
, SQL_AUTOCOMMIT
, &l
) != SQL_SUCCESS
)
461 return(DispAllErrors(henv
, hdbc
));
462 cout
<< "AUTOCOMMIT: " << (l
== SQL_AUTOCOMMIT_OFF
? "OFF" : "ON") << endl
;
464 if (SQLGetConnectOption(hdbc
, SQL_ODBC_CURSORS
, &l
) != SQL_SUCCESS
)
465 return(DispAllErrors(henv
, hdbc
));
466 cout
<< "ODBC CURSORS: ";
469 case(SQL_CUR_USE_IF_NEEDED
):
470 cout
<< "SQL_CUR_USE_IF_NEEDED";
472 case(SQL_CUR_USE_ODBC
):
473 cout
<< "SQL_CUR_USE_ODBC";
475 case(SQL_CUR_USE_DRIVER
):
476 cout
<< "SQL_CUR_USE_DRIVER";
481 if (SQLGetConnectOption(hdbc
, SQL_OPT_TRACE
, &l
) != SQL_SUCCESS
)
482 return(DispAllErrors(henv
, hdbc
));
483 cout
<< "TRACING: " << (l
== SQL_OPT_TRACE_OFF
? "OFF" : "ON") << endl
;
488 // Completed Successfully
491 } // wxDB::setConnectionOptions()
494 /********** wxDB::getDbInfo() **********/
495 bool wxDB::getDbInfo(void)
500 if (SQLGetInfo(hdbc
, SQL_SERVER_NAME
, (UCHAR
*) dbInf
.serverName
, 80, &cb
) != SQL_SUCCESS
)
501 return(DispAllErrors(henv
, hdbc
));
503 if (SQLGetInfo(hdbc
, SQL_DATABASE_NAME
, (UCHAR
*) dbInf
.databaseName
, 128, &cb
) != SQL_SUCCESS
)
504 return(DispAllErrors(henv
, hdbc
));
506 if (SQLGetInfo(hdbc
, SQL_DBMS_NAME
, (UCHAR
*) dbInf
.dbmsName
, 40, &cb
) != SQL_SUCCESS
)
507 return(DispAllErrors(henv
, hdbc
));
510 // After upgrading to MSVC6, the original 20 char buffer below was insufficient,
511 // causing database connectivity to fail in some cases.
512 retcode
= SQLGetInfo(hdbc
, SQL_DBMS_VER
, (UCHAR
*) dbInf
.dbmsVer
, 64, &cb
);
514 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
515 return(DispAllErrors(henv
, hdbc
));
517 if (SQLGetInfo(hdbc
, SQL_ACTIVE_CONNECTIONS
, (UCHAR
*) &dbInf
.maxConnections
, sizeof(dbInf
.maxConnections
), &cb
) != SQL_SUCCESS
)
518 return(DispAllErrors(henv
, hdbc
));
520 if (SQLGetInfo(hdbc
, SQL_ACTIVE_STATEMENTS
, (UCHAR
*) &dbInf
.maxStmts
, sizeof(dbInf
.maxStmts
), &cb
) != SQL_SUCCESS
)
521 return(DispAllErrors(henv
, hdbc
));
523 if (SQLGetInfo(hdbc
, SQL_DRIVER_NAME
, (UCHAR
*) dbInf
.driverName
, 40, &cb
) != SQL_SUCCESS
)
524 return(DispAllErrors(henv
, hdbc
));
526 if (SQLGetInfo(hdbc
, SQL_DRIVER_ODBC_VER
, (UCHAR
*) dbInf
.odbcVer
, 60, &cb
) == SQL_ERROR
)
527 return(DispAllErrors(henv
, hdbc
));
529 retcode
= SQLGetInfo(hdbc
, SQL_ODBC_VER
, (UCHAR
*) dbInf
.drvMgrOdbcVer
, 60, &cb
);
530 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
531 return(DispAllErrors(henv
, hdbc
));
533 if (SQLGetInfo(hdbc
, SQL_DRIVER_VER
, (UCHAR
*) dbInf
.driverVer
, 60, &cb
) == SQL_ERROR
)
534 return(DispAllErrors(henv
, hdbc
));
536 if (SQLGetInfo(hdbc
, SQL_ODBC_API_CONFORMANCE
, (UCHAR
*) &dbInf
.apiConfLvl
, sizeof(dbInf
.apiConfLvl
), &cb
) != SQL_SUCCESS
)
537 return(DispAllErrors(henv
, hdbc
));
539 if (SQLGetInfo(hdbc
, SQL_ODBC_SAG_CLI_CONFORMANCE
, (UCHAR
*) &dbInf
.cliConfLvl
, sizeof(dbInf
.cliConfLvl
), &cb
) != SQL_SUCCESS
)
540 return(DispAllErrors(henv
, hdbc
));
542 if (SQLGetInfo(hdbc
, SQL_ODBC_SQL_CONFORMANCE
, (UCHAR
*) &dbInf
.sqlConfLvl
, sizeof(dbInf
.sqlConfLvl
), &cb
) != SQL_SUCCESS
)
543 return(DispAllErrors(henv
, hdbc
));
545 if (SQLGetInfo(hdbc
, SQL_OUTER_JOINS
, (UCHAR
*) dbInf
.outerJoins
, 2, &cb
) != SQL_SUCCESS
)
546 return(DispAllErrors(henv
, hdbc
));
548 if (SQLGetInfo(hdbc
, SQL_PROCEDURES
, (UCHAR
*) dbInf
.procedureSupport
, 2, &cb
) != SQL_SUCCESS
)
549 return(DispAllErrors(henv
, hdbc
));
551 if (SQLGetInfo(hdbc
, SQL_CURSOR_COMMIT_BEHAVIOR
, (UCHAR
*) &dbInf
.cursorCommitBehavior
, sizeof(dbInf
.cursorCommitBehavior
), &cb
) != SQL_SUCCESS
)
552 return(DispAllErrors(henv
, hdbc
));
554 if (SQLGetInfo(hdbc
, SQL_CURSOR_ROLLBACK_BEHAVIOR
, (UCHAR
*) &dbInf
.cursorRollbackBehavior
, sizeof(dbInf
.cursorRollbackBehavior
), &cb
) != SQL_SUCCESS
)
555 return(DispAllErrors(henv
, hdbc
));
557 if (SQLGetInfo(hdbc
, SQL_NON_NULLABLE_COLUMNS
, (UCHAR
*) &dbInf
.supportNotNullClause
, sizeof(dbInf
.supportNotNullClause
), &cb
) != SQL_SUCCESS
)
558 return(DispAllErrors(henv
, hdbc
));
560 if (SQLGetInfo(hdbc
, SQL_ODBC_SQL_OPT_IEF
, (UCHAR
*) dbInf
.supportIEF
, 2, &cb
) != SQL_SUCCESS
)
561 return(DispAllErrors(henv
, hdbc
));
563 if (SQLGetInfo(hdbc
, SQL_DEFAULT_TXN_ISOLATION
, (UCHAR
*) &dbInf
.txnIsolation
, sizeof(dbInf
.txnIsolation
), &cb
) != SQL_SUCCESS
)
564 return(DispAllErrors(henv
, hdbc
));
566 if (SQLGetInfo(hdbc
, SQL_TXN_ISOLATION_OPTION
, (UCHAR
*) &dbInf
.txnIsolationOptions
, sizeof(dbInf
.txnIsolationOptions
), &cb
) != SQL_SUCCESS
)
567 return(DispAllErrors(henv
, hdbc
));
569 if (SQLGetInfo(hdbc
, SQL_FETCH_DIRECTION
, (UCHAR
*) &dbInf
.fetchDirections
, sizeof(dbInf
.fetchDirections
), &cb
) != SQL_SUCCESS
)
570 return(DispAllErrors(henv
, hdbc
));
572 if (SQLGetInfo(hdbc
, SQL_LOCK_TYPES
, (UCHAR
*) &dbInf
.lockTypes
, sizeof(dbInf
.lockTypes
), &cb
) != SQL_SUCCESS
)
573 return(DispAllErrors(henv
, hdbc
));
575 if (SQLGetInfo(hdbc
, SQL_POS_OPERATIONS
, (UCHAR
*) &dbInf
.posOperations
, sizeof(dbInf
.posOperations
), &cb
) != SQL_SUCCESS
)
576 return(DispAllErrors(henv
, hdbc
));
578 if (SQLGetInfo(hdbc
, SQL_POSITIONED_STATEMENTS
, (UCHAR
*) &dbInf
.posStmts
, sizeof(dbInf
.posStmts
), &cb
) != SQL_SUCCESS
)
579 return(DispAllErrors(henv
, hdbc
));
581 if (SQLGetInfo(hdbc
, SQL_SCROLL_CONCURRENCY
, (UCHAR
*) &dbInf
.scrollConcurrency
, sizeof(dbInf
.scrollConcurrency
), &cb
) != SQL_SUCCESS
)
582 return(DispAllErrors(henv
, hdbc
));
584 if (SQLGetInfo(hdbc
, SQL_SCROLL_OPTIONS
, (UCHAR
*) &dbInf
.scrollOptions
, sizeof(dbInf
.scrollOptions
), &cb
) != SQL_SUCCESS
)
585 return(DispAllErrors(henv
, hdbc
));
587 if (SQLGetInfo(hdbc
, SQL_STATIC_SENSITIVITY
, (UCHAR
*) &dbInf
.staticSensitivity
, sizeof(dbInf
.staticSensitivity
), &cb
) != SQL_SUCCESS
)
588 return(DispAllErrors(henv
, hdbc
));
590 if (SQLGetInfo(hdbc
, SQL_TXN_CAPABLE
, (UCHAR
*) &dbInf
.txnCapable
, sizeof(dbInf
.txnCapable
), &cb
) != SQL_SUCCESS
)
591 return(DispAllErrors(henv
, hdbc
));
593 if (SQLGetInfo(hdbc
, SQL_LOGIN_TIMEOUT
, (UCHAR
*) &dbInf
.loginTimeout
, sizeof(dbInf
.loginTimeout
), &cb
) != SQL_SUCCESS
)
594 return(DispAllErrors(henv
, hdbc
));
596 #ifdef DBDEBUG_CONSOLE
597 cout
<< "***** DATA SOURCE INFORMATION *****" << endl
;
598 cout
<< "SERVER Name: " << dbInf
.serverName
<< endl
;
599 cout
<< "DBMS Name: " << dbInf
.dbmsName
<< "; DBMS Version: " << dbInf
.dbmsVer
<< endl
;
600 cout
<< "ODBC Version: " << dbInf
.odbcVer
<< "; Driver Version: " << dbInf
.driverVer
<< endl
;
602 cout
<< "API Conf. Level: ";
603 switch(dbInf
.apiConfLvl
)
605 case SQL_OAC_NONE
: cout
<< "None"; break;
606 case SQL_OAC_LEVEL1
: cout
<< "Level 1"; break;
607 case SQL_OAC_LEVEL2
: cout
<< "Level 2"; break;
611 cout
<< "SAG CLI Conf. Level: ";
612 switch(dbInf
.cliConfLvl
)
614 case SQL_OSCC_NOT_COMPLIANT
: cout
<< "Not Compliant"; break;
615 case SQL_OSCC_COMPLIANT
: cout
<< "Compliant"; break;
619 cout
<< "SQL Conf. Level: ";
620 switch(dbInf
.sqlConfLvl
)
622 case SQL_OSC_MINIMUM
: cout
<< "Minimum Grammer"; break;
623 case SQL_OSC_CORE
: cout
<< "Core Grammer"; break;
624 case SQL_OSC_EXTENDED
: cout
<< "Extended Grammer"; break;
628 cout
<< "Max. Connections: " << dbInf
.maxConnections
<< endl
;
629 cout
<< "Outer Joins: " << dbInf
.outerJoins
<< endl
;
630 cout
<< "Support for Procedures: " << dbInf
.procedureSupport
<< endl
;
632 cout
<< "Cursor COMMIT Behavior: ";
633 switch(dbInf
.cursorCommitBehavior
)
635 case SQL_CB_DELETE
: cout
<< "Delete cursors"; break;
636 case SQL_CB_CLOSE
: cout
<< "Close cursors"; break;
637 case SQL_CB_PRESERVE
: cout
<< "Preserve cursors"; break;
641 cout
<< "Cursor ROLLBACK Behavior: ";
642 switch(dbInf
.cursorRollbackBehavior
)
644 case SQL_CB_DELETE
: cout
<< "Delete cursors"; break;
645 case SQL_CB_CLOSE
: cout
<< "Close cursors"; break;
646 case SQL_CB_PRESERVE
: cout
<< "Preserve cursors"; break;
650 cout
<< "Support NOT NULL clause: ";
651 switch(dbInf
.supportNotNullClause
)
653 case SQL_NNC_NULL
: cout
<< "No"; break;
654 case SQL_NNC_NON_NULL
: cout
<< "Yes"; break;
658 cout
<< "Support IEF (Ref. Integrity): " << dbInf
.supportIEF
<< endl
;
659 cout
<< "Login Timeout: " << dbInf
.loginTimeout
<< endl
;
661 cout
<< endl
<< endl
<< "more ..." << endl
;
664 cout
<< "Default Transaction Isolation: ";
665 switch(dbInf
.txnIsolation
)
667 case SQL_TXN_READ_UNCOMMITTED
: cout
<< "Read Uncommitted"; break;
668 case SQL_TXN_READ_COMMITTED
: cout
<< "Read Committed"; break;
669 case SQL_TXN_REPEATABLE_READ
: cout
<< "Repeatable Read"; break;
670 case SQL_TXN_SERIALIZABLE
: cout
<< "Serializable"; break;
672 case SQL_TXN_VERSIONING
: cout
<< "Versioning"; break;
677 cout
<< "Transaction Isolation Options: ";
678 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_UNCOMMITTED
)
679 cout
<< "Read Uncommitted, ";
680 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_COMMITTED
)
681 cout
<< "Read Committed, ";
682 if (dbInf
.txnIsolationOptions
& SQL_TXN_REPEATABLE_READ
)
683 cout
<< "Repeatable Read, ";
684 if (dbInf
.txnIsolationOptions
& SQL_TXN_SERIALIZABLE
)
685 cout
<< "Serializable, ";
687 if (dbInf
.txnIsolationOptions
& SQL_TXN_VERSIONING
)
688 cout
<< "Versioning";
692 cout
<< "Fetch Directions Supported:" << endl
<< " ";
693 if (dbInf
.fetchDirections
& SQL_FD_FETCH_NEXT
)
695 if (dbInf
.fetchDirections
& SQL_FD_FETCH_PRIOR
)
697 if (dbInf
.fetchDirections
& SQL_FD_FETCH_FIRST
)
699 if (dbInf
.fetchDirections
& SQL_FD_FETCH_LAST
)
701 if (dbInf
.fetchDirections
& SQL_FD_FETCH_ABSOLUTE
)
702 cout
<< "Absolute, ";
703 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RELATIVE
)
704 cout
<< "Relative, ";
706 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RESUME
)
709 if (dbInf
.fetchDirections
& SQL_FD_FETCH_BOOKMARK
)
713 cout
<< "Lock Types Supported (SQLSetPos): ";
714 if (dbInf
.lockTypes
& SQL_LCK_NO_CHANGE
)
715 cout
<< "No Change, ";
716 if (dbInf
.lockTypes
& SQL_LCK_EXCLUSIVE
)
717 cout
<< "Exclusive, ";
718 if (dbInf
.lockTypes
& SQL_LCK_UNLOCK
)
722 cout
<< "Position Operations Supported (SQLSetPos): ";
723 if (dbInf
.posOperations
& SQL_POS_POSITION
)
724 cout
<< "Position, ";
725 if (dbInf
.posOperations
& SQL_POS_REFRESH
)
727 if (dbInf
.posOperations
& SQL_POS_UPDATE
)
729 if (dbInf
.posOperations
& SQL_POS_DELETE
)
731 if (dbInf
.posOperations
& SQL_POS_ADD
)
735 cout
<< "Positioned Statements Supported: ";
736 if (dbInf
.posStmts
& SQL_PS_POSITIONED_DELETE
)
737 cout
<< "Pos delete, ";
738 if (dbInf
.posStmts
& SQL_PS_POSITIONED_UPDATE
)
739 cout
<< "Pos update, ";
740 if (dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
741 cout
<< "Select for update";
744 cout
<< "Scroll Concurrency: ";
745 if (dbInf
.scrollConcurrency
& SQL_SCCO_READ_ONLY
)
746 cout
<< "Read Only, ";
747 if (dbInf
.scrollConcurrency
& SQL_SCCO_LOCK
)
749 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_ROWVER
)
750 cout
<< "Opt. Rowver, ";
751 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_VALUES
)
752 cout
<< "Opt. Values";
755 cout
<< "Scroll Options: ";
756 if (dbInf
.scrollOptions
& SQL_SO_FORWARD_ONLY
)
757 cout
<< "Fwd Only, ";
758 if (dbInf
.scrollOptions
& SQL_SO_STATIC
)
760 if (dbInf
.scrollOptions
& SQL_SO_KEYSET_DRIVEN
)
761 cout
<< "Keyset Driven, ";
762 if (dbInf
.scrollOptions
& SQL_SO_DYNAMIC
)
764 if (dbInf
.scrollOptions
& SQL_SO_MIXED
)
768 cout
<< "Static Sensitivity: ";
769 if (dbInf
.staticSensitivity
& SQL_SS_ADDITIONS
)
770 cout
<< "Additions, ";
771 if (dbInf
.staticSensitivity
& SQL_SS_DELETIONS
)
772 cout
<< "Deletions, ";
773 if (dbInf
.staticSensitivity
& SQL_SS_UPDATES
)
777 cout
<< "Transaction Capable?: ";
778 switch(dbInf
.txnCapable
)
780 case SQL_TC_NONE
: cout
<< "No"; break;
781 case SQL_TC_DML
: cout
<< "DML Only"; break;
782 case SQL_TC_DDL_COMMIT
: cout
<< "DDL Commit"; break;
783 case SQL_TC_DDL_IGNORE
: cout
<< "DDL Ignore"; break;
784 case SQL_TC_ALL
: cout
<< "DDL & DML"; break;
791 // Completed Successfully
794 } // wxDB::getDbInfo()
797 /********** wxDB::getDataTypeInfo() **********/
798 bool wxDB::getDataTypeInfo(SWORD fSqlType
, wxSqlTypeInfo
&structSQLTypeInfo
)
801 * fSqlType will be something like SQL_VARCHAR. This parameter determines
802 * the data type inf. is gathered for.
804 * wxSqlTypeInfo is a structure that is filled in with data type information,
809 // Get information about the data type specified
810 if (SQLGetTypeInfo(hstmt
, fSqlType
) != SQL_SUCCESS
)
811 return(DispAllErrors(henv
, hdbc
, hstmt
));
813 if ((retcode
= SQLFetch(hstmt
)) != SQL_SUCCESS
)
815 #ifdef DBDEBUG_CONSOLE
816 if (retcode
== SQL_NO_DATA_FOUND
)
817 cout
<< "SQL_NO_DATA_FOUND fetching inf. about data type." << endl
;
819 DispAllErrors(henv
, hdbc
, hstmt
);
820 SQLFreeStmt(hstmt
, SQL_CLOSE
);
823 // Obtain columns from the record
824 if (SQLGetData(hstmt
, 1, SQL_C_CHAR
, (UCHAR
*) structSQLTypeInfo
.TypeName
, DB_TYPE_NAME_LEN
, &cbRet
) != SQL_SUCCESS
)
825 return(DispAllErrors(henv
, hdbc
, hstmt
));
828 if (Dbms() == dbmsMY_SQL
)
830 if (!strcmp(structSQLTypeInfo
.TypeName
, "middleint")) strcpy(structSQLTypeInfo
.TypeName
, "mediumint");
831 if (!strcmp(structSQLTypeInfo
.TypeName
, "middleint unsigned")) strcpy(structSQLTypeInfo
.TypeName
, "mediumint unsigned");
832 if (!strcmp(structSQLTypeInfo
.TypeName
, "integer")) strcpy(structSQLTypeInfo
.TypeName
, "int");
833 if (!strcmp(structSQLTypeInfo
.TypeName
, "integer unsigned")) strcpy(structSQLTypeInfo
.TypeName
, "int unsigned");
834 if (!strcmp(structSQLTypeInfo
.TypeName
, "middleint")) strcpy(structSQLTypeInfo
.TypeName
, "mediumint");
835 if (!strcmp(structSQLTypeInfo
.TypeName
, "varchar")) strcpy(structSQLTypeInfo
.TypeName
, "char");
838 if (SQLGetData(hstmt
, 3, SQL_C_LONG
, (UCHAR
*) &structSQLTypeInfo
.Precision
, 0, &cbRet
) != SQL_SUCCESS
)
839 return(DispAllErrors(henv
, hdbc
, hstmt
));
840 if (SQLGetData(hstmt
, 8, SQL_C_SHORT
, (UCHAR
*) &structSQLTypeInfo
.CaseSensitive
, 0, &cbRet
) != SQL_SUCCESS
)
841 return(DispAllErrors(henv
, hdbc
, hstmt
));
842 // if (SQLGetData(hstmt, 14, SQL_C_SHORT, (UCHAR*) &structSQLTypeInfo.MinimumScale, 0, &cbRet) != SQL_SUCCESS)
843 // return(DispAllErrors(henv, hdbc, hstmt));
845 if (SQLGetData(hstmt
, 15, SQL_C_SHORT
,(UCHAR
*) &structSQLTypeInfo
.MaximumScale
, 0, &cbRet
) != SQL_SUCCESS
)
846 return(DispAllErrors(henv
, hdbc
, hstmt
));
848 if (structSQLTypeInfo
.MaximumScale
< 0)
849 structSQLTypeInfo
.MaximumScale
= 0;
851 // Close the statement handle which closes open cursors
852 if (SQLFreeStmt(hstmt
, SQL_CLOSE
) != SQL_SUCCESS
)
853 return(DispAllErrors(henv
, hdbc
, hstmt
));
855 // Completed Successfully
858 } // wxDB::getDataTypeInfo()
861 /********** wxDB::Close() **********/
862 void wxDB::Close(void)
864 // Close the Sql Log file
871 // Free statement handle
874 if (SQLFreeStmt(hstmt
, SQL_DROP
) != SQL_SUCCESS
)
875 DispAllErrors(henv
, hdbc
);
878 // Disconnect from the datasource
879 if (SQLDisconnect(hdbc
) != SQL_SUCCESS
)
880 DispAllErrors(henv
, hdbc
);
882 // Free the connection to the datasource
883 if (SQLFreeConnect(hdbc
) != SQL_SUCCESS
)
884 DispAllErrors(henv
, hdbc
);
886 // There should be zero Ctable objects still connected to this db object
887 assert(nTables
== 0);
892 pNode
= TablesInUse
.First();
896 tiu
= (wxTablesInUse
*)pNode
->Data();
897 if (tiu
->pDb
== this)
899 s
.sprintf(wxT("(%-20s) tableID:[%6lu] pDb:[%p]"), tiu
->tableName
,tiu
->tableID
,tiu
->pDb
);
900 s2
.sprintf(wxT("Orphaned found using pDb:[%p]"),this);
901 wxLogDebug (s
.c_str(),s2
.c_str());
903 pNode
= pNode
->Next();
907 // Copy the error messages to a global variable
909 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
910 wxStrcpy(DBerrorList
[i
],errorList
[i
]);
915 /********** wxDB::CommitTrans() **********/
916 bool wxDB::CommitTrans(void)
920 // Commit the transaction
921 if (SQLTransact(henv
, hdbc
, SQL_COMMIT
) != SQL_SUCCESS
)
922 return(DispAllErrors(henv
, hdbc
));
925 // Completed successfully
928 } // wxDB::CommitTrans()
931 /********** wxDB::RollbackTrans() **********/
932 bool wxDB::RollbackTrans(void)
934 // Rollback the transaction
935 if (SQLTransact(henv
, hdbc
, SQL_ROLLBACK
) != SQL_SUCCESS
)
936 return(DispAllErrors(henv
, hdbc
));
938 // Completed successfully
941 } // wxDB::RollbackTrans()
944 /********** wxDB::DispAllErrors() **********/
945 bool wxDB::DispAllErrors(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
947 // char odbcErrMsg[DB_MAX_ERROR_MSG_LEN];
950 while (SQLError(aHenv
, aHdbc
, aHstmt
, (UCHAR FAR
*) sqlState
, &nativeError
, (UCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
952 odbcErrMsg
.sprintf("SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState
, nativeError
, errorMsg
);
953 logError(odbcErrMsg
.GetData(), sqlState
);
956 #ifdef DBDEBUG_CONSOLE
957 // When run in console mode, use standard out to display errors.
958 cout
<< odbcErrMsg
.GetData() << endl
;
959 cout
<< "Press any key to continue..." << endl
;
965 wxLogDebug(odbcErrMsg
.GetData(),"DEBUG MESSAGE from DispAllErrors()");
969 return(FALSE
); // This function always returns false.
971 } // wxDB::DispAllErrors()
974 /********** wxDB::GetNextError() **********/
975 bool wxDB::GetNextError(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
977 if (SQLError(aHenv
, aHdbc
, aHstmt
, (UCHAR FAR
*) sqlState
, &nativeError
, (UCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
982 } // wxDB::GetNextError()
985 /********** wxDB::DispNextError() **********/
986 void wxDB::DispNextError(void)
988 // char odbcErrMsg[DB_MAX_ERROR_MSG_LEN];
991 odbcErrMsg
.sprintf("SQL State = %s\nNative Error Code = %li\nError Message = %s\n", sqlState
, nativeError
, errorMsg
);
992 logError(odbcErrMsg
.GetData(), sqlState
);
997 #ifdef DBDEBUG_CONSOLE
998 // When run in console mode, use standard out to display errors.
999 cout
<< odbcErrMsg
.GetData() << endl
;
1000 cout
<< "Press any key to continue..." << endl
;
1004 } // wxDB::DispNextError()
1007 /********** wxDB::logError() **********/
1008 void wxDB::logError(const char *errMsg
, const char *SQLState
)
1010 assert(errMsg
&& wxStrlen(errMsg
));
1012 static int pLast
= -1;
1015 if (++pLast
== DB_MAX_ERROR_HISTORY
)
1018 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
1019 wxStrcpy(errorList
[i
], errorList
[i
+1]);
1023 wxStrcpy(errorList
[pLast
], errMsg
);
1025 if (SQLState
&& wxStrlen(SQLState
))
1026 if ((dbStatus
= TranslateSqlState(SQLState
)) != DB_ERR_FUNCTION_SEQUENCE_ERROR
)
1027 DB_STATUS
= dbStatus
;
1029 // Add the errmsg to the sql log
1030 WriteSqlLog(errMsg
);
1032 } // wxDB::logError()
1035 /**********wxDB::TranslateSqlState() **********/
1036 int wxDB::TranslateSqlState(const char *SQLState
)
1038 if (!wxStrcmp(SQLState
, "01000"))
1039 return(DB_ERR_GENERAL_WARNING
);
1040 if (!wxStrcmp(SQLState
, "01002"))
1041 return(DB_ERR_DISCONNECT_ERROR
);
1042 if (!wxStrcmp(SQLState
, "01004"))
1043 return(DB_ERR_DATA_TRUNCATED
);
1044 if (!wxStrcmp(SQLState
, "01006"))
1045 return(DB_ERR_PRIV_NOT_REVOKED
);
1046 if (!wxStrcmp(SQLState
, "01S00"))
1047 return(DB_ERR_INVALID_CONN_STR_ATTR
);
1048 if (!wxStrcmp(SQLState
, "01S01"))
1049 return(DB_ERR_ERROR_IN_ROW
);
1050 if (!wxStrcmp(SQLState
, "01S02"))
1051 return(DB_ERR_OPTION_VALUE_CHANGED
);
1052 if (!wxStrcmp(SQLState
, "01S03"))
1053 return(DB_ERR_NO_ROWS_UPD_OR_DEL
);
1054 if (!wxStrcmp(SQLState
, "01S04"))
1055 return(DB_ERR_MULTI_ROWS_UPD_OR_DEL
);
1056 if (!wxStrcmp(SQLState
, "07001"))
1057 return(DB_ERR_WRONG_NO_OF_PARAMS
);
1058 if (!wxStrcmp(SQLState
, "07006"))
1059 return(DB_ERR_DATA_TYPE_ATTR_VIOL
);
1060 if (!wxStrcmp(SQLState
, "08001"))
1061 return(DB_ERR_UNABLE_TO_CONNECT
);
1062 if (!wxStrcmp(SQLState
, "08002"))
1063 return(DB_ERR_CONNECTION_IN_USE
);
1064 if (!wxStrcmp(SQLState
, "08003"))
1065 return(DB_ERR_CONNECTION_NOT_OPEN
);
1066 if (!wxStrcmp(SQLState
, "08004"))
1067 return(DB_ERR_REJECTED_CONNECTION
);
1068 if (!wxStrcmp(SQLState
, "08007"))
1069 return(DB_ERR_CONN_FAIL_IN_TRANS
);
1070 if (!wxStrcmp(SQLState
, "08S01"))
1071 return(DB_ERR_COMM_LINK_FAILURE
);
1072 if (!wxStrcmp(SQLState
, "21S01"))
1073 return(DB_ERR_INSERT_VALUE_LIST_MISMATCH
);
1074 if (!wxStrcmp(SQLState
, "21S02"))
1075 return(DB_ERR_DERIVED_TABLE_MISMATCH
);
1076 if (!wxStrcmp(SQLState
, "22001"))
1077 return(DB_ERR_STRING_RIGHT_TRUNC
);
1078 if (!wxStrcmp(SQLState
, "22003"))
1079 return(DB_ERR_NUMERIC_VALUE_OUT_OF_RNG
);
1080 if (!wxStrcmp(SQLState
, "22005"))
1081 return(DB_ERR_ERROR_IN_ASSIGNMENT
);
1082 if (!wxStrcmp(SQLState
, "22008"))
1083 return(DB_ERR_DATETIME_FLD_OVERFLOW
);
1084 if (!wxStrcmp(SQLState
, "22012"))
1085 return(DB_ERR_DIVIDE_BY_ZERO
);
1086 if (!wxStrcmp(SQLState
, "22026"))
1087 return(DB_ERR_STR_DATA_LENGTH_MISMATCH
);
1088 if (!wxStrcmp(SQLState
, "23000"))
1089 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1090 if (!wxStrcmp(SQLState
, "24000"))
1091 return(DB_ERR_INVALID_CURSOR_STATE
);
1092 if (!wxStrcmp(SQLState
, "25000"))
1093 return(DB_ERR_INVALID_TRANS_STATE
);
1094 if (!wxStrcmp(SQLState
, "28000"))
1095 return(DB_ERR_INVALID_AUTH_SPEC
);
1096 if (!wxStrcmp(SQLState
, "34000"))
1097 return(DB_ERR_INVALID_CURSOR_NAME
);
1098 if (!wxStrcmp(SQLState
, "37000"))
1099 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL
);
1100 if (!wxStrcmp(SQLState
, "3C000"))
1101 return(DB_ERR_DUPLICATE_CURSOR_NAME
);
1102 if (!wxStrcmp(SQLState
, "40001"))
1103 return(DB_ERR_SERIALIZATION_FAILURE
);
1104 if (!wxStrcmp(SQLState
, "42000"))
1105 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2
);
1106 if (!wxStrcmp(SQLState
, "70100"))
1107 return(DB_ERR_OPERATION_ABORTED
);
1108 if (!wxStrcmp(SQLState
, "IM001"))
1109 return(DB_ERR_UNSUPPORTED_FUNCTION
);
1110 if (!wxStrcmp(SQLState
, "IM002"))
1111 return(DB_ERR_NO_DATA_SOURCE
);
1112 if (!wxStrcmp(SQLState
, "IM003"))
1113 return(DB_ERR_DRIVER_LOAD_ERROR
);
1114 if (!wxStrcmp(SQLState
, "IM004"))
1115 return(DB_ERR_SQLALLOCENV_FAILED
);
1116 if (!wxStrcmp(SQLState
, "IM005"))
1117 return(DB_ERR_SQLALLOCCONNECT_FAILED
);
1118 if (!wxStrcmp(SQLState
, "IM006"))
1119 return(DB_ERR_SQLSETCONNECTOPTION_FAILED
);
1120 if (!wxStrcmp(SQLState
, "IM007"))
1121 return(DB_ERR_NO_DATA_SOURCE_DLG_PROHIB
);
1122 if (!wxStrcmp(SQLState
, "IM008"))
1123 return(DB_ERR_DIALOG_FAILED
);
1124 if (!wxStrcmp(SQLState
, "IM009"))
1125 return(DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL
);
1126 if (!wxStrcmp(SQLState
, "IM010"))
1127 return(DB_ERR_DATA_SOURCE_NAME_TOO_LONG
);
1128 if (!wxStrcmp(SQLState
, "IM011"))
1129 return(DB_ERR_DRIVER_NAME_TOO_LONG
);
1130 if (!wxStrcmp(SQLState
, "IM012"))
1131 return(DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR
);
1132 if (!wxStrcmp(SQLState
, "IM013"))
1133 return(DB_ERR_TRACE_FILE_ERROR
);
1134 if (!wxStrcmp(SQLState
, "S0001"))
1135 return(DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS
);
1136 if (!wxStrcmp(SQLState
, "S0002"))
1137 return(DB_ERR_TABLE_NOT_FOUND
);
1138 if (!wxStrcmp(SQLState
, "S0011"))
1139 return(DB_ERR_INDEX_ALREADY_EXISTS
);
1140 if (!wxStrcmp(SQLState
, "S0012"))
1141 return(DB_ERR_INDEX_NOT_FOUND
);
1142 if (!wxStrcmp(SQLState
, "S0021"))
1143 return(DB_ERR_COLUMN_ALREADY_EXISTS
);
1144 if (!wxStrcmp(SQLState
, "S0022"))
1145 return(DB_ERR_COLUMN_NOT_FOUND
);
1146 if (!wxStrcmp(SQLState
, "S0023"))
1147 return(DB_ERR_NO_DEFAULT_FOR_COLUMN
);
1148 if (!wxStrcmp(SQLState
, "S1000"))
1149 return(DB_ERR_GENERAL_ERROR
);
1150 if (!wxStrcmp(SQLState
, "S1001"))
1151 return(DB_ERR_MEMORY_ALLOCATION_FAILURE
);
1152 if (!wxStrcmp(SQLState
, "S1002"))
1153 return(DB_ERR_INVALID_COLUMN_NUMBER
);
1154 if (!wxStrcmp(SQLState
, "S1003"))
1155 return(DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE
);
1156 if (!wxStrcmp(SQLState
, "S1004"))
1157 return(DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE
);
1158 if (!wxStrcmp(SQLState
, "S1008"))
1159 return(DB_ERR_OPERATION_CANCELLED
);
1160 if (!wxStrcmp(SQLState
, "S1009"))
1161 return(DB_ERR_INVALID_ARGUMENT_VALUE
);
1162 if (!wxStrcmp(SQLState
, "S1010"))
1163 return(DB_ERR_FUNCTION_SEQUENCE_ERROR
);
1164 if (!wxStrcmp(SQLState
, "S1011"))
1165 return(DB_ERR_OPERATION_INVALID_AT_THIS_TIME
);
1166 if (!wxStrcmp(SQLState
, "S1012"))
1167 return(DB_ERR_INVALID_TRANS_OPERATION_CODE
);
1168 if (!wxStrcmp(SQLState
, "S1015"))
1169 return(DB_ERR_NO_CURSOR_NAME_AVAIL
);
1170 if (!wxStrcmp(SQLState
, "S1090"))
1171 return(DB_ERR_INVALID_STR_OR_BUF_LEN
);
1172 if (!wxStrcmp(SQLState
, "S1091"))
1173 return(DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE
);
1174 if (!wxStrcmp(SQLState
, "S1092"))
1175 return(DB_ERR_OPTION_TYPE_OUT_OF_RANGE
);
1176 if (!wxStrcmp(SQLState
, "S1093"))
1177 return(DB_ERR_INVALID_PARAM_NO
);
1178 if (!wxStrcmp(SQLState
, "S1094"))
1179 return(DB_ERR_INVALID_SCALE_VALUE
);
1180 if (!wxStrcmp(SQLState
, "S1095"))
1181 return(DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE
);
1182 if (!wxStrcmp(SQLState
, "S1096"))
1183 return(DB_ERR_INF_TYPE_OUT_OF_RANGE
);
1184 if (!wxStrcmp(SQLState
, "S1097"))
1185 return(DB_ERR_COLUMN_TYPE_OUT_OF_RANGE
);
1186 if (!wxStrcmp(SQLState
, "S1098"))
1187 return(DB_ERR_SCOPE_TYPE_OUT_OF_RANGE
);
1188 if (!wxStrcmp(SQLState
, "S1099"))
1189 return(DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE
);
1190 if (!wxStrcmp(SQLState
, "S1100"))
1191 return(DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE
);
1192 if (!wxStrcmp(SQLState
, "S1101"))
1193 return(DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE
);
1194 if (!wxStrcmp(SQLState
, "S1103"))
1195 return(DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE
);
1196 if (!wxStrcmp(SQLState
, "S1104"))
1197 return(DB_ERR_INVALID_PRECISION_VALUE
);
1198 if (!wxStrcmp(SQLState
, "S1105"))
1199 return(DB_ERR_INVALID_PARAM_TYPE
);
1200 if (!wxStrcmp(SQLState
, "S1106"))
1201 return(DB_ERR_FETCH_TYPE_OUT_OF_RANGE
);
1202 if (!wxStrcmp(SQLState
, "S1107"))
1203 return(DB_ERR_ROW_VALUE_OUT_OF_RANGE
);
1204 if (!wxStrcmp(SQLState
, "S1108"))
1205 return(DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE
);
1206 if (!wxStrcmp(SQLState
, "S1109"))
1207 return(DB_ERR_INVALID_CURSOR_POSITION
);
1208 if (!wxStrcmp(SQLState
, "S1110"))
1209 return(DB_ERR_INVALID_DRIVER_COMPLETION
);
1210 if (!wxStrcmp(SQLState
, "S1111"))
1211 return(DB_ERR_INVALID_BOOKMARK_VALUE
);
1212 if (!wxStrcmp(SQLState
, "S1C00"))
1213 return(DB_ERR_DRIVER_NOT_CAPABLE
);
1214 if (!wxStrcmp(SQLState
, "S1T00"))
1215 return(DB_ERR_TIMEOUT_EXPIRED
);
1220 } // wxDB::TranslateSqlState()
1223 /********** wxDB::Grant() **********/
1224 bool wxDB::Grant(int privileges
, const char *tableName
, const char *userList
)
1226 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1229 // Build the grant statement
1231 if (privileges
== DB_GRANT_ALL
)
1236 if (privileges
& DB_GRANT_SELECT
)
1238 sqlStmt
+= "SELECT";
1241 if (privileges
& DB_GRANT_INSERT
)
1245 sqlStmt
+= "INSERT";
1247 if (privileges
& DB_GRANT_UPDATE
)
1251 sqlStmt
+= "UPDATE";
1253 if (privileges
& DB_GRANT_DELETE
)
1257 sqlStmt
+= "DELETE";
1262 sqlStmt
+= tableName
;
1264 sqlStmt
+= userList
;
1266 #ifdef DBDEBUG_CONSOLE
1267 cout
<< endl
<< sqlStmt
.GetData() << endl
;
1270 WriteSqlLog(sqlStmt
.GetData());
1272 return(ExecSql(sqlStmt
.GetData()));
1277 /********** wxDB::CreateView() **********/
1278 bool wxDB::CreateView(const char *viewName
, const char *colList
, const char *pSqlStmt
, bool attemptDrop
)
1280 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1283 // Drop the view first
1284 if (attemptDrop
&& !DropView(viewName
))
1287 // Build the create view statement
1288 sqlStmt
= "CREATE VIEW ";
1289 sqlStmt
+= viewName
;
1291 if (wxStrlen(colList
))
1299 sqlStmt
+= pSqlStmt
;
1301 WriteSqlLog(sqlStmt
.GetData());
1303 #ifdef DBDEBUG_CONSOLE
1304 cout
<< sqlStmt
.GetData() << endl
;
1307 return(ExecSql(sqlStmt
.GetData()));
1309 } // wxDB::CreateView()
1312 /********** wxDB::DropView() **********/
1313 bool wxDB::DropView(const char *viewName
)
1316 * NOTE: This function returns TRUE if the View does not exist, but
1317 * only for identified databases. Code will need to be added
1318 * below for any other databases when those databases are defined
1319 * to handle this situation consistently
1321 // char sqlStmt[DB_MAX_STATEMENT_LEN];
1324 sqlStmt
.sprintf("DROP VIEW %s", viewName
);
1326 WriteSqlLog(sqlStmt
.GetData());
1328 #ifdef DBDEBUG_CONSOLE
1329 cout
<< endl
<< sqlStmt
.GetData() << endl
;
1332 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) sqlStmt
.GetData(), SQL_NTS
) != SQL_SUCCESS
)
1334 // Check for "Base table not found" error and ignore
1335 GetNextError(henv
, hdbc
, hstmt
);
1336 if (wxStrcmp(sqlState
,"S0002")) // "Base table not found"
1338 // Check for product specific error codes
1339 if (!((Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(sqlState
,"42000")))) // 5.x (and lower?)
1342 DispAllErrors(henv
, hdbc
, hstmt
);
1349 // Commit the transaction
1350 if (! CommitTrans())
1355 } // wxDB::DropView()
1358 /********** wxDB::ExecSql() **********/
1359 bool wxDB::ExecSql(const char *pSqlStmt
)
1361 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1362 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) pSqlStmt
, SQL_NTS
) == SQL_SUCCESS
)
1366 DispAllErrors(henv
, hdbc
, hstmt
);
1370 } // wxDB::ExecSql()
1373 /********** wxDB::GetNext() **********/
1374 bool wxDB::GetNext(void)
1376 if (SQLFetch(hstmt
) == SQL_SUCCESS
)
1380 DispAllErrors(henv
, hdbc
, hstmt
);
1384 } // wxDB::GetNext()
1387 /********** wxDB::GetData() **********/
1388 bool wxDB::GetData(UWORD colNo
, SWORD cType
, PTR pData
, SDWORD maxLen
, SDWORD FAR
*cbReturned
)
1393 if (SQLGetData(hstmt
, colNo
, cType
, pData
, maxLen
, cbReturned
) == SQL_SUCCESS
)
1397 DispAllErrors(henv
, hdbc
, hstmt
);
1401 } // wxDB::GetData()
1404 /********** wxDB::GetKeyFields() **********/
1405 int wxDB::GetKeyFields(char *tableName
, wxColInf
* colInf
,int noCols
)
1407 char szPkTable
[DB_MAX_TABLE_NAME_LEN
+1]; /* Primary key table name */
1408 char szFkTable
[DB_MAX_TABLE_NAME_LEN
+1]; /* Foreign key table name */
1410 // SQLSMALLINT iKeySeq;
1411 char szPkCol
[DB_MAX_COLUMN_NAME_LEN
+1]; /* Primary key column */
1412 char szFkCol
[DB_MAX_COLUMN_NAME_LEN
+1]; /* Foreign key column */
1418 * ---------------------------------------------------------------------
1419 * -- 19991224 : mj10777@gmx.net : Create ------
1420 * -- : Three things are done and stored here : ------
1421 * -- : 1) which Column(s) is/are Primary Key(s) ------
1422 * -- : 2) which tables use this Key as a Foreign Key ------
1423 * -- : 3) which columns are Foreign Key and the name ------
1424 * -- : of the Table where the Key is the Primary Key -----
1425 * -- : Called from GetColumns(char *tableName, ------
1426 * -- int *numCols,const char *userID ) ------
1427 * ---------------------------------------------------------------------
1430 /*---------------------------------------------------------------------*/
1431 /* Get the names of the columns in the primary key. */
1432 /*---------------------------------------------------------------------*/
1433 retcode
= SQLPrimaryKeys(hstmt
,
1434 NULL
, 0, /* Catalog name */
1435 NULL
, 0, /* Schema name */
1436 (UCHAR
*) tableName
, SQL_NTS
); /* Table name */
1438 /*---------------------------------------------------------------------*/
1439 /* Fetch and display the result set. This will be a list of the */
1440 /* columns in the primary key of the tableName table. */
1441 /*---------------------------------------------------------------------*/
1442 while ((retcode
== SQL_SUCCESS
) || (retcode
== SQL_SUCCESS_WITH_INFO
))
1444 retcode
= SQLFetch(hstmt
);
1445 if (retcode
== SQL_SUCCESS
|| retcode
== SQL_SUCCESS_WITH_INFO
)
1447 GetData( 4, SQL_C_CHAR
, szPkCol
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1448 GetData( 5, SQL_C_SSHORT
, &iKeySeq
, 0, &cb
);
1450 for (i
=0;i
<noCols
;i
++) // Find the Column name
1451 if (!wxStrcmp(colInf
[i
].colName
,szPkCol
)) // We have found the Column
1452 colInf
[i
].PkCol
= iKeySeq
; // Which Primary Key is this (first, second usw.) ?
1453 } // if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
1454 } // while ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
1455 SQLFreeStmt(hstmt
, SQL_CLOSE
); /* Close the cursor (the hstmt is still allocated). */
1457 /*---------------------------------------------------------------------*/
1458 /* Get all the foreign keys that refer to tableName primary key. */
1459 /*---------------------------------------------------------------------*/
1460 retcode
= SQLForeignKeys(hstmt
,
1461 NULL
, 0, /* Primary catalog */
1462 NULL
, 0, /* Primary schema */
1463 (UCHAR
*)tableName
, SQL_NTS
, /* Primary table */
1464 NULL
, 0, /* Foreign catalog */
1465 NULL
, 0, /* Foreign schema */
1466 NULL
, 0); /* Foreign table */
1468 /*---------------------------------------------------------------------*/
1469 /* Fetch and display the result set. This will be all of the foreign */
1470 /* keys in other tables that refer to the tableName primary key. */
1471 /*---------------------------------------------------------------------*/
1474 while ((retcode
== SQL_SUCCESS
) || (retcode
== SQL_SUCCESS_WITH_INFO
))
1476 retcode
= SQLFetch(hstmt
);
1477 if (retcode
== SQL_SUCCESS
|| retcode
== SQL_SUCCESS_WITH_INFO
)
1479 GetData( 3, SQL_C_CHAR
, szPkTable
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
1480 GetData( 4, SQL_C_CHAR
, szPkCol
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1481 GetData( 5, SQL_C_SSHORT
, &iKeySeq
, 0, &cb
);
1482 GetData( 7, SQL_C_CHAR
, szFkTable
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
1483 GetData( 8, SQL_C_CHAR
, szFkCol
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1484 Temp0
.Printf("%s[%s] ",Temp0
.c_str(),szFkTable
); // [ ] in case there is a blank in the Table name
1485 } // if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
1486 } // while ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
1487 Temp0
.Trim(); // Get rid of any unneeded blanks
1490 for (i
=0;i
<noCols
;i
++) // Find the Column name
1491 if (!wxStrcmp(colInf
[i
].colName
,szPkCol
)) // We have found the Column, store the Information
1492 strcpy(colInf
[i
].PkTableName
,Temp0
); // Name of the Tables where this Primary Key is used as a Foreign Key
1493 } // if (Temp0 != "")
1494 SQLFreeStmt(hstmt
, SQL_CLOSE
); /* Close the cursor (the hstmt is still allocated). */
1496 /*---------------------------------------------------------------------*/
1497 /* Get all the foreign keys in the tablename table. */
1498 /*---------------------------------------------------------------------*/
1499 retcode
= SQLForeignKeys(hstmt
,
1500 NULL
, 0, /* Primary catalog */
1501 NULL
, 0, /* Primary schema */
1502 NULL
, 0, /* Primary table */
1503 NULL
, 0, /* Foreign catalog */
1504 NULL
, 0, /* Foreign schema */
1505 (UCHAR
*)tableName
, SQL_NTS
); /* Foreign table */
1507 /*---------------------------------------------------------------------*/
1508 /* Fetch and display the result set. This will be all of the */
1509 /* primary keys in other tables that are referred to by foreign */
1510 /* keys in the tableName table. */
1511 /*---------------------------------------------------------------------*/
1513 while ((retcode
== SQL_SUCCESS
) || (retcode
== SQL_SUCCESS_WITH_INFO
))
1515 retcode
= SQLFetch(hstmt
);
1516 if (retcode
== SQL_SUCCESS
|| retcode
== SQL_SUCCESS_WITH_INFO
)
1518 GetData( 3, SQL_C_CHAR
, szPkTable
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
1519 GetData( 5, SQL_C_SSHORT
, &iKeySeq
, 0, &cb
);
1520 GetData( 8, SQL_C_CHAR
, szFkCol
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1522 for (i
=0;i
<noCols
;i
++) // Find the Column name
1524 if (!wxStrcmp(colInf
[i
].colName
,szFkCol
)) // We have found the (Foreign Key) Column
1526 colInf
[i
].FkCol
= iKeySeq
; // Which Foreign Key is this (first, second usw.) ?
1527 strcpy(colInf
[i
].FkTableName
,szPkTable
); // Name of the Table where this Foriegn is the Primary Key
1528 } // if (!wxStrcmp(colInf[i].colName,szFkCol))
1529 } // for (i=0;i<noCols;i++)
1530 } // if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
1531 } // while ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
1532 SQLFreeStmt(hstmt
, SQL_CLOSE
); /* Close the cursor (the hstmt is still allocated). */
1534 /*---------------------------------------------------------------------*/
1536 } // wxDB::GetKeyFields()
1539 /********** wxDB::GetColumns() **********/
1540 wxColInf
*wxDB::GetColumns(char *tableName
[], const char *userID
)
1542 * 1) The last array element of the tableName[] argument must be zero (null).
1543 * This is how the end of the array is detected.
1544 * 2) This function returns an array of wxColInf structures. If no columns
1545 * were found, or an error occured, this pointer will be zero (null). THE
1546 * CALLING FUNCTION IS RESPONSIBLE FOR DELETING THE MEMORY RETURNED WHEN IT
1547 * IS FINISHED WITH IT. i.e.
1549 * wxColInf *colInf = pDb->GetColumns(tableList, userID);
1552 * // Use the column inf
1554 * // Destroy the memory
1558 * userID is evaluated in the following manner:
1559 * userID == NULL ... UserID is ignored
1560 * userID == "" ... UserID set equal to 'this->uid'
1561 * userID != "" ... UserID set equal to 'userID'
1563 * NOTE: ALL column bindings associated with this wxDB instance are unbound
1564 * by this function. This function should use its own wxDB instance
1565 * to avoid undesired unbinding of columns.
1570 wxColInf
*colInf
= 0;
1580 if (!wxStrlen(userID
))
1588 // dBase does not use user names, and some drivers fail if you try to pass one
1589 if (Dbms() == dbmsDBASE
)
1592 // Oracle user names may only be in uppercase, so force
1593 // the name to uppercase
1594 if (Dbms() == dbmsORACLE
)
1595 UserID
= UserID
.Upper();
1597 // Pass 1 - Determine how many columns there are.
1598 // Pass 2 - Allocate the wxColInf array and fill in
1599 // the array with the column information.
1601 for (pass
= 1; pass
<= 2; pass
++)
1605 if (noCols
== 0) // Probably a bogus table name(s)
1607 // Allocate n wxColInf objects to hold the column information
1608 colInf
= new wxColInf
[noCols
+1];
1611 // Mark the end of the array
1612 wxStrcpy(colInf
[noCols
].tableName
, "");
1613 wxStrcpy(colInf
[noCols
].colName
, "");
1614 colInf
[noCols
].sqlDataType
= 0;
1616 // Loop through each table name
1618 for (tbl
= 0; tableName
[tbl
]; tbl
++)
1620 TableName
= tableName
[tbl
];
1621 // Oracle table names are uppercase only, so force
1622 // the name to uppercase just in case programmer forgot to do this
1623 if (Dbms() == dbmsORACLE
)
1624 TableName
= TableName
.Upper();
1626 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1628 // MySQL and Access cannot accept a user name when looking up column names, so we
1629 // use the call below that leaves out the user name
1630 if (wxStrcmp(UserID
.GetData(),"") &&
1631 Dbms() != dbmsMY_SQL
&&
1632 Dbms() != dbmsACCESS
)
1634 retcode
= SQLColumns(hstmt
,
1635 NULL
, 0, // All qualifiers
1636 (UCHAR
*) UserID
.GetData(), SQL_NTS
, // Owner
1637 (UCHAR
*) TableName
.GetData(), SQL_NTS
,
1638 NULL
, 0); // All columns
1642 retcode
= SQLColumns(hstmt
,
1643 NULL
, 0, // All qualifiers
1645 (UCHAR
*) TableName
.GetData(), SQL_NTS
,
1646 NULL
, 0); // All columns
1648 if (retcode
!= SQL_SUCCESS
)
1649 { // Error occured, abort
1650 DispAllErrors(henv
, hdbc
, hstmt
);
1653 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1657 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
1659 if (pass
== 1) // First pass, just add up the number of columns
1661 else // Pass 2; Fill in the array of structures
1663 if (colNo
< noCols
) // Some extra error checking to prevent memory overwrites
1665 // NOTE: Only the ODBC 1.x fields are retrieved
1666 GetData( 1, SQL_C_CHAR
, (UCHAR
*) colInf
[colNo
].catalog
, 128+1, &cb
);
1667 GetData( 2, SQL_C_CHAR
, (UCHAR
*) colInf
[colNo
].schema
, 128+1, &cb
);
1668 GetData( 3, SQL_C_CHAR
, (UCHAR
*) colInf
[colNo
].tableName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
1669 GetData( 4, SQL_C_CHAR
, (UCHAR
*) colInf
[colNo
].colName
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1670 GetData( 5, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].sqlDataType
, 0, &cb
);
1671 GetData( 6, SQL_C_CHAR
, (UCHAR
*) colInf
[colNo
].typeName
, 128+1, &cb
);
1672 GetData( 7, SQL_C_SLONG
, (UCHAR
*) &colInf
[colNo
].columnSize
, 0, &cb
);
1673 GetData( 8, SQL_C_SLONG
, (UCHAR
*) &colInf
[colNo
].bufferLength
, 0, &cb
);
1674 GetData( 9, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].decimalDigits
,0, &cb
);
1675 GetData(10, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].numPrecRadix
, 0, &cb
);
1676 GetData(11, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].nullable
, 0, &cb
);
1677 GetData(12, SQL_C_CHAR
, (UCHAR
*) colInf
[colNo
].remarks
, 254+1, &cb
);
1679 // Determine the wxDB data type that is used to represent the native data type of this data source
1680 colInf
[colNo
].dbDataType
= 0;
1681 if (!wxStricmp(typeInfVarchar
.TypeName
,colInf
[colNo
].typeName
))
1683 if (colInf
[colNo
].columnSize
< 1)
1685 // IODBC does not return a correct columnSize, so we set
1686 // columnSize = bufferLength if no column size was returned
1687 colInf
[colNo
].columnSize
= colInf
[colNo
].bufferLength
;
1689 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_VARCHAR
;
1691 else if (!wxStricmp(typeInfInteger
.TypeName
,colInf
[colNo
].typeName
))
1692 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_INTEGER
;
1693 else if (!wxStricmp(typeInfFloat
.TypeName
,colInf
[colNo
].typeName
))
1694 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_FLOAT
;
1695 else if (!wxStricmp(typeInfDate
.TypeName
,colInf
[colNo
].typeName
))
1696 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_DATE
;
1702 if (retcode
!= SQL_NO_DATA_FOUND
)
1703 { // Error occured, abort
1704 DispAllErrors(henv
, hdbc
, hstmt
);
1707 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1713 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1716 } // wxDB::GetColumns()
1719 /********** wxDB::GetColumns() **********/
1720 wxColInf
*wxDB::GetColumns(char *tableName
, int *numCols
, const char *userID
)
1722 * Same as the above GetColumns() function except this one gets columns
1723 * only for a single table, and if 'numCols' is not NULL, the number of
1724 * columns stored in the returned wxColInf is set in '*numCols'
1726 * userID is evaluated in the following manner:
1727 * userID == NULL ... UserID is ignored
1728 * userID == "" ... UserID set equal to 'this->uid'
1729 * userID != "" ... UserID set equal to 'userID'
1731 * NOTE: ALL column bindings associated with this wxDB instance are unbound
1732 * by this function. This function should use its own wxDB instance
1733 * to avoid undesired unbinding of columns.
1738 wxColInf
*colInf
= 0;
1748 if (!wxStrlen(userID
))
1756 // dBase does not use user names, and some drivers fail if you try to pass one
1757 if (Dbms() == dbmsDBASE
)
1760 // Oracle user names may only be in uppercase, so force
1761 // the name to uppercase
1762 if (Dbms() == dbmsORACLE
)
1763 UserID
= UserID
.Upper();
1765 // Pass 1 - Determine how many columns there are.
1766 // Pass 2 - Allocate the wxColInf array and fill in
1767 // the array with the column information.
1769 for (pass
= 1; pass
<= 2; pass
++)
1773 if (noCols
== 0) // Probably a bogus table name(s)
1775 // Allocate n wxColInf objects to hold the column information
1776 colInf
= new wxColInf
[noCols
+1];
1779 // Mark the end of the array
1780 wxStrcpy(colInf
[noCols
].tableName
, "");
1781 wxStrcpy(colInf
[noCols
].colName
, "");
1782 colInf
[noCols
].sqlDataType
= 0;
1785 TableName
= tableName
;
1786 // Oracle table names are uppercase only, so force
1787 // the name to uppercase just in case programmer forgot to do this
1788 if (Dbms() == dbmsORACLE
)
1789 TableName
= TableName
.Upper();
1791 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1793 // MySQL and Access cannot accept a user name when looking up column names, so we
1794 // use the call below that leaves out the user name
1795 if (wxStrcmp(UserID
.GetData(),"") &&
1796 Dbms() != dbmsMY_SQL
&&
1797 Dbms() != dbmsACCESS
)
1799 retcode
= SQLColumns(hstmt
,
1800 NULL
, 0, // All qualifiers
1801 (UCHAR
*) UserID
.GetData(), SQL_NTS
, // Owner
1802 (UCHAR
*) TableName
.GetData(), SQL_NTS
,
1803 NULL
, 0); // All columns
1807 retcode
= SQLColumns(hstmt
,
1808 NULL
, 0, // All qualifiers
1810 (UCHAR
*) TableName
.GetData(), SQL_NTS
,
1811 NULL
, 0); // All columns
1813 if (retcode
!= SQL_SUCCESS
)
1814 { // Error occured, abort
1815 DispAllErrors(henv
, hdbc
, hstmt
);
1818 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1824 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
1826 if (pass
== 1) // First pass, just add up the number of columns
1828 else // Pass 2; Fill in the array of structures
1830 if (colNo
< noCols
) // Some extra error checking to prevent memory overwrites
1832 // NOTE: Only the ODBC 1.x fields are retrieved
1833 GetData( 1, SQL_C_CHAR
, (UCHAR
*) colInf
[colNo
].catalog
, 128+1, &cb
);
1834 GetData( 2, SQL_C_CHAR
, (UCHAR
*) colInf
[colNo
].schema
, 128+1, &cb
);
1835 GetData( 3, SQL_C_CHAR
, (UCHAR
*) colInf
[colNo
].tableName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
1836 GetData( 4, SQL_C_CHAR
, (UCHAR
*) colInf
[colNo
].colName
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
1837 GetData( 5, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].sqlDataType
, 0, &cb
);
1838 GetData( 6, SQL_C_CHAR
, (UCHAR
*) colInf
[colNo
].typeName
, 128+1, &cb
);
1839 GetData( 7, SQL_C_SLONG
, (UCHAR
*) &colInf
[colNo
].columnSize
, 0, &cb
);
1840 // BJO 991214 : SQL_C_SSHORT instead of SQL_C_SLONG, otherwise fails on Sparc (probably all 64 bit architectures)
1841 GetData( 8, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].bufferLength
, 0, &cb
);
1842 GetData( 9, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].decimalDigits
,0, &cb
);
1843 GetData(10, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].numPrecRadix
, 0, &cb
);
1844 GetData(11, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].nullable
, 0, &cb
);
1845 GetData(12, SQL_C_CHAR
, (UCHAR
*) colInf
[colNo
].remarks
, 254+1, &cb
);
1846 // Start Values for Primary/Foriegn Key (=No)
1847 colInf
[colNo
].PkCol
= 0; // Primary key column 0=No; 1= First Key, 2 = Second Key etc.
1848 colInf
[colNo
].PkTableName
[0] = 0; // Tablenames where Primary Key is used as a Foreign Key
1849 colInf
[colNo
].FkCol
= 0; // Foreign key column 0=No; 1= First Key, 2 = Second Key etc.
1850 colInf
[colNo
].FkTableName
[0] = 0; // Foreign key table name
1852 // Determine the wxDB data type that is used to represent the native data type of this data source
1853 colInf
[colNo
].dbDataType
= 0;
1854 if (!wxStricmp(typeInfVarchar
.TypeName
,colInf
[colNo
].typeName
))
1856 if (colInf
[colNo
].columnSize
< 1)
1858 // IODBC does not return a correct columnSize, so we set
1859 // columnSize = bufferLength if no column size was returned
1860 colInf
[colNo
].columnSize
= colInf
[colNo
].bufferLength
;
1862 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_VARCHAR
;
1864 else if (!wxStricmp(typeInfInteger
.TypeName
,colInf
[colNo
].typeName
))
1865 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_INTEGER
;
1866 else if (!wxStricmp(typeInfFloat
.TypeName
,colInf
[colNo
].typeName
))
1867 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_FLOAT
;
1868 else if (!wxStricmp(typeInfDate
.TypeName
,colInf
[colNo
].typeName
))
1869 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_DATE
;
1875 if (retcode
!= SQL_NO_DATA_FOUND
)
1876 { // Error occured, abort
1877 DispAllErrors(henv
, hdbc
, hstmt
);
1880 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1887 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1889 // Store Primary and Foriegn Keys
1890 GetKeyFields(tableName
,colInf
,noCols
);
1896 } // wxDB::GetColumns()
1899 /********** wxDB::GetColumnCount() **********/
1900 int wxDB::GetColumnCount(char *tableName
, const char *userID
)
1902 * Returns a count of how many columns are in a table.
1903 * If an error occurs in computing the number of columns
1904 * this function will return a -1 for the count
1906 * userID is evaluated in the following manner:
1907 * userID == NULL ... UserID is ignored
1908 * userID == "" ... UserID set equal to 'this->uid'
1909 * userID != "" ... UserID set equal to 'userID'
1911 * NOTE: ALL column bindings associated with this wxDB instance are unbound
1912 * by this function. This function should use its own wxDB instance
1913 * to avoid undesired unbinding of columns.
1925 if (!wxStrlen(userID
))
1933 // dBase does not use user names, and some drivers fail if you try to pass one
1934 if (Dbms() == dbmsDBASE
)
1937 // Oracle user names may only be in uppercase, so force
1938 // the name to uppercase
1939 if (Dbms() == dbmsORACLE
)
1940 UserID
= UserID
.Upper();
1943 // Loop through each table name
1945 TableName
= tableName
;
1946 // Oracle table names are uppercase only, so force
1947 // the name to uppercase just in case programmer forgot to do this
1948 if (Dbms() == dbmsORACLE
)
1949 TableName
= TableName
.Upper();
1951 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1953 // MySQL and Access cannot accept a user name when looking up column names, so we
1954 // use the call below that leaves out the user name
1955 if (wxStrcmp(UserID
.GetData(),"") &&
1956 Dbms() != dbmsMY_SQL
&&
1957 Dbms() != dbmsACCESS
)
1959 retcode
= SQLColumns(hstmt
,
1960 NULL
, 0, // All qualifiers
1961 (UCHAR
*) UserID
.GetData(), SQL_NTS
, // Owner
1962 (UCHAR
*) TableName
.GetData(), SQL_NTS
,
1963 NULL
, 0); // All columns
1967 retcode
= SQLColumns(hstmt
,
1968 NULL
, 0, // All qualifiers
1970 (UCHAR
*) TableName
.GetData(), SQL_NTS
,
1971 NULL
, 0); // All columns
1973 if (retcode
!= SQL_SUCCESS
)
1974 { // Error occured, abort
1975 DispAllErrors(henv
, hdbc
, hstmt
);
1976 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1980 // Count the columns
1981 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
1984 if (retcode
!= SQL_NO_DATA_FOUND
)
1985 { // Error occured, abort
1986 DispAllErrors(henv
, hdbc
, hstmt
);
1987 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1993 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1996 } // wxDB::GetColumnCount()
1999 /********** wxDB::GetCatalog() *******/
2000 wxDbInf
*wxDB::GetCatalog(char *userID
)
2002 * ---------------------------------------------------------------------
2003 * -- 19991203 : mj10777@gmx.net : Create ------
2004 * -- : Creates a wxDbInf with Tables / Cols Array ------
2005 * -- : uses SQLTables and fills pTableInf; ------
2006 * -- : pColInf is set to NULL and numCols to 0; ------
2007 * -- : returns pDbInf (wxDbInf) ------
2008 * -- - if unsuccesfull (pDbInf == NULL) ------
2009 * -- : pColInf can be filled with GetColumns(..); ------
2010 * -- : numCols can be filled with GetColumnCount(..); ------
2011 * ---------------------------------------------------------------------
2013 * userID is evaluated in the following manner:
2014 * userID == NULL ... UserID is ignored
2015 * userID == "" ... UserID set equal to 'this->uid'
2016 * userID != "" ... UserID set equal to 'userID'
2018 * NOTE: ALL column bindings associated with this wxDB instance are unbound
2019 * by this function. This function should use its own wxDB instance
2020 * to avoid undesired unbinding of columns.
2023 wxDbInf
*pDbInf
= NULL
; // Array of catalog entries
2024 int noTab
= 0; // Counter while filling table entries
2028 // char tblNameSave[DB_MAX_TABLE_NAME_LEN+1];
2029 wxString tblNameSave
;
2035 if (!wxStrlen(userID
))
2043 // dBase does not use user names, and some drivers fail if you try to pass one
2044 if (Dbms() == dbmsDBASE
)
2047 // Oracle user names may only be in uppercase, so force
2048 // the name to uppercase
2049 if (Dbms() == dbmsORACLE
)
2050 UserID
= UserID
.Upper();
2052 //-------------------------------------------------------------
2053 pDbInf
= new wxDbInf
; // Create the Database Arrray
2054 pDbInf
->catalog
[0] = 0;
2055 pDbInf
->schema
[0] = 0;
2056 pDbInf
->numTables
= 0; // Counter for Tables
2057 pDbInf
->pTableInf
= NULL
; // Array of Tables
2058 //-------------------------------------------------------------
2059 // Table Information
2060 // Pass 1 - Determine how many Tables there are.
2061 // Pass 2 - Create the Table array and fill it
2062 // - Create the Cols array = NULL
2063 //-------------------------------------------------------------
2064 for (pass
= 1; pass
<= 2; pass
++)
2066 SQLFreeStmt(hstmt
, SQL_CLOSE
); // Close if Open
2069 if (wxStrcmp(UserID
.GetData(),"") &&
2070 Dbms() != dbmsMY_SQL
&&
2071 Dbms() != dbmsACCESS
)
2073 retcode
= SQLTables(hstmt
,
2074 NULL
, 0, // All qualifiers
2075 (UCHAR
*) UserID
.GetData(), SQL_NTS
, // User specified
2076 NULL
, 0, // All tables
2077 NULL
, 0); // All columns
2081 retcode
= SQLTables(hstmt
,
2082 NULL
, 0, // All qualifiers
2083 NULL
, 0, // User specified
2084 NULL
, 0, // All tables
2085 NULL
, 0); // All columns
2087 if (retcode
!= SQL_SUCCESS
)
2089 DispAllErrors(henv
, hdbc
, hstmt
);
2091 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2095 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
) // Table Information
2097 if (pass
== 1) // First pass, just count the Tables
2099 if (pDbInf
->numTables
== 0)
2101 GetData( 1, SQL_C_CHAR
, (UCHAR
*) pDbInf
->catalog
, 128+1, &cb
);
2102 GetData( 2, SQL_C_CHAR
, (UCHAR
*) pDbInf
->schema
, 128+1, &cb
);
2104 pDbInf
->numTables
++; // Counter for Tables
2106 if (pass
== 2) // Create and fill the Table entries
2108 if (pDbInf
->pTableInf
== NULL
) // Has the Table Array been created
2109 { // no, then create the Array
2110 pDbInf
->pTableInf
= new wxTableInf
[pDbInf
->numTables
];
2111 for (noTab
=0;noTab
<pDbInf
->numTables
;noTab
++)
2113 (pDbInf
->pTableInf
+noTab
)->tableName
[0] = 0;
2114 (pDbInf
->pTableInf
+noTab
)->tableType
[0] = 0;
2115 (pDbInf
->pTableInf
+noTab
)->tableRemarks
[0] = 0;
2116 (pDbInf
->pTableInf
+noTab
)->numCols
= 0;
2117 (pDbInf
->pTableInf
+noTab
)->pColInf
= NULL
;
2120 } // if (pDbInf->pTableInf == NULL) // Has the Table Array been created
2121 GetData( 3, SQL_C_CHAR
, (UCHAR
*) (pDbInf
->pTableInf
+noTab
)->tableName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
2122 GetData( 4, SQL_C_CHAR
, (UCHAR
*) (pDbInf
->pTableInf
+noTab
)->tableType
, 30+1, &cb
);
2123 GetData( 5, SQL_C_CHAR
, (UCHAR
*) (pDbInf
->pTableInf
+noTab
)->tableRemarks
, 254+1, &cb
);
2125 } // if (pass == 2) We now know the amount of Tables
2126 } // while ((retcode = SQLFetch(hstmt)) == SQL_SUCCESS)
2127 } // for (pass = 1; pass <= 2; pass++)
2128 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2130 // Query how many columns are in each table
2131 for (noTab
=0;noTab
<pDbInf
->numTables
;noTab
++)
2133 (pDbInf
->pTableInf
+noTab
)->numCols
= GetColumnCount((pDbInf
->pTableInf
+noTab
)->tableName
,UserID
);
2136 } // wxDB::GetCatalog()
2139 /********** wxDB::Catalog() **********/
2140 bool wxDB::Catalog(const char *userID
, const char *fileName
)
2142 * Creates the text file specified in 'filename' which will contain
2143 * a minimal data dictionary of all tables accessible by the user specified
2146 * userID is evaluated in the following manner:
2147 * userID == NULL ... UserID is ignored
2148 * userID == "" ... UserID set equal to 'this->uid'
2149 * userID != "" ... UserID set equal to 'userID'
2151 * NOTE: ALL column bindings associated with this wxDB instance are unbound
2152 * by this function. This function should use its own wxDB instance
2153 * to avoid undesired unbinding of columns.
2156 assert(fileName
&& wxStrlen(fileName
));
2160 char tblName
[DB_MAX_TABLE_NAME_LEN
+1];
2161 wxString tblNameSave
;
2162 char colName
[DB_MAX_COLUMN_NAME_LEN
+1];
2164 char typeName
[30+1];
2165 SWORD precision
, length
;
2169 FILE *fp
= fopen(fileName
,"wt");
2173 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2177 if (!wxStrlen(userID
))
2185 // dBase does not use user names, and some drivers fail if you try to pass one
2186 if (Dbms() == dbmsDBASE
)
2189 // Oracle user names may only be in uppercase, so force
2190 // the name to uppercase
2191 if (Dbms() == dbmsORACLE
)
2192 UserID
= UserID
.Upper();
2194 if (wxStrcmp(UserID
.GetData(),"") &&
2195 Dbms() != dbmsMY_SQL
&&
2196 Dbms() != dbmsACCESS
)
2198 retcode
= SQLColumns(hstmt
,
2199 NULL
, 0, // All qualifiers
2200 (UCHAR
*) UserID
.GetData(), SQL_NTS
, // User specified
2201 NULL
, 0, // All tables
2202 NULL
, 0); // All columns
2206 retcode
= SQLColumns(hstmt
,
2207 NULL
, 0, // All qualifiers
2208 NULL
, 0, // User specified
2209 NULL
, 0, // All tables
2210 NULL
, 0); // All columns
2212 if (retcode
!= SQL_SUCCESS
)
2214 DispAllErrors(henv
, hdbc
, hstmt
);
2223 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
2225 if (wxStrcmp(tblName
,tblNameSave
.GetData()))
2229 fputs("================================ ", fp
);
2230 fputs("================================ ", fp
);
2231 fputs("===================== ", fp
);
2232 fputs("========= ", fp
);
2233 fputs("=========\n", fp
);
2234 outStr
.sprintf("%-32s %-32s %-21s %9s %9s\n",
2235 "TABLE NAME", "COLUMN NAME", "DATA TYPE", "PRECISION", "LENGTH");
2236 fputs(outStr
.GetData(), fp
);
2237 fputs("================================ ", fp
);
2238 fputs("================================ ", fp
);
2239 fputs("===================== ", fp
);
2240 fputs("========= ", fp
);
2241 fputs("=========\n", fp
);
2242 tblNameSave
= tblName
;
2245 GetData(3,SQL_C_CHAR
, (UCHAR
*)tblName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
2246 GetData(4,SQL_C_CHAR
, (UCHAR
*)colName
, DB_MAX_COLUMN_NAME_LEN
+1,&cb
);
2247 GetData(5,SQL_C_SSHORT
,(UCHAR
*)&sqlDataType
,0, &cb
);
2248 GetData(6,SQL_C_CHAR
, (UCHAR
*)typeName
, sizeof(typeName
), &cb
);
2249 GetData(7,SQL_C_SSHORT
,(UCHAR
*)&precision
, 0, &cb
);
2250 GetData(8,SQL_C_SSHORT
,(UCHAR
*)&length
, 0, &cb
);
2252 outStr
.sprintf("%-32s %-32s (%04d)%-15s %9d %9d\n",
2253 tblName
, colName
, sqlDataType
, typeName
, precision
, length
);
2254 if (fputs(outStr
.GetData(), fp
) == EOF
)
2256 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2263 if (retcode
!= SQL_NO_DATA_FOUND
)
2264 DispAllErrors(henv
, hdbc
, hstmt
);
2266 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2269 return(retcode
== SQL_NO_DATA_FOUND
);
2271 } // wxDB::Catalog()
2274 bool wxDB::TableExists(const char *tableName
, const char *userID
, const char *tablePath
)
2276 * Table name can refer to a table, view, alias or synonym. Returns true
2277 * if the object exists in the database. This function does not indicate
2278 * whether or not the user has privleges to query or perform other functions
2281 * userID is evaluated in the following manner:
2282 * userID == NULL ... UserID is ignored
2283 * userID == "" ... UserID set equal to 'this->uid'
2284 * userID != "" ... UserID set equal to 'userID'
2290 assert(tableName
&& wxStrlen(tableName
));
2292 if (Dbms() == dbmsDBASE
)
2295 if (tablePath
&& wxStrlen(tablePath
))
2296 dbName
.sprintf("%s/%s.dbf",tablePath
,tableName
);
2298 dbName
.sprintf("%s.dbf",tableName
);
2301 exists
= wxFileExists(dbName
.GetData());
2307 if (!wxStrlen(userID
))
2315 // Oracle user names may only be in uppercase, so force
2316 // the name to uppercase
2317 if (Dbms() == dbmsORACLE
)
2318 UserID
= UserID
.Upper();
2320 TableName
= tableName
;
2321 // Oracle table names are uppercase only, so force
2322 // the name to uppercase just in case programmer forgot to do this
2323 if (Dbms() == dbmsORACLE
)
2324 TableName
= TableName
.Upper();
2326 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2329 // MySQL and Access cannot accept a user name when looking up table names, so we
2330 // use the call below that leaves out the user name
2331 if (wxStrcmp(UserID
,"") &&
2332 Dbms() != dbmsMY_SQL
&&
2333 Dbms() != dbmsACCESS
)
2335 retcode
= SQLTables(hstmt
,
2336 NULL
, 0, // All qualifiers
2337 (UCHAR
*) UserID
.GetData(), SQL_NTS
, // All owners
2338 (UCHAR FAR
*)TableName
.GetData(), SQL_NTS
,
2339 NULL
, 0); // All table types
2343 retcode
= SQLTables(hstmt
,
2344 NULL
, 0, // All qualifiers
2345 NULL
, 0, // All owners
2346 (UCHAR FAR
*)TableName
.GetData(), SQL_NTS
,
2347 NULL
, 0); // All table types
2349 if (retcode
!= SQL_SUCCESS
)
2350 return(DispAllErrors(henv
, hdbc
, hstmt
));
2352 retcode
= SQLFetch(hstmt
);
2353 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
2355 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2356 return(DispAllErrors(henv
, hdbc
, hstmt
));
2359 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2362 } // wxDB::TableExists()
2365 /********** wxDB::SqlLog() **********/
2366 bool wxDB::SqlLog(wxSqlLogState state
, const char *filename
, bool append
)
2368 assert(state
== sqlLogON
|| state
== sqlLogOFF
);
2369 assert(state
== sqlLogOFF
|| filename
);
2371 if (state
== sqlLogON
)
2375 fpSqlLog
= fopen(filename
, (append
? "at" : "wt"));
2376 if (fpSqlLog
== NULL
)
2384 if (fclose(fpSqlLog
))
2390 sqlLogState
= state
;
2396 /********** wxDB::WriteSqlLog() **********/
2397 bool wxDB::WriteSqlLog(const char *logMsg
)
2401 if (fpSqlLog
== 0 || sqlLogState
== sqlLogOFF
)
2404 if (fputs("\n", fpSqlLog
) == EOF
) return(FALSE
);
2405 if (fputs(logMsg
, fpSqlLog
) == EOF
) return(FALSE
);
2406 if (fputs("\n", fpSqlLog
) == EOF
) return(FALSE
);
2410 } // wxDB::WriteSqlLog()
2413 /********** wxDB::Dbms() **********/
2414 wxDBMS
wxDB::Dbms(void)
2416 * Be aware that not all database engines use the exact same syntax, and not
2417 * every ODBC compliant database is compliant to the same level of compliancy.
2418 * Some manufacturers support the minimum Level 1 compliancy, and others up
2419 * through Level 3. Others support subsets of features for levels above 1.
2421 * If you find an inconsistency between the wxDB class and a specific database
2422 * engine, and an identifier to this section, and special handle the database in
2423 * the area where behavior is non-conforming with the other databases.
2426 * NOTES ABOUT ISSUES SPECIFIC TO EACH DATABASE ENGINE
2427 * ---------------------------------------------------
2430 * - Currently the only database supported by the class to support VIEWS
2433 * - Does not support the SQL_TIMESTAMP structure
2434 * - Supports only one cursor and one connect (apparently? with Microsoft driver only?)
2435 * - Does not automatically create the primary index if the 'keyField' param of SetColDef
2436 * is TRUE. The user must create ALL indexes from their program.
2437 * - Table names can only be 8 characters long
2438 * - Column names can only be 10 characters long
2441 * - To lock a record during QUERY functions, the reserved word 'HOLDLOCK' must be added
2442 * after every table name involved in the query/join if that tables matching record(s)
2444 * - Ignores the keywords 'FOR UPDATE'. Use the HOLDLOCK functionality described above
2446 * SYBASE (Enterprise)
2447 * - If a column is part of the Primary Key, the column cannot be NULL
2450 * - If a column is part of the Primary Key, the column cannot be NULL
2451 * - Cannot support selecting for update [::CanSelectForUpdate()]. Always returns FALSE
2454 * - Does not support the keywords 'ASC' or 'DESC' as of release v6.5.0
2459 wxChar baseName
[25+1];
2461 wxStrncpy(baseName
,dbInf
.dbmsName
,25);
2462 if (!wxStricmp(dbInf
.dbmsName
,"Adaptive Server Anywhere"))
2463 return(dbmsSYBASE_ASA
);
2464 if (!wxStricmp(dbInf
.dbmsName
,"SQL Server")) // Sybase Adaptive Server
2465 return(dbmsSYBASE_ASE
);
2466 if (!wxStricmp(dbInf
.dbmsName
,"Microsoft SQL Server"))
2467 return(dbmsMS_SQL_SERVER
);
2468 if (!wxStricmp(dbInf
.dbmsName
,"MySQL"))
2470 if (!wxStricmp(dbInf
.dbmsName
,"PostgreSQL")) // v6.5.0
2471 return(dbmsPOSTGRES
);
2474 if (!wxStricmp(baseName
,"Informix"))
2475 return(dbmsINFORMIX
);
2478 if (!wxStricmp(baseName
,"Oracle"))
2480 if (!wxStricmp(dbInf
.dbmsName
,"ACCESS"))
2482 if (!wxStricmp(dbInf
.dbmsName
,"MySQL"))
2486 if (!wxStricmp(baseName
,"DBASE"))
2489 return(dbmsUNIDENTIFIED
);
2493 /********** wxDbGetConnection() **********/
2494 wxDB WXDLLEXPORT
*wxDbGetConnection(wxDbConnectInf
*pDbConfig
, bool FwdOnlyCursors
)
2498 // Scan the linked list searching for an available database connection
2499 // that's already been opened but is currently not in use.
2500 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
2502 // The database connection must be for the same datasource
2503 // name and must currently not be in use.
2504 if (pList
->Free
&& (! wxStrcmp(pDbConfig
->Dsn
, pList
->Dsn
))) // Found a free connection
2506 pList
->Free
= FALSE
;
2507 return(pList
->PtrDb
);
2511 // No available connections. A new connection must be made and
2512 // appended to the end of the linked list.
2515 // Find the end of the list
2516 for (pList
= PtrBegDbList
; pList
->PtrNext
; pList
= pList
->PtrNext
);
2517 // Append a new list item
2518 pList
->PtrNext
= new wxDbList
;
2519 pList
->PtrNext
->PtrPrev
= pList
;
2520 pList
= pList
->PtrNext
;
2524 // Create the first node on the list
2525 pList
= PtrBegDbList
= new wxDbList
;
2529 // Initialize new node in the linked list
2531 pList
->Free
= FALSE
;
2532 wxStrcpy(pList
->Dsn
, pDbConfig
->Dsn
);
2533 pList
->PtrDb
= new wxDB(pDbConfig
->Henv
,FwdOnlyCursors
);
2535 // Connect to the datasource
2536 if (pList
->PtrDb
->Open(pDbConfig
->Dsn
, pDbConfig
->Uid
, pDbConfig
->AuthStr
))
2538 pList
->PtrDb
->SqlLog(SQLLOGstate
,SQLLOGfn
,TRUE
);
2539 return(pList
->PtrDb
);
2541 else // Unable to connect, destroy list item
2544 pList
->PtrPrev
->PtrNext
= 0;
2546 PtrBegDbList
= 0; // Empty list again
2547 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDB object
2548 pList
->PtrDb
->Close(); // Close the wxDB object
2549 delete pList
->PtrDb
; // Deletes the wxDB object
2550 delete pList
; // Deletes the linked list object
2554 } // wxDbGetConnection()
2557 /********** wxDbFreeConnection() **********/
2558 bool WXDLLEXPORT
wxDbFreeConnection(wxDB
*pDb
)
2562 // Scan the linked list searching for the database connection
2563 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
2565 if (pList
->PtrDb
== pDb
) // Found it!!!
2566 return(pList
->Free
= TRUE
);
2569 // Never found the database object, return failure
2572 } // wxDbFreeConnection()
2575 /********** wxDbCloseConnections() **********/
2576 void WXDLLEXPORT
wxDbCloseConnections(void)
2578 wxDbList
*pList
, *pNext
;
2580 // Traverse the linked list closing database connections and freeing memory as I go.
2581 for (pList
= PtrBegDbList
; pList
; pList
= pNext
)
2583 pNext
= pList
->PtrNext
; // Save the pointer to next
2584 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDB object
2585 pList
->PtrDb
->Close(); // Close the wxDB object
2586 delete pList
->PtrDb
; // Deletes the wxDB object
2587 delete pList
; // Deletes the linked list object
2590 // Mark the list as empty
2593 } // wxDbCloseConnections()
2596 /********** wxDbNumberConnectionsInUse() **********/
2597 int WXDLLEXPORT
wxDbConnectionsInUse(void)
2602 // Scan the linked list counting db connections that are currently in use
2603 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
2605 if (pList
->Free
== FALSE
)
2611 } // wxDbConnectionsInUse()
2614 /********** wxDbSqlLog() **********/
2615 bool wxDbSqlLog(wxSqlLogState state
, char *filename
)
2617 bool append
= FALSE
;
2620 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
2622 if (!pList
->PtrDb
->SqlLog(state
,filename
,append
))
2627 SQLLOGstate
= state
;
2628 wxStrcpy(SQLLOGfn
,filename
);
2636 /********** wxDbCreateDataSource() **********/
2637 int wxDbCreateDataSource(const char *driverName
, const char *dsn
, const char *description
,
2638 bool sysDSN
, const char *defDir
, wxWindow
*parent
)
2640 * !!!! ONLY FUNCTIONAL UNDER MSW with VC6 !!!!
2641 * Very rudimentary creation of an ODBC data source.
2651 dsnLocation
= ODBC_ADD_SYS_DSN
;
2653 dsnLocation
= ODBC_ADD_DSN
;
2655 // NOTE: The decimal 2 is an invalid character in all keyword pairs
2656 // so that is why I used it, as wxString does not deal well with
2657 // embedded nulls in strings
2658 setupStr
.sprintf("DSN=%s%cDescription=%s%cDefaultDir=%s%c",dsn
,2,description
,2,defDir
,2);
2660 // Replace the separator from above with the '\0' seperator needed
2661 // by the SQLConfigDataSource() function
2665 k
= setupStr
.Find((wxChar
)2,TRUE
);
2666 if (k
!= wxNOT_FOUND
)
2667 setupStr
[(UINT
)k
] = '\0';
2669 while (k
!= wxNOT_FOUND
);
2671 result
= SQLConfigDataSource((HWND
)parent
->GetHWND(), dsnLocation
,
2672 driverName
, setupStr
.GetData());
2676 // check for errors caused by ConfigDSN based functions
2679 wxChar errMsg
[500+1];
2682 SQLInstallerError(1,&retcode
,errMsg
,500,&cb
);
2685 // logError(errMsg, sqlState);
2688 #ifdef DBDEBUG_CONSOLE
2689 // When run in console mode, use standard out to display errors.
2690 cout
<< errMsg
<< endl
;
2691 cout
<< "Press any key to continue..." << endl
;
2693 #endif // DBDEBUG_CONSOLE
2697 wxLogDebug(errMsg
,"DEBUG MESSAGE");
2698 #endif // __WXDEBUG__
2706 wxLogDebug("wxDbCreateDataSource() not available except under MSW","DEBUG MESSAGE");
2712 } // wxDbCreateDataSource()
2716 /********** wxDbGetDataSource() **********/
2717 bool wxDbGetDataSource(HENV henv
, char *Dsn
, SWORD DsnMax
, char *DsDesc
, SWORD DsDescMax
,
2720 * Dsn and DsDesc will contain the data source name and data source
2721 * description upon return
2726 if (SQLDataSources(henv
, direction
, (UCHAR FAR
*) Dsn
, DsnMax
, &cb1
,
2727 (UCHAR FAR
*) DsDesc
, DsDescMax
, &cb2
) == SQL_SUCCESS
)
2732 } // wxDbGetDataSource()
2735 // Change this to 0 to remove use of all deprecated functions
2737 /********************************************************************
2738 ********************************************************************
2740 * The following functions are all DEPRECATED and are included for
2741 * backward compatability reasons only
2743 ********************************************************************
2744 ********************************************************************/
2745 bool SqlLog(sqlLog state
, char *filename
)
2747 return wxDbSqlLog((enum wxSqlLogState
)state
, filename
);
2749 /***** DEPRECATED: use wxGetDataSource() *****/
2750 bool GetDataSource(HENV henv
, char *Dsn
, SWORD DsnMax
, char *DsDesc
, SWORD DsDescMax
,
2753 return wxDbGetDataSource(henv
, Dsn
, DsnMax
, DsDesc
, DsDescMax
, direction
);
2755 /***** DEPRECATED: use wxDbGetConnection() *****/
2756 wxDB WXDLLEXPORT
*GetDbConnection(DbStuff
*pDbStuff
, bool FwdOnlyCursors
)
2758 return wxDbGetConnection((wxDbConnectInf
*)pDbStuff
, FwdOnlyCursors
);
2760 /***** DEPRECATED: use wxDbFreeConnection() *****/
2761 bool WXDLLEXPORT
FreeDbConnection(wxDB
*pDb
)
2763 return wxDbFreeConnection(pDb
);
2765 /***** DEPRECATED: use wxDbCloseConnections() *****/
2766 void WXDLLEXPORT
CloseDbConnections(void)
2768 wxDbCloseConnections();
2770 /***** DEPRECATED: use wxDbConnectionsInUse() *****/
2771 int WXDLLEXPORT
NumberDbConnectionsInUse(void)
2773 return wxDbConnectionsInUse();