src/odbc/odbcError.cxx

Go to the documentation of this file.
00001 #include "odbcCommon.h"
00002 
00003 // Global error object definition
00004 CSqlOdbcError globalError( SQL_HANDLE_ENV );
00005 
00006 CSqlOdbcError::CSqlOdbcError( void )
00007 {
00008     fprintf(stderr,"This should not happen!!\n");
00009     exit(1);
00010 }
00011 
00012 CSqlOdbcError::CSqlOdbcError( long handleType ) : 
00013     csqlErrCode ( OK ), 
00014     hdlType( handleType ) 
00015 {}
00016 
00017 void CSqlOdbcError::set( long err ) 
00018 { 
00019     csqlErrCode = err; 
00020     
00021     if( err != OK );
00022         printStr( SQL_OV_ODBC3 ); 
00023 }
00024 
00025 SQLRETURN CSqlOdbcError::printStr( SQLUINTEGER odbcVersion )
00026 {
00027     ErrorInfo_t *errorInfoTbl;
00028     int code = 0, i = 0;
00029     
00030 #ifndef DEBUG_INFO
00031     return( SQL_SUCCESS );
00032 #endif
00033 
00034     // Select error table.
00035     if( odbcVersion >= SQL_OV_ODBC3 )
00036         errorInfoTbl = errorInfoTable_3x;
00037     else
00038         errorInfoTbl = errorInfoTable_2x;
00039 
00040     // Search in table.
00041     while( errorInfoTbl[i].csqlErrCode != csqlErrCode && errorInfoTbl[i].csqlErrCode != ERROR_ENDOFTABLE ) i++;
00042 
00043     // Print details, if found
00044     if( errorInfoTbl[i].csqlErrCode == csqlErrCode )
00045     {
00046         fprintf( stderr, "[NativeCode: %ld][SqlState: %s][Message: %s].\n", 
00047             csqlErrCode, 
00048             (char*) errorInfoTbl[i].sqlState,
00049             (char*) errorInfoTbl[i].sqlMessage );
00050 
00051         return( SQL_SUCCESS );
00052     }
00053 
00054     return( SQL_ERROR );
00055 }
00056 
00057 SQLRETURN SQLError(
00058     SQLHENV EnvironmentHandle,  // IN
00059     SQLHDBC ConnectionHandle,   // IN
00060     SQLHSTMT StatementHandle,   // IN
00061     SQLCHAR *Sqlstate,          // OUT
00062     SQLINTEGER *NativeError,    // OUT
00063     SQLCHAR *MessageText,       // OUT
00064     SQLSMALLINT BufferLength,   // IN
00065     SQLSMALLINT *TextLength)    // OUT
00066 {
00067     // Call SQLGetDiagRec() on handle whichever is valid.
00068     if( isValidHandle( EnvironmentHandle, SQL_HANDLE_ENV ) == SQL_SUCCESS )
00069         return( ((CSqlOdbcEnv*)EnvironmentHandle)->err_.SQLGetDiagRec(EnvironmentHandle, Sqlstate, NativeError, MessageText, BufferLength, TextLength) );
00070 
00071     if( isValidHandle( ConnectionHandle, SQL_HANDLE_DBC ) == SQL_SUCCESS )
00072         return( ((CSqlOdbcDbc*)ConnectionHandle)->err_.SQLGetDiagRec(ConnectionHandle, Sqlstate, NativeError, MessageText, BufferLength, TextLength) );
00073 
00074     if( isValidHandle( StatementHandle, SQL_HANDLE_STMT ) == SQL_SUCCESS )
00075         return( ((CSqlOdbcStmt*)StatementHandle)->err_.SQLGetDiagRec(StatementHandle, Sqlstate, NativeError, MessageText, BufferLength, TextLength) );
00076 
00077     return( SQL_INVALID_HANDLE );
00078 } 
00079 
00080 // ODBC API's
00081 SQLRETURN SQLGetDiagRec(
00082     SQLSMALLINT HandleType, 
00083     SQLHANDLE Handle,
00084     SQLSMALLINT RecNumber,
00085     SQLCHAR *Sqlstate,
00086     SQLINTEGER *NativeError, 
00087     SQLCHAR *MessageText,
00088     SQLSMALLINT BufferLength, 
00089     SQLSMALLINT *TextLength)
00090 {
00091     // Validate handle
00092     if( isValidHandle( Handle, HandleType ) != SQL_SUCCESS )
00093         return( SQL_ERROR );
00094 
00095     return( ((CSqlOdbcEnv*)Handle)->err_.SQLGetDiagRec(Handle, Sqlstate, NativeError, MessageText, BufferLength, TextLength) );
00096 }
00097 
00098 SQLRETURN CSqlOdbcError::SQLGetDiagRec(
00099             SQLHANDLE   handle,     // IN
00100             SQLCHAR     *sqlState,  // OUT
00101             SQLINTEGER  *sqlErrCode,// OUT
00102             SQLCHAR     *Msg,       // OUT
00103             SQLSMALLINT  bufLen,    // IN
00104             SQLSMALLINT *textLen)   // OUT
00105 {
00106     ErrorInfo_t *errorInfoTbl;
00107     int code = 0, i = 0;
00108     SQLUINTEGER odbcVersion;
00109     
00110     // Get ODBC Version
00111     switch( hdlType )
00112     {
00113         case SQL_HANDLE_ENV:
00114             odbcVersion = ((CSqlOdbcEnv*) handle)->odbcVersion_; break;
00115         case SQL_HANDLE_DBC:
00116             odbcVersion = ((CSqlOdbcDbc*) handle)->parentEnv_->odbcVersion_; break;
00117         case SQL_HANDLE_STMT:
00118             odbcVersion = ((CSqlOdbcStmt*) handle)->parentDbc_->parentEnv_->odbcVersion_; break;
00119         default:
00120             return( SQL_ERROR );
00121     }
00122 
00123     // If No Error
00124     if( csqlErrCode == OK )
00125         return( SQL_NO_DATA_FOUND );
00126 
00127     // Select Error Msg Table based on ODBC Version
00128     if( odbcVersion >= SQL_OV_ODBC3 )
00129         errorInfoTbl = errorInfoTable_3x;
00130     else
00131         errorInfoTbl = errorInfoTable_2x;
00132 
00133     // Search in table
00134     while( errorInfoTbl[i].csqlErrCode != csqlErrCode ) i++;
00135     if( errorInfoTbl[i].csqlErrCode == csqlErrCode )
00136     {
00137         // Fill error info to user space
00138         strcpy( (char*) sqlState, (char*) errorInfoTbl[i].sqlState );
00139         *sqlErrCode = csqlErrCode;
00140         *textLen = (SQLSMALLINT) strlen( (char*) errorInfoTbl[i].sqlMessage );
00141         if( bufLen > *textLen )
00142             bufLen = *textLen + 1;
00143         strncpy( (char*) Msg, (char*) errorInfoTbl[i].sqlMessage, (int) bufLen );
00144         return( SQL_SUCCESS );
00145     }
00146 
00147     return( SQL_ERROR );
00148 }
00149 
00150 void CSqlOdbcError::printDbg( char *msg )
00151 {
00152 #ifdef DEBUG_INFO
00153     fprintf( stderr, "ODBC DBG: %s\n", msg );
00154 #endif
00155     return;
00156 }

Generated on Mon Jun 9 22:37:14 2008 for csql by  doxygen 1.4.7