db.h

来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C头文件 代码 · 共 850 行 · 第 1/3 页

H
850
字号
    #define MAXNAME         31
#endif

#ifndef SQL_MAX_AUTHSTR_LEN
    // There does not seem to be a standard for this, so I am
    // defaulting to the value that MS uses
    #define SQL_MAX_AUTHSTR_LEN MAXNAME
#endif

#ifndef SQL_MAX_CONNECTSTR_LEN
    // There does not seem to be a standard for this, so I am
    // defaulting to the value that MS recommends
    #define SQL_MAX_CONNECTSTR_LEN 1024
#endif


class WXDLLIMPEXP_ODBC wxDbConnectInf
{
    private:
        bool freeHenvOnDestroy;
        bool useConnectionStr;

    public:
        HENV Henv;
        wxChar Dsn[SQL_MAX_DSN_LENGTH+1];                  // Data Source Name
        wxChar Uid[SQL_MAX_USER_NAME_LEN+1];               // User ID
        wxChar AuthStr[SQL_MAX_AUTHSTR_LEN+1];             // Authorization string (password)
        wxChar ConnectionStr[SQL_MAX_CONNECTSTR_LEN+1];    // Connection string (password)

        wxString Description;                              // Not sure what the max length is
        wxString FileType;                                 // Not sure what the max length is

        // Optionals needed for some databases like dBase
        wxString DefaultDir;                               // Directory that db file resides in

    public:

        wxDbConnectInf();
        wxDbConnectInf(HENV henv, const wxString &dsn, const wxString &userID=wxEmptyString,
                       const wxString &password=wxEmptyString, const wxString &defaultDir=wxEmptyString,
                       const wxString &description=wxEmptyString, const wxString &fileType=wxEmptyString);

        ~wxDbConnectInf();

        bool             Initialize();

        bool             AllocHenv();
        void             FreeHenv();

        // Accessors
        const HENV       &GetHenv()          { return Henv; };

        const wxChar    *GetDsn()           { return Dsn; };

        const wxChar    *GetUid()           { return Uid; };
        const wxChar    *GetUserID()        { return Uid; };

        const wxChar    *GetAuthStr()       { return AuthStr; };
        const wxChar    *GetPassword()      { return AuthStr; };

        const wxChar    *GetConnectionStr() { return ConnectionStr; };
        bool             UseConnectionStr() { return useConnectionStr; };

        const wxChar    *GetDescription()   { return Description; };
        const wxChar    *GetFileType()      { return FileType; };
        const wxChar    *GetDefaultDir()    { return DefaultDir; };

        void             SetHenv(const HENV henv)               { Henv = henv; };

        void             SetDsn(const wxString &dsn);

        void             SetUserID(const wxString &userID);
        void             SetUid(const wxString &uid)            { SetUserID(uid); };

        void             SetPassword(const wxString &password);
        void             SetAuthStr(const wxString &authstr)    { SetPassword(authstr); };

        void             SetConnectionStr(const wxString &connectStr);

        void             SetDescription(const wxString &desc)   { Description   = desc;     };
        void             SetFileType(const wxString &fileType)  { FileType      = fileType; };
        void             SetDefaultDir(const wxString &defDir)  { DefaultDir    = defDir;   };
};  // class wxDbConnectInf


struct WXDLLIMPEXP_ODBC wxDbSqlTypeInfo
{
    wxString    TypeName;
    SWORD       FsqlType;
    long        Precision;
    short       CaseSensitive;
    short       MaximumScale;
};


class WXDLLIMPEXP_ODBC wxDbColFor
{
public:
    wxString       s_Field;              // Formated String for Output
    wxString       s_Format[7];          // Formated Objects - TIMESTAMP has the biggest (7)
    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;

    wxDbColFor();
    ~wxDbColFor(){}

    void           Initialize();
    int            Format(int Nation, int dbDataType, SWORD sqlDataType, short columnLength, short decimalDigits);
};


class WXDLLIMPEXP_ODBC wxDbColInf
{
public:
    wxChar       catalog[128+1];
    wxChar       schema[128+1];
    wxChar       tableName[DB_MAX_TABLE_NAME_LEN+1];
    wxChar       colName[DB_MAX_COLUMN_NAME_LEN+1];
    SWORD        sqlDataType;
    wxChar       typeName[128+1];
    SWORD        columnLength;
    SWORD        bufferSize;
    short        decimalDigits;
    short        numPrecRadix;
    short        nullable;
    wxChar       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.
    wxChar       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.
    wxChar       FkTableName[DB_MAX_TABLE_NAME_LEN+1]; // Foreign key table name
    wxDbColFor  *pColFor;                              // How should this columns be formatted

    wxDbColInf();
    ~wxDbColInf();

    bool Initialize();
};


class WXDLLIMPEXP_ODBC wxDbTableInf        // Description of a Table
{
public:
    wxChar      tableName[DB_MAX_TABLE_NAME_LEN+1];
    wxChar      tableType[254+1];           // "TABLE" or "SYSTEM TABLE" etc.
    wxChar      tableRemarks[254+1];
    UWORD       numCols;                    // How many Columns does this Table have: GetColumnCount(..);
    wxDbColInf *pColInf;                    // pColInf = NULL ; User can later call GetColumns(..);

    wxDbTableInf();
    ~wxDbTableInf();

    bool             Initialize();
};


class WXDLLIMPEXP_ODBC wxDbInf     // Description of a Database
{
public:
    wxChar        catalog[128+1];
    wxChar        schema[128+1];
    int           numTables;           // How many tables does this database have
    wxDbTableInf *pTableInf;           // pTableInf = new wxDbTableInf[numTables];

    wxDbInf();
    ~wxDbInf();

    bool          Initialize();
};


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
// each of these database engines
enum wxDBMS
{
    dbmsUNIDENTIFIED,
    dbmsORACLE,
    dbmsSYBASE_ASA,        // Adaptive Server Anywhere
    dbmsSYBASE_ASE,        // Adaptive Server Enterprise
    dbmsMS_SQL_SERVER,
    dbmsMY_SQL,
    dbmsPOSTGRES,
    dbmsACCESS,
    dbmsDBASE,
    dbmsINFORMIX,
    dbmsVIRTUOSO,
    dbmsDB2,
    dbmsINTERBASE,
    dbmsPERVASIVE_SQL,
    dbmsXBASE_SEQUITER,
    dbmsFIREBIRD,
    dbmsMAXDB,
    dbmsFuture1,
    dbmsFuture2,
    dbmsFuture3,
    dbmsFuture4,
    dbmsFuture5,
    dbmsFuture6,
    dbmsFuture7,
    dbmsFuture8,
    dbmsFuture9,
    dbmsFuture10
};


// 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 WXDLLIMPEXP_DATA_ODBC(wxChar)
    DBerrorList[DB_MAX_ERROR_HISTORY][DB_MAX_ERROR_MSG_LEN+1];


class WXDLLIMPEXP_ODBC wxDb
{
private:
    bool             dbIsOpen;
    bool             dbIsCached;      // Was connection created by caching functions
    bool             dbOpenedWithConnectionString;  // Was the database connection opened with a connection string
    wxString         dsn;             // Data source name
    wxString         uid;             // User ID
    wxString         authStr;         // Authorization string (password)
    wxString         inConnectionStr; // Connection string used to connect to the database
    wxString         outConnectionStr;// Connection string returned by the database when a connection is successfully opened
    FILE            *fpSqlLog;        // Sql Log file pointer
    wxDbSqlLogState  sqlLogState;     // On or Off
    bool             fwdOnlyCursors;
    wxDBMS           dbmsType;        // Type of datasource - i.e. Oracle, dBase, SQLServer, etc

    // Private member functions
    bool             getDbInfo(bool failOnDataTypeUnsupported=true);
    bool             getDataTypeInfo(SWORD fSqlType, wxDbSqlTypeInfo &structSQLTypeInfo);
    bool             setConnectionOptions(void);
    void             logError(const wxString &errMsg, const wxString &SQLState);
    const wxChar    *convertUserID(const wxChar *userID, wxString &UserID);
    bool             determineDataTypes(bool failOnDataTypeUnsupported);
    void             initialize();
    bool             open(bool failOnDataTypeUnsupported=true);

#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;
    wxDbSqlTypeInfo typeInfBlob;
#endif

public:

    void             setCached(bool cached)  { dbIsCached = cached; };  // This function must only be called by wxDbGetConnection() and wxDbCloseConnections!!!
    bool             IsCached() { return dbIsCached; };

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?