_AN SQL SERVER MESSAGE-HANDLING CLASS_ by Mark Betz Listing One //************************************************************************ // DBMSG.H Class, data structure, and constant declarations for SQL // Server/DB Lib message handling -- by Mark Betz //************************************************************************ #ifndef DBMSG_H # define DBMSG_H # include # define DBMSWIN // DB Library needs this for Windows extern "C" // so the linker doesn't look for { // mangled names #include // Microsoft includes for DB Library #include } # include // string class // action messages returned by DBMsgHandler after processing a server or // DB Library message. enum SqlAction { SA_CANCEL, // exit current procedure SA_PROCEED, // proceed, non-fatal or informational SA_RETRY // retry last operation }; // error codes for use within DBMsgHandler and related classes. These // represent errors which occur in the database interface. Server and // DB Lib errors are signaled through the DBMsgHandler class. enum DBErr { DB_OK, // no error DB_ALLOCFAILED, // memory allocation failed DB_IOERR, // file or device i/o error }; // error-source constants, used by DBMsgHandler enum ErrSource { ES_DBLIB, // error source was DB Library ES_SERVER // error source was SQL Server }; // error-display handling constants, used by DBMsgHandler enum ErrDisplay { ED_ALERTONLY, // display an error alert/no info ED_BRIEF, // display error text info only ED_VERBOSE // display all error info }; // default action levels for handling errors without custom strategies. These // constants define the severity levels at which certain actions will occur, // and the number of retries allowed. const DEF_DISPLAY_LEVEL = EXCONVERSION; // display severity >= const DEF_TERM_LEVEL = EXUSER; // terminate severity >= const DEF_WRITE_LEVEL = EXUSER; // disk log severity >= const DEF_DISPLAY_TYPE = ED_VERBOSE; // default display handling const DEF_RETRY_CNT = 0; // default retries // structure for DB_lib error messages, used by DBMsgHandler struct ErrorStruct { int received; // true if error message received int severity; // error severity int dberr; // DB error code int oserr; // operating system error code String dberrstr; // DB error message text String oserrstr; // OS error message text }; // structure for SQL Server messages, used by DBMsgHandler struct MessageStruct { int received; // true if server message received int msgno; // server message number int msgstate; // server message state int severity; // message severity String msgtext; // server message text String server; // name of server issuing message String process; // name of process causing message int lineno; // line of process causing message }; // function pointer type used in ErrStrategy typedef void (*SqlErrCall)(MessageStruct&, ErrorStruct&); class DBMsgHandler; // forward declaration // SQL/DB Lib error strategy class. Used in DBMsgHandler to set custom // strategies for handling DB Lib and SQL Server errors. // IMPLEMENTATION: DBMSG.CPP class ErrStrategy { friend class DBMsgHandler; public: ErrStrategy(); ErrStrategy( ErrSource src, int num, int retCnt, bool show, bool notFatal, bool log, ErrDisplay disp, SqlErrCall callFunc = NULL ); ErrStrategy( const ErrStrategy& ); void operator = ( const ErrStrategy& ); int operator == ( const ErrStrategy& ); void SetSource( ErrSource src ) { source = src; } ErrSource GetSource() const { return source; } void SetErrNo( int num ) { errNo = num; } int GetErrNo() const { return errNo; } void SetRetryCnt( unsigned retCnt ) { retryCnt = retCnt; } unsigned GetRetryCnt() const { return retryCnt; } void SetDisplay( bool show ) { display = show; } bool GetDisplay() const { return display; } void SetProceed( bool procd ) { proceed = procd; } bool GetProceed() const { return proceed; } void SetWrite( bool log ) { write = log; } bool GetWrite() const { return write; } void SetDispType( ErrDisplay dispt ) { disptyp = dispt; } ErrDisplay GetDispType() const { return disptyp; } private: ErrSource source; // the error source, ES_SERVER or ES_DBLIB int errNo; // the error number bool display; // display the error message bool proceed; // ok to proceed after handling bool write; // flush the error to a disk file unsigned retryCnt; // number of retries allowed ErrDisplay dispTyp; // how error display is handled if display == TRUE SqlErrCall callBk; // function called on this error ErrStrategy* next; // next strategy in the list }; // for comparing against after an operation on strategies. MSGIMP is // defined in DBMSG.CPP #ifndef MSGIMP extern ErrStrategy ESZERO; #else ErrStrategy ESZERO( ES_SERVER, -32768, -1, FALSE, FALSE, FALSE, ED_VERBOSE, NULL); #endif // DBMsgHandler class. This class contains all the logic for handling // errors using default and custom strategies. // IMPLEMENTATION: DBMSG.CPP class DBMsgHandler { public: DBMsgHandler( const String& logName = "" ); DBMsgHandler( const DBMsgHandler& ); virtual ~DBMsgHandler(); void operator = ( const DBMsgHandler& ); void SetDisplayLevel( int severity ); int GetDisplayLevel() const { return displayLevel; } void SetTermLevel( int severity ); int GetTermLevel() const { return termLevel; } void SetWriteLevel( int severity ); int GetWriteLevel() const { return writeLevel; } void SetRetryCount( unsigned retCount ) { retryCnt = retCount; } unsigned GetRetryCnt () const { return retryCnt; } void SetDisplayType( ErrDisplay dispt ) { disptyp = dispt; } ErrDisplay GetDisplayType() const { return disptyp; } DBErr GetStatus(); ErrStrategy AddErrStrategy( const ErrStrategy& ); ErrStrategy GetErrStrategy( int errno, ErrSource source ); ErrStrategy DelErrStrategy( int errno, ErrSource source ); DBErr LoadStrategies( const ErrStrategy* strats, int count, bool clear = FALSE); void ClearStrategies(); virtual SqlAction HandleMsg( const ErrorStruct&, const MessageStruct& ); protected: struct PendingErr { ErrSource source; int errNo; unsigned retry; unsigned retryCnt; bool display; bool write; bool proceed; ErrDisplay dispType; ErrorStruct es; MessageStruct ms; SqlErrCall callf; } pending; private: SqlAction ResolveErr(); bool IsPending( ErrSource msgSource, int msgNum ); void SetMsgData( const ErrorStruct&, const MessageStruct& ); void NotifyUser(); DBErr WriteLog(); int displayLevel; int termLevel; int writeLevel; int retryCnt; ErrDisplay dispTyp; ErrStrategy* stratList; String log; String message; DBErr status; }; #endif // DBMSG_H