windows changes
[csql.git] / src / odbc / odbcError.cxx
blob88daae3fa9964058264be811de3c561b4bc69edf
1 #include "odbcCommon.h"
3 // Global error object definition
4 CSqlOdbcError globalError( SQL_HANDLE_ENV );
6 CSqlOdbcError::CSqlOdbcError( void )
8 fprintf(stderr,"This should not happen!!\n");
9 exit(1);
12 CSqlOdbcError::CSqlOdbcError( long handleType ) :
13 csqlErrCode ( OK ), count(0),
14 hdlType( handleType )
17 void CSqlOdbcError::set( long err )
19 csqlErrCode = err;
20 count = 0;
21 if( err != OK );
22 printStr( SQL_OV_ODBC3 );
25 SQLRETURN CSqlOdbcError::printStr( SQLUINTEGER odbcVersion )
27 ErrorInfo_t *errorInfoTbl;
28 int code = 0, i = 0;
30 #ifndef DEBUG_INFO
31 return( SQL_SUCCESS );
32 #endif
34 // Select error table.
35 if( odbcVersion >= SQL_OV_ODBC3 )
36 errorInfoTbl = errorInfoTable_3x;
37 else
38 errorInfoTbl = errorInfoTable_2x;
40 // Search in table.
41 while( errorInfoTbl[i].csqlErrCode != csqlErrCode && errorInfoTbl[i].csqlErrCode != ERROR_ENDOFTABLE ) i++;
43 // Print details, if found
44 if( errorInfoTbl[i].csqlErrCode == csqlErrCode )
46 fprintf( stderr, "[NativeCode: %ld][SqlState: %s][Message: %s].\n",
47 csqlErrCode,
48 (char*) errorInfoTbl[i].sqlState,
49 (char*) errorInfoTbl[i].sqlMessage );
51 return( SQL_SUCCESS );
54 return( SQL_ERROR );
57 SQLRETURN SQLError(
58 SQLHENV EnvironmentHandle, // IN
59 SQLHDBC ConnectionHandle, // IN
60 SQLHSTMT StatementHandle, // IN
61 SQLCHAR *Sqlstate, // OUT
62 SQLINTEGER *NativeError, // OUT
63 SQLCHAR *MessageText, // OUT
64 SQLSMALLINT BufferLength, // IN
65 SQLSMALLINT *TextLength) // OUT
67 // Call SQLGetDiagRec() on handle whichever is valid.
68 if( isValidHandle( EnvironmentHandle, SQL_HANDLE_ENV ) == SQL_SUCCESS )
69 return( ((CSqlOdbcEnv*)EnvironmentHandle)->err_.SQLGetDiagRec(EnvironmentHandle, Sqlstate, NativeError, MessageText, BufferLength, TextLength) );
71 if( isValidHandle( ConnectionHandle, SQL_HANDLE_DBC ) == SQL_SUCCESS )
72 return( ((CSqlOdbcDbc*)ConnectionHandle)->err_.SQLGetDiagRec(ConnectionHandle, Sqlstate, NativeError, MessageText, BufferLength, TextLength) );
74 if( isValidHandle( StatementHandle, SQL_HANDLE_STMT ) == SQL_SUCCESS )
75 return( ((CSqlOdbcStmt*)StatementHandle)->err_.SQLGetDiagRec(StatementHandle, Sqlstate, NativeError, MessageText, BufferLength, TextLength) );
77 return( SQL_INVALID_HANDLE );
80 // ODBC API's
81 SQLRETURN SQLGetDiagRec(
82 SQLSMALLINT HandleType,
83 SQLHANDLE Handle,
84 SQLSMALLINT RecNumber,
85 SQLCHAR *Sqlstate,
86 SQLINTEGER *NativeError,
87 SQLCHAR *MessageText,
88 SQLSMALLINT BufferLength,
89 SQLSMALLINT *TextLength)
91 // Validate handle
92 if( isValidHandle( Handle, HandleType ) != SQL_SUCCESS )
93 return( SQL_ERROR );
95 return( ((CSqlOdbcEnv*)Handle)->err_.SQLGetDiagRec(Handle, Sqlstate, NativeError, MessageText, BufferLength, TextLength) );
98 SQLRETURN CSqlOdbcError::SQLGetDiagRec(
99 SQLHANDLE handle, // IN
100 SQLCHAR *sqlState, // OUT
101 SQLINTEGER *sqlErrCode,// OUT
102 SQLCHAR *Msg, // OUT
103 SQLSMALLINT bufLen, // IN
104 SQLSMALLINT *textLen) // OUT
106 ErrorInfo_t *errorInfoTbl;
107 int code = 0, i = 0;
108 SQLUINTEGER odbcVersion;
110 // Get ODBC Version
111 switch( hdlType )
113 case SQL_HANDLE_ENV:
114 odbcVersion = ((CSqlOdbcEnv*) handle)->odbcVersion_; break;
115 case SQL_HANDLE_DBC:
116 odbcVersion = ((CSqlOdbcDbc*) handle)->parentEnv_->odbcVersion_; break;
117 case SQL_HANDLE_STMT:
118 odbcVersion = ((CSqlOdbcStmt*) handle)->parentDbc_->parentEnv_->odbcVersion_; break;
119 default:
120 return( SQL_ERROR );
123 // If No Error
124 if( csqlErrCode == OK )
125 return( SQL_NO_DATA_FOUND );
127 // Select Error Msg Table based on ODBC Version
128 if( odbcVersion >= SQL_OV_ODBC3 )
129 errorInfoTbl = errorInfoTable_3x;
130 else
131 errorInfoTbl = errorInfoTable_2x;
132 // Search in table
133 while( errorInfoTbl[i].csqlErrCode != csqlErrCode ) i++;
134 count++;
135 if( errorInfoTbl[i].csqlErrCode == csqlErrCode )
137 // Fill error info to user space
138 strcpy( (char*) sqlState, (char*) errorInfoTbl[i].sqlState );
139 *sqlErrCode = csqlErrCode;
140 *textLen = (SQLSMALLINT) strlen( (char*) errorInfoTbl[i].sqlMessage );
141 if( bufLen > *textLen )
142 bufLen = *textLen + 1;
143 strncpy( (char*) Msg, (char*) errorInfoTbl[i].sqlMessage, (int) bufLen );
144 if(count < 2)
145 return( SQL_SUCCESS );
146 else
147 return( SQL_ERROR );
149 return( SQL_SUCCESS );
152 void CSqlOdbcError::printDbg( char *msg )
154 #ifdef DEBUG_INFO
155 fprintf( stderr, "ODBC DBG: %s\n", msg );
156 #endif
157 return;