]> git.saurik.com Git - wxWidgets.git/blobdiff - include/wx/db.h
VA 4.0 fixes
[wxWidgets.git] / include / wx / db.h
index 25edff65da6344daf2dece61c8bd8e4eb4e1c895..228a03326271927022bb750029ede330105a6992 100644 (file)
@@ -4,14 +4,21 @@
 //              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:
-// Mods:        Dec, 1998: Added support for SQL statement logging and database
-//              cataloging
+// Modified by: George Tasker
+// 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
+//                                             -Added additional SQL logging code
+//                -Added DEBUG-ONLY tracking of Ctable objects to detect orphaned DB connections
+//                                             -Set ODBC option to only read committed writes to the DB so all
+//                   databases operate the same in that respect
+//
 // Created:     9.96
 // RCS-ID:      $Id$
 // Copyright:   (c) 1996 Remstar International, Inc.
 // Licence:     wxWindows licence, plus:
-// Notice:             This class library and its intellectual design are free of charge for use,
+// Notice:              This class library and its intellectual design are free of charge for use,
 //              modification, enhancement, debugging under the following conditions:
 //              1) These classes may only be used as part of the implementation of a
 //                 wxWindows-based application
 
 #ifndef DB_DOT_H
 #define DB_DOT_H
-#ifdef __GNUG__
-#pragma interface "db.h"
+
+// Use this line for wxWindows v1.x
+//#include "wx_ver.h"
+// Use this line for wxWindows v2.x
+#include "wx/version.h"
+
+#if wxMAJOR_VERSION == 2
+       #ifdef __GNUG__
+       #pragma interface "db.h"
+       #endif
+#endif
+
+#if wxMAJOR_VERSION == 2
+       extern "C" {
+       #include "wx/unix/isql.h"
+       #include "wx/unix/isqlext.h"
+       }
+#else  // version == 1
+       extern "C" {
+       #include "iodbc.h"
+       #include "isqlext.h"
+       }
 #endif
 
-#if defined(__WXMSW__) || defined(WIN32)
-#include <windows.h>
+       typedef float SFLOAT; 
+       typedef double SDOUBLE; 
+       typedef unsigned int UINT;
+       #define ULONG UDWORD
+/*
+#ifdef __UNIX__ 
+#   ifndef strnicmp 
+#      define strnicmp strncasecmp 
+#   endif 
+#   ifndef stricmp 
+#      define stricmp strcasecmp 
+#   endif 
+#elif defined(__WXMAC__)
+#else 
+#   include <io.h> 
+#endif
+*/
+
+#ifndef wxODBC_FWD_ONLY_CURSORS
+#define wxODBC_FWD_ONLY_CURSORS 1
 #endif
 
-#define ODBCVER 0x0250
-#include <sql.h>
-#include <sqlext.h>
 
 enum           enumDummy               {enumDum1};
 
-#define SQL_C_BOOLEAN (sizeof(int) == 2 ? SQL_C_USHORT : SQL_C_ULONG)
-#define SQL_C_ENUM (sizeof(enumDummy) == 2 ? SQL_C_USHORT : SQL_C_ULONG)    //glt 2-21-97
+#define SQL_C_BOOLEAN(datatype) (sizeof(datatype) == 1 ? SQL_C_UTINYINT : (sizeof(datatype) == 2 ? SQL_C_USHORT : SQL_C_ULONG))
+//     #define SQL_C_BOOLEAN (sizeof(Bool) == 2 ? SQL_C_USHORT : SQL_C_ULONG)
+
+#define SQL_C_ENUM (sizeof(enumDummy) == 2 ? SQL_C_USHORT : SQL_C_ULONG)
+
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+
+const int DB_PATH_MAX                                                  = 254;
 
 // Database Globals
-const DB_TYPE_NAME_LEN                                         = 40;
-const DB_MAX_STATEMENT_LEN                                     = 2048;
-const DB_MAX_WHERE_CLAUSE_LEN                          = 1024;
-const DB_MAX_ERROR_MSG_LEN                                     = 512;
-const DB_MAX_ERROR_HISTORY                                     = 5;
-const DB_MAX_TABLE_NAME_LEN                            = 128;
-const DB_MAX_COLUMN_NAME_LEN                           = 128;
-
-const DB_DATA_TYPE_VARCHAR                                     = 1;
-const DB_DATA_TYPE_INTEGER                                     = 2;
-const DB_DATA_TYPE_FLOAT                                       = 3;
-const  DB_DATA_TYPE_DATE                                               = 4;
-
-const DB_SELECT_KEYFIELDS                                      = 1;
-const DB_SELECT_WHERE                                          = 2;
-const DB_SELECT_MATCHING                                       = 3;
-const DB_SELECT_STATEMENT                                      = 4;
-
-const DB_UPD_KEYFIELDS                                         = 1;
-const DB_UPD_WHERE                                                     = 2;
-
-const DB_DEL_KEYFIELDS                                         = 1;
-const DB_DEL_WHERE                                                     = 2;
-const DB_DEL_MATCHING                                          = 3;
-
-const DB_WHERE_KEYFIELDS                                       = 1;
-const DB_WHERE_MATCHING                                                = 2;
-
-const DB_CURSOR0                                                               = 0;
-const DB_CURSOR1                                                               = 1;
-const DB_CURSOR2                                                               = 2;
-//const DB_CURSOR3                                                     = 3;
-//const DB_CURSOR4                                                     = 4;
-//const DB_CURSOR5                                                     = 5;
-
-const DB_GRANT_SELECT                                          = 1;
-const DB_GRANT_INSERT                                          = 2;
-const DB_GRANT_UPDATE                                          = 4;
-const DB_GRANT_DELETE                                          = 8;
-const DB_GRANT_ALL                                                     = DB_GRANT_SELECT | DB_GRANT_INSERT | DB_GRANT_UPDATE | DB_GRANT_DELETE;
+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_ERROR_MSG_LEN                         = 512;
+const int DB_MAX_ERROR_HISTORY                         = 5;
+const int DB_MAX_TABLE_NAME_LEN                                = 128;
+const int DB_MAX_COLUMN_NAME_LEN                               = 128;
+
+const int DB_DATA_TYPE_VARCHAR                         = 1;
+const int DB_DATA_TYPE_INTEGER                         = 2;
+const int DB_DATA_TYPE_FLOAT                                   = 3;
+const int DB_DATA_TYPE_DATE                                    = 4;
+
+const int DB_SELECT_KEYFIELDS                                  = 1;
+const int DB_SELECT_WHERE                                              = 2;
+const int DB_SELECT_MATCHING                                   = 3;
+const int DB_SELECT_STATEMENT                                  = 4;
+
+const int DB_UPD_KEYFIELDS                                             = 1;
+const int DB_UPD_WHERE                                                 = 2;
+
+const int DB_DEL_KEYFIELDS                                             = 1;
+const int DB_DEL_WHERE                                                 = 2;
+const int DB_DEL_MATCHING                                              = 3;
+
+const int DB_WHERE_KEYFIELDS                                   = 1;
+const int DB_WHERE_MATCHING                                    = 2;
+
+const int DB_GRANT_SELECT                                              = 1;
+const int DB_GRANT_INSERT                                              = 2;
+const int DB_GRANT_UPDATE                                              = 4;
+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
 {
-       DB_FAILURE                                                                      = 0,
-       DB_SUCCESS                                                                      = 1,
+       DB_FAILURE                                                                              = 0,
+       DB_SUCCESS                                                                              = 1,
        DB_ERR_NOT_IN_USE,
        DB_ERR_GENERAL_WARNING,                                                                 // SqlState = '01000'
        DB_ERR_DISCONNECT_ERROR,                                                                // SqlState = '01002'
@@ -190,9 +236,15 @@ enum ODBC_ERRORS
 struct DbStuff
 {
        HENV Henv;
-       char Dsn[SQL_MAX_DSN_LENGTH+1]; // Data Source Name
-       char Uid[20];                                                   // User ID
-       char AuthStr[20];                                               // Authorization string (password)
+       char Dsn[SQL_MAX_DSN_LENGTH+1];                         // Data Source Name
+       char Uid[20+1];                                                                 // User ID
+       char AuthStr[20+1];                                                             // Authorization string (password)
+
+       char description[SQL_MAX_DSN_LENGTH+1]; // Not sure what the max length is
+       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
 };
 
 typedef struct
@@ -205,12 +257,63 @@ typedef struct
        short MaximumScale;
 } SqlTypeInfo;
 
-class CcolInf
+class WXDLLEXPORT wxColFor
+{
+ public:
+  wxString       s_Field;             // Formated String for Output
+  wxString       s_Format[7];         // Formated Objekts - TIMESTAMP has the biggest (7)
+  wxString       s_Menge[7];          // Formated Objekts -  amount of things that can be formatted
+  int            i_Menge[7];          // Formated Objekts -  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();
+  int            Format(int Nation, int dbDataType,SWORD sqlDataType,short columnSize,short decimalDigits);
+};
+
+class WXDLLEXPORT wxColInf
 {
 public:
-       char tableName[DB_MAX_TABLE_NAME_LEN+1];
-       char colName[DB_MAX_COLUMN_NAME_LEN+1];
-       int  sqlDataType;
+       char         catalog[128+1];
+       char         schema[128+1];
+       char         tableName[DB_MAX_TABLE_NAME_LEN+1];
+       char         colName[DB_MAX_COLUMN_NAME_LEN+1];
+       SWORD        sqlDataType;
+       char         typeName[128+1];
+       SWORD        columnSize;
+       SWORD        bufferLength;
+       short        decimalDigits;
+       short        numPrecRadix;
+       short        nullable;
+       char         remarks[254+1];
+       int                  dbDataType;  // conversion of the 'sqlDataType' to the generic data type used by these classes
+ // mj10777.19991224 : new
+    int          PkCol;       // Primary key column       0=No; 1= First Key, 2 = Second Key etc.
+    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
+};
+
+class WXDLLEXPORT wxTableInf        // Description of a Table
+{   // mj10777 : used in wxDbInf and GetCatalog(..)
+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(..);
+};
+
+class WXDLLEXPORT wxDbInf     // Description of a Database
+{   // mj10777 : used in GetCatalog(..)
+public:
+   char        catalog[128+1];
+   char        schema[128+1];
+   int         numTables;           // How many tables does this database have
+   wxTableInf *pTableInf;           // pTableInf = new wxTableInf[numTables];
 };
 
 enum sqlLog
@@ -219,7 +322,37 @@ enum sqlLog
        sqlLogON
 };
 
-class wxDB
+enum dbms
+{
+       dbmsUNIDENTIFIED,
+       dbmsORACLE,
+       dbmsSYBASE_ASA,         // Adaptive Server Anywhere
+       dbmsSYBASE_ASE,         // Adaptive Server Enterprise
+       dbmsMS_SQL_SERVER,
+       dbmsMY_SQL,
+       dbmsPOSTGRES,
+       dbmsACCESS,
+       dbmsDBASE,
+       dbmsINFORMIX
+};
+
+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 
+// 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 
+// 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
 {
 private:
 
@@ -235,7 +368,9 @@ private:
        bool getDbInfo(void);
        bool getDataTypeInfo(SWORD fSqlType, SqlTypeInfo &structSQLTypeInfo);
        bool setConnectionOptions(void);
-       void logError(char *errMsg, char *SQLState);
+       void logError(const char *errMsg, const char *SQLState);
+
+       bool                    fwdOnlyCursors;
 
 public:
 
@@ -244,12 +379,12 @@ public:
        struct
        {
                char   dbmsName[40];                                                            // Name of the dbms product
-               char   dbmsVer[20];                                                             // Version # of the dbms product
+               char   dbmsVer[64];                                                             // Version # of the dbms product
                char   driverName[40];                                                  // Driver name
-               char   odbcVer[20];                                                             // ODBC version of the driver
-               char   drvMgrOdbcVer[20];                                               // ODBC version of the driver manager
-               char   driverVer[40];                                                   // Driver version
-               char   serverName[40];                                                  // Server Name, typically a connect string
+               char   odbcVer[60];                                                             // ODBC version of the driver
+               char   drvMgrOdbcVer[60];                                               // ODBC version of the driver manager
+               char   driverVer[60];                                                   // Driver version
+               char   serverName[80];                                                  // Server Name, typically a connect string
                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
@@ -291,6 +426,9 @@ public:
        //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
@@ -299,7 +437,7 @@ public:
        SqlTypeInfo typeInfVarchar, typeInfInteger, typeInfFloat, typeInfDate;
        
        // Public member functions
-       wxDB(HENV &aHenv);
+       wxDB(HENV &aHenv, bool FwdOnlyCursors=(bool)TRUE);
        bool             Open(char *Dsn, char *Uid, char *AuthStr);  // Data Source Name, User ID, Password
        void             Close(void);
        bool             CommitTrans(void);
@@ -307,24 +445,33 @@ public:
        bool             DispAllErrors(HENV aHenv, HDBC aHdbc = SQL_NULL_HDBC, HSTMT aHstmt = SQL_NULL_HSTMT);
        bool             GetNextError(HENV aHenv, HDBC aHdbc = SQL_NULL_HDBC, HSTMT aHstmt = SQL_NULL_HSTMT);
        void             DispNextError(void);
-       bool             CreateView(char *viewName, char *colList, char *pSqlStmt);
-       bool             ExecSql(char *pSqlStmt);
-       bool        Grant(int privileges, char *tableName, char *userList = "PUBLIC");
-       int         TranslateSqlState(char *SQLState);
-       bool             Catalog(char *userID, char *fileName = "Catalog.txt");
-       CcolInf *GetColumns(char *tableName[]);
+       bool             CreateView(const char *viewName, const char *colList, const char *pSqlStmt, bool attemptDrop=TRUE);
+       bool             DropView(const char *viewName);
+       bool             ExecSql(const char *pSqlStmt);
+       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);
+   wxDbInf      *GetCatalog(char *userID);
+       bool             Catalog(const char *userID=NULL, const char *fileName = "Catalog.txt");
+    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                  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;}
+       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;}
-       bool             TableExists(char *tableName);  // Table name can refer to a table, view, alias or synonym
-       void             LogError(char *errMsg, char *SQLState = 0) {logError(errMsg, SQLState);}
-       bool             SqlLog(enum sqlLog state, char *filename = "sqllog.txt", bool append = FALSE);
-       bool             WriteSqlLog(char *logMsg);
+       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 = "sqllog.txt", bool append = FALSE);
+       bool             WriteSqlLog(const char *logMsg);
+       DBMS             Dbms(void);
+       bool             FwdOnlyCursors(void) {return fwdOnlyCursors;}
 
 };  // wxDB
 
@@ -342,20 +489,35 @@ struct DbList
        DbList *PtrNext;                                                        // Pointer to next item in the list
 };
 
+
+#ifdef __WXDEBUG__
+#include "wx/object.h"
+class CstructTablesInUse : public wxObject
+{
+       public:
+               const char      *tableName;
+               ULONG                    tableID;
+               class wxDB      *pDb;
+};  // CstructTablesInUse
+#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  *GetDbConnection(DbStuff *pDbStuff);
-bool  FreeDbConnection(wxDB *pDb);
-void  CloseDbConnections(void);
-int    NumberDbConnectionsInUse(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 SqlLog(enum sqlLog state, const char *filename = "sqllog.txt");
 
 // 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 GetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDescMax,
+bool WXDLLEXPORT GetDataSource(HENV henv, char *Dsn, SWORD DsnMax, char *DsDesc, SWORD DsDescMax,
                                                 UWORD direction = SQL_FETCH_NEXT);
 
 #endif