From fdc0367851c7a1179d549f087317d0fba2714a97 Mon Sep 17 00:00:00 2001 From: George Tasker Date: Thu, 16 Mar 2000 19:00:24 +0000 Subject: [PATCH] Cleaned up the API for class/structure/function names to follow the wxWindows conventions (mostly prefixing wx to all of them). Backward compatability is maintained by keeping the old names calling the new functions, or typedef-ing the old names to the new names. The old names can be totally disabled at the end of the file by changing the #if 1 block that surrounds the old names Added a wxDbCreateDataSource() function, though it only works under MSW using VC6, because the API is not available with iODBC. To use this, it requires linking in odbccp32.lib and #including odbcinst.h in db.h. This function is disabled with #if 0 for now. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6767 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/db.h | 108 +++++++++++++++--------- src/common/db.cpp | 206 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 235 insertions(+), 79 deletions(-) diff --git a/include/wx/db.h b/include/wx/db.h index f57cb6a841..d7e73abc11 100644 --- a/include/wx/db.h +++ b/include/wx/db.h @@ -55,6 +55,12 @@ extern "C" { #include "wx/isql.h" #include "wx/isqlext.h" +// If you use the wxCreateDataSource() function with MSW/VC6, +// you cannot use the iODBC headers, you must use the VC headers, +// plus the odbcinst.h header + //#include "sql.h" + //#include "sqlext.h" + //#include "odbcinst.h" } #else // version == 1 extern "C" { @@ -129,7 +135,7 @@ const int DB_GRANT_DELETE = 8; const int DB_GRANT_ALL = DB_GRANT_SELECT | DB_GRANT_INSERT | DB_GRANT_UPDATE | DB_GRANT_DELETE; // ODBC Error codes (derived from ODBC SqlState codes) -enum ODBC_ERRORS +enum wxODBC_ERRORS { DB_FAILURE = 0, DB_SUCCESS = 1, @@ -226,7 +232,7 @@ enum ODBC_ERRORS }; -struct DbStuff +struct wxDbConnectInf { HENV Henv; char Dsn[SQL_MAX_DSN_LENGTH+1]; // Data Source Name @@ -240,8 +246,7 @@ struct DbStuff char defaultDir[DB_PATH_MAX]; // Directory that db file resides in }; - -typedef struct +struct wxSqlTypeInfo { char TypeName[DB_TYPE_NAME_LEN]; int FsqlType; @@ -249,7 +254,7 @@ typedef struct short CaseSensitive; // short MinimumScale; short MaximumScale; -} SqlTypeInfo; +}; class WXDLLEXPORT wxColFor @@ -316,17 +321,16 @@ public: }; -enum sqlLog +enum wxSqlLogState { sqlLogOFF, sqlLogON }; - // These are the databases currently tested and working with these classes // See the comments in wxDB::Dbms() for exceptions/issues with // each of these database engines -enum dbms +enum wxDBMS { dbmsUNIDENTIFIED, dbmsORACLE, @@ -341,9 +345,6 @@ enum dbms }; -typedef enum dbms DBMS; - - // The wxDB::errorList is copied to this variable when the wxDB object // is closed. This way, the error list is still available after the // database object is closed. This is necessary if the database @@ -354,28 +355,20 @@ typedef enum dbms DBMS; extern char DBerrorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN]; -// Backward compability for Remstar classes. This -// will eventually go away, so the wxColXxxx classes -// should be used -typedef wxColInf CcolInf; - - class WXDLLEXPORT wxDB { private: - - // Private data bool dbIsOpen; char *dsn; // Data source name char *uid; // User ID char *authStr; // Authorization string (password) FILE *fpSqlLog; // Sql Log file pointer - enum sqlLog sqlLogState; // On or Off + wxSqlLogState sqlLogState; // On or Off bool fwdOnlyCursors; // Private member functions bool getDbInfo(void); - bool getDataTypeInfo(SWORD fSqlType, SqlTypeInfo &structSQLTypeInfo); + bool getDataTypeInfo(SWORD fSqlType, wxSqlTypeInfo &structSQLTypeInfo); bool setConnectionOptions(void); void logError(const char *errMsg, const char *SQLState); @@ -440,7 +433,7 @@ public: // SQLGetTypeInfo() function. The key piece of inf. is the // type name the data source uses for each logical data type. // e.g. VARCHAR; Oracle calls it VARCHAR2. - SqlTypeInfo typeInfVarchar, typeInfInteger, typeInfFloat, typeInfDate; + wxSqlTypeInfo typeInfVarchar, typeInfInteger, typeInfFloat, typeInfDate; // Public member functions wxDB(HENV &aHenv, bool FwdOnlyCursors=(bool)TRUE); @@ -474,57 +467,92 @@ public: HSTMT GetHSTMT(void) {return hstmt;} bool TableExists(const char *tableName, const char *userID=NULL, const char *path=NULL); // Table name can refer to a table, view, alias or synonym void LogError(const char *errMsg, const char *SQLState = 0) {logError(errMsg, SQLState);} - bool SqlLog(enum sqlLog state, const char *filename = SQL_LOG_FILENAME, bool append = FALSE); + bool SqlLog(wxSqlLogState state, const char *filename = SQL_LOG_FILENAME, bool append = FALSE); bool WriteSqlLog(const char *logMsg); - DBMS Dbms(void); + wxDBMS Dbms(void); bool FwdOnlyCursors(void) {return fwdOnlyCursors;} }; // wxDB + // This structure forms a node in a linked list. The linked list of "DbList" objects // keeps track of allocated database connections. This allows the application to // open more than one database connection through ODBC for multiple transaction support // or for multiple database support. - -struct DbList +struct wxDbList { - DbList *PtrPrev; // Pointer to previous item in the list - char Dsn[SQL_MAX_DSN_LENGTH+1]; // Data Source Name - wxDB *PtrDb; // Pointer to the wxDB object - bool Free; // Is item free or in use? - DbList *PtrNext; // Pointer to next item in the list + wxDbList *PtrPrev; // Pointer to previous item in the list + char Dsn[SQL_MAX_DSN_LENGTH+1]; // Data Source Name + wxDB *PtrDb; // Pointer to the wxDB object + bool Free; // Is item free or in use? + wxDbList *PtrNext; // Pointer to next item in the list }; - #ifdef __WXDEBUG__ #include "wx/object.h" -class CstructTablesInUse : public wxObject +class wxTablesInUse : public wxObject { public: const char *tableName; ULONG tableID; class wxDB *pDb; -}; // CstructTablesInUse +}; // wxTablesInUse #endif + // The following routines allow a user to get new database connections, free them // for other code segments to use, or close all of them when the application has // completed. - -wxDB WXDLLEXPORT *GetDbConnection(DbStuff *pDbStuff, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS); -bool WXDLLEXPORT FreeDbConnection(wxDB *pDb); -void WXDLLEXPORT CloseDbConnections(void); -int WXDLLEXPORT NumberDbConnectionsInUse(void); +wxDB WXDLLEXPORT *wxDbGetConnection(wxDbConnectInf *pDbConfig, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS); +bool WXDLLEXPORT wxDbFreeConnection(wxDB *pDb); +void WXDLLEXPORT wxDbCloseConnections(void); +int WXDLLEXPORT wxDbNumberConnectionsInUse(void); // This function sets the sql log state for all open wxDB objects -bool SqlLog(enum sqlLog state, const char *filename = SQL_LOG_FILENAME); +bool wxDbSqlLog(wxSqlLogState state, const char *filename = SQL_LOG_FILENAME); + +#if 0 +// MSW/VC6 ONLY!!! Experimental +int WXDLLEXPORT wxDbCreateDataSource(const char *driverName, const char *dsn, const char *description="", + bool sysDSN=FALSE, const char *defDir="", wxWindow *parent=NULL); +#endif // This routine allows you to query a driver manager // for a list of available datasources. Call this routine // the first time using SQL_FETCH_FIRST. Continue to call it // using SQL_FETCH_NEXT until you've exhausted the list. +bool WXDLLEXPORT wxDbGetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDescMax, + UWORD direction = SQL_FETCH_NEXT); + + +// Change this to 0 to remove use of all deprecated functions +#if 1 +//################################################################################# +//############### DEPRECATED functions for backward compatability ################# +//################################################################################# + +// Backward compability structures/classes. This will eventually go away +typedef wxColInf CcolInf; +typedef wxSqlTypeInfo SqlTypeInfo; +typedef enum wxSqlLogState sqlLog; +typedef enum wxDBMS dbms; +typedef enum wxDBMS DBMS; +typedef wxODBC_ERRORS ODBC_ERRORS; +typedef wxDbConnectInf DbStuff; +typedef wxDbList DbList; +typedef wxTablesInUse CstructTablesInUse; + +// Deprecated function names that are replaced by the function names listed above +wxDB WXDLLEXPORT *GetDbConnection(DbStuff *pDbStuff, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS); +bool WXDLLEXPORT FreeDbConnection(wxDB *pDb); +void WXDLLEXPORT CloseDbConnections(void); +int WXDLLEXPORT NumberDbConnectionsInUse(void); + +bool SqlLog(sqlLog state, const char *filename = SQL_LOG_FILENAME); + bool WXDLLEXPORT GetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDescMax, UWORD direction = SQL_FETCH_NEXT); +#endif // Deprecated structures/classes/functions #endif diff --git a/src/common/db.cpp b/src/common/db.cpp index 12f614d4c5..7f3c69ece9 100644 --- a/src/common/db.cpp +++ b/src/common/db.cpp @@ -92,7 +92,7 @@ #include "wx/db.h" #endif -DbList WXDLLEXPORT *PtrBegDbList = 0; +wxDbList WXDLLEXPORT *PtrBegDbList = 0; char const *SQL_LOG_FILENAME = "sqllog.txt"; char const *SQL_CATALOG_FILENAME = "catalog.txt"; @@ -102,7 +102,7 @@ char const *SQL_CATALOG_FILENAME = "catalog.txt"; #endif // SQL Log defaults to be used by GetDbConnection -enum sqlLog SQLLOGstate = sqlLogOFF; +wxSqlLogState SQLLOGstate = sqlLogOFF; //char SQLLOGfn[DB_PATH_MAX+1] = SQL_LOG_FILENAME; char *SQLLOGfn = (char*) SQL_LOG_FILENAME; @@ -795,13 +795,13 @@ bool wxDB::getDbInfo(void) /********** wxDB::getDataTypeInfo() **********/ -bool wxDB::getDataTypeInfo(SWORD fSqlType, SqlTypeInfo &structSQLTypeInfo) +bool wxDB::getDataTypeInfo(SWORD fSqlType, wxSqlTypeInfo &structSQLTypeInfo) { /* * fSqlType will be something like SQL_VARCHAR. This parameter determines * the data type inf. is gathered for. * - * SqlTypeInfo is a structure that is filled in with data type information, + * wxSqlTypeInfo is a structure that is filled in with data type information, */ RETCODE retcode; SDWORD cbRet; @@ -887,13 +887,13 @@ void wxDB::Close(void) assert(nTables == 0); #ifdef __WXDEBUG__ - CstructTablesInUse *tiu; + wxTablesInUse *tiu; wxNode *pNode; pNode = TablesInUse.First(); wxString s,s2; while (pNode) { - tiu = (CstructTablesInUse *)pNode->Data(); + tiu = (wxTablesInUse *)pNode->Data(); if (tiu->pDb == this) { s.sprintf("(%-20s) tableID:[%6lu] pDb:[%p]", tiu->tableName,tiu->tableID,tiu->pDb); @@ -2363,7 +2363,7 @@ bool wxDB::TableExists(const char *tableName, const char *userID, const char *ta /********** wxDB::SqlLog() **********/ -bool wxDB::SqlLog(enum sqlLog state, const char *filename, bool append) +bool wxDB::SqlLog(wxSqlLogState state, const char *filename, bool append) { assert(state == sqlLogON || state == sqlLogOFF); assert(state == sqlLogOFF || filename); @@ -2411,7 +2411,7 @@ bool wxDB::WriteSqlLog(const char *logMsg) /********** wxDB::Dbms() **********/ -DBMS wxDB::Dbms(void) +wxDBMS wxDB::Dbms(void) /* * Be aware that not all database engines use the exact same syntax, and not * every ODBC compliant database is compliant to the same level of compliancy. @@ -2432,8 +2432,8 @@ DBMS wxDB::Dbms(void) * DBASE * - Does not support the SQL_TIMESTAMP structure * - Supports only one cursor and one connect (apparently? with Microsoft driver only?) - * - Does not automatically create the primary index if the 'keyField' param of SetColDef - * is TRUE. The user must create ALL indexes from their program. + * - Does not automatically create the primary index if the 'keyField' param of SetColDef + * is TRUE. The user must create ALL indexes from their program. * - Table names can only be 8 characters long * - Column names can only be 10 characters long * @@ -2490,10 +2490,10 @@ DBMS wxDB::Dbms(void) } // wxDB::Dbms() -/********** GetDbConnection() **********/ -wxDB WXDLLEXPORT *GetDbConnection(DbStuff *pDbStuff, bool FwdOnlyCursors) +/********** wxDbGetConnection() **********/ +wxDB WXDLLEXPORT *wxDbGetConnection(wxDbConnectInf *pDbConfig, bool FwdOnlyCursors) { - DbList *pList; + wxDbList *pList; // Scan the linked list searching for an available database connection // that's already been opened but is currently not in use. @@ -2501,7 +2501,7 @@ wxDB WXDLLEXPORT *GetDbConnection(DbStuff *pDbStuff, bool FwdOnlyCursors) { // The database connection must be for the same datasource // name and must currently not be in use. - if (pList->Free && (! wxStrcmp(pDbStuff->Dsn, pList->Dsn))) // Found a free connection + if (pList->Free && (! wxStrcmp(pDbConfig->Dsn, pList->Dsn))) // Found a free connection { pList->Free = FALSE; return(pList->PtrDb); @@ -2515,25 +2515,25 @@ wxDB WXDLLEXPORT *GetDbConnection(DbStuff *pDbStuff, bool FwdOnlyCursors) // Find the end of the list for (pList = PtrBegDbList; pList->PtrNext; pList = pList->PtrNext); // Append a new list item - pList->PtrNext = new DbList; + pList->PtrNext = new wxDbList; pList->PtrNext->PtrPrev = pList; pList = pList->PtrNext; } else // Empty list { // Create the first node on the list - pList = PtrBegDbList = new DbList; + pList = PtrBegDbList = new wxDbList; pList->PtrPrev = 0; } // Initialize new node in the linked list pList->PtrNext = 0; pList->Free = FALSE; - wxStrcpy(pList->Dsn, pDbStuff->Dsn); - pList->PtrDb = new wxDB(pDbStuff->Henv,FwdOnlyCursors); + wxStrcpy(pList->Dsn, pDbConfig->Dsn); + pList->PtrDb = new wxDB(pDbConfig->Henv,FwdOnlyCursors); // Connect to the datasource - if (pList->PtrDb->Open(pDbStuff->Dsn, pDbStuff->Uid, pDbStuff->AuthStr)) + if (pList->PtrDb->Open(pDbConfig->Dsn, pDbConfig->Uid, pDbConfig->AuthStr)) { pList->PtrDb->SqlLog(SQLLOGstate,SQLLOGfn,TRUE); return(pList->PtrDb); @@ -2551,13 +2551,13 @@ wxDB WXDLLEXPORT *GetDbConnection(DbStuff *pDbStuff, bool FwdOnlyCursors) return(0); } -} // GetDbConnection() +} // wxDbGetConnection() -/********** FreeDbConnection() **********/ -bool WXDLLEXPORT FreeDbConnection(wxDB *pDb) +/********** wxDbFreeConnection() **********/ +bool WXDLLEXPORT wxDbFreeConnection(wxDB *pDb) { - DbList *pList; + wxDbList *pList; // Scan the linked list searching for the database connection for (pList = PtrBegDbList; pList; pList = pList->PtrNext) @@ -2569,13 +2569,13 @@ bool WXDLLEXPORT FreeDbConnection(wxDB *pDb) // Never found the database object, return failure return(FALSE); -} // FreeDbConnection() +} // wxDbFreeConnection() -/********** CloseDbConnections() **********/ -void WXDLLEXPORT CloseDbConnections(void) +/********** wxDbCloseConnections() **********/ +void WXDLLEXPORT wxDbCloseConnections(void) { - DbList *pList, *pNext; + wxDbList *pList, *pNext; // Traverse the linked list closing database connections and freeing memory as I go. for (pList = PtrBegDbList; pList; pList = pNext) @@ -2590,13 +2590,13 @@ void WXDLLEXPORT CloseDbConnections(void) // Mark the list as empty PtrBegDbList = 0; -} // CloseDbConnections() +} // wxDbCloseConnections() -/********** NumberDbConnectionsInUse() **********/ -int WXDLLEXPORT NumberDbConnectionsInUse(void) +/********** wxDbNumberConnectionsInUse() **********/ +int WXDLLEXPORT wxDbConnectionsInUse(void) { - DbList *pList; + wxDbList *pList; int cnt = 0; // Scan the linked list counting db connections that are currently in use @@ -2608,14 +2608,14 @@ int WXDLLEXPORT NumberDbConnectionsInUse(void) return(cnt); -} // NumberDbConnectionsInUse() +} // wxDbConnectionsInUse() -/********** SqlLog() **********/ -bool SqlLog(enum sqlLog state, char *filename) +/********** wxDbSqlLog() **********/ +bool wxDbSqlLog(wxSqlLogState state, char *filename) { bool append = FALSE; - DbList *pList; + wxDbList *pList; for (pList = PtrBegDbList; pList; pList = pList->PtrNext) { @@ -2629,11 +2629,92 @@ bool SqlLog(enum sqlLog state, char *filename) return(TRUE); -} // SqlLog() +} // wxDbSqlLog() -/********** GetDataSource() **********/ -bool GetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDescMax, +#if 0 +/********** wxDbCreateDataSource() **********/ +int wxDbCreateDataSource(const char *driverName, const char *dsn, const char *description, + bool sysDSN, const char *defDir, wxWindow *parent) +/* + * !!!! ONLY FUNCTIONAL UNDER MSW with VC6 !!!! + * Very rudimentary creation of an ODBC data source. + */ +{ + int result = FALSE; + +#ifdef __WXMSW__ + int dsnLocation; + wxString setupStr; + + if (sysDSN) + dsnLocation = ODBC_ADD_SYS_DSN; + else + dsnLocation = ODBC_ADD_DSN; + + // NOTE: The decimal 2 is an invalid character in all keyword pairs + // so that is why I used it, as wxString does not deal well with + // embedded nulls in strings + setupStr.sprintf("DSN=%s%cDescription=%s%cDefaultDir=%s%c",dsn,2,description,2,defDir,2); + + // Replace the separator from above with the '\0' seperator needed + // by the SQLConfigDataSource() function + int k; + do + { + k = setupStr.Find((wxChar)2,TRUE); + if (k != wxNOT_FOUND) + setupStr[(UINT)k] = '\0'; + } + while (k != wxNOT_FOUND); + + result = SQLConfigDataSource((HWND)parent->GetHWND(), dsnLocation, + driverName, setupStr.GetData()); + + if (!result) + { + // check for errors caused by ConfigDSN based functions + DWORD retcode = 0; + WORD cb; + wxChar errMsg[500+1]; + errMsg[0] = '\0'; + + SQLInstallerError(1,&retcode,errMsg,500,&cb); + if (retcode) + { +// logError(errMsg, sqlState); +// if (!silent) +// { +#ifdef DBDEBUG_CONSOLE + // When run in console mode, use standard out to display errors. + cout << errMsg << endl; + cout << "Press any key to continue..." << endl; + getchar(); +#endif // DBDEBUG_CONSOLE +// } + +#ifdef __WXDEBUG__ + wxLogDebug(errMsg,"DEBUG MESSAGE"); +#endif // __WXDEBUG__ + } + } + else + result = TRUE; + +#else // __WXMSW__ +#ifdef __WXDEBUG__ + wxLogDebug("wxDbCreateDataSource() not available except under MSW","DEBUG MESSAGE"); +#endif +#endif // __WXMSW__ + + return result; + +} // wxDbCreateDataSource() +#endif + + +/********** wxDbGetDataSource() **********/ +bool wxDbGetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDescMax, UWORD direction) /* * Dsn and DsDesc will contain the data source name and data source @@ -2648,7 +2729,54 @@ bool GetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDes else return(FALSE); -} // GetDataSource() +} // wxDbGetDataSource() + + +// Change this to 0 to remove use of all deprecated functions +#if 1 +/******************************************************************** + ******************************************************************** + * + * The following functions are all DEPRECATED and are included for + * backward compatability reasons only + * + ******************************************************************** + ********************************************************************/ +bool SqlLog(sqlLog state, char *filename) +{ + return wxDbSqlLog((enum wxSqlLogState)state, filename); +} +/***** DEPRECATED: use wxGetDataSource() *****/ +bool GetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDescMax, + UWORD direction) +{ + return wxDbGetDataSource(henv, Dsn, DsnMax, DsDesc, DsDescMax, direction); +} +/***** DEPRECATED: use wxDbGetConnection() *****/ +wxDB WXDLLEXPORT *GetDbConnection(DbStuff *pDbStuff, bool FwdOnlyCursors) +{ + return wxDbGetConnection((wxDbConnectInf *)pDbStuff, FwdOnlyCursors); +} +/***** DEPRECATED: use wxDbFreeConnection() *****/ +bool WXDLLEXPORT FreeDbConnection(wxDB *pDb) +{ + return wxDbFreeConnection(pDb); +} +/***** DEPRECATED: use wxDbCloseConnections() *****/ +void WXDLLEXPORT CloseDbConnections(void) +{ + wxDbCloseConnections(); +} +/***** DEPRECATED: use wxDbConnectionsInUse() *****/ +int WXDLLEXPORT NumberDbConnectionsInUse(void) +{ + return wxDbConnectionsInUse(); +} +#endif + + + + #endif // wxUSE_ODBC -- 2.45.2