X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/95ca6a20b55f81bcd1f8a055fc9fae1c90056ca4..71499aafed86032f0e8ed874a9007f19b921d206:/include/wx/db.h diff --git a/include/wx/db.h b/include/wx/db.h index 73c6e7ee12..5848ef0b63 100644 --- a/include/wx/db.h +++ b/include/wx/db.h @@ -1,11 +1,13 @@ /////////////////////////////////////////////////////////////////////////////// // Name: db.h -// Purpose: Header file wxDB class. The wxDB class represents a connection -// to an ODBC data source. The wxDB class allows operations on the data +// Purpose: Header file wxDb class. The wxDb class represents a connection +// to an ODBC data source. The wxDb class allows operations on the data // source such as opening and closing the data source. // Author: Doug Card // Modified by: George Tasker -// Mods: Dec, 1998: +// Bart Jourquin +// Mark Johnson, wxWindows@mj10777.de +// Mods: Dec, 1998: // -Added support for SQL statement logging and database cataloging // April, 1999 // -Added QUERY_ONLY mode support to reduce default number of cursors @@ -38,6 +40,12 @@ #ifndef DB_DOT_H #define DB_DOT_H + +// BJO 20000503: introduce new GetColumns members which are more database independant and +// return columns in the order they were created +#define OLD_GETCOLUMNS 1 +#define EXPERIMENTAL_WXDB_FUNCTIONS 1 + // Use this line for wxWindows v1.x //#include "wx_ver.h" // Use this line for wxWindows v2.x @@ -45,7 +53,7 @@ #if wxMAJOR_VERSION == 2 #ifdef __GNUG__ - #pragma interface "db.h" + #pragma interface "db.h" #endif #endif @@ -53,18 +61,39 @@ #if wxMAJOR_VERSION == 2 extern "C" { +#ifdef __VISUALC__ +// If you use the wxDbCreateDataSource() function with MSW/VC6, +// you cannot use the iODBC headers, you must use the VC headers, +// plus the odbcinst.h header - gt Nov 2 2000 +// +// Must add "odbccp32.lib" in \wx2\wxWindows\src\makevc.env to the WINLIBS= line +// + #include "sql.h" + #include "sqlext.h" + #include "odbcinst.h" +#else #include "wx/isql.h" #include "wx/isqlext.h" +#endif } #else // version == 1 extern "C" { +#ifdef __VISUALC__ +// If you use the wxDbCreateDataSource() function with MSW/VC6, +// you cannot use the iODBC headers, you must use the VC headers, +// plus the odbcinst.h header - gt Nov 2 2000 + #include "sql.h" + #include "sqlext.h" + #include "odbcinst.h" +#else #include "iodbc.h" #include "isqlext.h" +#endif } #endif -typedef float SFLOAT; -typedef double SDOUBLE; +typedef float SFLOAT; +typedef double SDOUBLE; typedef unsigned int UINT; #define ULONG UDWORD @@ -87,15 +116,16 @@ enum enumDummy {enumDum1}; #define FALSE 0 #endif -const int DB_PATH_MAX = 254; +const int wxDB_PATH_MAX = 254; + +extern char const *SQL_LOG_FILENAME; +extern char const *SQL_CATALOG_FILENAME; -char * const SQL_LOG_FILENAME = "sqllog.txt"; -char * const SQL_CATALOG_FILENAME = "catalog.txt"; // Database Globals const int DB_TYPE_NAME_LEN = 40; -const int DB_MAX_STATEMENT_LEN = 2048; -const int DB_MAX_WHERE_CLAUSE_LEN = 1024; +const int DB_MAX_STATEMENT_LEN = 4096; +const int DB_MAX_WHERE_CLAUSE_LEN = 2048; const int DB_MAX_ERROR_MSG_LEN = 512; const int DB_MAX_ERROR_HISTORY = 5; const int DB_MAX_TABLE_NAME_LEN = 128; @@ -128,7 +158,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, @@ -225,7 +255,7 @@ enum ODBC_ERRORS }; -struct DbStuff +struct wxDbConnectInf { HENV Henv; char Dsn[SQL_MAX_DSN_LENGTH+1]; // Data Source Name @@ -236,11 +266,10 @@ struct DbStuff char fileType[SQL_MAX_DSN_LENGTH+1]; // Not sure what the max length is // Optionals needed for some databases like dBase - char defaultDir[DB_PATH_MAX]; // Directory that db file resides in + char defaultDir[wxDB_PATH_MAX]; // Directory that db file resides in }; - -typedef struct +struct wxDbSqlTypeInfo { char TypeName[DB_TYPE_NAME_LEN]; int FsqlType; @@ -248,28 +277,28 @@ typedef struct short CaseSensitive; // short MinimumScale; short MaximumScale; -} SqlTypeInfo; +}; -class WXDLLEXPORT wxColFor +class WXDLLEXPORT wxDbColFor { public: wxString s_Field; // Formated String for Output wxString s_Format[7]; // Formated Objects - TIMESTAMP has the biggest (7) - wxString s_Menge[7]; // Formated Objects - amount of things that can be formatted - int i_Menge[7]; // Formated Objects - TT MM YYYY HH MM SS m + wxString s_Amount[7]; // Formated Objects - amount of things that can be formatted + int i_Amount[7]; // Formated Objects - TT MM YYYY HH MM SS m int i_Nation; // 0 = timestamp , 1=EU, 2=UK, 3=International, 4=US int i_dbDataType; // conversion of the 'sqlDataType' to the generic data type used by these classes SWORD i_sqlDataType; - wxColFor(); - ~wxColFor(); - + wxDbColFor(); + ~wxDbColFor(); + int Format(int Nation, int dbDataType, SWORD sqlDataType, short columnSize, short decimalDigits); }; -class WXDLLEXPORT wxColInf +class WXDLLEXPORT wxDbColInf { public: char catalog[128+1]; @@ -290,42 +319,51 @@ public: char PkTableName[DB_MAX_TABLE_NAME_LEN+1]; // Tables that use this PKey as a FKey int FkCol; // Foreign key column 0=No; 1= First Key, 2 = Second Key etc. char FkTableName[DB_MAX_TABLE_NAME_LEN+1]; // Foreign key table name - wxColFor *pColFor; // How should this columns be formatted + wxDbColFor *pColFor; // How should this columns be formatted + + wxDbColInf(); + ~wxDbColInf(); }; -class WXDLLEXPORT wxTableInf // Description of a Table + + +class WXDLLEXPORT wxDbTableInf // Description of a Table { public: - char tableName[DB_MAX_TABLE_NAME_LEN+1]; - char tableType[254+1]; // "TABLE" or "SYSTEM TABLE" etc. - char tableRemarks[254+1]; - int numCols; // How many Columns does this Table have: GetColumnCount(..); - wxColInf *pColInf; // pColInf = NULL ; User can later call GetColumns(..); + char tableName[DB_MAX_TABLE_NAME_LEN+1]; + char tableType[254+1]; // "TABLE" or "SYSTEM TABLE" etc. + char tableRemarks[254+1]; + int numCols; // How many Columns does this Table have: GetColumnCount(..); + wxDbColInf *pColInf; // pColInf = NULL ; User can later call GetColumns(..); + wxDbTableInf(); + ~wxDbTableInf(); }; class WXDLLEXPORT wxDbInf // Description of a Database { public: - char catalog[128+1]; - char schema[128+1]; - int numTables; // How many tables does this database have - wxTableInf *pTableInf; // pTableInf = new wxTableInf[numTables]; + char catalog[128+1]; + char schema[128+1]; + int numTables; // How many tables does this database have + wxDbTableInf *pTableInf; // pTableInf = new wxDbTableInf[numTables]; + + wxDbInf(); + ~wxDbInf(); }; -enum sqlLog +enum wxDbSqlLogState { sqlLogOFF, sqlLogON }; - // These are the databases currently tested and working with these classes -// See the comments in wxDB::Dbms() for exceptions/issues with +// See the comments in wxDb::Dbms() for exceptions/issues with // each of these database engines -enum dbms +enum wxDBMS { dbmsUNIDENTIFIED, dbmsORACLE, @@ -336,49 +374,79 @@ enum dbms dbmsPOSTGRES, dbmsACCESS, dbmsDBASE, - dbmsINFORMIX + dbmsINFORMIX, + dbmsVIRTUOSO, + dbmsDB2, + dbmsINTERBASE }; -typedef enum dbms DBMS; - - -// The wxDB::errorList is copied to this variable when the wxDB object +// 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 +// database object is closed. This is necessary if the database // connection fails so the calling application can show the operator -// why the connection failed. Note: as each wxDB object is closed, it -// will overwrite the errors of the previously destroyed wxDB object in +// why the connection failed. Note: as each wxDb object is closed, it +// will overwrite the errors of the previously destroyed wxDb object in // this variable. 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 +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 + wxDbSqlLogState sqlLogState; // On or Off bool fwdOnlyCursors; + wxDBMS dbmsType; // Type of datasource - i.e. Oracle, dBase, SQLServer, etc // Private member functions bool getDbInfo(void); - bool getDataTypeInfo(SWORD fSqlType, SqlTypeInfo &structSQLTypeInfo); + bool getDataTypeInfo(SWORD fSqlType, wxDbSqlTypeInfo &structSQLTypeInfo); bool setConnectionOptions(void); void logError(const char *errMsg, const char *SQLState); + void initialize(); +#if !wxODBC_BACKWARD_COMPATABILITY + // ODBC handles + HENV henv; // ODBC Environment handle + HDBC hdbc; // ODBC DB Connection handle + HSTMT hstmt; // ODBC Statement handle + + //Error reporting mode + bool silent; + + // Number of Ctable objects connected to this db object. FOR INTERNAL USE ONLY!!! + unsigned int nTables; + + // Information about logical data types VARCHAR, INTEGER, FLOAT and DATE. + // + // This information is obtained from the ODBC driver by use of the + // SQLGetTypeInfo() function. The key piece of information is the + // type name the data source uses for each logical data type. + // e.g. VARCHAR; Oracle calls it VARCHAR2. + wxDbSqlTypeInfo typeInfVarchar; + wxDbSqlTypeInfo typeInfInteger; + wxDbSqlTypeInfo typeInfFloat; + wxDbSqlTypeInfo typeInfDate; +#endif public: +#if wxODBC_BACKWARD_COMPATABILITY + // ODBC handles + HENV henv; // ODBC Environment handle + HDBC hdbc; // ODBC DB Connection handle + HSTMT hstmt; // ODBC Statement handle + + //Error reporting mode + bool silent; + + // Number of Ctable objects connected to this db object. FOR INTERNAL USE ONLY!!! + unsigned int nTables; +#endif + // The following structure contains database information gathered from the // datasource when the datasource is first opened. struct @@ -393,6 +461,7 @@ public: char databaseName[128]; // Database filename char outerJoins[2]; // Indicates whether the data source supports outer joins char procedureSupport[2]; // Indicates whether the data source supports stored procedures + char accessibleTables[2]; // Indicates whether the data source only reports accessible tables in SQLTables. UWORD maxConnections; // Maximum # of connections the data source supports UWORD maxStmts; // Maximum # of HSTMTs per HDBC UWORD apiConfLvl; // ODBC API conformance level @@ -415,35 +484,31 @@ public: UDWORD loginTimeout; // Number seconds to wait for a login request } dbInf; - // ODBC handles - HENV henv; // ODBC Environment handle - HDBC hdbc; // ODBC DB Connection handle - HSTMT hstmt; // ODBC Statement handle - // ODBC Error Inf. - char sqlState[20]; - SDWORD nativeError; - char errorMsg[SQL_MAX_MESSAGE_LENGTH]; SWORD cbErrorMsg; - char errorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN]; int DB_STATUS; + wxChar errorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN]; + wxChar errorMsg[SQL_MAX_MESSAGE_LENGTH]; + SDWORD nativeError; + wxChar sqlState[20]; - //Error reporting mode - bool silent; - - // Number of Ctable objects connected to this db object - unsigned int nTables; - - // Inf. about logical data types VARCHAR, INTEGER, FLOAT and DATE. - // This inf. is obtained from the ODBC driver by use of the - // SQLGetTypeInfo() function. The key piece of inf. is the +#if wxODBC_BACKWARD_COMPATABILITY + // Information about logical data types VARCHAR, INTEGER, FLOAT and DATE. + // + // This information is obtained from the ODBC driver by use of the + // SQLGetTypeInfo() function. The key piece of information 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; - + wxDbSqlTypeInfo typeInfVarchar; + wxDbSqlTypeInfo typeInfInteger; + wxDbSqlTypeInfo typeInfFloat; + wxDbSqlTypeInfo typeInfDate; +#endif + // Public member functions - wxDB(HENV &aHenv, bool FwdOnlyCursors=(bool)TRUE); + wxDb(HENV &aHenv, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS); bool Open(char *Dsn, char *Uid, char *AuthStr); // Data Source Name, User ID, Password + bool Open(wxDb *copyDb); // pointer to a wxDb whose connection info should be copied rather than re-queried void Close(void); bool CommitTrans(void); bool RollbackTrans(void); @@ -456,74 +521,138 @@ public: bool GetNext(void); bool GetData(UWORD colNo, SWORD cType, PTR pData, SDWORD maxLen, SDWORD FAR *cbReturned); bool Grant(int privileges, const char *tableName, const char *userList = "PUBLIC"); - int TranslateSqlState(const char *SQLState); + int TranslateSqlState(const wxChar *SQLState); wxDbInf *GetCatalog(char *userID); bool Catalog(const char *userID=NULL, const char *fileName = SQL_CATALOG_FILENAME); - int GetKeyFields(char *tableName, wxColInf* colInf,int nocols); - wxColInf *GetColumns(char *tableName[], const char *userID=NULL); - wxColInf *GetColumns(char *tableName, int *numCols, const char *userID=NULL); + int GetKeyFields(char *tableName, wxDbColInf* colInf, int nocols); + + wxDbColInf *GetColumns(char *tableName[], const char *userID=NULL); + wxDbColInf *GetColumns(char *tableName, int *numCols, const char *userID=NULL); + int GetColumnCount(char *tableName, const char *userID=NULL); - char *GetDatabaseName(void) {return dbInf.dbmsName;} - char *GetDataSource(void) {return dsn;} - char *GetUsername(void) {return uid;} - char *GetPassword(void) {return authStr;} - bool IsOpen(void) {return dbIsOpen;} - HENV GetHENV(void) {return henv;} - HDBC GetHDBC(void) {return hdbc;} - HSTMT GetHSTMT(void) {return hstmt;} + const char *GetDatabaseName(void) {return dbInf.dbmsName;} + const char *GetDataSource(void) {return (const char *)dsn;} + const char *GetDatasourceName(void){return (const char *)dsn;} + const char *GetUsername(void) {return (const char *)uid;} + const char *GetPassword(void) {return (const char *)authStr;} + bool IsOpen(void) {return dbIsOpen;} + HENV GetHENV(void) {return henv;} + HDBC GetHDBC(void) {return hdbc;} + HSTMT GetHSTMT(void) {return hstmt;} + int GetTableCount() {return nTables;}; // number of tables using this connection + wxDbSqlTypeInfo GetTypeInfVarchar() {return typeInfVarchar;} + wxDbSqlTypeInfo GetTypeInfInteger() {return typeInfInteger;} + wxDbSqlTypeInfo GetTypeInfFloat() {return typeInfFloat;} + wxDbSqlTypeInfo GetTypeInfDate() {return typeInfDate;} + 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 WriteSqlLog(const char *logMsg); - DBMS Dbms(void); - bool FwdOnlyCursors(void) {return fwdOnlyCursors;} -}; // wxDB + bool TablePrivileges(const char *tableName, const char* priv, const char *userID="", const char *schema=NULL, const char *path=""); // Table name can refer to a table, view, alias or synonym + void LogError(const char *errMsg, const char *SQLState = NULL) {logError(errMsg, SQLState);} + void SetDebugErrorMessages(bool state) { silent = !state; } + bool SetSqlLogging(wxDbSqlLogState state, const wxChar *filename = SQL_LOG_FILENAME, bool append = FALSE); + bool WriteSqlLog(const wxChar *logMsg); + wxDBMS Dbms(void); + bool FwdOnlyCursors(void) {return fwdOnlyCursors;} + + // These two functions are provided strictly for use by wxDbTable. + // DO NOT USE THESE FUNCTIONS, OR MEMORY LEAKS MAY OCCUR + void incrementTableCount() { nTables++; return; } + void decrementTableCount() { nTables--; return; } + +}; // 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 + wxChar Dsn[SQL_MAX_DSN_LENGTH+1]; // Data Source Name + wxChar Uid[20+1]; // User ID + wxChar AuthStr[20+1]; // Authorization string (password) + 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 + class wxDb *pDb; +}; // 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 *wxDbGetConnection(wxDbConnectInf *pDbConfig, bool FwdOnlyCursors=(bool)wxODBC_FWD_ONLY_CURSORS); +bool WXDLLEXPORT wxDbFreeConnection(wxDb *pDb); +void WXDLLEXPORT wxDbCloseConnections(void); +int WXDLLEXPORT wxDbConnectionsInUse(void); -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); +// This function sets the sql log state for all open wxDb objects +bool wxDbSqlLog(wxDbSqlLogState state, const wxChar *filename = SQL_LOG_FILENAME); -// This function sets the sql log state for all open wxDB objects -bool SqlLog(enum sqlLog 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 wxODBC_BACKWARD_COMPATABILITY +//################################################################################# +//############### DEPRECATED functions for backward compatability ################# +//################################################################################# + +// Backward compability structures/classes. This will eventually go away +const int DB_PATH_MAX = wxDB_PATH_MAX; + +typedef wxDb wxDB; +typedef wxDbTableInf wxTableInf; +typedef wxDbColInf wxColInf; +typedef wxDbColInf CcolInf; +typedef wxDbColFor wxColFor; +typedef wxDbSqlTypeInfo SqlTypeInfo; +typedef wxDbSqlTypeInfo wxSqlTypeInfo; +typedef enum wxDbSqlLogState sqlLog; +typedef enum wxDbSqlLogState wxSqlLogState; +typedef enum wxDBMS dbms; +typedef enum wxDBMS DBMS; +typedef wxODBC_ERRORS ODBC_ERRORS; +typedef wxDbConnectInf DbStuff; +typedef wxDbList DbList; +#ifdef __WXDEBUG__ +typedef wxTablesInUse CstructTablesInUse; +#endif + +// 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 wxChar *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