1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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.
7 // Modified by: George Tasker
9 // Mark Johnson, wxWindows@mj10777.de
11 // -Added support for SQL statement logging and database cataloging
13 // -Added QUERY_ONLY mode support to reduce default number of cursors
14 // -Added additional SQL logging code
15 // -Added DEBUG-ONLY tracking of wxTable objects to detect orphaned DB connections
16 // -Set ODBC option to only read committed writes to the DB so all
17 // databases operate the same in that respect
20 // Copyright: (c) 1996 Remstar International, Inc.
21 // Licence: wxWindows licence
22 ///////////////////////////////////////////////////////////////////////////////
24 #include "wx/wxprec.h"
33 #include "wx/object.h"
35 #include "wx/string.h"
41 #ifdef DBDEBUG_CONSOLE
42 #include "wx/ioswrap.h"
45 #include "wx/filefn.h"
46 #include "wx/wxchar.h"
56 // DLL options compatibility check:
57 WX_CHECK_BUILD_OPTIONS("wxODBC")
59 WXDLLIMPEXP_DATA_ODBC(wxDbList
*) PtrBegDbList
= 0;
61 wxChar
const *SQL_LOG_FILENAME
= wxT("sqllog.txt");
62 wxChar
const *SQL_CATALOG_FILENAME
= wxT("catalog.txt");
65 extern wxList TablesInUse
;
68 // SQL Log defaults to be used by GetDbConnection
69 wxDbSqlLogState SQLLOGstate
= sqlLogOFF
;
71 static wxString SQLLOGfn
= SQL_LOG_FILENAME
;
73 // The wxDb::errorList is copied to this variable when the wxDb object
74 // is closed. This way, the error list is still available after the
75 // database object is closed. This is necessary if the database
76 // connection fails so the calling application can show the operator
77 // why the connection failed. Note: as each wxDb object is closed, it
78 // will overwrite the errors of the previously destroyed wxDb object in
79 // this variable. NOTE: This occurs during a CLOSE, not a FREEing of the
81 wxChar DBerrorList
[DB_MAX_ERROR_HISTORY
][DB_MAX_ERROR_MSG_LEN
+1];
84 // This type defines the return row-struct form
85 // SQLTablePrivileges, and is used by wxDB::TablePrivileges.
88 wxChar tableQual
[128+1];
89 wxChar tableOwner
[128+1];
90 wxChar tableName
[128+1];
91 wxChar grantor
[128+1];
92 wxChar grantee
[128+1];
93 wxChar privilege
[128+1];
94 wxChar grantable
[3+1];
95 } wxDbTablePrivilegeInfo
;
98 /********** wxDbConnectInf Constructor - form 1 **********/
99 wxDbConnectInf::wxDbConnectInf()
102 freeHenvOnDestroy
= false;
108 /********** wxDbConnectInf Constructor - form 2 **********/
109 wxDbConnectInf::wxDbConnectInf(HENV henv
, const wxString
&dsn
, const wxString
&userID
,
110 const wxString
&password
, const wxString
&defaultDir
,
111 const wxString
&fileType
, const wxString
&description
)
114 freeHenvOnDestroy
= false;
125 SetPassword(password
);
126 SetDescription(description
);
127 SetFileType(fileType
);
128 SetDefaultDir(defaultDir
);
129 } // wxDbConnectInf Constructor
132 wxDbConnectInf::~wxDbConnectInf()
134 if (freeHenvOnDestroy
)
138 } // wxDbConnectInf Destructor
142 /********** wxDbConnectInf::Initialize() **********/
143 bool wxDbConnectInf::Initialize()
145 freeHenvOnDestroy
= false;
147 if (freeHenvOnDestroy
&& Henv
)
154 ConnectionStr
[0] = 0;
159 useConnectionStr
= false;
162 } // wxDbConnectInf::Initialize()
165 /********** wxDbConnectInf::AllocHenv() **********/
166 bool wxDbConnectInf::AllocHenv()
168 // This is here to help trap if you are getting a new henv
169 // without releasing an existing henv
172 // Initialize the ODBC Environment for Database Operations
173 if (SQLAllocEnv(&Henv
) != SQL_SUCCESS
)
175 wxLogDebug(wxT("A problem occurred while trying to get a connection to the data source"));
179 freeHenvOnDestroy
= true;
182 } // wxDbConnectInf::AllocHenv()
185 void wxDbConnectInf::FreeHenv()
193 freeHenvOnDestroy
= false;
195 } // wxDbConnectInf::FreeHenv()
198 void wxDbConnectInf::SetDsn(const wxString
&dsn
)
200 wxASSERT(dsn
.length() < WXSIZEOF(Dsn
));
202 wxStrncpy(Dsn
, dsn
, WXSIZEOF(Dsn
)-1);
203 Dsn
[WXSIZEOF(Dsn
)-1] = 0; // Prevent buffer overrun
204 } // wxDbConnectInf::SetDsn()
207 void wxDbConnectInf::SetUserID(const wxString
&uid
)
209 wxASSERT(uid
.length() < WXSIZEOF(Uid
));
210 wxStrncpy(Uid
, uid
, WXSIZEOF(Uid
)-1);
211 Uid
[WXSIZEOF(Uid
)-1] = 0; // Prevent buffer overrun
212 } // wxDbConnectInf::SetUserID()
215 void wxDbConnectInf::SetPassword(const wxString
&password
)
217 wxASSERT(password
.length() < WXSIZEOF(AuthStr
));
219 wxStrncpy(AuthStr
, password
, WXSIZEOF(AuthStr
)-1);
220 AuthStr
[WXSIZEOF(AuthStr
)-1] = 0; // Prevent buffer overrun
221 } // wxDbConnectInf::SetPassword()
223 void wxDbConnectInf::SetConnectionStr(const wxString
&connectStr
)
225 wxASSERT(connectStr
.length() < WXSIZEOF(ConnectionStr
));
227 useConnectionStr
= wxStrlen(connectStr
) > 0;
229 wxStrncpy(ConnectionStr
, connectStr
, WXSIZEOF(ConnectionStr
)-1);
230 ConnectionStr
[WXSIZEOF(ConnectionStr
)-1] = 0; // Prevent buffer overrun
231 } // wxDbConnectInf::SetConnectionStr()
234 /********** wxDbColFor Constructor **********/
235 wxDbColFor::wxDbColFor()
238 } // wxDbColFor::wxDbColFor()
241 /********** wxDbColFor::Initialize() **********/
242 void wxDbColFor::Initialize()
252 i_Nation
= 0; // 0=EU, 1=UK, 2=International, 3=US
255 Format(1,DB_DATA_TYPE_VARCHAR
,0,0,0); // the Function that does the work
256 } // wxDbColFor::Initialize()
259 /********** wxDbColFor::Format() **********/
260 int wxDbColFor::Format(int Nation
, int dbDataType
, SWORD sqlDataType
,
261 short columnLength
, short decimalDigits
)
263 // ----------------------------------------------------------------------------------------
264 // -- 19991224 : mj10777 : Create
265 // There is still a lot of work to do here, but it is a start
266 // It handles all the basic data-types that I have run into up to now
267 // The main work will have be with Dates and float Formatting
268 // (US 1,000.00 ; EU 1.000,00)
269 // There are wxWindow plans for locale support and the new wxDateTime. If
270 // they define some constants (wxEUROPEAN) that can be gloably used,
271 // they should be used here.
272 // ----------------------------------------------------------------------------------------
273 // There should also be a function to scan in a string to fill the variable
274 // ----------------------------------------------------------------------------------------
276 i_Nation
= Nation
; // 0 = timestamp , 1=EU, 2=UK, 3=International, 4=US
277 i_dbDataType
= dbDataType
;
278 i_sqlDataType
= sqlDataType
;
279 s_Field
.Printf(wxT("%s%d"),s_Amount
[1].c_str(),i_Amount
[1]); // OK for VARCHAR, INTEGER and FLOAT
281 if (i_dbDataType
== 0) // Filter unsupported dbDataTypes
283 if ((i_sqlDataType
== SQL_VARCHAR
)
285 #if defined(SQL_WCHAR)
286 || (i_sqlDataType
== SQL_WCHAR
)
288 #if defined(SQL_WVARCHAR)
289 || (i_sqlDataType
== SQL_WVARCHAR
)
292 || (i_sqlDataType
== SQL_LONGVARCHAR
))
293 i_dbDataType
= DB_DATA_TYPE_VARCHAR
;
294 if ((i_sqlDataType
== SQL_C_DATE
) || (i_sqlDataType
== SQL_C_TIMESTAMP
))
295 i_dbDataType
= DB_DATA_TYPE_DATE
;
296 if (i_sqlDataType
== SQL_C_BIT
)
297 i_dbDataType
= DB_DATA_TYPE_INTEGER
;
298 if (i_sqlDataType
== SQL_NUMERIC
)
299 i_dbDataType
= DB_DATA_TYPE_VARCHAR
; // glt - ??? is this right?
300 if (i_sqlDataType
== SQL_REAL
)
301 i_dbDataType
= DB_DATA_TYPE_FLOAT
;
302 if (i_sqlDataType
== SQL_C_BINARY
)
303 i_dbDataType
= DB_DATA_TYPE_BLOB
;
306 if ((i_dbDataType
== DB_DATA_TYPE_INTEGER
) && (i_sqlDataType
== SQL_C_DOUBLE
))
308 i_dbDataType
= DB_DATA_TYPE_FLOAT
;
311 switch(i_dbDataType
) // TBD: Still a lot of proper formatting to do
313 case DB_DATA_TYPE_VARCHAR
:
316 case DB_DATA_TYPE_INTEGER
:
319 case DB_DATA_TYPE_FLOAT
:
320 if (decimalDigits
== 0)
322 tempStr
.Printf(wxT("%%%d.%d"), columnLength
, decimalDigits
);
323 s_Field
.Printf(wxT("%sf"), tempStr
.c_str());
325 case DB_DATA_TYPE_DATE
:
326 if (i_Nation
== 0) // timestamp YYYY-MM-DD HH:MM:SS.SSS (tested for SYBASE)
328 s_Field
= wxT("%04d-%02d-%02d %02d:%02d:%02d.%03d");
330 if (i_Nation
== 1) // European DD.MM.YYYY HH:MM:SS.SSS
332 s_Field
= wxT("%02d.%02d.%04d %02d:%02d:%02d.%03d");
334 if (i_Nation
== 2) // UK DD/MM/YYYY HH:MM:SS.SSS
336 s_Field
= wxT("%02d/%02d/%04d %02d:%02d:%02d.%03d");
338 if (i_Nation
== 3) // International YYYY-MM-DD HH:MM:SS.SSS
340 s_Field
= wxT("%04d-%02d-%02d %02d:%02d:%02d.%03d");
342 if (i_Nation
== 4) // US MM/DD/YYYY HH:MM:SS.SSS
344 s_Field
= wxT("%02d/%02d/%04d %02d:%02d:%02d.%03d");
347 case DB_DATA_TYPE_BLOB
:
348 s_Field
.Printf(wxT("Unable to format(%d)-SQL(%d)"), dbDataType
,sqlDataType
); //
351 s_Field
.Printf(wxT("Unknown Format(%d)-SQL(%d)"), dbDataType
,sqlDataType
); //
355 } // wxDbColFor::Format()
358 /********** wxDbColInf Constructor **********/
359 wxDbColInf::wxDbColInf()
362 } // wxDbColInf::wxDbColInf()
365 /********** wxDbColInf Destructor ********/
366 wxDbColInf::~wxDbColInf()
371 } // wxDbColInf::~wxDbColInf()
374 bool wxDbColInf::Initialize()
396 } // wxDbColInf::Initialize()
399 /********** wxDbTableInf Constructor ********/
400 wxDbTableInf::wxDbTableInf()
403 } // wxDbTableInf::wxDbTableInf()
406 /********** wxDbTableInf Constructor ********/
407 wxDbTableInf::~wxDbTableInf()
412 } // wxDbTableInf::~wxDbTableInf()
415 bool wxDbTableInf::Initialize()
424 } // wxDbTableInf::Initialize()
427 /********** wxDbInf Constructor *************/
431 } // wxDbInf::wxDbInf()
434 /********** wxDbInf Destructor *************/
440 } // wxDbInf::~wxDbInf()
443 /********** wxDbInf::Initialize() *************/
444 bool wxDbInf::Initialize()
452 } // wxDbInf::Initialize()
455 /********** wxDb Constructor **********/
456 wxDb::wxDb(const HENV
&aHenv
, bool FwdOnlyCursors
)
458 // Copy the HENV into the db class
460 fwdOnlyCursors
= FwdOnlyCursors
;
466 /********** wxDb Destructor **********/
469 wxASSERT_MSG(!IsCached(),wxT("Cached connections must not be manually deleted, use\nwxDbFreeConnection() or wxDbCloseConnections()."));
479 /********** PRIVATE! wxDb::initialize PRIVATE! **********/
480 /********** wxDb::initialize() **********/
481 void wxDb::initialize()
483 * Private member function that sets all wxDb member variables to
484 * known values at creation of the wxDb
489 fpSqlLog
= 0; // Sql Log file pointer
490 sqlLogState
= sqlLogOFF
; // By default, logging is turned off
492 dbmsType
= dbmsUNIDENTIFIED
;
494 wxStrcpy(sqlState
,wxEmptyString
);
495 wxStrcpy(errorMsg
,wxEmptyString
);
496 nativeError
= cbErrorMsg
= 0;
497 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
498 wxStrcpy(errorList
[i
], wxEmptyString
);
500 // Init typeInf structures
501 typeInfVarchar
.TypeName
.Empty();
502 typeInfVarchar
.FsqlType
= 0;
503 typeInfVarchar
.Precision
= 0;
504 typeInfVarchar
.CaseSensitive
= 0;
505 typeInfVarchar
.MaximumScale
= 0;
507 typeInfInteger
.TypeName
.Empty();
508 typeInfInteger
.FsqlType
= 0;
509 typeInfInteger
.Precision
= 0;
510 typeInfInteger
.CaseSensitive
= 0;
511 typeInfInteger
.MaximumScale
= 0;
513 typeInfFloat
.TypeName
.Empty();
514 typeInfFloat
.FsqlType
= 0;
515 typeInfFloat
.Precision
= 0;
516 typeInfFloat
.CaseSensitive
= 0;
517 typeInfFloat
.MaximumScale
= 0;
519 typeInfDate
.TypeName
.Empty();
520 typeInfDate
.FsqlType
= 0;
521 typeInfDate
.Precision
= 0;
522 typeInfDate
.CaseSensitive
= 0;
523 typeInfDate
.MaximumScale
= 0;
525 typeInfBlob
.TypeName
.Empty();
526 typeInfBlob
.FsqlType
= 0;
527 typeInfBlob
.Precision
= 0;
528 typeInfBlob
.CaseSensitive
= 0;
529 typeInfBlob
.MaximumScale
= 0;
531 typeInfMemo
.TypeName
.Empty();
532 typeInfMemo
.FsqlType
= 0;
533 typeInfMemo
.Precision
= 0;
534 typeInfMemo
.CaseSensitive
= 0;
535 typeInfMemo
.MaximumScale
= 0;
537 // Error reporting is turned OFF by default
540 // Allocate a data source connection handle
541 if (SQLAllocConnect(henv
, &hdbc
) != SQL_SUCCESS
)
544 // Initialize the db status flag
547 // Mark database as not open as of yet
550 dbOpenedWithConnectionString
= false;
551 } // wxDb::initialize()
554 /********** PRIVATE! wxDb::convertUserID PRIVATE! **********/
556 // NOTE: Return value from this function MUST be copied
557 // immediately, as the value is not good after
558 // this function has left scope.
560 const wxChar
*wxDb::convertUserID(const wxChar
*userID
, wxString
&UserID
)
564 if (!wxStrlen(userID
))
572 // dBase does not use user names, and some drivers fail if you try to pass one
573 if ( Dbms() == dbmsDBASE
574 || Dbms() == dbmsXBASE_SEQUITER
)
577 // Some databases require user names to be specified in uppercase,
578 // so force the name to uppercase
579 if ((Dbms() == dbmsORACLE
) ||
580 (Dbms() == dbmsMAXDB
))
581 UserID
= UserID
.Upper();
583 return UserID
.c_str();
584 } // wxDb::convertUserID()
587 bool wxDb::determineDataTypes(bool failOnDataTypeUnsupported
)
591 // These are the possible SQL types we check for use against the datasource we are connected
592 // to for the purpose of determining which data type to use for the basic character strings
595 // NOTE: The first type in this enumeration that is determined to be supported by the
596 // datasource/driver is the one that will be used.
597 SWORD PossibleSqlCharTypes
[] = {
598 #if wxUSE_UNICODE && defined(SQL_WVARCHAR)
602 #if wxUSE_UNICODE && defined(SQL_WVARCHAR)
608 // These are the possible SQL types we check for use against the datasource we are connected
609 // to for the purpose of determining which data type to use for the basic non-floating point
612 // NOTE: The first type in this enumeration that is determined to be supported by the
613 // datasource/driver is the one that will be used.
614 SWORD PossibleSqlIntegerTypes
[] = {
618 // These are the possible SQL types we check for use against the datasource we are connected
619 // to for the purpose of determining which data type to use for the basic floating point number
622 // NOTE: The first type in this enumeration that is determined to be supported by the
623 // datasource/driver is the one that will be used.
624 SWORD PossibleSqlFloatTypes
[] = {
632 // These are the possible SQL types we check for use agains the datasource we are connected
633 // to for the purpose of determining which data type to use for the date/time column types
635 // NOTE: The first type in this enumeration that is determined to be supported by the
636 // datasource/driver is the one that will be used.
637 SWORD PossibleSqlDateTypes
[] = {
645 // These are the possible SQL types we check for use agains the datasource we are connected
646 // to for the purpose of determining which data type to use for the BLOB column types.
648 // NOTE: The first type in this enumeration that is determined to be supported by the
649 // datasource/driver is the one that will be used.
650 SWORD PossibleSqlBlobTypes
[] = {
655 // These are the possible SQL types we check for use agains the datasource we are connected
656 // to for the purpose of determining which data type to use for the MEMO column types
657 // (a type which allow to store large strings; like VARCHAR just with a bigger precision)
659 // NOTE: The first type in this enumeration that is determined to be supported by the
660 // datasource/driver is the one that will be used.
661 SWORD PossibleSqlMemoTypes
[] = {
666 // Query the data source regarding data type information
669 // The way it was determined which SQL data types to use was by calling SQLGetInfo
670 // for all of the possible SQL data types to see which ones were supported. If
671 // a type is not supported, the SQLFetch() that's called from getDataTypeInfo()
672 // fails with SQL_NO_DATA_FOUND. This is ugly because I'm sure the three SQL data
673 // types I've selected below will not always be what we want. These are just
674 // what happened to work against an Oracle 7/Intersolv combination. The following is
675 // a complete list of the results I got back against the Oracle 7 database:
677 // SQL_BIGINT SQL_NO_DATA_FOUND
678 // SQL_BINARY SQL_NO_DATA_FOUND
679 // SQL_BIT SQL_NO_DATA_FOUND
680 // SQL_CHAR type name = 'CHAR', Precision = 255
681 // SQL_DATE SQL_NO_DATA_FOUND
682 // SQL_DECIMAL type name = 'NUMBER', Precision = 38
683 // SQL_DOUBLE type name = 'NUMBER', Precision = 15
684 // SQL_FLOAT SQL_NO_DATA_FOUND
685 // SQL_INTEGER SQL_NO_DATA_FOUND
686 // SQL_LONGVARBINARY type name = 'LONG RAW', Precision = 2 billion
687 // SQL_LONGVARCHAR type name = 'LONG', Precision = 2 billion
688 // SQL_NUMERIC SQL_NO_DATA_FOUND
689 // SQL_REAL SQL_NO_DATA_FOUND
690 // SQL_SMALLINT SQL_NO_DATA_FOUND
691 // SQL_TIME SQL_NO_DATA_FOUND
692 // SQL_TIMESTAMP type name = 'DATE', Precision = 19
693 // SQL_VARBINARY type name = 'RAW', Precision = 255
694 // SQL_VARCHAR type name = 'VARCHAR2', Precision = 2000
695 // =====================================================================
696 // Results from a Microsoft Access 7.0 db, using a driver from Microsoft
698 // SQL_VARCHAR type name = 'TEXT', Precision = 255
699 // SQL_TIMESTAMP type name = 'DATETIME'
700 // SQL_DECIMAL SQL_NO_DATA_FOUND
701 // SQL_NUMERIC type name = 'CURRENCY', Precision = 19
702 // SQL_FLOAT SQL_NO_DATA_FOUND
703 // SQL_REAL type name = 'SINGLE', Precision = 7
704 // SQL_DOUBLE type name = 'DOUBLE', Precision = 15
705 // SQL_INTEGER type name = 'LONG', Precision = 10
707 // Query the data source for info about itself
708 if (!getDbInfo(failOnDataTypeUnsupported
))
711 // --------------- Varchar - (Variable length character string) ---------------
712 for (iIndex
= 0; iIndex
< WXSIZEOF(PossibleSqlCharTypes
) &&
713 !getDataTypeInfo(PossibleSqlCharTypes
[iIndex
], typeInfVarchar
); ++iIndex
)
716 if (iIndex
< WXSIZEOF(PossibleSqlCharTypes
))
717 typeInfVarchar
.FsqlType
= PossibleSqlCharTypes
[iIndex
];
718 else if (failOnDataTypeUnsupported
)
721 // --------------- Float ---------------
722 for (iIndex
= 0; iIndex
< WXSIZEOF(PossibleSqlFloatTypes
) &&
723 !getDataTypeInfo(PossibleSqlFloatTypes
[iIndex
], typeInfFloat
); ++iIndex
)
726 if (iIndex
< WXSIZEOF(PossibleSqlFloatTypes
))
727 typeInfFloat
.FsqlType
= PossibleSqlFloatTypes
[iIndex
];
728 else if (failOnDataTypeUnsupported
)
731 // --------------- Integer -------------
732 for (iIndex
= 0; iIndex
< WXSIZEOF(PossibleSqlIntegerTypes
) &&
733 !getDataTypeInfo(PossibleSqlIntegerTypes
[iIndex
], typeInfInteger
); ++iIndex
)
736 if (iIndex
< WXSIZEOF(PossibleSqlIntegerTypes
))
737 typeInfInteger
.FsqlType
= PossibleSqlIntegerTypes
[iIndex
];
738 else if (failOnDataTypeUnsupported
)
740 // If no non-floating point data types are supported, we'll
741 // use the type assigned for floats to store integers as well
742 if (!getDataTypeInfo(typeInfFloat
.FsqlType
, typeInfInteger
))
744 if (failOnDataTypeUnsupported
)
748 typeInfInteger
.FsqlType
= typeInfFloat
.FsqlType
;
751 // --------------- Date/Time ---------------
752 for (iIndex
= 0; iIndex
< WXSIZEOF(PossibleSqlDateTypes
) &&
753 !getDataTypeInfo(PossibleSqlDateTypes
[iIndex
], typeInfDate
); ++iIndex
)
756 if (iIndex
< WXSIZEOF(PossibleSqlDateTypes
))
757 typeInfDate
.FsqlType
= PossibleSqlDateTypes
[iIndex
];
758 else if (failOnDataTypeUnsupported
)
761 // --------------- BLOB ---------------
762 for (iIndex
= 0; iIndex
< WXSIZEOF(PossibleSqlBlobTypes
) &&
763 !getDataTypeInfo(PossibleSqlBlobTypes
[iIndex
], typeInfBlob
); ++iIndex
)
766 if (iIndex
< WXSIZEOF(PossibleSqlBlobTypes
))
767 typeInfBlob
.FsqlType
= PossibleSqlBlobTypes
[iIndex
];
768 else if (failOnDataTypeUnsupported
)
771 // --------------- MEMO ---------------
772 for (iIndex
= 0; iIndex
< WXSIZEOF(PossibleSqlMemoTypes
) &&
773 !getDataTypeInfo(PossibleSqlMemoTypes
[iIndex
], typeInfMemo
); ++iIndex
)
776 if (iIndex
< WXSIZEOF(PossibleSqlMemoTypes
))
777 typeInfMemo
.FsqlType
= PossibleSqlMemoTypes
[iIndex
];
778 else if (failOnDataTypeUnsupported
)
782 } // wxDb::determineDataTypes
785 bool wxDb::open(bool failOnDataTypeUnsupported
)
788 If using Intersolv branded ODBC drivers, this is the place where you would substitute
789 your branded driver license information
791 SQLSetConnectOption(hdbc, 1041, (UDWORD) wxEmptyString);
792 SQLSetConnectOption(hdbc, 1042, (UDWORD) wxEmptyString);
795 // Mark database as open
798 // Allocate a statement handle for the database connection
799 if (SQLAllocStmt(hdbc
, &hstmt
) != SQL_SUCCESS
)
800 return(DispAllErrors(henv
, hdbc
));
802 // Set Connection Options
803 if (!setConnectionOptions())
806 if (!determineDataTypes(failOnDataTypeUnsupported
))
809 #ifdef DBDEBUG_CONSOLE
810 cout
<< wxT("VARCHAR DATA TYPE: ") << typeInfVarchar
.TypeName
<< endl
;
811 cout
<< wxT("INTEGER DATA TYPE: ") << typeInfInteger
.TypeName
<< endl
;
812 cout
<< wxT("FLOAT DATA TYPE: ") << typeInfFloat
.TypeName
<< endl
;
813 cout
<< wxT("DATE DATA TYPE: ") << typeInfDate
.TypeName
<< endl
;
814 cout
<< wxT("BLOB DATA TYPE: ") << typeInfBlob
.TypeName
<< endl
;
815 cout
<< wxT("MEMO DATA TYPE: ") << typeInfMemo
.TypeName
<< endl
;
819 // Completed Successfully
823 bool wxDb::Open(const wxString
& inConnectStr
, bool failOnDataTypeUnsupported
)
825 wxASSERT(inConnectStr
.length());
826 return Open(inConnectStr
, NULL
, failOnDataTypeUnsupported
);
829 bool wxDb::Open(const wxString
& inConnectStr
, SQLHWND parentWnd
, bool failOnDataTypeUnsupported
)
833 authStr
= wxEmptyString
;
837 if (!FwdOnlyCursors())
839 // Specify that the ODBC cursor library be used, if needed. This must be
840 // specified before the connection is made.
841 retcode
= SQLSetConnectOption(hdbc
, SQL_ODBC_CURSORS
, SQL_CUR_USE_IF_NEEDED
);
843 #ifdef DBDEBUG_CONSOLE
844 if (retcode
== SQL_SUCCESS
)
845 cout
<< wxT("SQLSetConnectOption(CURSOR_LIB) successful") << endl
;
847 cout
<< wxT("SQLSetConnectOption(CURSOR_LIB) failed") << endl
;
849 wxUnusedVar(retcode
);
853 // Connect to the data source
854 SQLTCHAR outConnectBuffer
[SQL_MAX_CONNECTSTR_LEN
+1]; // MS recommends at least 1k buffer
855 short outConnectBufferLen
;
857 inConnectionStr
= inConnectStr
;
859 retcode
= SQLDriverConnect(hdbc
, parentWnd
, (SQLTCHAR FAR
*)inConnectionStr
.c_str(),
860 (SWORD
)inConnectionStr
.length(), (SQLTCHAR FAR
*)outConnectBuffer
,
861 WXSIZEOF(outConnectBuffer
), &outConnectBufferLen
, SQL_DRIVER_COMPLETE
);
863 if ((retcode
!= SQL_SUCCESS
) &&
864 (retcode
!= SQL_SUCCESS_WITH_INFO
))
865 return(DispAllErrors(henv
, hdbc
));
867 outConnectBuffer
[outConnectBufferLen
] = 0;
868 outConnectionStr
= outConnectBuffer
;
869 dbOpenedWithConnectionString
= true;
871 return open(failOnDataTypeUnsupported
);
874 /********** wxDb::Open() **********/
875 bool wxDb::Open(const wxString
&Dsn
, const wxString
&Uid
, const wxString
&AuthStr
, bool failOnDataTypeUnsupported
)
877 wxASSERT(!Dsn
.empty());
882 inConnectionStr
= wxEmptyString
;
883 outConnectionStr
= wxEmptyString
;
887 if (!FwdOnlyCursors())
889 // Specify that the ODBC cursor library be used, if needed. This must be
890 // specified before the connection is made.
891 retcode
= SQLSetConnectOption(hdbc
, SQL_ODBC_CURSORS
, SQL_CUR_USE_IF_NEEDED
);
893 #ifdef DBDEBUG_CONSOLE
894 if (retcode
== SQL_SUCCESS
)
895 cout
<< wxT("SQLSetConnectOption(CURSOR_LIB) successful") << endl
;
897 cout
<< wxT("SQLSetConnectOption(CURSOR_LIB) failed") << endl
;
899 wxUnusedVar( retcode
);
903 // Connect to the data source
904 retcode
= SQLConnect(hdbc
, (SQLTCHAR FAR
*) dsn
.c_str(), SQL_NTS
,
905 (SQLTCHAR FAR
*) uid
.c_str(), SQL_NTS
,
906 (SQLTCHAR FAR
*) authStr
.c_str(), SQL_NTS
);
908 if ((retcode
!= SQL_SUCCESS
) &&
909 (retcode
!= SQL_SUCCESS_WITH_INFO
))
910 return(DispAllErrors(henv
, hdbc
));
912 return open(failOnDataTypeUnsupported
);
917 bool wxDb::Open(wxDbConnectInf
*dbConnectInf
, bool failOnDataTypeUnsupported
)
919 wxASSERT(dbConnectInf
);
921 // Use the connection string if one is present
922 if (dbConnectInf
->UseConnectionStr())
923 return Open(dbConnectInf
->GetConnectionStr(), failOnDataTypeUnsupported
);
925 return Open(dbConnectInf
->GetDsn(), dbConnectInf
->GetUserID(),
926 dbConnectInf
->GetPassword(), failOnDataTypeUnsupported
);
930 bool wxDb::Open(wxDb
*copyDb
)
932 dsn
= copyDb
->GetDatasourceName();
933 uid
= copyDb
->GetUsername();
934 authStr
= copyDb
->GetPassword();
935 inConnectionStr
= copyDb
->GetConnectionInStr();
936 outConnectionStr
= copyDb
->GetConnectionOutStr();
940 if (!FwdOnlyCursors())
942 // Specify that the ODBC cursor library be used, if needed. This must be
943 // specified before the connection is made.
944 retcode
= SQLSetConnectOption(hdbc
, SQL_ODBC_CURSORS
, SQL_CUR_USE_IF_NEEDED
);
946 #ifdef DBDEBUG_CONSOLE
947 if (retcode
== SQL_SUCCESS
)
948 cout
<< wxT("SQLSetConnectOption(CURSOR_LIB) successful") << endl
;
950 cout
<< wxT("SQLSetConnectOption(CURSOR_LIB) failed") << endl
;
952 wxUnusedVar( retcode
);
956 if (copyDb
->OpenedWithConnectionString())
958 // Connect to the data source
959 SQLTCHAR outConnectBuffer
[SQL_MAX_CONNECTSTR_LEN
+1];
960 short outConnectBufferLen
;
962 inConnectionStr
= copyDb
->GetConnectionInStr();
964 retcode
= SQLDriverConnect(hdbc
, NULL
, (SQLTCHAR FAR
*)inConnectionStr
.c_str(),
965 (SWORD
)inConnectionStr
.length(), (SQLTCHAR FAR
*)outConnectBuffer
,
966 WXSIZEOF(outConnectBuffer
), &outConnectBufferLen
, SQL_DRIVER_COMPLETE
);
968 if ((retcode
!= SQL_SUCCESS
) &&
969 (retcode
!= SQL_SUCCESS_WITH_INFO
))
970 return(DispAllErrors(henv
, hdbc
));
972 outConnectBuffer
[outConnectBufferLen
] = 0;
973 outConnectionStr
= outConnectBuffer
;
974 dbOpenedWithConnectionString
= true;
978 // Connect to the data source
979 retcode
= SQLConnect(hdbc
, (SQLTCHAR FAR
*) dsn
.c_str(), SQL_NTS
,
980 (SQLTCHAR FAR
*) uid
.c_str(), SQL_NTS
,
981 (SQLTCHAR FAR
*) authStr
.c_str(), SQL_NTS
);
984 if ((retcode
!= SQL_SUCCESS
) &&
985 (retcode
!= SQL_SUCCESS_WITH_INFO
))
986 return(DispAllErrors(henv
, hdbc
));
989 If using Intersolv branded ODBC drivers, this is the place where you would substitute
990 your branded driver license information
992 SQLSetConnectOption(hdbc, 1041, (UDWORD) wxEmptyString);
993 SQLSetConnectOption(hdbc, 1042, (UDWORD) wxEmptyString);
996 // Mark database as open
999 // Allocate a statement handle for the database connection
1000 if (SQLAllocStmt(hdbc
, &hstmt
) != SQL_SUCCESS
)
1001 return(DispAllErrors(henv
, hdbc
));
1003 // Set Connection Options
1004 if (!setConnectionOptions())
1007 // Instead of Querying the data source for info about itself, it can just be copied
1008 // from the wxDb instance that was passed in (copyDb).
1009 wxStrcpy(dbInf
.serverName
,copyDb
->dbInf
.serverName
);
1010 wxStrcpy(dbInf
.databaseName
,copyDb
->dbInf
.databaseName
);
1011 wxStrcpy(dbInf
.dbmsName
,copyDb
->dbInf
.dbmsName
);
1012 wxStrcpy(dbInf
.dbmsVer
,copyDb
->dbInf
.dbmsVer
);
1013 dbInf
.maxConnections
= copyDb
->dbInf
.maxConnections
;
1014 dbInf
.maxStmts
= copyDb
->dbInf
.maxStmts
;
1015 wxStrcpy(dbInf
.driverName
,copyDb
->dbInf
.driverName
);
1016 wxStrcpy(dbInf
.odbcVer
,copyDb
->dbInf
.odbcVer
);
1017 wxStrcpy(dbInf
.drvMgrOdbcVer
,copyDb
->dbInf
.drvMgrOdbcVer
);
1018 wxStrcpy(dbInf
.driverVer
,copyDb
->dbInf
.driverVer
);
1019 dbInf
.apiConfLvl
= copyDb
->dbInf
.apiConfLvl
;
1020 dbInf
.cliConfLvl
= copyDb
->dbInf
.cliConfLvl
;
1021 dbInf
.sqlConfLvl
= copyDb
->dbInf
.sqlConfLvl
;
1022 wxStrcpy(dbInf
.outerJoins
,copyDb
->dbInf
.outerJoins
);
1023 wxStrcpy(dbInf
.procedureSupport
,copyDb
->dbInf
.procedureSupport
);
1024 wxStrcpy(dbInf
.accessibleTables
,copyDb
->dbInf
.accessibleTables
);
1025 dbInf
.cursorCommitBehavior
= copyDb
->dbInf
.cursorCommitBehavior
;
1026 dbInf
.cursorRollbackBehavior
= copyDb
->dbInf
.cursorRollbackBehavior
;
1027 dbInf
.supportNotNullClause
= copyDb
->dbInf
.supportNotNullClause
;
1028 wxStrcpy(dbInf
.supportIEF
,copyDb
->dbInf
.supportIEF
);
1029 dbInf
.txnIsolation
= copyDb
->dbInf
.txnIsolation
;
1030 dbInf
.txnIsolationOptions
= copyDb
->dbInf
.txnIsolationOptions
;
1031 dbInf
.fetchDirections
= copyDb
->dbInf
.fetchDirections
;
1032 dbInf
.lockTypes
= copyDb
->dbInf
.lockTypes
;
1033 dbInf
.posOperations
= copyDb
->dbInf
.posOperations
;
1034 dbInf
.posStmts
= copyDb
->dbInf
.posStmts
;
1035 dbInf
.scrollConcurrency
= copyDb
->dbInf
.scrollConcurrency
;
1036 dbInf
.scrollOptions
= copyDb
->dbInf
.scrollOptions
;
1037 dbInf
.staticSensitivity
= copyDb
->dbInf
.staticSensitivity
;
1038 dbInf
.txnCapable
= copyDb
->dbInf
.txnCapable
;
1039 dbInf
.loginTimeout
= copyDb
->dbInf
.loginTimeout
;
1041 // VARCHAR = Variable length character string
1042 typeInfVarchar
.FsqlType
= copyDb
->typeInfVarchar
.FsqlType
;
1043 typeInfVarchar
.TypeName
= copyDb
->typeInfVarchar
.TypeName
;
1044 typeInfVarchar
.Precision
= copyDb
->typeInfVarchar
.Precision
;
1045 typeInfVarchar
.CaseSensitive
= copyDb
->typeInfVarchar
.CaseSensitive
;
1046 typeInfVarchar
.MaximumScale
= copyDb
->typeInfVarchar
.MaximumScale
;
1049 typeInfFloat
.FsqlType
= copyDb
->typeInfFloat
.FsqlType
;
1050 typeInfFloat
.TypeName
= copyDb
->typeInfFloat
.TypeName
;
1051 typeInfFloat
.Precision
= copyDb
->typeInfFloat
.Precision
;
1052 typeInfFloat
.CaseSensitive
= copyDb
->typeInfFloat
.CaseSensitive
;
1053 typeInfFloat
.MaximumScale
= copyDb
->typeInfFloat
.MaximumScale
;
1056 typeInfInteger
.FsqlType
= copyDb
->typeInfInteger
.FsqlType
;
1057 typeInfInteger
.TypeName
= copyDb
->typeInfInteger
.TypeName
;
1058 typeInfInteger
.Precision
= copyDb
->typeInfInteger
.Precision
;
1059 typeInfInteger
.CaseSensitive
= copyDb
->typeInfInteger
.CaseSensitive
;
1060 typeInfInteger
.MaximumScale
= copyDb
->typeInfInteger
.MaximumScale
;
1063 typeInfDate
.FsqlType
= copyDb
->typeInfDate
.FsqlType
;
1064 typeInfDate
.TypeName
= copyDb
->typeInfDate
.TypeName
;
1065 typeInfDate
.Precision
= copyDb
->typeInfDate
.Precision
;
1066 typeInfDate
.CaseSensitive
= copyDb
->typeInfDate
.CaseSensitive
;
1067 typeInfDate
.MaximumScale
= copyDb
->typeInfDate
.MaximumScale
;
1070 typeInfBlob
.FsqlType
= copyDb
->typeInfBlob
.FsqlType
;
1071 typeInfBlob
.TypeName
= copyDb
->typeInfBlob
.TypeName
;
1072 typeInfBlob
.Precision
= copyDb
->typeInfBlob
.Precision
;
1073 typeInfBlob
.CaseSensitive
= copyDb
->typeInfBlob
.CaseSensitive
;
1074 typeInfBlob
.MaximumScale
= copyDb
->typeInfBlob
.MaximumScale
;
1077 typeInfMemo
.FsqlType
= copyDb
->typeInfMemo
.FsqlType
;
1078 typeInfMemo
.TypeName
= copyDb
->typeInfMemo
.TypeName
;
1079 typeInfMemo
.Precision
= copyDb
->typeInfMemo
.Precision
;
1080 typeInfMemo
.CaseSensitive
= copyDb
->typeInfMemo
.CaseSensitive
;
1081 typeInfMemo
.MaximumScale
= copyDb
->typeInfMemo
.MaximumScale
;
1083 #ifdef DBDEBUG_CONSOLE
1084 cout
<< wxT("VARCHAR DATA TYPE: ") << typeInfVarchar
.TypeName
<< endl
;
1085 cout
<< wxT("INTEGER DATA TYPE: ") << typeInfInteger
.TypeName
<< endl
;
1086 cout
<< wxT("FLOAT DATA TYPE: ") << typeInfFloat
.TypeName
<< endl
;
1087 cout
<< wxT("DATE DATA TYPE: ") << typeInfDate
.TypeName
<< endl
;
1088 cout
<< wxT("BLOB DATA TYPE: ") << typeInfBlob
.TypeName
<< endl
;
1089 cout
<< wxT("MEMO DATA TYPE: ") << typeInfMemo
.TypeName
<< endl
;
1093 // Completed Successfully
1098 /********** wxDb::setConnectionOptions() **********/
1099 bool wxDb::setConnectionOptions(void)
1101 * NOTE: The Intersolv/Oracle 7 driver was "Not Capable" of setting the login timeout.
1106 // I need to get the DBMS name here, because some of the connection options
1107 // are database specific and need to call the Dbms() function.
1110 retcode
= SQLGetInfo(hdbc
, SQL_DBMS_NAME
, (UCHAR
*) dbInf
.dbmsName
, sizeof(dbInf
.dbmsName
), &cb
);
1111 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1112 return(DispAllErrors(henv
, hdbc
));
1114 /* retcode = */ SQLSetConnectOption(hdbc
, SQL_AUTOCOMMIT
, SQL_AUTOCOMMIT_OFF
);
1115 /* retcode = */ SQLSetConnectOption(hdbc
, SQL_OPT_TRACE
, SQL_OPT_TRACE_OFF
);
1116 // SQLSetConnectOption(hdbc, SQL_TXN_ISOLATION, SQL_TXN_READ_COMMITTED); // No dirty reads
1118 // By default, MS Sql Server closes cursors on commit and rollback. The following
1119 // call to SQLSetConnectOption() is needed to force SQL Server to preserve cursors
1120 // after a transaction. This is a driver specific option and is not part of the
1121 // ODBC standard. Note: this behavior is specific to the ODBC interface to SQL Server.
1122 // The database settings don't have any effect one way or the other.
1123 if (Dbms() == dbmsMS_SQL_SERVER
)
1125 const long SQL_PRESERVE_CURSORS
= 1204L;
1126 const long SQL_PC_ON
= 1L;
1127 /* retcode = */ SQLSetConnectOption(hdbc
, SQL_PRESERVE_CURSORS
, SQL_PC_ON
);
1130 // Display the connection options to verify them
1131 #ifdef DBDEBUG_CONSOLE
1133 cout
<< wxT("****** CONNECTION OPTIONS ******") << endl
;
1135 retcode
= SQLGetConnectOption(hdbc
, SQL_AUTOCOMMIT
, &l
);
1136 if (retcode
!= SQL_SUCCESS
)
1137 return(DispAllErrors(henv
, hdbc
));
1138 cout
<< wxT("AUTOCOMMIT: ") << (l
== SQL_AUTOCOMMIT_OFF
? "OFF" : "ON") << endl
;
1140 retcode
= SQLGetConnectOption(hdbc
, SQL_ODBC_CURSORS
, &l
);
1141 if (retcode
!= SQL_SUCCESS
)
1142 return(DispAllErrors(henv
, hdbc
));
1143 cout
<< wxT("ODBC CURSORS: ");
1146 case(SQL_CUR_USE_IF_NEEDED
):
1147 cout
<< wxT("SQL_CUR_USE_IF_NEEDED");
1149 case(SQL_CUR_USE_ODBC
):
1150 cout
<< wxT("SQL_CUR_USE_ODBC");
1152 case(SQL_CUR_USE_DRIVER
):
1153 cout
<< wxT("SQL_CUR_USE_DRIVER");
1158 retcode
= SQLGetConnectOption(hdbc
, SQL_OPT_TRACE
, &l
)
1159 if (retcode
!= SQL_SUCCESS
)
1160 return(DispAllErrors(henv
, hdbc
));
1161 cout
<< wxT("TRACING: ") << (l
== SQL_OPT_TRACE_OFF
? wxT("OFF") : wxT("ON")) << endl
;
1166 // Completed Successfully
1169 } // wxDb::setConnectionOptions()
1172 /********** wxDb::getDbInfo() **********/
1173 bool wxDb::getDbInfo(bool failOnDataTypeUnsupported
)
1178 retcode
= SQLGetInfo(hdbc
, SQL_SERVER_NAME
, (UCHAR
*) dbInf
.serverName
, sizeof(dbInf
.serverName
), &cb
);
1179 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1181 DispAllErrors(henv
, hdbc
);
1182 if (failOnDataTypeUnsupported
)
1186 retcode
= SQLGetInfo(hdbc
, SQL_DATABASE_NAME
, (UCHAR
*) dbInf
.databaseName
, sizeof(dbInf
.databaseName
), &cb
);
1187 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1189 DispAllErrors(henv
, hdbc
);
1190 if (failOnDataTypeUnsupported
)
1194 retcode
= SQLGetInfo(hdbc
, SQL_DBMS_NAME
, (UCHAR
*) dbInf
.dbmsName
, sizeof(dbInf
.dbmsName
), &cb
);
1195 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1197 DispAllErrors(henv
, hdbc
);
1198 if (failOnDataTypeUnsupported
)
1203 // After upgrading to MSVC6, the original 20 char buffer below was insufficient,
1204 // causing database connectivity to fail in some cases.
1205 retcode
= SQLGetInfo(hdbc
, SQL_DBMS_VER
, (UCHAR
*) dbInf
.dbmsVer
, sizeof(dbInf
.dbmsVer
), &cb
);
1206 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1208 DispAllErrors(henv
, hdbc
);
1209 if (failOnDataTypeUnsupported
)
1213 retcode
= SQLGetInfo(hdbc
, SQL_ACTIVE_CONNECTIONS
, (UCHAR
*) &dbInf
.maxConnections
, sizeof(dbInf
.maxConnections
), &cb
);
1214 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1216 DispAllErrors(henv
, hdbc
);
1217 if (failOnDataTypeUnsupported
)
1221 retcode
= SQLGetInfo(hdbc
, SQL_ACTIVE_STATEMENTS
, (UCHAR
*) &dbInf
.maxStmts
, sizeof(dbInf
.maxStmts
), &cb
);
1222 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1224 DispAllErrors(henv
, hdbc
);
1225 if (failOnDataTypeUnsupported
)
1229 retcode
= SQLGetInfo(hdbc
, SQL_DRIVER_NAME
, (UCHAR
*) dbInf
.driverName
, sizeof(dbInf
.driverName
), &cb
);
1230 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1232 DispAllErrors(henv
, hdbc
);
1233 if (failOnDataTypeUnsupported
)
1237 retcode
= SQLGetInfo(hdbc
, SQL_DRIVER_ODBC_VER
, (UCHAR
*) dbInf
.odbcVer
, sizeof(dbInf
.odbcVer
), &cb
);
1238 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1240 DispAllErrors(henv
, hdbc
);
1241 if (failOnDataTypeUnsupported
)
1245 retcode
= SQLGetInfo(hdbc
, SQL_ODBC_VER
, (UCHAR
*) dbInf
.drvMgrOdbcVer
, sizeof(dbInf
.drvMgrOdbcVer
), &cb
);
1246 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1248 DispAllErrors(henv
, hdbc
);
1249 if (failOnDataTypeUnsupported
)
1253 retcode
= SQLGetInfo(hdbc
, SQL_DRIVER_VER
, (UCHAR
*) dbInf
.driverVer
, sizeof(dbInf
.driverVer
), &cb
);
1254 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1256 DispAllErrors(henv
, hdbc
);
1257 if (failOnDataTypeUnsupported
)
1261 retcode
= SQLGetInfo(hdbc
, SQL_ODBC_API_CONFORMANCE
, (UCHAR
*) &dbInf
.apiConfLvl
, sizeof(dbInf
.apiConfLvl
), &cb
);
1262 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1264 DispAllErrors(henv
, hdbc
);
1265 if (failOnDataTypeUnsupported
)
1269 retcode
= SQLGetInfo(hdbc
, SQL_ODBC_SAG_CLI_CONFORMANCE
, (UCHAR
*) &dbInf
.cliConfLvl
, sizeof(dbInf
.cliConfLvl
), &cb
);
1270 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1272 // Not all drivers support this call - Nick Gorham(unixODBC)
1273 dbInf
.cliConfLvl
= 0;
1274 DispAllErrors(henv
, hdbc
);
1275 if (failOnDataTypeUnsupported
)
1279 retcode
= SQLGetInfo(hdbc
, SQL_ODBC_SQL_CONFORMANCE
, (UCHAR
*) &dbInf
.sqlConfLvl
, sizeof(dbInf
.sqlConfLvl
), &cb
);
1280 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1282 DispAllErrors(henv
, hdbc
);
1283 if (failOnDataTypeUnsupported
)
1287 retcode
= SQLGetInfo(hdbc
, SQL_OUTER_JOINS
, (UCHAR
*) dbInf
.outerJoins
, sizeof(dbInf
.outerJoins
), &cb
);
1288 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1290 DispAllErrors(henv
, hdbc
);
1291 if (failOnDataTypeUnsupported
)
1295 retcode
= SQLGetInfo(hdbc
, SQL_PROCEDURES
, (UCHAR
*) dbInf
.procedureSupport
, sizeof(dbInf
.procedureSupport
), &cb
);
1296 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1298 DispAllErrors(henv
, hdbc
);
1299 if (failOnDataTypeUnsupported
)
1303 retcode
= SQLGetInfo(hdbc
, SQL_ACCESSIBLE_TABLES
, (UCHAR
*) dbInf
.accessibleTables
, sizeof(dbInf
.accessibleTables
), &cb
);
1304 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1306 DispAllErrors(henv
, hdbc
);
1307 if (failOnDataTypeUnsupported
)
1311 retcode
= SQLGetInfo(hdbc
, SQL_CURSOR_COMMIT_BEHAVIOR
, (UCHAR
*) &dbInf
.cursorCommitBehavior
, sizeof(dbInf
.cursorCommitBehavior
), &cb
);
1312 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1314 DispAllErrors(henv
, hdbc
);
1315 if (failOnDataTypeUnsupported
)
1319 retcode
= SQLGetInfo(hdbc
, SQL_CURSOR_ROLLBACK_BEHAVIOR
, (UCHAR
*) &dbInf
.cursorRollbackBehavior
, sizeof(dbInf
.cursorRollbackBehavior
), &cb
);
1320 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1322 DispAllErrors(henv
, hdbc
);
1323 if (failOnDataTypeUnsupported
)
1327 retcode
= SQLGetInfo(hdbc
, SQL_NON_NULLABLE_COLUMNS
, (UCHAR
*) &dbInf
.supportNotNullClause
, sizeof(dbInf
.supportNotNullClause
), &cb
);
1328 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1330 DispAllErrors(henv
, hdbc
);
1331 if (failOnDataTypeUnsupported
)
1335 retcode
= SQLGetInfo(hdbc
, SQL_ODBC_SQL_OPT_IEF
, (UCHAR
*) dbInf
.supportIEF
, sizeof(dbInf
.supportIEF
), &cb
);
1336 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1338 DispAllErrors(henv
, hdbc
);
1339 if (failOnDataTypeUnsupported
)
1343 retcode
= SQLGetInfo(hdbc
, SQL_DEFAULT_TXN_ISOLATION
, (UCHAR
*) &dbInf
.txnIsolation
, sizeof(dbInf
.txnIsolation
), &cb
);
1344 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1346 DispAllErrors(henv
, hdbc
);
1347 if (failOnDataTypeUnsupported
)
1351 retcode
= SQLGetInfo(hdbc
, SQL_TXN_ISOLATION_OPTION
, (UCHAR
*) &dbInf
.txnIsolationOptions
, sizeof(dbInf
.txnIsolationOptions
), &cb
);
1352 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1354 DispAllErrors(henv
, hdbc
);
1355 if (failOnDataTypeUnsupported
)
1359 retcode
= SQLGetInfo(hdbc
, SQL_FETCH_DIRECTION
, (UCHAR
*) &dbInf
.fetchDirections
, sizeof(dbInf
.fetchDirections
), &cb
);
1360 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1362 DispAllErrors(henv
, hdbc
);
1363 if (failOnDataTypeUnsupported
)
1367 retcode
= SQLGetInfo(hdbc
, SQL_LOCK_TYPES
, (UCHAR
*) &dbInf
.lockTypes
, sizeof(dbInf
.lockTypes
), &cb
);
1368 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1370 DispAllErrors(henv
, hdbc
);
1371 if (failOnDataTypeUnsupported
)
1375 retcode
= SQLGetInfo(hdbc
, SQL_POS_OPERATIONS
, (UCHAR
*) &dbInf
.posOperations
, sizeof(dbInf
.posOperations
), &cb
);
1376 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1378 DispAllErrors(henv
, hdbc
);
1379 if (failOnDataTypeUnsupported
)
1383 retcode
= SQLGetInfo(hdbc
, SQL_POSITIONED_STATEMENTS
, (UCHAR
*) &dbInf
.posStmts
, sizeof(dbInf
.posStmts
), &cb
);
1384 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1386 DispAllErrors(henv
, hdbc
);
1387 if (failOnDataTypeUnsupported
)
1391 retcode
= SQLGetInfo(hdbc
, SQL_SCROLL_CONCURRENCY
, (UCHAR
*) &dbInf
.scrollConcurrency
, sizeof(dbInf
.scrollConcurrency
), &cb
);
1392 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1394 DispAllErrors(henv
, hdbc
);
1395 if (failOnDataTypeUnsupported
)
1399 retcode
= SQLGetInfo(hdbc
, SQL_SCROLL_OPTIONS
, (UCHAR
*) &dbInf
.scrollOptions
, sizeof(dbInf
.scrollOptions
), &cb
);
1400 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1402 DispAllErrors(henv
, hdbc
);
1403 if (failOnDataTypeUnsupported
)
1407 retcode
= SQLGetInfo(hdbc
, SQL_STATIC_SENSITIVITY
, (UCHAR
*) &dbInf
.staticSensitivity
, sizeof(dbInf
.staticSensitivity
), &cb
);
1408 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1410 DispAllErrors(henv
, hdbc
);
1411 if (failOnDataTypeUnsupported
)
1415 retcode
= SQLGetInfo(hdbc
, SQL_TXN_CAPABLE
, (UCHAR
*) &dbInf
.txnCapable
, sizeof(dbInf
.txnCapable
), &cb
);
1416 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1418 DispAllErrors(henv
, hdbc
);
1419 if (failOnDataTypeUnsupported
)
1423 retcode
= SQLGetInfo(hdbc
, SQL_LOGIN_TIMEOUT
, (UCHAR
*) &dbInf
.loginTimeout
, sizeof(dbInf
.loginTimeout
), &cb
);
1424 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
1426 DispAllErrors(henv
, hdbc
);
1427 if (failOnDataTypeUnsupported
)
1431 #ifdef DBDEBUG_CONSOLE
1432 cout
<< wxT("***** DATA SOURCE INFORMATION *****") << endl
;
1433 cout
<< wxT(wxT("SERVER Name: ") << dbInf
.serverName
<< endl
;
1434 cout
<< wxT("DBMS Name: ") << dbInf
.dbmsName
<< wxT("; DBMS Version: ") << dbInf
.dbmsVer
<< endl
;
1435 cout
<< wxT("ODBC Version: ") << dbInf
.odbcVer
<< wxT("; Driver Version: ") << dbInf
.driverVer
<< endl
;
1437 cout
<< wxT("API Conf. Level: ");
1438 switch(dbInf
.apiConfLvl
)
1440 case SQL_OAC_NONE
: cout
<< wxT("None"); break;
1441 case SQL_OAC_LEVEL1
: cout
<< wxT("Level 1"); break;
1442 case SQL_OAC_LEVEL2
: cout
<< wxT("Level 2"); break;
1446 cout
<< wxT("SAG CLI Conf. Level: ");
1447 switch(dbInf
.cliConfLvl
)
1449 case SQL_OSCC_NOT_COMPLIANT
: cout
<< wxT("Not Compliant"); break;
1450 case SQL_OSCC_COMPLIANT
: cout
<< wxT("Compliant"); break;
1454 cout
<< wxT("SQL Conf. Level: ");
1455 switch(dbInf
.sqlConfLvl
)
1457 case SQL_OSC_MINIMUM
: cout
<< wxT("Minimum Grammar"); break;
1458 case SQL_OSC_CORE
: cout
<< wxT("Core Grammar"); break;
1459 case SQL_OSC_EXTENDED
: cout
<< wxT("Extended Grammar"); break;
1463 cout
<< wxT("Max. Connections: ") << dbInf
.maxConnections
<< endl
;
1464 cout
<< wxT("Outer Joins: ") << dbInf
.outerJoins
<< endl
;
1465 cout
<< wxT("Support for Procedures: ") << dbInf
.procedureSupport
<< endl
;
1466 cout
<< wxT("All tables accessible : ") << dbInf
.accessibleTables
<< endl
;
1467 cout
<< wxT("Cursor COMMIT Behavior: ");
1468 switch(dbInf
.cursorCommitBehavior
)
1470 case SQL_CB_DELETE
: cout
<< wxT("Delete cursors"); break;
1471 case SQL_CB_CLOSE
: cout
<< wxT("Close cursors"); break;
1472 case SQL_CB_PRESERVE
: cout
<< wxT("Preserve cursors"); break;
1476 cout
<< wxT("Cursor ROLLBACK Behavior: ");
1477 switch(dbInf
.cursorRollbackBehavior
)
1479 case SQL_CB_DELETE
: cout
<< wxT("Delete cursors"); break;
1480 case SQL_CB_CLOSE
: cout
<< wxT("Close cursors"); break;
1481 case SQL_CB_PRESERVE
: cout
<< wxT("Preserve cursors"); break;
1485 cout
<< wxT("Support NOT NULL clause: ");
1486 switch(dbInf
.supportNotNullClause
)
1488 case SQL_NNC_NULL
: cout
<< wxT("No"); break;
1489 case SQL_NNC_NON_NULL
: cout
<< wxT("Yes"); break;
1493 cout
<< wxT("Support IEF (Ref. Integrity): ") << dbInf
.supportIEF
<< endl
;
1494 cout
<< wxT("Login Timeout: ") << dbInf
.loginTimeout
<< endl
;
1496 cout
<< endl
<< endl
<< wxT("more ...") << endl
;
1499 cout
<< wxT("Default Transaction Isolation: ";
1500 switch(dbInf
.txnIsolation
)
1502 case SQL_TXN_READ_UNCOMMITTED
: cout
<< wxT("Read Uncommitted"); break;
1503 case SQL_TXN_READ_COMMITTED
: cout
<< wxT("Read Committed"); break;
1504 case SQL_TXN_REPEATABLE_READ
: cout
<< wxT("Repeatable Read"); break;
1505 case SQL_TXN_SERIALIZABLE
: cout
<< wxT("Serializable"); break;
1507 case SQL_TXN_VERSIONING
: cout
<< wxT("Versioning"); break;
1512 cout
<< wxT("Transaction Isolation Options: ");
1513 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_UNCOMMITTED
)
1514 cout
<< wxT("Read Uncommitted, ");
1515 if (dbInf
.txnIsolationOptions
& SQL_TXN_READ_COMMITTED
)
1516 cout
<< wxT("Read Committed, ");
1517 if (dbInf
.txnIsolationOptions
& SQL_TXN_REPEATABLE_READ
)
1518 cout
<< wxT("Repeatable Read, ");
1519 if (dbInf
.txnIsolationOptions
& SQL_TXN_SERIALIZABLE
)
1520 cout
<< wxT("Serializable, ");
1522 if (dbInf
.txnIsolationOptions
& SQL_TXN_VERSIONING
)
1523 cout
<< wxT("Versioning");
1527 cout
<< wxT("Fetch Directions Supported:") << endl
<< wxT(" ");
1528 if (dbInf
.fetchDirections
& SQL_FD_FETCH_NEXT
)
1529 cout
<< wxT("Next, ");
1530 if (dbInf
.fetchDirections
& SQL_FD_FETCH_PRIOR
)
1531 cout
<< wxT("Prev, ");
1532 if (dbInf
.fetchDirections
& SQL_FD_FETCH_FIRST
)
1533 cout
<< wxT("First, ");
1534 if (dbInf
.fetchDirections
& SQL_FD_FETCH_LAST
)
1535 cout
<< wxT("Last, ");
1536 if (dbInf
.fetchDirections
& SQL_FD_FETCH_ABSOLUTE
)
1537 cout
<< wxT("Absolute, ");
1538 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RELATIVE
)
1539 cout
<< wxT("Relative, ");
1541 if (dbInf
.fetchDirections
& SQL_FD_FETCH_RESUME
)
1542 cout
<< wxT("Resume, ");
1544 if (dbInf
.fetchDirections
& SQL_FD_FETCH_BOOKMARK
)
1545 cout
<< wxT("Bookmark");
1548 cout
<< wxT("Lock Types Supported (SQLSetPos): ");
1549 if (dbInf
.lockTypes
& SQL_LCK_NO_CHANGE
)
1550 cout
<< wxT("No Change, ");
1551 if (dbInf
.lockTypes
& SQL_LCK_EXCLUSIVE
)
1552 cout
<< wxT("Exclusive, ");
1553 if (dbInf
.lockTypes
& SQL_LCK_UNLOCK
)
1554 cout
<< wxT("UnLock");
1557 cout
<< wxT("Position Operations Supported (SQLSetPos): ");
1558 if (dbInf
.posOperations
& SQL_POS_POSITION
)
1559 cout
<< wxT("Position, ");
1560 if (dbInf
.posOperations
& SQL_POS_REFRESH
)
1561 cout
<< wxT("Refresh, ");
1562 if (dbInf
.posOperations
& SQL_POS_UPDATE
)
1563 cout
<< wxT("Upd, "));
1564 if (dbInf
.posOperations
& SQL_POS_DELETE
)
1565 cout
<< wxT("Del, ");
1566 if (dbInf
.posOperations
& SQL_POS_ADD
)
1570 cout
<< wxT("Positioned Statements Supported: ");
1571 if (dbInf
.posStmts
& SQL_PS_POSITIONED_DELETE
)
1572 cout
<< wxT("Pos delete, ");
1573 if (dbInf
.posStmts
& SQL_PS_POSITIONED_UPDATE
)
1574 cout
<< wxT("Pos update, ");
1575 if (dbInf
.posStmts
& SQL_PS_SELECT_FOR_UPDATE
)
1576 cout
<< wxT("Select for update");
1579 cout
<< wxT("Scroll Concurrency: ");
1580 if (dbInf
.scrollConcurrency
& SQL_SCCO_READ_ONLY
)
1581 cout
<< wxT("Read Only, ");
1582 if (dbInf
.scrollConcurrency
& SQL_SCCO_LOCK
)
1583 cout
<< wxT("Lock, ");
1584 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_ROWVER
)
1585 cout
<< wxT("Opt. Rowver, ");
1586 if (dbInf
.scrollConcurrency
& SQL_SCCO_OPT_VALUES
)
1587 cout
<< wxT("Opt. Values");
1590 cout
<< wxT("Scroll Options: ");
1591 if (dbInf
.scrollOptions
& SQL_SO_FORWARD_ONLY
)
1592 cout
<< wxT("Fwd Only, ");
1593 if (dbInf
.scrollOptions
& SQL_SO_STATIC
)
1594 cout
<< wxT("Static, ");
1595 if (dbInf
.scrollOptions
& SQL_SO_KEYSET_DRIVEN
)
1596 cout
<< wxT("Keyset Driven, ");
1597 if (dbInf
.scrollOptions
& SQL_SO_DYNAMIC
)
1598 cout
<< wxT("Dynamic, ");
1599 if (dbInf
.scrollOptions
& SQL_SO_MIXED
)
1600 cout
<< wxT("Mixed");
1603 cout
<< wxT("Static Sensitivity: ");
1604 if (dbInf
.staticSensitivity
& SQL_SS_ADDITIONS
)
1605 cout
<< wxT("Additions, ");
1606 if (dbInf
.staticSensitivity
& SQL_SS_DELETIONS
)
1607 cout
<< wxT("Deletions, ");
1608 if (dbInf
.staticSensitivity
& SQL_SS_UPDATES
)
1609 cout
<< wxT("Updates");
1612 cout
<< wxT("Transaction Capable?: ");
1613 switch(dbInf
.txnCapable
)
1615 case SQL_TC_NONE
: cout
<< wxT("No"); break;
1616 case SQL_TC_DML
: cout
<< wxT("DML Only"); break;
1617 case SQL_TC_DDL_COMMIT
: cout
<< wxT("DDL Commit"); break;
1618 case SQL_TC_DDL_IGNORE
: cout
<< wxT("DDL Ignore"); break;
1619 case SQL_TC_ALL
: cout
<< wxT("DDL & DML"); break;
1626 // Completed Successfully
1629 } // wxDb::getDbInfo()
1632 /********** wxDb::getDataTypeInfo() **********/
1633 bool wxDb::getDataTypeInfo(SWORD fSqlType
, wxDbSqlTypeInfo
&structSQLTypeInfo
)
1636 * fSqlType will be something like SQL_VARCHAR. This parameter determines
1637 * the data type inf. is gathered for.
1639 * wxDbSqlTypeInfo is a structure that is filled in with data type information,
1644 // Get information about the data type specified
1645 if (SQLGetTypeInfo(hstmt
, fSqlType
) != SQL_SUCCESS
)
1646 return(DispAllErrors(henv
, hdbc
, hstmt
));
1649 retcode
= SQLFetch(hstmt
);
1650 if (retcode
!= SQL_SUCCESS
)
1652 #ifdef DBDEBUG_CONSOLE
1653 if (retcode
== SQL_NO_DATA_FOUND
)
1654 cout
<< wxT("SQL_NO_DATA_FOUND fetching information about data type.") << endl
;
1656 DispAllErrors(henv
, hdbc
, hstmt
);
1657 SQLFreeStmt(hstmt
, SQL_CLOSE
);
1661 wxChar typeName
[DB_TYPE_NAME_LEN
+1];
1663 // Obtain columns from the record
1664 if (SQLGetData(hstmt
, 1, SQL_C_WXCHAR
, typeName
, sizeof(typeName
), &cbRet
) != SQL_SUCCESS
)
1665 return(DispAllErrors(henv
, hdbc
, hstmt
));
1667 structSQLTypeInfo
.TypeName
= typeName
;
1669 // BJO 20000503: no more needed with new GetColumns...
1672 if (Dbms() == dbmsMY_SQL
)
1674 if (structSQLTypeInfo
.TypeName
== wxT("middleint"))
1675 structSQLTypeInfo
.TypeName
= wxT("mediumint");
1676 else if (structSQLTypeInfo
.TypeName
== wxT("middleint unsigned"))
1677 structSQLTypeInfo
.TypeName
= wxT("mediumint unsigned");
1678 else if (structSQLTypeInfo
.TypeName
== wxT("integer"))
1679 structSQLTypeInfo
.TypeName
= wxT("int");
1680 else if (structSQLTypeInfo
.TypeName
== wxT("integer unsigned"))
1681 structSQLTypeInfo
.TypeName
= wxT("int unsigned");
1682 else if (structSQLTypeInfo
.TypeName
== wxT("middleint"))
1683 structSQLTypeInfo
.TypeName
= wxT("mediumint");
1684 else if (structSQLTypeInfo
.TypeName
== wxT("varchar"))
1685 structSQLTypeInfo
.TypeName
= wxT("char");
1688 // BJO 20000427 : OpenLink driver
1689 if (!wxStrncmp(dbInf
.driverName
, wxT("oplodbc"), 7) ||
1690 !wxStrncmp(dbInf
.driverName
, wxT("OLOD"), 4))
1692 if (structSQLTypeInfo
.TypeName
== wxT("double precision"))
1693 structSQLTypeInfo
.TypeName
= wxT("real");
1697 if (SQLGetData(hstmt
, 3, SQL_C_LONG
, (UCHAR
*) &structSQLTypeInfo
.Precision
, 0, &cbRet
) != SQL_SUCCESS
)
1698 return(DispAllErrors(henv
, hdbc
, hstmt
));
1699 if (SQLGetData(hstmt
, 8, SQL_C_SHORT
, (UCHAR
*) &structSQLTypeInfo
.CaseSensitive
, 0, &cbRet
) != SQL_SUCCESS
)
1700 return(DispAllErrors(henv
, hdbc
, hstmt
));
1701 // if (SQLGetData(hstmt, 14, SQL_C_SHORT, (UCHAR*) &structSQLTypeInfo.MinimumScale, 0, &cbRet) != SQL_SUCCESS)
1702 // return(DispAllErrors(henv, hdbc, hstmt));
1704 if (SQLGetData(hstmt
, 15, SQL_C_SHORT
,(UCHAR
*) &structSQLTypeInfo
.MaximumScale
, 0, &cbRet
) != SQL_SUCCESS
)
1705 return(DispAllErrors(henv
, hdbc
, hstmt
));
1707 if (structSQLTypeInfo
.MaximumScale
< 0)
1708 structSQLTypeInfo
.MaximumScale
= 0;
1710 // Close the statement handle which closes open cursors
1711 if (SQLFreeStmt(hstmt
, SQL_CLOSE
) != SQL_SUCCESS
)
1712 return(DispAllErrors(henv
, hdbc
, hstmt
));
1714 // Completed Successfully
1717 } // wxDb::getDataTypeInfo()
1720 /********** wxDb::Close() **********/
1721 void wxDb::Close(void)
1723 // Close the Sql Log file
1730 // Free statement handle
1733 if (SQLFreeStmt(hstmt
, SQL_DROP
) != SQL_SUCCESS
)
1734 DispAllErrors(henv
, hdbc
);
1737 // Disconnect from the datasource
1738 if (SQLDisconnect(hdbc
) != SQL_SUCCESS
)
1739 DispAllErrors(henv
, hdbc
);
1741 // Free the connection to the datasource
1742 if (SQLFreeConnect(hdbc
) != SQL_SUCCESS
)
1743 DispAllErrors(henv
, hdbc
);
1745 // There should be zero Ctable objects still connected to this db object
1746 wxASSERT(nTables
== 0);
1750 wxList::compatibility_iterator pNode
;
1751 pNode
= TablesInUse
.GetFirst();
1755 tiu
= (wxTablesInUse
*)pNode
->GetData();
1756 if (tiu
->pDb
== this)
1758 s
.Printf(wxT("(%-20s) tableID:[%6lu] pDb:[%p]"),
1759 tiu
->tableName
, tiu
->tableID
, wx_static_cast(void*, tiu
->pDb
));
1760 s2
.Printf(wxT("Orphaned table found using pDb:[%p]"), wx_static_cast(void*, this));
1761 wxLogDebug(s
.c_str(),s2
.c_str());
1763 pNode
= pNode
->GetNext();
1767 // Copy the error messages to a global variable
1769 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
1770 wxStrcpy(DBerrorList
[i
], errorList
[i
]);
1772 dbmsType
= dbmsUNIDENTIFIED
;
1778 /********** wxDb::CommitTrans() **********/
1779 bool wxDb::CommitTrans(void)
1783 // Commit the transaction
1784 if (SQLTransact(henv
, hdbc
, SQL_COMMIT
) != SQL_SUCCESS
)
1785 return(DispAllErrors(henv
, hdbc
));
1788 // Completed successfully
1791 } // wxDb::CommitTrans()
1794 /********** wxDb::RollbackTrans() **********/
1795 bool wxDb::RollbackTrans(void)
1797 // Rollback the transaction
1798 if (SQLTransact(henv
, hdbc
, SQL_ROLLBACK
) != SQL_SUCCESS
)
1799 return(DispAllErrors(henv
, hdbc
));
1801 // Completed successfully
1804 } // wxDb::RollbackTrans()
1807 /********** wxDb::DispAllErrors() **********/
1808 bool wxDb::DispAllErrors(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
1810 * This function is called internally whenever an error condition prevents the user's
1811 * request from being executed. This function will query the datasource as to the
1812 * actual error(s) that just occurred on the previous request of the datasource.
1814 * The function will retrieve each error condition from the datasource and
1815 * Printf the codes/text values into a string which it then logs via logError().
1816 * If in DBDEBUG_CONSOLE mode, the constructed string will be displayed in the console
1817 * window and program execution will be paused until the user presses a key.
1819 * This function always returns false, so that functions which call this function
1820 * can have a line like "return (DispAllErrors(henv, hdbc));" to indicate the failure
1821 * of the user's request, so that the calling code can then process the error message log.
1824 wxString odbcErrMsg
;
1826 while (SQLError(aHenv
, aHdbc
, aHstmt
, (SQLTCHAR FAR
*) sqlState
, &nativeError
, (SQLTCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
1828 odbcErrMsg
.Printf(wxT("SQL State = %s\nNative Error Code = %li\nError Message = %s\n"),
1829 sqlState
, (long)nativeError
, errorMsg
);
1830 logError(odbcErrMsg
, sqlState
);
1833 #ifdef DBDEBUG_CONSOLE
1834 // When run in console mode, use standard out to display errors.
1835 cout
<< odbcErrMsg
.c_str() << endl
;
1836 cout
<< wxT("Press any key to continue...") << endl
;
1841 wxLogDebug(odbcErrMsg
,wxT("ODBC DEBUG MESSAGE from DispAllErrors()"));
1846 return false; // This function always returns false.
1848 } // wxDb::DispAllErrors()
1851 /********** wxDb::GetNextError() **********/
1852 bool wxDb::GetNextError(HENV aHenv
, HDBC aHdbc
, HSTMT aHstmt
)
1854 if (SQLError(aHenv
, aHdbc
, aHstmt
, (SQLTCHAR FAR
*) sqlState
, &nativeError
, (SQLTCHAR FAR
*) errorMsg
, SQL_MAX_MESSAGE_LENGTH
- 1, &cbErrorMsg
) == SQL_SUCCESS
)
1859 } // wxDb::GetNextError()
1862 /********** wxDb::DispNextError() **********/
1863 void wxDb::DispNextError(void)
1865 wxString odbcErrMsg
;
1867 odbcErrMsg
.Printf(wxT("SQL State = %s\nNative Error Code = %li\nError Message = %s\n"),
1868 sqlState
, (long)nativeError
, errorMsg
);
1869 logError(odbcErrMsg
, sqlState
);
1874 #ifdef DBDEBUG_CONSOLE
1875 // When run in console mode, use standard out to display errors.
1876 cout
<< odbcErrMsg
.c_str() << endl
;
1877 cout
<< wxT("Press any key to continue...") << endl
;
1882 wxLogDebug(odbcErrMsg
,wxT("ODBC DEBUG MESSAGE"));
1883 #endif // __WXDEBUG__
1885 } // wxDb::DispNextError()
1888 /********** wxDb::logError() **********/
1889 void wxDb::logError(const wxString
&errMsg
, const wxString
&SQLState
)
1891 wxASSERT(errMsg
.length());
1893 static int pLast
= -1;
1896 if (++pLast
== DB_MAX_ERROR_HISTORY
)
1899 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
-1; i
++)
1900 wxStrcpy(errorList
[i
], errorList
[i
+1]);
1904 wxStrncpy(errorList
[pLast
], errMsg
, DB_MAX_ERROR_MSG_LEN
);
1905 errorList
[pLast
][DB_MAX_ERROR_MSG_LEN
] = 0;
1907 if (SQLState
.length())
1908 if ((dbStatus
= TranslateSqlState(SQLState
)) != DB_ERR_FUNCTION_SEQUENCE_ERROR
)
1909 DB_STATUS
= dbStatus
;
1911 // Add the errmsg to the sql log
1912 WriteSqlLog(errMsg
);
1914 } // wxDb::logError()
1917 /**********wxDb::TranslateSqlState() **********/
1918 int wxDb::TranslateSqlState(const wxString
&SQLState
)
1920 if (!wxStrcmp(SQLState
, wxT("01000")))
1921 return(DB_ERR_GENERAL_WARNING
);
1922 if (!wxStrcmp(SQLState
, wxT("01002")))
1923 return(DB_ERR_DISCONNECT_ERROR
);
1924 if (!wxStrcmp(SQLState
, wxT("01004")))
1925 return(DB_ERR_DATA_TRUNCATED
);
1926 if (!wxStrcmp(SQLState
, wxT("01006")))
1927 return(DB_ERR_PRIV_NOT_REVOKED
);
1928 if (!wxStrcmp(SQLState
, wxT("01S00")))
1929 return(DB_ERR_INVALID_CONN_STR_ATTR
);
1930 if (!wxStrcmp(SQLState
, wxT("01S01")))
1931 return(DB_ERR_ERROR_IN_ROW
);
1932 if (!wxStrcmp(SQLState
, wxT("01S02")))
1933 return(DB_ERR_OPTION_VALUE_CHANGED
);
1934 if (!wxStrcmp(SQLState
, wxT("01S03")))
1935 return(DB_ERR_NO_ROWS_UPD_OR_DEL
);
1936 if (!wxStrcmp(SQLState
, wxT("01S04")))
1937 return(DB_ERR_MULTI_ROWS_UPD_OR_DEL
);
1938 if (!wxStrcmp(SQLState
, wxT("07001")))
1939 return(DB_ERR_WRONG_NO_OF_PARAMS
);
1940 if (!wxStrcmp(SQLState
, wxT("07006")))
1941 return(DB_ERR_DATA_TYPE_ATTR_VIOL
);
1942 if (!wxStrcmp(SQLState
, wxT("08001")))
1943 return(DB_ERR_UNABLE_TO_CONNECT
);
1944 if (!wxStrcmp(SQLState
, wxT("08002")))
1945 return(DB_ERR_CONNECTION_IN_USE
);
1946 if (!wxStrcmp(SQLState
, wxT("08003")))
1947 return(DB_ERR_CONNECTION_NOT_OPEN
);
1948 if (!wxStrcmp(SQLState
, wxT("08004")))
1949 return(DB_ERR_REJECTED_CONNECTION
);
1950 if (!wxStrcmp(SQLState
, wxT("08007")))
1951 return(DB_ERR_CONN_FAIL_IN_TRANS
);
1952 if (!wxStrcmp(SQLState
, wxT("08S01")))
1953 return(DB_ERR_COMM_LINK_FAILURE
);
1954 if (!wxStrcmp(SQLState
, wxT("21S01")))
1955 return(DB_ERR_INSERT_VALUE_LIST_MISMATCH
);
1956 if (!wxStrcmp(SQLState
, wxT("21S02")))
1957 return(DB_ERR_DERIVED_TABLE_MISMATCH
);
1958 if (!wxStrcmp(SQLState
, wxT("22001")))
1959 return(DB_ERR_STRING_RIGHT_TRUNC
);
1960 if (!wxStrcmp(SQLState
, wxT("22003")))
1961 return(DB_ERR_NUMERIC_VALUE_OUT_OF_RNG
);
1962 if (!wxStrcmp(SQLState
, wxT("22005")))
1963 return(DB_ERR_ERROR_IN_ASSIGNMENT
);
1964 if (!wxStrcmp(SQLState
, wxT("22008")))
1965 return(DB_ERR_DATETIME_FLD_OVERFLOW
);
1966 if (!wxStrcmp(SQLState
, wxT("22012")))
1967 return(DB_ERR_DIVIDE_BY_ZERO
);
1968 if (!wxStrcmp(SQLState
, wxT("22026")))
1969 return(DB_ERR_STR_DATA_LENGTH_MISMATCH
);
1970 if (!wxStrcmp(SQLState
, wxT("23000")))
1971 return(DB_ERR_INTEGRITY_CONSTRAINT_VIOL
);
1972 if (!wxStrcmp(SQLState
, wxT("24000")))
1973 return(DB_ERR_INVALID_CURSOR_STATE
);
1974 if (!wxStrcmp(SQLState
, wxT("25000")))
1975 return(DB_ERR_INVALID_TRANS_STATE
);
1976 if (!wxStrcmp(SQLState
, wxT("28000")))
1977 return(DB_ERR_INVALID_AUTH_SPEC
);
1978 if (!wxStrcmp(SQLState
, wxT("34000")))
1979 return(DB_ERR_INVALID_CURSOR_NAME
);
1980 if (!wxStrcmp(SQLState
, wxT("37000")))
1981 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL
);
1982 if (!wxStrcmp(SQLState
, wxT("3C000")))
1983 return(DB_ERR_DUPLICATE_CURSOR_NAME
);
1984 if (!wxStrcmp(SQLState
, wxT("40001")))
1985 return(DB_ERR_SERIALIZATION_FAILURE
);
1986 if (!wxStrcmp(SQLState
, wxT("42000")))
1987 return(DB_ERR_SYNTAX_ERROR_OR_ACCESS_VIOL2
);
1988 if (!wxStrcmp(SQLState
, wxT("70100")))
1989 return(DB_ERR_OPERATION_ABORTED
);
1990 if (!wxStrcmp(SQLState
, wxT("IM001")))
1991 return(DB_ERR_UNSUPPORTED_FUNCTION
);
1992 if (!wxStrcmp(SQLState
, wxT("IM002")))
1993 return(DB_ERR_NO_DATA_SOURCE
);
1994 if (!wxStrcmp(SQLState
, wxT("IM003")))
1995 return(DB_ERR_DRIVER_LOAD_ERROR
);
1996 if (!wxStrcmp(SQLState
, wxT("IM004")))
1997 return(DB_ERR_SQLALLOCENV_FAILED
);
1998 if (!wxStrcmp(SQLState
, wxT("IM005")))
1999 return(DB_ERR_SQLALLOCCONNECT_FAILED
);
2000 if (!wxStrcmp(SQLState
, wxT("IM006")))
2001 return(DB_ERR_SQLSETCONNECTOPTION_FAILED
);
2002 if (!wxStrcmp(SQLState
, wxT("IM007")))
2003 return(DB_ERR_NO_DATA_SOURCE_DLG_PROHIB
);
2004 if (!wxStrcmp(SQLState
, wxT("IM008")))
2005 return(DB_ERR_DIALOG_FAILED
);
2006 if (!wxStrcmp(SQLState
, wxT("IM009")))
2007 return(DB_ERR_UNABLE_TO_LOAD_TRANSLATION_DLL
);
2008 if (!wxStrcmp(SQLState
, wxT("IM010")))
2009 return(DB_ERR_DATA_SOURCE_NAME_TOO_LONG
);
2010 if (!wxStrcmp(SQLState
, wxT("IM011")))
2011 return(DB_ERR_DRIVER_NAME_TOO_LONG
);
2012 if (!wxStrcmp(SQLState
, wxT("IM012")))
2013 return(DB_ERR_DRIVER_KEYWORD_SYNTAX_ERROR
);
2014 if (!wxStrcmp(SQLState
, wxT("IM013")))
2015 return(DB_ERR_TRACE_FILE_ERROR
);
2016 if (!wxStrcmp(SQLState
, wxT("S0001")))
2017 return(DB_ERR_TABLE_OR_VIEW_ALREADY_EXISTS
);
2018 if (!wxStrcmp(SQLState
, wxT("S0002")))
2019 return(DB_ERR_TABLE_NOT_FOUND
);
2020 if (!wxStrcmp(SQLState
, wxT("S0011")))
2021 return(DB_ERR_INDEX_ALREADY_EXISTS
);
2022 if (!wxStrcmp(SQLState
, wxT("S0012")))
2023 return(DB_ERR_INDEX_NOT_FOUND
);
2024 if (!wxStrcmp(SQLState
, wxT("S0021")))
2025 return(DB_ERR_COLUMN_ALREADY_EXISTS
);
2026 if (!wxStrcmp(SQLState
, wxT("S0022")))
2027 return(DB_ERR_COLUMN_NOT_FOUND
);
2028 if (!wxStrcmp(SQLState
, wxT("S0023")))
2029 return(DB_ERR_NO_DEFAULT_FOR_COLUMN
);
2030 if (!wxStrcmp(SQLState
, wxT("S1000")))
2031 return(DB_ERR_GENERAL_ERROR
);
2032 if (!wxStrcmp(SQLState
, wxT("S1001")))
2033 return(DB_ERR_MEMORY_ALLOCATION_FAILURE
);
2034 if (!wxStrcmp(SQLState
, wxT("S1002")))
2035 return(DB_ERR_INVALID_COLUMN_NUMBER
);
2036 if (!wxStrcmp(SQLState
, wxT("S1003")))
2037 return(DB_ERR_PROGRAM_TYPE_OUT_OF_RANGE
);
2038 if (!wxStrcmp(SQLState
, wxT("S1004")))
2039 return(DB_ERR_SQL_DATA_TYPE_OUT_OF_RANGE
);
2040 if (!wxStrcmp(SQLState
, wxT("S1008")))
2041 return(DB_ERR_OPERATION_CANCELLED
);
2042 if (!wxStrcmp(SQLState
, wxT("S1009")))
2043 return(DB_ERR_INVALID_ARGUMENT_VALUE
);
2044 if (!wxStrcmp(SQLState
, wxT("S1010")))
2045 return(DB_ERR_FUNCTION_SEQUENCE_ERROR
);
2046 if (!wxStrcmp(SQLState
, wxT("S1011")))
2047 return(DB_ERR_OPERATION_INVALID_AT_THIS_TIME
);
2048 if (!wxStrcmp(SQLState
, wxT("S1012")))
2049 return(DB_ERR_INVALID_TRANS_OPERATION_CODE
);
2050 if (!wxStrcmp(SQLState
, wxT("S1015")))
2051 return(DB_ERR_NO_CURSOR_NAME_AVAIL
);
2052 if (!wxStrcmp(SQLState
, wxT("S1090")))
2053 return(DB_ERR_INVALID_STR_OR_BUF_LEN
);
2054 if (!wxStrcmp(SQLState
, wxT("S1091")))
2055 return(DB_ERR_DESCRIPTOR_TYPE_OUT_OF_RANGE
);
2056 if (!wxStrcmp(SQLState
, wxT("S1092")))
2057 return(DB_ERR_OPTION_TYPE_OUT_OF_RANGE
);
2058 if (!wxStrcmp(SQLState
, wxT("S1093")))
2059 return(DB_ERR_INVALID_PARAM_NO
);
2060 if (!wxStrcmp(SQLState
, wxT("S1094")))
2061 return(DB_ERR_INVALID_SCALE_VALUE
);
2062 if (!wxStrcmp(SQLState
, wxT("S1095")))
2063 return(DB_ERR_FUNCTION_TYPE_OUT_OF_RANGE
);
2064 if (!wxStrcmp(SQLState
, wxT("S1096")))
2065 return(DB_ERR_INF_TYPE_OUT_OF_RANGE
);
2066 if (!wxStrcmp(SQLState
, wxT("S1097")))
2067 return(DB_ERR_COLUMN_TYPE_OUT_OF_RANGE
);
2068 if (!wxStrcmp(SQLState
, wxT("S1098")))
2069 return(DB_ERR_SCOPE_TYPE_OUT_OF_RANGE
);
2070 if (!wxStrcmp(SQLState
, wxT("S1099")))
2071 return(DB_ERR_NULLABLE_TYPE_OUT_OF_RANGE
);
2072 if (!wxStrcmp(SQLState
, wxT("S1100")))
2073 return(DB_ERR_UNIQUENESS_OPTION_TYPE_OUT_OF_RANGE
);
2074 if (!wxStrcmp(SQLState
, wxT("S1101")))
2075 return(DB_ERR_ACCURACY_OPTION_TYPE_OUT_OF_RANGE
);
2076 if (!wxStrcmp(SQLState
, wxT("S1103")))
2077 return(DB_ERR_DIRECTION_OPTION_OUT_OF_RANGE
);
2078 if (!wxStrcmp(SQLState
, wxT("S1104")))
2079 return(DB_ERR_INVALID_PRECISION_VALUE
);
2080 if (!wxStrcmp(SQLState
, wxT("S1105")))
2081 return(DB_ERR_INVALID_PARAM_TYPE
);
2082 if (!wxStrcmp(SQLState
, wxT("S1106")))
2083 return(DB_ERR_FETCH_TYPE_OUT_OF_RANGE
);
2084 if (!wxStrcmp(SQLState
, wxT("S1107")))
2085 return(DB_ERR_ROW_VALUE_OUT_OF_RANGE
);
2086 if (!wxStrcmp(SQLState
, wxT("S1108")))
2087 return(DB_ERR_CONCURRENCY_OPTION_OUT_OF_RANGE
);
2088 if (!wxStrcmp(SQLState
, wxT("S1109")))
2089 return(DB_ERR_INVALID_CURSOR_POSITION
);
2090 if (!wxStrcmp(SQLState
, wxT("S1110")))
2091 return(DB_ERR_INVALID_DRIVER_COMPLETION
);
2092 if (!wxStrcmp(SQLState
, wxT("S1111")))
2093 return(DB_ERR_INVALID_BOOKMARK_VALUE
);
2094 if (!wxStrcmp(SQLState
, wxT("S1C00")))
2095 return(DB_ERR_DRIVER_NOT_CAPABLE
);
2096 if (!wxStrcmp(SQLState
, wxT("S1T00")))
2097 return(DB_ERR_TIMEOUT_EXPIRED
);
2102 } // wxDb::TranslateSqlState()
2105 /********** wxDb::Grant() **********/
2106 bool wxDb::Grant(int privileges
, const wxString
&tableName
, const wxString
&userList
)
2110 // Build the grant statement
2111 sqlStmt
= wxT("GRANT ");
2112 if (privileges
== DB_GRANT_ALL
)
2113 sqlStmt
+= wxT("ALL");
2117 if (privileges
& DB_GRANT_SELECT
)
2119 sqlStmt
+= wxT("SELECT");
2122 if (privileges
& DB_GRANT_INSERT
)
2125 sqlStmt
+= wxT(", ");
2126 sqlStmt
+= wxT("INSERT");
2128 if (privileges
& DB_GRANT_UPDATE
)
2131 sqlStmt
+= wxT(", ");
2132 sqlStmt
+= wxT("UPDATE");
2134 if (privileges
& DB_GRANT_DELETE
)
2137 sqlStmt
+= wxT(", ");
2138 sqlStmt
+= wxT("DELETE");
2142 sqlStmt
+= wxT(" ON ");
2143 sqlStmt
+= SQLTableName(tableName
);
2144 sqlStmt
+= wxT(" TO ");
2145 sqlStmt
+= userList
;
2147 #ifdef DBDEBUG_CONSOLE
2148 cout
<< endl
<< sqlStmt
.c_str() << endl
;
2151 WriteSqlLog(sqlStmt
);
2153 return(ExecSql(sqlStmt
));
2158 /********** wxDb::CreateView() **********/
2159 bool wxDb::CreateView(const wxString
&viewName
, const wxString
&colList
,
2160 const wxString
&pSqlStmt
, bool attemptDrop
)
2164 // Drop the view first
2165 if (attemptDrop
&& !DropView(viewName
))
2168 // Build the create view statement
2169 sqlStmt
= wxT("CREATE VIEW ");
2170 sqlStmt
+= viewName
;
2172 if (colList
.length())
2174 sqlStmt
+= wxT(" (");
2176 sqlStmt
+= wxT(")");
2179 sqlStmt
+= wxT(" AS ");
2180 sqlStmt
+= pSqlStmt
;
2182 WriteSqlLog(sqlStmt
);
2184 #ifdef DBDEBUG_CONSOLE
2185 cout
<< sqlStmt
.c_str() << endl
;
2188 return(ExecSql(sqlStmt
));
2190 } // wxDb::CreateView()
2193 /********** wxDb::DropView() **********/
2194 bool wxDb::DropView(const wxString
&viewName
)
2197 * NOTE: This function returns true if the View does not exist, but
2198 * only for identified databases. Code will need to be added
2199 * below for any other databases when those databases are defined
2200 * to handle this situation consistently
2204 sqlStmt
.Printf(wxT("DROP VIEW %s"), viewName
.c_str());
2206 WriteSqlLog(sqlStmt
);
2208 #ifdef DBDEBUG_CONSOLE
2209 cout
<< endl
<< sqlStmt
.c_str() << endl
;
2212 if (SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) sqlStmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
2214 // Check for "Base table not found" error and ignore
2215 GetNextError(henv
, hdbc
, hstmt
);
2216 if (wxStrcmp(sqlState
,wxT("S0002"))) // "Base table not found"
2218 // Check for product specific error codes
2219 if (!((Dbms() == dbmsSYBASE_ASA
&& !wxStrcmp(sqlState
,wxT("42000"))))) // 5.x (and lower?)
2222 DispAllErrors(henv
, hdbc
, hstmt
);
2229 // Commit the transaction
2235 } // wxDb::DropView()
2238 /********** wxDb::ExecSql() **********/
2239 bool wxDb::ExecSql(const wxString
&pSqlStmt
)
2243 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2245 retcode
= SQLExecDirect(hstmt
, (SQLTCHAR FAR
*) pSqlStmt
.c_str(), SQL_NTS
);
2246 if (retcode
== SQL_SUCCESS
||
2247 (Dbms() == dbmsDB2
&& (retcode
== SQL_SUCCESS_WITH_INFO
|| retcode
== SQL_NO_DATA_FOUND
)))
2253 DispAllErrors(henv
, hdbc
, hstmt
);
2257 } // wxDb::ExecSql()
2260 /********** wxDb::ExecSql() with column info **********/
2261 bool wxDb::ExecSql(const wxString
&pSqlStmt
, wxDbColInf
** columns
, short& numcols
)
2263 //execute the statement first
2264 if (!ExecSql(pSqlStmt
))
2268 if (SQLNumResultCols(hstmt
, &noCols
) != SQL_SUCCESS
)
2270 DispAllErrors(henv
, hdbc
, hstmt
);
2279 // Get column information
2281 wxChar name
[DB_MAX_COLUMN_NAME_LEN
+1];
2284 wxDbColInf
* pColInf
= new wxDbColInf
[noCols
];
2286 // Fill in column information (name, datatype)
2287 for (colNum
= 0; colNum
< noCols
; colNum
++)
2289 if (SQLColAttributes(hstmt
, (UWORD
)(colNum
+1), SQL_COLUMN_NAME
,
2291 &Sword
, &Sqllen
) != SQL_SUCCESS
)
2293 DispAllErrors(henv
, hdbc
, hstmt
);
2298 wxStrncpy(pColInf
[colNum
].colName
, name
, DB_MAX_COLUMN_NAME_LEN
);
2299 pColInf
[colNum
].colName
[DB_MAX_COLUMN_NAME_LEN
] = 0; // Prevent buffer overrun
2301 if (SQLColAttributes(hstmt
, (UWORD
)(colNum
+1), SQL_COLUMN_TYPE
,
2302 NULL
, 0, &Sword
, &Sqllen
) != SQL_SUCCESS
)
2304 DispAllErrors(henv
, hdbc
, hstmt
);
2309 pColInf
[colNum
].sqlDataType
= Sqllen
;
2313 #if defined(SQL_WCHAR)
2316 #if defined(SQL_WVARCHAR)
2322 pColInf
[colNum
].dbDataType
= DB_DATA_TYPE_VARCHAR
;
2324 case SQL_LONGVARCHAR
:
2325 pColInf
[colNum
].dbDataType
= DB_DATA_TYPE_MEMO
;
2331 pColInf
[colNum
].dbDataType
= DB_DATA_TYPE_INTEGER
;
2338 pColInf
[colNum
].dbDataType
= DB_DATA_TYPE_FLOAT
;
2342 pColInf
[colNum
].dbDataType
= DB_DATA_TYPE_DATE
;
2345 pColInf
[colNum
].dbDataType
= DB_DATA_TYPE_BLOB
;
2350 errMsg
.Printf(wxT("SQL Data type %ld currently not supported by wxWidgets"), (long)Sqllen
);
2351 wxLogDebug(errMsg
,wxT("ODBC DEBUG MESSAGE"));
2358 } // wxDb::ExecSql()
2360 /********** wxDb::GetNext() **********/
2361 bool wxDb::GetNext(void)
2363 if (SQLFetch(hstmt
) == SQL_SUCCESS
)
2367 DispAllErrors(henv
, hdbc
, hstmt
);
2371 } // wxDb::GetNext()
2374 /********** wxDb::GetData() **********/
2375 bool wxDb::GetData(UWORD colNo
, SWORD cType
, PTR pData
, SDWORD maxLen
, SQLLEN FAR
*cbReturned
)
2378 wxASSERT(cbReturned
);
2380 long bufferSize
= maxLen
;
2382 if (cType
== SQL_C_WXCHAR
)
2383 bufferSize
= maxLen
* sizeof(wxChar
);
2385 if (SQLGetData(hstmt
, colNo
, cType
, pData
, bufferSize
, cbReturned
) == SQL_SUCCESS
)
2389 DispAllErrors(henv
, hdbc
, hstmt
);
2393 } // wxDb::GetData()
2396 /********** wxDb::GetKeyFields() **********/
2397 int wxDb::GetKeyFields(const wxString
&tableName
, wxDbColInf
* colInf
, UWORD noCols
)
2399 wxChar szPkTable
[DB_MAX_TABLE_NAME_LEN
+1]; /* Primary key table name */
2400 wxChar szFkTable
[DB_MAX_TABLE_NAME_LEN
+1]; /* Foreign key table name */
2402 wxChar szPkCol
[DB_MAX_COLUMN_NAME_LEN
+1]; /* Primary key column */
2403 wxChar szFkCol
[DB_MAX_COLUMN_NAME_LEN
+1]; /* Foreign key column */
2409 * -----------------------------------------------------------------------
2410 * -- 19991224 : mj10777 : Create ------
2411 * -- : Three things are done and stored here : ------
2412 * -- : 1) which Column(s) is/are Primary Key(s) ------
2413 * -- : 2) which tables use this Key as a Foreign Key ------
2414 * -- : 3) which columns are Foreign Key and the name ------
2415 * -- : of the Table where the Key is the Primary Key -----
2416 * -- : Called from GetColumns(const wxString &tableName, ------
2417 * -- int *numCols,const wxChar *userID ) ------
2418 * -----------------------------------------------------------------------
2421 /*---------------------------------------------------------------------*/
2422 /* Get the names of the columns in the primary key. */
2423 /*---------------------------------------------------------------------*/
2424 retcode
= SQLPrimaryKeys(hstmt
,
2425 NULL
, 0, /* Catalog name */
2426 NULL
, 0, /* Schema name */
2427 (SQLTCHAR FAR
*) tableName
.c_str(), SQL_NTS
); /* Table name */
2429 /*---------------------------------------------------------------------*/
2430 /* Fetch and display the result set. This will be a list of the */
2431 /* columns in the primary key of the tableName table. */
2432 /*---------------------------------------------------------------------*/
2433 while ((retcode
== SQL_SUCCESS
) || (retcode
== SQL_SUCCESS_WITH_INFO
))
2435 retcode
= SQLFetch(hstmt
);
2436 if (retcode
== SQL_SUCCESS
|| retcode
== SQL_SUCCESS_WITH_INFO
)
2438 GetData( 4, SQL_C_WXCHAR
, szPkCol
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
2439 GetData( 5, SQL_C_SSHORT
, &iKeySeq
, 0, &cb
);
2441 for (i
=0;i
<noCols
;i
++) // Find the Column name
2442 if (!wxStrcmp(colInf
[i
].colName
,szPkCol
)) // We have found the Column
2443 colInf
[i
].PkCol
= iKeySeq
; // Which Primary Key is this (first, second usw.) ?
2446 SQLFreeStmt(hstmt
, SQL_CLOSE
); /* Close the cursor (the hstmt is still allocated). */
2448 /*---------------------------------------------------------------------*/
2449 /* Get all the foreign keys that refer to tableName primary key. */
2450 /*---------------------------------------------------------------------*/
2451 retcode
= SQLForeignKeys(hstmt
,
2452 NULL
, 0, /* Primary catalog */
2453 NULL
, 0, /* Primary schema */
2454 (SQLTCHAR FAR
*)tableName
.c_str(), SQL_NTS
,/* Primary table */
2455 NULL
, 0, /* Foreign catalog */
2456 NULL
, 0, /* Foreign schema */
2457 NULL
, 0); /* Foreign table */
2459 /*---------------------------------------------------------------------*/
2460 /* Fetch and display the result set. This will be all of the foreign */
2461 /* keys in other tables that refer to the tableName primary key. */
2462 /*---------------------------------------------------------------------*/
2465 while ((retcode
== SQL_SUCCESS
) || (retcode
== SQL_SUCCESS_WITH_INFO
))
2467 retcode
= SQLFetch(hstmt
);
2468 if (retcode
== SQL_SUCCESS
|| retcode
== SQL_SUCCESS_WITH_INFO
)
2470 GetData( 3, SQL_C_WXCHAR
, szPkTable
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
2471 GetData( 4, SQL_C_WXCHAR
, szPkCol
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
2472 GetData( 5, SQL_C_SSHORT
, &iKeySeq
, 0, &cb
);
2473 GetData( 7, SQL_C_WXCHAR
, szFkTable
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
2474 GetData( 8, SQL_C_WXCHAR
, szFkCol
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
2475 tempStr
<< _T('[') << szFkTable
<< _T(']'); // [ ] in case there is a blank in the Table name
2479 tempStr
.Trim(); // Get rid of any unneeded blanks
2480 if (!tempStr
.empty())
2482 for (i
=0; i
<noCols
; i
++)
2483 { // Find the Column name
2484 if (!wxStrcmp(colInf
[i
].colName
, szPkCol
)) // We have found the Column, store the Information
2486 wxStrncpy(colInf
[i
].PkTableName
, tempStr
.c_str(), DB_MAX_TABLE_NAME_LEN
); // Name of the Tables where this Primary Key is used as a Foreign Key
2487 colInf
[i
].PkTableName
[DB_MAX_TABLE_NAME_LEN
] = 0; // Prevent buffer overrun
2492 SQLFreeStmt(hstmt
, SQL_CLOSE
); /* Close the cursor (the hstmt is still allocated). */
2494 /*---------------------------------------------------------------------*/
2495 /* Get all the foreign keys in the tablename table. */
2496 /*---------------------------------------------------------------------*/
2497 retcode
= SQLForeignKeys(hstmt
,
2498 NULL
, 0, /* Primary catalog */
2499 NULL
, 0, /* Primary schema */
2500 NULL
, 0, /* Primary table */
2501 NULL
, 0, /* Foreign catalog */
2502 NULL
, 0, /* Foreign schema */
2503 (SQLTCHAR
*)tableName
.c_str(), SQL_NTS
);/* Foreign table */
2505 /*---------------------------------------------------------------------*/
2506 /* Fetch and display the result set. This will be all of the */
2507 /* primary keys in other tables that are referred to by foreign */
2508 /* keys in the tableName table. */
2509 /*---------------------------------------------------------------------*/
2510 while ((retcode
== SQL_SUCCESS
) || (retcode
== SQL_SUCCESS_WITH_INFO
))
2512 retcode
= SQLFetch(hstmt
);
2513 if (retcode
== SQL_SUCCESS
|| retcode
== SQL_SUCCESS_WITH_INFO
)
2515 GetData( 3, SQL_C_WXCHAR
, szPkTable
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
2516 GetData( 5, SQL_C_SSHORT
, &iKeySeq
, 0, &cb
);
2517 GetData( 8, SQL_C_WXCHAR
, szFkCol
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
2519 for (i
=0; i
<noCols
; i
++) // Find the Column name
2521 if (!wxStrcmp(colInf
[i
].colName
,szFkCol
)) // We have found the (Foreign Key) Column
2523 colInf
[i
].FkCol
= iKeySeq
; // Which Foreign Key is this (first, second usw.) ?
2524 wxStrncpy(colInf
[i
].FkTableName
, szFkTable
, DB_MAX_TABLE_NAME_LEN
); // Name of the Table where this Foriegn is the Primary Key
2525 colInf
[i
].FkTableName
[DB_MAX_TABLE_NAME_LEN
] = 0; // Prevent buffer overrun
2530 SQLFreeStmt(hstmt
, SQL_CLOSE
); /* Close the cursor (the hstmt is still allocated). */
2534 } // wxDb::GetKeyFields()
2538 /********** wxDb::GetColumns() **********/
2539 wxDbColInf
*wxDb::GetColumns(wxChar
*tableName
[], const wxChar
*userID
)
2541 * 1) The last array element of the tableName[] argument must be zero (null).
2542 * This is how the end of the array is detected.
2543 * 2) This function returns an array of wxDbColInf structures. If no columns
2544 * were found, or an error occurred, this pointer will be zero (null). THE
2545 * CALLING FUNCTION IS RESPONSIBLE FOR DELETING THE MEMORY RETURNED WHEN IT
2546 * IS FINISHED WITH IT. i.e.
2548 * wxDbColInf *colInf = pDb->GetColumns(tableList, userID);
2551 * // Use the column inf
2553 * // Destroy the memory
2557 * userID is evaluated in the following manner:
2558 * userID == NULL ... UserID is ignored
2559 * userID == "" ... UserID set equal to 'this->uid'
2560 * userID != "" ... UserID set equal to 'userID'
2562 * NOTE: ALL column bindings associated with this wxDb instance are unbound
2563 * by this function. This function should use its own wxDb instance
2564 * to avoid undesired unbinding of columns.
2569 wxDbColInf
*colInf
= 0;
2577 convertUserID(userID
,UserID
);
2579 // Pass 1 - Determine how many columns there are.
2580 // Pass 2 - Allocate the wxDbColInf array and fill in
2581 // the array with the column information.
2583 for (pass
= 1; pass
<= 2; pass
++)
2587 if (noCols
== 0) // Probably a bogus table name(s)
2589 // Allocate n wxDbColInf objects to hold the column information
2590 colInf
= new wxDbColInf
[noCols
+1];
2593 // Mark the end of the array
2594 wxStrcpy(colInf
[noCols
].tableName
, wxEmptyString
);
2595 wxStrcpy(colInf
[noCols
].colName
, wxEmptyString
);
2596 colInf
[noCols
].sqlDataType
= 0;
2598 // Loop through each table name
2600 for (tbl
= 0; tableName
[tbl
]; tbl
++)
2602 TableName
= tableName
[tbl
];
2603 // Oracle and Interbase table names are uppercase only, so force
2604 // the name to uppercase just in case programmer forgot to do this
2605 if ((Dbms() == dbmsORACLE
) ||
2606 (Dbms() == dbmsFIREBIRD
) ||
2607 (Dbms() == dbmsINTERBASE
))
2608 TableName
= TableName
.Upper();
2610 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2612 // MySQL, SQLServer, and Access cannot accept a user name when looking up column names, so we
2613 // use the call below that leaves out the user name
2614 if (!UserID
.empty() &&
2615 Dbms() != dbmsMY_SQL
&&
2616 Dbms() != dbmsACCESS
&&
2617 Dbms() != dbmsMS_SQL_SERVER
)
2619 retcode
= SQLColumns(hstmt
,
2620 NULL
, 0, // All qualifiers
2621 (SQLTCHAR
*) UserID
.c_str(), SQL_NTS
, // Owner
2622 (SQLTCHAR
*) TableName
.c_str(), SQL_NTS
,
2623 NULL
, 0); // All columns
2627 retcode
= SQLColumns(hstmt
,
2628 NULL
, 0, // All qualifiers
2630 (SQLTCHAR
*) TableName
.c_str(), SQL_NTS
,
2631 NULL
, 0); // All columns
2633 if (retcode
!= SQL_SUCCESS
)
2634 { // Error occurred, abort
2635 DispAllErrors(henv
, hdbc
, hstmt
);
2638 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2642 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
2644 if (pass
== 1) // First pass, just add up the number of columns
2646 else // Pass 2; Fill in the array of structures
2648 if (colNo
< noCols
) // Some extra error checking to prevent memory overwrites
2650 // NOTE: Only the ODBC 1.x fields are retrieved
2651 GetData( 1, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].catalog
, 128+1, &cb
);
2652 GetData( 2, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].schema
, 128+1, &cb
);
2653 GetData( 3, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].tableName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
2654 GetData( 4, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].colName
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
2655 GetData( 5, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].sqlDataType
, 0, &cb
);
2656 GetData( 6, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].typeName
, 128+1, &cb
);
2657 GetData( 7, SQL_C_SLONG
, (UCHAR
*) &colInf
[colNo
].columnLength
, 0, &cb
);
2658 GetData( 8, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].bufferSize
, 0, &cb
);
2659 GetData( 9, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].decimalDigits
,0, &cb
);
2660 GetData(10, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].numPrecRadix
, 0, &cb
);
2661 GetData(11, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].nullable
, 0, &cb
);
2662 GetData(12, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].remarks
, 254+1, &cb
);
2664 // Determine the wxDb data type that is used to represent the native data type of this data source
2665 colInf
[colNo
].dbDataType
= 0;
2666 if (!wxStricmp(typeInfVarchar
.TypeName
,colInf
[colNo
].typeName
))
2669 // IODBC does not return a correct columnLength, so we set
2670 // columnLength = bufferSize if no column length was returned
2671 // IODBC returns the columnLength in bufferSize. (bug)
2672 if (colInf
[colNo
].columnLength
< 1)
2674 colInf
[colNo
].columnLength
= colInf
[colNo
].bufferSize
;
2677 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_VARCHAR
;
2679 else if (!wxStricmp(typeInfInteger
.TypeName
, colInf
[colNo
].typeName
))
2680 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_INTEGER
;
2681 else if (!wxStricmp(typeInfFloat
.TypeName
, colInf
[colNo
].typeName
))
2682 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_FLOAT
;
2683 else if (!wxStricmp(typeInfDate
.TypeName
, colInf
[colNo
].typeName
))
2684 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_DATE
;
2685 else if (!wxStricmp(typeInfBlob
.TypeName
, colInf
[colNo
].typeName
))
2686 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_BLOB
;
2691 if (retcode
!= SQL_NO_DATA_FOUND
)
2692 { // Error occurred, abort
2693 DispAllErrors(henv
, hdbc
, hstmt
);
2696 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2702 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2705 } // wxDb::GetColumns()
2708 /********** wxDb::GetColumns() **********/
2710 wxDbColInf
*wxDb::GetColumns(const wxString
&tableName
, UWORD
*numCols
, const wxChar
*userID
)
2712 // Same as the above GetColumns() function except this one gets columns
2713 // only for a single table, and if 'numCols' is not NULL, the number of
2714 // columns stored in the returned wxDbColInf is set in '*numCols'
2716 // userID is evaluated in the following manner:
2717 // userID == NULL ... UserID is ignored
2718 // userID == "" ... UserID set equal to 'this->uid'
2719 // userID != "" ... UserID set equal to 'userID'
2721 // NOTE: ALL column bindings associated with this wxDb instance are unbound
2722 // by this function. This function should use its own wxDb instance
2723 // to avoid undesired unbinding of columns.
2728 wxDbColInf
*colInf
= 0;
2736 convertUserID(userID
,UserID
);
2738 // Pass 1 - Determine how many columns there are.
2739 // Pass 2 - Allocate the wxDbColInf array and fill in
2740 // the array with the column information.
2742 for (pass
= 1; pass
<= 2; pass
++)
2746 if (noCols
== 0) // Probably a bogus table name(s)
2748 // Allocate n wxDbColInf objects to hold the column information
2749 colInf
= new wxDbColInf
[noCols
+1];
2752 // Mark the end of the array
2753 wxStrcpy(colInf
[noCols
].tableName
, wxEmptyString
);
2754 wxStrcpy(colInf
[noCols
].colName
, wxEmptyString
);
2755 colInf
[noCols
].sqlDataType
= 0;
2758 TableName
= tableName
;
2759 // Oracle and Interbase table names are uppercase only, so force
2760 // the name to uppercase just in case programmer forgot to do this
2761 if ((Dbms() == dbmsORACLE
) ||
2762 (Dbms() == dbmsFIREBIRD
) ||
2763 (Dbms() == dbmsINTERBASE
))
2764 TableName
= TableName
.Upper();
2766 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2768 // MySQL, SQLServer, and Access cannot accept a user name when looking up column names, so we
2769 // use the call below that leaves out the user name
2770 if (!UserID
.empty() &&
2771 Dbms() != dbmsMY_SQL
&&
2772 Dbms() != dbmsACCESS
&&
2773 Dbms() != dbmsMS_SQL_SERVER
)
2775 retcode
= SQLColumns(hstmt
,
2776 NULL
, 0, // All qualifiers
2777 (SQLTCHAR
*) UserID
.c_str(), SQL_NTS
, // Owner
2778 (SQLTCHAR
*) TableName
.c_str(), SQL_NTS
,
2779 NULL
, 0); // All columns
2783 retcode
= SQLColumns(hstmt
,
2784 NULL
, 0, // All qualifiers
2786 (SQLTCHAR
*) TableName
.c_str(), SQL_NTS
,
2787 NULL
, 0); // All columns
2789 if (retcode
!= SQL_SUCCESS
)
2790 { // Error occurred, abort
2791 DispAllErrors(henv
, hdbc
, hstmt
);
2794 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2800 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
2802 if (pass
== 1) // First pass, just add up the number of columns
2804 else // Pass 2; Fill in the array of structures
2806 if (colNo
< noCols
) // Some extra error checking to prevent memory overwrites
2808 // NOTE: Only the ODBC 1.x fields are retrieved
2809 GetData( 1, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].catalog
, 128+1, &cb
);
2810 GetData( 2, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].schema
, 128+1, &cb
);
2811 GetData( 3, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].tableName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
2812 GetData( 4, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].colName
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
2813 GetData( 5, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].sqlDataType
, 0, &cb
);
2814 GetData( 6, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].typeName
, 128+1, &cb
);
2815 GetData( 7, SQL_C_SLONG
, (UCHAR
*) &colInf
[colNo
].columnLength
, 0, &cb
);
2816 // BJO 991214 : SQL_C_SSHORT instead of SQL_C_SLONG, otherwise fails on Sparc (probably all 64 bit architectures)
2817 GetData( 8, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].bufferSize
, 0, &cb
);
2818 GetData( 9, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].decimalDigits
,0, &cb
);
2819 GetData(10, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].numPrecRadix
, 0, &cb
);
2820 GetData(11, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].nullable
, 0, &cb
);
2821 GetData(12, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].remarks
, 254+1, &cb
);
2822 // Start Values for Primary/Foriegn Key (=No)
2823 colInf
[colNo
].PkCol
= 0; // Primary key column 0=No; 1= First Key, 2 = Second Key etc.
2824 colInf
[colNo
].PkTableName
[0] = 0; // Tablenames where Primary Key is used as a Foreign Key
2825 colInf
[colNo
].FkCol
= 0; // Foreign key column 0=No; 1= First Key, 2 = Second Key etc.
2826 colInf
[colNo
].FkTableName
[0] = 0; // Foreign key table name
2828 // BJO 20000428 : Virtuoso returns type names with upper cases!
2829 if (Dbms() == dbmsVIRTUOSO
)
2831 wxString s
= colInf
[colNo
].typeName
;
2833 wxStrcmp(colInf
[colNo
].typeName
, s
.c_str());
2836 // Determine the wxDb data type that is used to represent the native data type of this data source
2837 colInf
[colNo
].dbDataType
= 0;
2838 if (!wxStricmp(typeInfVarchar
.TypeName
, colInf
[colNo
].typeName
))
2841 // IODBC does not return a correct columnLength, so we set
2842 // columnLength = bufferSize if no column length was returned
2843 // IODBC returns the columnLength in bufferSize. (bug)
2844 if (colInf
[colNo
].columnLength
< 1)
2846 colInf
[colNo
].columnLength
= colInf
[colNo
].bufferSize
;
2850 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_VARCHAR
;
2852 else if (!wxStricmp(typeInfInteger
.TypeName
, colInf
[colNo
].typeName
))
2853 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_INTEGER
;
2854 else if (!wxStricmp(typeInfFloat
.TypeName
, colInf
[colNo
].typeName
))
2855 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_FLOAT
;
2856 else if (!wxStricmp(typeInfDate
.TypeName
, colInf
[colNo
].typeName
))
2857 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_DATE
;
2858 else if (!wxStricmp(typeInfBlob
.TypeName
, colInf
[colNo
].typeName
))
2859 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_BLOB
;
2865 if (retcode
!= SQL_NO_DATA_FOUND
)
2866 { // Error occurred, abort
2867 DispAllErrors(henv
, hdbc
, hstmt
);
2870 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2877 SQLFreeStmt(hstmt
, SQL_CLOSE
);
2879 // Store Primary and Foriegn Keys
2880 GetKeyFields(tableName
,colInf
,noCols
);
2886 } // wxDb::GetColumns()
2889 #else // New GetColumns
2894 These are tentative new GetColumns members which should be more database
2895 independent and which always returns the columns in the order they were
2898 - The first one (wxDbColInf *wxDb::GetColumns(wxChar *tableName[], const
2899 wxChar* userID)) calls the second implementation for each separate table
2900 before merging the results. This makes the code easier to maintain as
2901 only one member (the second) makes the real work
2902 - wxDbColInf *wxDb::GetColumns(const wxString &tableName, int *numCols, const
2903 wxChar *userID) is a little bit improved
2904 - It doesn't anymore rely on the type-name to find out which database-type
2906 - It ends by sorting the columns, so that they are returned in the same
2907 order they were created
2917 wxDbColInf
*wxDb::GetColumns(wxChar
*tableName
[], const wxChar
*userID
)
2920 // The last array element of the tableName[] argument must be zero (null).
2921 // This is how the end of the array is detected.
2925 // How many tables ?
2927 for (tbl
= 0 ; tableName
[tbl
]; tbl
++);
2929 // Create a table to maintain the columns for each separate table
2930 _TableColumns
*TableColumns
= new _TableColumns
[tbl
];
2933 for (i
= 0 ; i
< tbl
; i
++)
2936 TableColumns
[i
].colInf
= GetColumns(tableName
[i
], &TableColumns
[i
].noCols
, userID
);
2937 if (TableColumns
[i
].colInf
== NULL
)
2939 noCols
+= TableColumns
[i
].noCols
;
2942 // Now merge all the separate table infos
2943 wxDbColInf
*colInf
= new wxDbColInf
[noCols
+1];
2945 // Mark the end of the array
2946 wxStrcpy(colInf
[noCols
].tableName
, wxEmptyString
);
2947 wxStrcpy(colInf
[noCols
].colName
, wxEmptyString
);
2948 colInf
[noCols
].sqlDataType
= 0;
2953 for (i
= 0 ; i
< tbl
; i
++)
2955 for (j
= 0 ; j
< TableColumns
[i
].noCols
; j
++)
2957 colInf
[offset
++] = TableColumns
[i
].colInf
[j
];
2961 delete [] TableColumns
;
2964 } // wxDb::GetColumns() -- NEW
2967 wxDbColInf
*wxDb::GetColumns(const wxString
&tableName
, int *numCols
, const wxChar
*userID
)
2969 // Same as the above GetColumns() function except this one gets columns
2970 // only for a single table, and if 'numCols' is not NULL, the number of
2971 // columns stored in the returned wxDbColInf is set in '*numCols'
2973 // userID is evaluated in the following manner:
2974 // userID == NULL ... UserID is ignored
2975 // userID == "" ... UserID set equal to 'this->uid'
2976 // userID != "" ... UserID set equal to 'userID'
2978 // NOTE: ALL column bindings associated with this wxDb instance are unbound
2979 // by this function. This function should use its own wxDb instance
2980 // to avoid undesired unbinding of columns.
2984 wxDbColInf
*colInf
= 0;
2992 convertUserID(userID
,UserID
);
2994 // Pass 1 - Determine how many columns there are.
2995 // Pass 2 - Allocate the wxDbColInf array and fill in
2996 // the array with the column information.
2998 for (pass
= 1; pass
<= 2; pass
++)
3002 if (noCols
== 0) // Probably a bogus table name(s)
3004 // Allocate n wxDbColInf objects to hold the column information
3005 colInf
= new wxDbColInf
[noCols
+1];
3008 // Mark the end of the array
3009 wxStrcpy(colInf
[noCols
].tableName
, wxEmptyString
);
3010 wxStrcpy(colInf
[noCols
].colName
, wxEmptyString
);
3011 colInf
[noCols
].sqlDataType
= 0;
3014 TableName
= tableName
;
3015 // Oracle and Interbase table names are uppercase only, so force
3016 // the name to uppercase just in case programmer forgot to do this
3017 if ((Dbms() == dbmsORACLE
) ||
3018 (Dbms() == dbmsFIREBIRD
) ||
3019 (Dbms() == dbmsINTERBASE
))
3020 TableName
= TableName
.Upper();
3022 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3024 // MySQL, SQLServer, and Access cannot accept a user name when looking up column names, so we
3025 // use the call below that leaves out the user name
3026 if (!UserID
.empty() &&
3027 Dbms() != dbmsMY_SQL
&&
3028 Dbms() != dbmsACCESS
&&
3029 Dbms() != dbmsMS_SQL_SERVER
)
3031 retcode
= SQLColumns(hstmt
,
3032 NULL
, 0, // All qualifiers
3033 (UCHAR
*) UserID
.c_str(), SQL_NTS
, // Owner
3034 (UCHAR
*) TableName
.c_str(), SQL_NTS
,
3035 NULL
, 0); // All columns
3039 retcode
= SQLColumns(hstmt
,
3040 NULL
, 0, // All qualifiers
3042 (UCHAR
*) TableName
.c_str(), SQL_NTS
,
3043 NULL
, 0); // All columns
3045 if (retcode
!= SQL_SUCCESS
)
3046 { // Error occurred, abort
3047 DispAllErrors(henv
, hdbc
, hstmt
);
3050 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3056 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
3058 if (pass
== 1) // First pass, just add up the number of columns
3060 else // Pass 2; Fill in the array of structures
3062 if (colNo
< noCols
) // Some extra error checking to prevent memory overwrites
3064 // NOTE: Only the ODBC 1.x fields are retrieved
3065 GetData( 1, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].catalog
, 128+1, &cb
);
3066 GetData( 2, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].schema
, 128+1, &cb
);
3067 GetData( 3, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].tableName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
3068 GetData( 4, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].colName
, DB_MAX_COLUMN_NAME_LEN
+1, &cb
);
3069 GetData( 5, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].sqlDataType
, 0, &cb
);
3070 GetData( 6, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].typeName
, 128+1, &cb
);
3071 GetData( 7, SQL_C_SLONG
, (UCHAR
*) &colInf
[colNo
].columnLength
, 0, &cb
);
3072 GetData( 8, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].bufferSize
, 0, &cb
);
3073 GetData( 9, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].decimalDigits
,0, &cb
);
3074 GetData(10, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].numPrecRadix
, 0, &cb
);
3075 GetData(11, SQL_C_SSHORT
, (UCHAR
*) &colInf
[colNo
].nullable
, 0, &cb
);
3076 GetData(12, SQL_C_WXCHAR
, (UCHAR
*) colInf
[colNo
].remarks
, 254+1, &cb
);
3077 // Start Values for Primary/Foriegn Key (=No)
3078 colInf
[colNo
].PkCol
= 0; // Primary key column 0=No; 1= First Key, 2 = Second Key etc.
3079 colInf
[colNo
].PkTableName
[0] = 0; // Tablenames where Primary Key is used as a Foreign Key
3080 colInf
[colNo
].FkCol
= 0; // Foreign key column 0=No; 1= First Key, 2 = Second Key etc.
3081 colInf
[colNo
].FkTableName
[0] = 0; // Foreign key table name
3084 // IODBC does not return a correct columnLength, so we set
3085 // columnLength = bufferSize if no column length was returned
3086 // IODBC returns the columnLength in bufferSize. (bug)
3087 if (colInf
[colNo
].columnLength
< 1)
3089 colInf
[colNo
].columnLength
= colInf
[colNo
].bufferSize
;
3093 // Determine the wxDb data type that is used to represent the native data type of this data source
3094 colInf
[colNo
].dbDataType
= 0;
3095 // Get the intern datatype
3096 switch (colInf
[colNo
].sqlDataType
)
3099 #if defined(SQL_WCHAR)
3102 #if defined(SQL_WVARCHAR)
3108 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_VARCHAR
;
3110 case SQL_LONGVARCHAR
:
3111 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_MEMO
;
3117 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_INTEGER
;
3124 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_FLOAT
;
3128 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_DATE
;
3131 colInf
[colNo
].dbDataType
= DB_DATA_TYPE_BLOB
;
3136 errMsg
.Printf(wxT("SQL Data type %d currently not supported by wxWidgets"), colInf
[colNo
].sqlDataType
);
3137 wxLogDebug(errMsg
,wxT("ODBC DEBUG MESSAGE"));
3144 if (retcode
!= SQL_NO_DATA_FOUND
)
3145 { // Error occurred, abort
3146 DispAllErrors(henv
, hdbc
, hstmt
);
3149 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3156 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3158 // Store Primary and Foreign Keys
3159 GetKeyFields(tableName
,colInf
,noCols
);
3161 ///////////////////////////////////////////////////////////////////////////
3162 // Now sort the the columns in order to make them appear in the right order
3163 ///////////////////////////////////////////////////////////////////////////
3165 // Build a generic SELECT statement which returns 0 rows
3168 Stmt
.Printf(wxT("select * from \"%s\" where 0=1"), tableName
);
3171 if (SQLExecDirect(hstmt
, (UCHAR FAR
*) Stmt
.c_str(), SQL_NTS
) != SQL_SUCCESS
)
3173 DispAllErrors(henv
, hdbc
, hstmt
);
3177 // Get the number of result columns
3178 if (SQLNumResultCols (hstmt
, &noCols
) != SQL_SUCCESS
)
3180 DispAllErrors(henv
, hdbc
, hstmt
);
3184 if (noCols
== 0) // Probably a bogus table name
3193 for (colNum
= 0; colNum
< noCols
; colNum
++)
3195 if (SQLColAttributes(hstmt
,colNum
+1, SQL_COLUMN_NAME
,
3197 &Sword
, &Sdword
) != SQL_SUCCESS
)
3199 DispAllErrors(henv
, hdbc
, hstmt
);
3203 wxString Name1
= name
;
3204 Name1
= Name1
.Upper();
3206 // Where is this name in the array ?
3207 for (i
= colNum
; i
< noCols
; i
++)
3209 wxString Name2
= colInf
[i
].colName
;
3210 Name2
= Name2
.Upper();
3213 if (colNum
!= i
) // swap to sort
3215 wxDbColInf tmpColInf
= colInf
[colNum
];
3216 colInf
[colNum
] = colInf
[i
];
3217 colInf
[i
] = tmpColInf
;
3223 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3225 ///////////////////////////////////////////////////////////////////////////
3227 ///////////////////////////////////////////////////////////////////////////
3233 } // wxDb::GetColumns()
3236 #endif // #else OLD_GETCOLUMNS
3239 /********** wxDb::GetColumnCount() **********/
3240 int wxDb::GetColumnCount(const wxString
&tableName
, const wxChar
*userID
)
3242 * Returns a count of how many columns are in a table.
3243 * If an error occurs in computing the number of columns
3244 * this function will return a -1 for the count
3246 * userID is evaluated in the following manner:
3247 * userID == NULL ... UserID is ignored
3248 * userID == "" ... UserID set equal to 'this->uid'
3249 * userID != "" ... UserID set equal to 'userID'
3251 * NOTE: ALL column bindings associated with this wxDb instance are unbound
3252 * by this function. This function should use its own wxDb instance
3253 * to avoid undesired unbinding of columns.
3263 convertUserID(userID
,UserID
);
3265 TableName
= tableName
;
3266 // Oracle and Interbase table names are uppercase only, so force
3267 // the name to uppercase just in case programmer forgot to do this
3268 if ((Dbms() == dbmsORACLE
) ||
3269 (Dbms() == dbmsFIREBIRD
) ||
3270 (Dbms() == dbmsINTERBASE
))
3271 TableName
= TableName
.Upper();
3273 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3275 // MySQL, SQLServer, and Access cannot accept a user name when looking up column names, so we
3276 // use the call below that leaves out the user name
3277 if (!UserID
.empty() &&
3278 Dbms() != dbmsMY_SQL
&&
3279 Dbms() != dbmsACCESS
&&
3280 Dbms() != dbmsMS_SQL_SERVER
)
3282 retcode
= SQLColumns(hstmt
,
3283 NULL
, 0, // All qualifiers
3284 (SQLTCHAR
*) UserID
.c_str(), SQL_NTS
, // Owner
3285 (SQLTCHAR
*) TableName
.c_str(), SQL_NTS
,
3286 NULL
, 0); // All columns
3290 retcode
= SQLColumns(hstmt
,
3291 NULL
, 0, // All qualifiers
3293 (SQLTCHAR
*) TableName
.c_str(), SQL_NTS
,
3294 NULL
, 0); // All columns
3296 if (retcode
!= SQL_SUCCESS
)
3297 { // Error occurred, abort
3298 DispAllErrors(henv
, hdbc
, hstmt
);
3299 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3303 // Count the columns
3304 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
)
3307 if (retcode
!= SQL_NO_DATA_FOUND
)
3308 { // Error occurred, abort
3309 DispAllErrors(henv
, hdbc
, hstmt
);
3310 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3314 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3317 } // wxDb::GetColumnCount()
3320 /********** wxDb::GetCatalog() *******/
3321 wxDbInf
*wxDb::GetCatalog(const wxChar
*userID
)
3323 * ---------------------------------------------------------------------
3324 * -- 19991203 : mj10777 : Create ------
3325 * -- : Creates a wxDbInf with Tables / Cols Array ------
3326 * -- : uses SQLTables and fills pTableInf; ------
3327 * -- : pColInf is set to NULL and numCols to 0; ------
3328 * -- : returns pDbInf (wxDbInf) ------
3329 * -- - if unsuccessful (pDbInf == NULL) ------
3330 * -- : pColInf can be filled with GetColumns(..); ------
3331 * -- : numCols can be filled with GetColumnCount(..); ------
3332 * ---------------------------------------------------------------------
3334 * userID is evaluated in the following manner:
3335 * userID == NULL ... UserID is ignored
3336 * userID == "" ... UserID set equal to 'this->uid'
3337 * userID != "" ... UserID set equal to 'userID'
3339 * NOTE: ALL column bindings associated with this wxDb instance are unbound
3340 * by this function. This function should use its own wxDb instance
3341 * to avoid undesired unbinding of columns.
3344 int noTab
= 0; // Counter while filling table entries
3348 wxString tblNameSave
;
3351 convertUserID(userID
,UserID
);
3353 //-------------------------------------------------------------
3354 // Create the Database Array of catalog entries
3356 wxDbInf
*pDbInf
= new wxDbInf
;
3358 //-------------------------------------------------------------
3359 // Table Information
3360 // Pass 1 - Determine how many Tables there are.
3361 // Pass 2 - Create the Table array and fill it
3362 // - Create the Cols array = NULL
3363 //-------------------------------------------------------------
3365 for (pass
= 1; pass
<= 2; pass
++)
3367 SQLFreeStmt(hstmt
, SQL_CLOSE
); // Close if Open
3368 tblNameSave
.Empty();
3370 if (!UserID
.empty() &&
3371 Dbms() != dbmsMY_SQL
&&
3372 Dbms() != dbmsACCESS
&&
3373 Dbms() != dbmsMS_SQL_SERVER
)
3375 retcode
= SQLTables(hstmt
,
3376 NULL
, 0, // All qualifiers
3377 (SQLTCHAR
*) UserID
.c_str(), SQL_NTS
, // User specified
3378 NULL
, 0, // All tables
3379 NULL
, 0); // All columns
3383 retcode
= SQLTables(hstmt
,
3384 NULL
, 0, // All qualifiers
3385 NULL
, 0, // User specified
3386 NULL
, 0, // All tables
3387 NULL
, 0); // All columns
3390 if (retcode
!= SQL_SUCCESS
)
3392 DispAllErrors(henv
, hdbc
, hstmt
);
3394 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3398 while ((retcode
= SQLFetch(hstmt
)) == SQL_SUCCESS
) // Table Information
3400 if (pass
== 1) // First pass, just count the Tables
3402 if (pDbInf
->numTables
== 0)
3404 GetData( 1, SQL_C_WXCHAR
, (UCHAR
*) pDbInf
->catalog
, 128+1, &cb
);
3405 GetData( 2, SQL_C_WXCHAR
, (UCHAR
*) pDbInf
->schema
, 128+1, &cb
);
3407 pDbInf
->numTables
++; // Counter for Tables
3409 if (pass
== 2) // Create and fill the Table entries
3411 if (pDbInf
->pTableInf
== NULL
) // Has the Table Array been created
3412 { // no, then create the Array
3413 pDbInf
->pTableInf
= new wxDbTableInf
[pDbInf
->numTables
];
3415 } // if (pDbInf->pTableInf == NULL) // Has the Table Array been created
3417 GetData( 3, SQL_C_WXCHAR
, (UCHAR
*) (pDbInf
->pTableInf
+noTab
)->tableName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
3418 GetData( 4, SQL_C_WXCHAR
, (UCHAR
*) (pDbInf
->pTableInf
+noTab
)->tableType
, 30+1, &cb
);
3419 GetData( 5, SQL_C_WXCHAR
, (UCHAR
*) (pDbInf
->pTableInf
+noTab
)->tableRemarks
, 254+1, &cb
);
3425 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3427 // Query how many columns are in each table
3428 for (noTab
=0;noTab
<pDbInf
->numTables
;noTab
++)
3430 (pDbInf
->pTableInf
+noTab
)->numCols
= (UWORD
)GetColumnCount((pDbInf
->pTableInf
+noTab
)->tableName
,UserID
);
3435 } // wxDb::GetCatalog()
3438 /********** wxDb::Catalog() **********/
3439 bool wxDb::Catalog(const wxChar
*userID
, const wxString
&fileName
)
3441 * Creates the text file specified in 'filename' which will contain
3442 * a minimal data dictionary of all tables accessible by the user specified
3445 * userID is evaluated in the following manner:
3446 * userID == NULL ... UserID is ignored
3447 * userID == "" ... UserID set equal to 'this->uid'
3448 * userID != "" ... UserID set equal to 'userID'
3450 * NOTE: ALL column bindings associated with this wxDb instance are unbound
3451 * by this function. This function should use its own wxDb instance
3452 * to avoid undesired unbinding of columns.
3455 wxASSERT(fileName
.length());
3459 wxChar tblName
[DB_MAX_TABLE_NAME_LEN
+1];
3460 wxString tblNameSave
;
3461 wxChar colName
[DB_MAX_COLUMN_NAME_LEN
+1];
3463 wxChar typeName
[30+1];
3464 SDWORD precision
, length
;
3466 FILE *fp
= wxFopen(fileName
.c_str(),wxT("wt"));
3470 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3473 convertUserID(userID
,UserID
);
3475 if (!UserID
.empty() &&
3476 Dbms() != dbmsMY_SQL
&&
3477 Dbms() != dbmsACCESS
&&
3478 Dbms() != dbmsFIREBIRD
&&
3479 Dbms() != dbmsINTERBASE
&&
3480 Dbms() != dbmsMS_SQL_SERVER
)
3482 retcode
= SQLColumns(hstmt
,
3483 NULL
, 0, // All qualifiers
3484 (SQLTCHAR
*) UserID
.c_str(), SQL_NTS
, // User specified
3485 NULL
, 0, // All tables
3486 NULL
, 0); // All columns
3490 retcode
= SQLColumns(hstmt
,
3491 NULL
, 0, // All qualifiers
3492 NULL
, 0, // User specified
3493 NULL
, 0, // All tables
3494 NULL
, 0); // All columns
3496 if (retcode
!= SQL_SUCCESS
)
3498 DispAllErrors(henv
, hdbc
, hstmt
);
3504 tblNameSave
.Empty();
3509 retcode
= SQLFetch(hstmt
);
3510 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
3513 GetData(3,SQL_C_WXCHAR
, (UCHAR
*) tblName
, DB_MAX_TABLE_NAME_LEN
+1, &cb
);
3514 GetData(4,SQL_C_WXCHAR
, (UCHAR
*) colName
, DB_MAX_COLUMN_NAME_LEN
+1,&cb
);
3515 GetData(5,SQL_C_SSHORT
, (UCHAR
*)&sqlDataType
, 0, &cb
);
3516 GetData(6,SQL_C_WXCHAR
, (UCHAR
*) typeName
, sizeof(typeName
), &cb
);
3517 GetData(7,SQL_C_SLONG
, (UCHAR
*)&precision
, 0, &cb
);
3518 GetData(8,SQL_C_SLONG
, (UCHAR
*)&length
, 0, &cb
);
3520 if (wxStrcmp(tblName
, tblNameSave
.c_str()))
3523 wxFputs(wxT("\n"), fp
);
3524 wxFputs(wxT("================================ "), fp
);
3525 wxFputs(wxT("================================ "), fp
);
3526 wxFputs(wxT("===================== "), fp
);
3527 wxFputs(wxT("========= "), fp
);
3528 wxFputs(wxT("=========\n"), fp
);
3529 outStr
.Printf(wxT("%-32s %-32s %-21s %9s %9s\n"),
3530 wxT("TABLE NAME"), wxT("COLUMN NAME"), wxT("DATA TYPE"), wxT("PRECISION"), wxT("LENGTH"));
3531 wxFputs(outStr
.c_str(), fp
);
3532 wxFputs(wxT("================================ "), fp
);
3533 wxFputs(wxT("================================ "), fp
);
3534 wxFputs(wxT("===================== "), fp
);
3535 wxFputs(wxT("========= "), fp
);
3536 wxFputs(wxT("=========\n"), fp
);
3537 tblNameSave
= tblName
;
3540 outStr
.Printf(wxT("%-32s %-32s (%04d)%-15s %9ld %9ld\n"),
3541 tblName
, colName
, sqlDataType
, typeName
, precision
, length
);
3542 if (wxFputs(outStr
.c_str(), fp
) == EOF
)
3544 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3551 if (retcode
!= SQL_NO_DATA_FOUND
)
3552 DispAllErrors(henv
, hdbc
, hstmt
);
3554 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3557 return(retcode
== SQL_NO_DATA_FOUND
);
3559 } // wxDb::Catalog()
3562 bool wxDb::TableExists(const wxString
&tableName
, const wxChar
*userID
, const wxString
&tablePath
)
3564 * Table name can refer to a table, view, alias or synonym. Returns true
3565 * if the object exists in the database. This function does not indicate
3566 * whether or not the user has privleges to query or perform other functions
3569 * userID is evaluated in the following manner:
3570 * userID == NULL ... UserID is ignored
3571 * userID == "" ... UserID set equal to 'this->uid'
3572 * userID != "" ... UserID set equal to 'userID'
3575 wxASSERT(tableName
.length());
3579 if (Dbms() == dbmsDBASE
)
3582 if (tablePath
.length())
3583 dbName
.Printf(wxT("%s/%s.dbf"), tablePath
.c_str(), tableName
.c_str());
3585 dbName
.Printf(wxT("%s.dbf"), tableName
.c_str());
3588 exists
= wxFileExists(dbName
);
3593 convertUserID(userID
,UserID
);
3595 TableName
= tableName
;
3596 // Oracle and Interbase table names are uppercase only, so force
3597 // the name to uppercase just in case programmer forgot to do this
3598 if ((Dbms() == dbmsORACLE
) ||
3599 (Dbms() == dbmsFIREBIRD
) ||
3600 (Dbms() == dbmsINTERBASE
))
3601 TableName
= TableName
.Upper();
3603 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3606 // Some databases cannot accept a user name when looking up table names,
3607 // so we use the call below that leaves out the user name
3608 if (!UserID
.empty() &&
3609 Dbms() != dbmsMY_SQL
&&
3610 Dbms() != dbmsACCESS
&&
3611 Dbms() != dbmsMS_SQL_SERVER
&&
3612 Dbms() != dbmsDB2
&&
3613 Dbms() != dbmsFIREBIRD
&&
3614 Dbms() != dbmsINTERBASE
&&
3615 Dbms() != dbmsPERVASIVE_SQL
)
3617 retcode
= SQLTables(hstmt
,
3618 NULL
, 0, // All qualifiers
3619 (SQLTCHAR
*) UserID
.c_str(), SQL_NTS
, // Only tables owned by this user
3620 (SQLTCHAR FAR
*)TableName
.c_str(), SQL_NTS
,
3621 NULL
, 0); // All table types
3625 retcode
= SQLTables(hstmt
,
3626 NULL
, 0, // All qualifiers
3627 NULL
, 0, // All owners
3628 (SQLTCHAR FAR
*)TableName
.c_str(), SQL_NTS
,
3629 NULL
, 0); // All table types
3631 if (retcode
!= SQL_SUCCESS
)
3632 return(DispAllErrors(henv
, hdbc
, hstmt
));
3634 retcode
= SQLFetch(hstmt
);
3635 if (retcode
!= SQL_SUCCESS
&& retcode
!= SQL_SUCCESS_WITH_INFO
)
3637 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3638 return(DispAllErrors(henv
, hdbc
, hstmt
));
3641 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3645 } // wxDb::TableExists()
3648 /********** wxDb::TablePrivileges() **********/
3649 bool wxDb::TablePrivileges(const wxString
&tableName
, const wxString
&priv
, const wxChar
*userID
,
3650 const wxChar
*schema
, const wxString
&WXUNUSED(tablePath
))
3652 wxASSERT(tableName
.length());
3654 wxDbTablePrivilegeInfo result
;
3658 // We probably need to be able to dynamically set this based on
3659 // the driver type, and state.
3660 wxChar curRole
[]=wxT("public");
3664 wxString UserID
,Schema
;
3665 convertUserID(userID
,UserID
);
3666 convertUserID(schema
,Schema
);
3668 TableName
= tableName
;
3669 // Oracle and Interbase table names are uppercase only, so force
3670 // the name to uppercase just in case programmer forgot to do this
3671 if ((Dbms() == dbmsORACLE
) ||
3672 (Dbms() == dbmsFIREBIRD
) ||
3673 (Dbms() == dbmsINTERBASE
))
3674 TableName
= TableName
.Upper();
3676 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3678 // Some databases cannot accept a user name when looking up table names,
3679 // so we use the call below that leaves out the user name
3680 if (!Schema
.empty() &&
3681 Dbms() != dbmsMY_SQL
&&
3682 Dbms() != dbmsACCESS
&&
3683 Dbms() != dbmsMS_SQL_SERVER
)
3685 retcode
= SQLTablePrivileges(hstmt
,
3687 (SQLTCHAR FAR
*)Schema
.c_str(), SQL_NTS
, // Schema
3688 (SQLTCHAR FAR
*)TableName
.c_str(), SQL_NTS
);
3692 retcode
= SQLTablePrivileges(hstmt
,
3695 (SQLTCHAR FAR
*)TableName
.c_str(), SQL_NTS
);
3698 #ifdef DBDEBUG_CONSOLE
3699 wxFprintf(stderr
,wxT("SQLTablePrivileges() returned %i \n"),retcode
);
3702 if ((retcode
!= SQL_SUCCESS
) && (retcode
!= SQL_SUCCESS_WITH_INFO
))
3703 return (DispAllErrors(henv
, hdbc
, hstmt
));
3705 bool failed
= false;
3706 retcode
= SQLFetch(hstmt
);
3707 while (retcode
== SQL_SUCCESS
|| retcode
== SQL_SUCCESS_WITH_INFO
)
3709 if (SQLGetData(hstmt
, 1, SQL_C_WXCHAR
, (UCHAR
*) result
.tableQual
, sizeof(result
.tableQual
), &cbRetVal
) != SQL_SUCCESS
)
3712 if (!failed
&& SQLGetData(hstmt
, 2, SQL_C_WXCHAR
, (UCHAR
*) result
.tableOwner
, sizeof(result
.tableOwner
), &cbRetVal
) != SQL_SUCCESS
)
3715 if (!failed
&& SQLGetData(hstmt
, 3, SQL_C_WXCHAR
, (UCHAR
*) result
.tableName
, sizeof(result
.tableName
), &cbRetVal
) != SQL_SUCCESS
)
3718 if (!failed
&& SQLGetData(hstmt
, 4, SQL_C_WXCHAR
, (UCHAR
*) result
.grantor
, sizeof(result
.grantor
), &cbRetVal
) != SQL_SUCCESS
)
3721 if (!failed
&& SQLGetData(hstmt
, 5, SQL_C_WXCHAR
, (UCHAR
*) result
.grantee
, sizeof(result
.grantee
), &cbRetVal
) != SQL_SUCCESS
)
3724 if (!failed
&& SQLGetData(hstmt
, 6, SQL_C_WXCHAR
, (UCHAR
*) result
.privilege
, sizeof(result
.privilege
), &cbRetVal
) != SQL_SUCCESS
)
3727 if (!failed
&& SQLGetData(hstmt
, 7, SQL_C_WXCHAR
, (UCHAR
*) result
.grantable
, sizeof(result
.grantable
), &cbRetVal
) != SQL_SUCCESS
)
3732 return(DispAllErrors(henv
, hdbc
, hstmt
));
3734 #ifdef DBDEBUG_CONSOLE
3735 wxFprintf(stderr
,wxT("Scanning %s privilege on table %s.%s granted by %s to %s\n"),
3736 result
.privilege
,result
.tableOwner
,result
.tableName
,
3737 result
.grantor
, result
.grantee
);
3740 if (UserID
.IsSameAs(result
.tableOwner
,false))
3742 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3746 if (UserID
.IsSameAs(result
.grantee
,false) &&
3747 !wxStrcmp(result
.privilege
,priv
))
3749 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3753 if (!wxStrcmp(result
.grantee
,curRole
) &&
3754 !wxStrcmp(result
.privilege
,priv
))
3756 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3760 retcode
= SQLFetch(hstmt
);
3763 SQLFreeStmt(hstmt
, SQL_CLOSE
);
3766 } // wxDb::TablePrivileges
3769 const wxString
wxDb::SQLTableName(const wxChar
*tableName
)
3773 if (Dbms() == dbmsACCESS
)
3774 TableName
= _T("\"");
3775 TableName
+= tableName
;
3776 if (Dbms() == dbmsACCESS
)
3777 TableName
+= _T("\"");
3780 } // wxDb::SQLTableName()
3783 const wxString
wxDb::SQLColumnName(const wxChar
*colName
)
3787 if (Dbms() == dbmsACCESS
)
3790 if (Dbms() == dbmsACCESS
)
3791 ColName
+= _T("\"");
3794 } // wxDb::SQLColumnName()
3797 /********** wxDb::SetSqlLogging() **********/
3798 bool wxDb::SetSqlLogging(wxDbSqlLogState state
, const wxString
&filename
, bool append
)
3800 wxASSERT(state
== sqlLogON
|| state
== sqlLogOFF
);
3801 wxASSERT(state
== sqlLogOFF
|| filename
.length());
3803 if (state
== sqlLogON
)
3807 fpSqlLog
= wxFopen(filename
.c_str(), (append
? wxT("at") : wxT("wt")));
3808 if (fpSqlLog
== NULL
)
3816 if (fclose(fpSqlLog
))
3822 sqlLogState
= state
;
3825 } // wxDb::SetSqlLogging()
3828 /********** wxDb::WriteSqlLog() **********/
3829 bool wxDb::WriteSqlLog(const wxString
&logMsg
)
3831 wxASSERT(logMsg
.length());
3833 if (fpSqlLog
== 0 || sqlLogState
== sqlLogOFF
)
3836 if (wxFputs(wxT("\n"), fpSqlLog
) == EOF
)
3838 if (wxFputs(logMsg
, fpSqlLog
) == EOF
)
3840 if (wxFputs(wxT("\n"), fpSqlLog
) == EOF
)
3845 } // wxDb::WriteSqlLog()
3848 /********** wxDb::Dbms() **********/
3849 wxDBMS
wxDb::Dbms(void)
3851 * Be aware that not all database engines use the exact same syntax, and not
3852 * every ODBC compliant database is compliant to the same level of compliancy.
3853 * Some manufacturers support the minimum Level 1 compliancy, and others up
3854 * through Level 3. Others support subsets of features for levels above 1.
3856 * If you find an inconsistency between the wxDb class and a specific database
3857 * engine, and an identifier to this section, and special handle the database in
3858 * the area where behavior is non-conforming with the other databases.
3861 * NOTES ABOUT ISSUES SPECIFIC TO EACH DATABASE ENGINE
3862 * ---------------------------------------------------
3865 * - Currently the only database supported by the class to support VIEWS
3868 * - Does not support the SQL_TIMESTAMP structure
3869 * - Supports only one cursor and one connect (apparently? with Microsoft driver only?)
3870 * - Does not automatically create the primary index if the 'keyField' param of SetColDef
3871 * is true. The user must create ALL indexes from their program.
3872 * - Table names can only be 8 characters long
3873 * - Column names can only be 10 characters long
3876 * - To lock a record during QUERY functions, the reserved word 'HOLDLOCK' must be added
3877 * after every table name involved in the query/join if that tables matching record(s)
3879 * - Ignores the keywords 'FOR UPDATE'. Use the HOLDLOCK functionality described above
3881 * SYBASE (Enterprise)
3882 * - If a column is part of the Primary Key, the column cannot be NULL
3883 * - Maximum row size is somewhere in the neighborhood of 1920 bytes
3886 * - If a column is part of the Primary Key, the column cannot be NULL
3887 * - Cannot support selecting for update [::CanSelectForUpdate()]. Always returns FALSE
3888 * - Columns that are part of primary or secondary keys must be defined as being NOT NULL
3889 * when they are created. Some code is added in ::CreateIndex to try to adjust the
3890 * column definition if it is not defined correctly, but it is experimental
3891 * - Does not support sub-queries in SQL statements
3894 * - Does not support the keywords 'ASC' or 'DESC' as of release v6.5.0
3895 * - Does not support sub-queries in SQL statements
3898 * - Primary keys must be declared as NOT NULL
3899 * - Table and index names must not be longer than 13 characters in length (technically
3900 * table names can be up to 18 characters, but the primary index is created using the
3901 * base table name plus "_PIDX", so the limit if the table has a primary index is 13.
3906 * - Columns that are part of primary keys must be defined as being NOT NULL
3907 * when they are created. Some code is added in ::CreateIndex to try to adjust the
3908 * column definition if it is not defined correctly, but it is experimental
3911 // Should only need to do this once for each new database connection
3912 // so return the value we already determined it to be to save time
3913 // and lots of string comparisons
3914 if (dbmsType
!= dbmsUNIDENTIFIED
)
3917 #ifdef DBDEBUG_CONSOLE
3918 // When run in console mode, use standard out to display errors.
3919 cout
<< "Database connecting to: " << dbInf
.dbmsName
<< endl
;
3920 #endif // DBDEBUG_CONSOLE
3922 wxLogDebug(wxT("Database connecting to: "));
3923 wxLogDebug(dbInf
.dbmsName
);
3925 wxChar baseName
[25+1];
3926 wxStrncpy(baseName
, dbInf
.dbmsName
, 25);
3929 // RGG 20001025 : add support for Interbase
3930 // GT : Integrated to base classes on 20001121
3931 if (!wxStricmp(dbInf
.dbmsName
,wxT("Interbase")))
3932 return((wxDBMS
)(dbmsType
= dbmsINTERBASE
));
3934 // BJO 20000428 : add support for Virtuoso
3935 if (!wxStricmp(dbInf
.dbmsName
,wxT("OpenLink Virtuoso VDBMS")))
3936 return((wxDBMS
)(dbmsType
= dbmsVIRTUOSO
));
3938 if (!wxStricmp(dbInf
.dbmsName
,wxT("Adaptive Server Anywhere")))
3939 return((wxDBMS
)(dbmsType
= dbmsSYBASE_ASA
));
3941 // BJO 20000427 : The "SQL Server" string is also returned by SQLServer when
3942 // connected through an OpenLink driver.
3943 // Is it also returned by Sybase Adapatitve server?
3944 // OpenLink driver name is OLOD3032.DLL for msw and oplodbc.so for unix
3945 if (!wxStricmp(dbInf
.dbmsName
,wxT("SQL Server")))
3947 if (!wxStrncmp(dbInf
.driverName
, wxT("oplodbc"), 7) ||
3948 !wxStrncmp(dbInf
.driverName
, wxT("OLOD"), 4))
3949 return ((wxDBMS
)(dbmsMS_SQL_SERVER
));
3951 return ((wxDBMS
)(dbmsType
= dbmsSYBASE_ASE
));
3954 if (!wxStricmp(dbInf
.dbmsName
,wxT("Microsoft SQL Server")))
3955 return((wxDBMS
)(dbmsType
= dbmsMS_SQL_SERVER
));
3958 if (!wxStricmp(baseName
,wxT("PostgreSQL"))) // v6.5.0
3959 return((wxDBMS
)(dbmsType
= dbmsPOSTGRES
));
3962 if (!wxStricmp(baseName
,wxT("Pervasive")))
3963 return((wxDBMS
)(dbmsType
= dbmsPERVASIVE_SQL
));
3966 if (!wxStricmp(baseName
,wxT("Informix")))
3967 return((wxDBMS
)(dbmsType
= dbmsINFORMIX
));
3969 if (!wxStricmp(baseName
,wxT("Firebird")))
3970 return((wxDBMS
)(dbmsType
= dbmsFIREBIRD
));
3973 if (!wxStricmp(baseName
,wxT("Oracle")))
3974 return((wxDBMS
)(dbmsType
= dbmsORACLE
));
3975 if (!wxStricmp(baseName
,wxT("ACCESS")))
3976 return((wxDBMS
)(dbmsType
= dbmsACCESS
));
3977 if (!wxStricmp(baseName
,wxT("Sybase")))
3978 return((wxDBMS
)(dbmsType
= dbmsSYBASE_ASE
));
3981 if (!wxStricmp(baseName
,wxT("DBASE")))
3982 return((wxDBMS
)(dbmsType
= dbmsDBASE
));
3983 if (!wxStricmp(baseName
,wxT("xBase")))
3984 return((wxDBMS
)(dbmsType
= dbmsXBASE_SEQUITER
));
3985 if (!wxStricmp(baseName
,wxT("MySQL")))
3986 return((wxDBMS
)(dbmsType
= dbmsMY_SQL
));
3987 if (!wxStricmp(baseName
,wxT("MaxDB")))
3988 return((wxDBMS
)(dbmsType
= dbmsMAXDB
));
3991 if (!wxStricmp(baseName
,wxT("DB2")))
3992 return((wxDBMS
)(dbmsType
= dbmsDB2
));
3994 return((wxDBMS
)(dbmsType
= dbmsUNIDENTIFIED
));
3999 bool wxDb::ModifyColumn(const wxString
&tableName
, const wxString
&columnName
,
4000 int dataType
, ULONG columnLength
,
4001 const wxString
&optionalParam
)
4003 wxASSERT(tableName
.length());
4004 wxASSERT(columnName
.length());
4005 wxASSERT((dataType
== DB_DATA_TYPE_VARCHAR
&& columnLength
> 0) ||
4006 dataType
!= DB_DATA_TYPE_VARCHAR
);
4008 // Must specify a columnLength if modifying a VARCHAR type column
4009 if (dataType
== DB_DATA_TYPE_VARCHAR
&& !columnLength
)
4012 wxString dataTypeName
;
4014 wxString alterSlashModify
;
4018 case DB_DATA_TYPE_VARCHAR
:
4019 dataTypeName
= typeInfVarchar
.TypeName
;
4021 case DB_DATA_TYPE_INTEGER
:
4022 dataTypeName
= typeInfInteger
.TypeName
;
4024 case DB_DATA_TYPE_FLOAT
:
4025 dataTypeName
= typeInfFloat
.TypeName
;
4027 case DB_DATA_TYPE_DATE
:
4028 dataTypeName
= typeInfDate
.TypeName
;
4030 case DB_DATA_TYPE_BLOB
:
4031 dataTypeName
= typeInfBlob
.TypeName
;
4037 // Set the modify or alter syntax depending on the type of database connected to
4041 alterSlashModify
= _T("MODIFY");
4043 case dbmsMS_SQL_SERVER
:
4044 alterSlashModify
= _T("ALTER COLUMN");
4046 case dbmsUNIDENTIFIED
:
4048 case dbmsSYBASE_ASA
:
4049 case dbmsSYBASE_ASE
:
4054 case dbmsXBASE_SEQUITER
:
4056 alterSlashModify
= _T("MODIFY");
4060 // create the SQL statement
4061 if ( Dbms() == dbmsMY_SQL
)
4063 sqlStmt
.Printf(wxT("ALTER TABLE %s %s %s %s"), tableName
.c_str(), alterSlashModify
.c_str(),
4064 columnName
.c_str(), dataTypeName
.c_str());
4068 sqlStmt
.Printf(wxT("ALTER TABLE \"%s\" \"%s\" \"%s\" %s"), tableName
.c_str(), alterSlashModify
.c_str(),
4069 columnName
.c_str(), dataTypeName
.c_str());
4072 // For varchars only, append the size of the column
4073 if (dataType
== DB_DATA_TYPE_VARCHAR
&&
4074 (Dbms() != dbmsMY_SQL
|| dataTypeName
!= _T("text")))
4077 s
.Printf(wxT("(%lu)"), columnLength
);
4081 // for passing things like "NOT NULL"
4082 if (optionalParam
.length())
4084 sqlStmt
+= wxT(" ");
4085 sqlStmt
+= optionalParam
;
4088 return ExecSql(sqlStmt
);
4090 } // wxDb::ModifyColumn()
4092 /********** wxDb::EscapeSqlChars() **********/
4093 wxString
wxDb::EscapeSqlChars(const wxString
& valueOrig
)
4095 wxString
value(valueOrig
);
4099 // Access doesn't seem to care about backslashes, so only escape single quotes.
4100 value
.Replace(wxT("'"), wxT("''"));
4104 // All the others are supposed to be the same for now, add special
4105 // handling for them if necessary
4106 value
.Replace(wxT("\\"), wxT("\\\\"));
4107 value
.Replace(wxT("'"), wxT("\\'"));
4112 } // wxDb::EscapeSqlChars()
4115 /********** wxDbGetConnection() **********/
4116 wxDb WXDLLIMPEXP_ODBC
*wxDbGetConnection(wxDbConnectInf
*pDbConfig
, bool FwdOnlyCursors
)
4120 // Used to keep a pointer to a DB connection that matches the requested
4121 // DSN and FwdOnlyCursors settings, even if it is not FREE, so that the
4122 // data types can be copied from it (using the wxDb::Open(wxDb *) function)
4123 // rather than having to re-query the datasource to get all the values
4124 // using the wxDb::Open(Dsn,Uid,AuthStr) function
4125 wxDb
*matchingDbConnection
= NULL
;
4127 // Scan the linked list searching for an available database connection
4128 // that's already been opened but is currently not in use.
4129 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
4131 // The database connection must be for the same datasource
4132 // name and must currently not be in use.
4134 (pList
->PtrDb
->FwdOnlyCursors() == FwdOnlyCursors
))
4136 if (pDbConfig
->UseConnectionStr())
4138 if (pList
->PtrDb
->OpenedWithConnectionString() &&
4139 (!wxStrcmp(pDbConfig
->GetConnectionStr(), pList
->ConnectionStr
)))
4141 // Found a free connection
4142 pList
->Free
= false;
4143 return(pList
->PtrDb
);
4148 if (!pList
->PtrDb
->OpenedWithConnectionString() &&
4149 (!wxStrcmp(pDbConfig
->GetDsn(), pList
->Dsn
)))
4151 // Found a free connection
4152 pList
->Free
= false;
4153 return(pList
->PtrDb
);
4158 if (pDbConfig
->UseConnectionStr())
4160 if (!wxStrcmp(pDbConfig
->GetConnectionStr(), pList
->ConnectionStr
))
4161 matchingDbConnection
= pList
->PtrDb
;
4165 if (!wxStrcmp(pDbConfig
->GetDsn(), pList
->Dsn
) &&
4166 !wxStrcmp(pDbConfig
->GetUserID(), pList
->Uid
) &&
4167 !wxStrcmp(pDbConfig
->GetPassword(), pList
->AuthStr
))
4168 matchingDbConnection
= pList
->PtrDb
;
4172 // No available connections. A new connection must be made and
4173 // appended to the end of the linked list.
4176 // Find the end of the list
4177 for (pList
= PtrBegDbList
; pList
->PtrNext
; pList
= pList
->PtrNext
);
4178 // Append a new list item
4179 pList
->PtrNext
= new wxDbList
;
4180 pList
->PtrNext
->PtrPrev
= pList
;
4181 pList
= pList
->PtrNext
;
4185 // Create the first node on the list
4186 pList
= PtrBegDbList
= new wxDbList
;
4190 // Initialize new node in the linked list
4192 pList
->Free
= false;
4193 pList
->Dsn
= pDbConfig
->GetDsn();
4194 pList
->Uid
= pDbConfig
->GetUserID();
4195 pList
->AuthStr
= pDbConfig
->GetPassword();
4196 pList
->ConnectionStr
= pDbConfig
->GetConnectionStr();
4198 pList
->PtrDb
= new wxDb(pDbConfig
->GetHenv(), FwdOnlyCursors
);
4202 if (!matchingDbConnection
)
4204 if (pDbConfig
->UseConnectionStr())
4206 opened
= pList
->PtrDb
->Open(pDbConfig
->GetConnectionStr());
4210 opened
= pList
->PtrDb
->Open(pDbConfig
->GetDsn(), pDbConfig
->GetUserID(), pDbConfig
->GetPassword());
4214 opened
= pList
->PtrDb
->Open(matchingDbConnection
);
4216 // Connect to the datasource
4219 pList
->PtrDb
->setCached(true); // Prevent a user from deleting a cached connection
4220 pList
->PtrDb
->SetSqlLogging(SQLLOGstate
, SQLLOGfn
, true);
4221 return(pList
->PtrDb
);
4223 else // Unable to connect, destroy list item
4226 pList
->PtrPrev
->PtrNext
= 0;
4228 PtrBegDbList
= 0; // Empty list again
4230 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDb object
4231 pList
->PtrDb
->Close(); // Close the wxDb object
4232 delete pList
->PtrDb
; // Deletes the wxDb object
4233 delete pList
; // Deletes the linked list object
4237 } // wxDbGetConnection()
4240 /********** wxDbFreeConnection() **********/
4241 bool WXDLLIMPEXP_ODBC
wxDbFreeConnection(wxDb
*pDb
)
4245 // Scan the linked list searching for the database connection
4246 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
4248 if (pList
->PtrDb
== pDb
) // Found it, now free it!!!
4249 return (pList
->Free
= true);
4252 // Never found the database object, return failure
4255 } // wxDbFreeConnection()
4258 /********** wxDbCloseConnections() **********/
4259 void WXDLLIMPEXP_ODBC
wxDbCloseConnections(void)
4261 wxDbList
*pList
, *pNext
;
4263 // Traverse the linked list closing database connections and freeing memory as I go.
4264 for (pList
= PtrBegDbList
; pList
; pList
= pNext
)
4266 pNext
= pList
->PtrNext
; // Save the pointer to next
4267 pList
->PtrDb
->CommitTrans(); // Commit any open transactions on wxDb object
4268 pList
->PtrDb
->Close(); // Close the wxDb object
4269 pList
->PtrDb
->setCached(false); // Allows deletion of the wxDb instance
4270 delete pList
->PtrDb
; // Deletes the wxDb object
4271 delete pList
; // Deletes the linked list object
4274 // Mark the list as empty
4277 } // wxDbCloseConnections()
4280 /********** wxDbConnectionsInUse() **********/
4281 int WXDLLIMPEXP_ODBC
wxDbConnectionsInUse(void)
4286 // Scan the linked list counting db connections that are currently in use
4287 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
4289 if (pList
->Free
== false)
4295 } // wxDbConnectionsInUse()
4299 /********** wxDbLogExtendedErrorMsg() **********/
4300 // DEBUG ONLY function
4301 const wxChar WXDLLIMPEXP_ODBC
*wxDbLogExtendedErrorMsg(const wxChar
*userText
,
4303 const wxChar
*ErrFile
,
4306 static wxString msg
;
4311 if (ErrFile
|| ErrLine
)
4313 msg
+= wxT("File: ");
4315 msg
+= wxT(" Line: ");
4316 tStr
.Printf(wxT("%d"),ErrLine
);
4317 msg
+= tStr
.c_str();
4321 msg
.Append (wxT("\nODBC errors:\n"));
4324 // Display errors for this connection
4326 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
4328 if (pDb
->errorList
[i
])
4330 msg
.Append(pDb
->errorList
[i
]);
4331 if (wxStrcmp(pDb
->errorList
[i
], wxEmptyString
) != 0)
4332 msg
.Append(wxT("\n"));
4333 // Clear the errmsg buffer so the next error will not
4334 // end up showing the previous error that have occurred
4335 wxStrcpy(pDb
->errorList
[i
], wxEmptyString
);
4340 wxLogDebug(msg
.c_str());
4343 } // wxDbLogExtendedErrorMsg()
4346 /********** wxDbSqlLog() **********/
4347 bool wxDbSqlLog(wxDbSqlLogState state
, const wxChar
*filename
)
4349 bool append
= false;
4352 for (pList
= PtrBegDbList
; pList
; pList
= pList
->PtrNext
)
4354 if (!pList
->PtrDb
->SetSqlLogging(state
,filename
,append
))
4359 SQLLOGstate
= state
;
4360 SQLLOGfn
= filename
;
4368 /********** wxDbCreateDataSource() **********/
4369 int wxDbCreateDataSource(const wxString
&driverName
, const wxString
&dsn
, const wxString
&description
,
4370 bool sysDSN
, const wxString
&defDir
, wxWindow
*parent
)
4372 * !!!! ONLY FUNCTIONAL UNDER MSW with VC6 !!!!
4373 * Very rudimentary creation of an ODBC data source.
4375 * ODBC driver must be ODBC 3.0 compliant to use this function
4380 //!!!! ONLY FUNCTIONAL UNDER MSW with VC6 !!!!
4386 dsnLocation
= ODBC_ADD_SYS_DSN
;
4388 dsnLocation
= ODBC_ADD_DSN
;
4390 // NOTE: The decimal 2 is an invalid character in all keyword pairs
4391 // so that is why I used it, as wxString does not deal well with
4392 // embedded nulls in strings
4393 setupStr
.Printf(wxT("DSN=%s%cDescription=%s%cDefaultDir=%s%c"),dsn
,2,description
,2,defDir
,2);
4395 // Replace the separator from above with the '\0' separator needed
4396 // by the SQLConfigDataSource() function
4400 k
= setupStr
.Find((wxChar
)2,true);
4401 if (k
!= wxNOT_FOUND
)
4402 setupStr
[(UINT
)k
] = wxT('\0');
4404 while (k
!= wxNOT_FOUND
);
4406 result
= SQLConfigDataSource((HWND
)parent
->GetHWND(), dsnLocation
,
4407 driverName
, setupStr
.c_str());
4409 if ((result
!= SQL_SUCCESS
) && (result
!= SQL_SUCCESS_WITH_INFO
))
4411 // check for errors caused by ConfigDSN based functions
4414 wxChar errMsg
[SQL_MAX_MESSAGE_LENGTH
];
4415 errMsg
[0] = wxT('\0');
4417 // This function is only supported in ODBC drivers v3.0 compliant and above
4418 SQLInstallerError(1,&retcode
,errMsg
,SQL_MAX_MESSAGE_LENGTH
-1,&cb
);
4421 #ifdef DBDEBUG_CONSOLE
4422 // When run in console mode, use standard out to display errors.
4423 cout
<< errMsg
<< endl
;
4424 cout
<< wxT("Press any key to continue...") << endl
;
4426 #endif // DBDEBUG_CONSOLE
4429 wxLogDebug(errMsg
,wxT("ODBC DEBUG MESSAGE"));
4430 #endif // __WXDEBUG__
4436 // Using iODBC/unixODBC or some other compiler which does not support the APIs
4437 // necessary to use this function, so this function is not supported
4439 wxLogDebug(wxT("wxDbCreateDataSource() not available except under VC++/MSW"),wxT("ODBC DEBUG MESSAGE"));
4442 #endif // __VISUALC__
4446 } // wxDbCreateDataSource()
4450 /********** wxDbGetDataSource() **********/
4451 bool wxDbGetDataSource(HENV henv
, wxChar
*Dsn
, SWORD DsnMaxLength
, wxChar
*DsDesc
,
4452 SWORD DsDescMaxLength
, UWORD direction
)
4454 * Dsn and DsDesc will contain the data source name and data source
4455 * description upon return
4459 SWORD lengthDsn
= (SWORD
)(DsnMaxLength
*sizeof(wxChar
));
4460 SWORD lengthDsDesc
= (SWORD
)(DsDescMaxLength
*sizeof(wxChar
));
4462 if (SQLDataSources(henv
, direction
, (SQLTCHAR FAR
*) Dsn
, lengthDsn
, &cb1
,
4463 (SQLTCHAR FAR
*) DsDesc
, lengthDsDesc
, &cb2
) == SQL_SUCCESS
)
4468 } // wxDbGetDataSource()
4471 // Change this to 0 to remove use of all deprecated functions
4472 #if wxODBC_BACKWARD_COMPATABILITY
4473 /********************************************************************
4474 ********************************************************************
4476 * The following functions are all DEPRECATED and are included for
4477 * backward compatibility reasons only
4479 ********************************************************************
4480 ********************************************************************/
4481 bool SqlLog(sqlLog state
, const wxChar
*filename
)
4483 return wxDbSqlLog((enum wxDbSqlLogState
)state
, filename
);
4485 /***** DEPRECATED: use wxGetDataSource() *****/
4486 bool GetDataSource(HENV henv
, char *Dsn
, SWORD DsnMax
, char *DsDesc
, SWORD DsDescMax
,
4489 return wxDbGetDataSource(henv
, Dsn
, DsnMax
, DsDesc
, DsDescMax
, direction
);
4491 /***** DEPRECATED: use wxDbGetConnection() *****/
4492 wxDb WXDLLIMPEXP_ODBC
*GetDbConnection(DbStuff
*pDbStuff
, bool FwdOnlyCursors
)
4494 return wxDbGetConnection((wxDbConnectInf
*)pDbStuff
, FwdOnlyCursors
);
4496 /***** DEPRECATED: use wxDbFreeConnection() *****/
4497 bool WXDLLIMPEXP_ODBC
FreeDbConnection(wxDb
*pDb
)
4499 return wxDbFreeConnection(pDb
);
4501 /***** DEPRECATED: use wxDbCloseConnections() *****/
4502 void WXDLLIMPEXP_ODBC
CloseDbConnections(void)
4504 wxDbCloseConnections();
4506 /***** DEPRECATED: use wxDbConnectionsInUse() *****/
4507 int WXDLLIMPEXP_ODBC
NumberDbConnectionsInUse(void)
4509 return wxDbConnectionsInUse();