src/diag.cpp

Go to the documentation of this file.
00001 //
00002 // File:        src/diag.cpp
00003 // Object:      Object handling the diagnostics (generally errors)
00004 //
00005 // Copyright (C)   2008 Made to Order Software Corp.
00006 //
00007 // This program is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 2 of the License, or
00010 // (at your option) any later version.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to:
00019 //
00020 //   Free Software Foundation, Inc.
00021 //   51 Franklin St, Fifth Floor
00022 //   Boston, MA  02110-1301
00023 //   USA
00024 //
00025 
00026 #include        "diag.h"
00027 
00028 
00029 namespace odbcpp
00030 {
00031 
00032 
00041 diag_t::diag_t() :
00042         // f_server -- auto-init
00043         // f_connection -- auto-init
00044         // f_message -- auto-init
00045         // f_odbc_state -- auto-init
00046         f_native_errno(0)
00047 {
00048 }
00049 
00100 diagnostic::diagnostic(SQLINTEGER odbcpp_errno, const std::string& message)
00101 {
00102         diag_t d;
00103 
00104         //d.f_server = ... -- none, empty is good
00105         //d.f_connection = ... -- none, empty is good
00106         d.f_message = message;
00107         d.f_odbc_state = "HY000";
00108         d.f_native_errno = odbcpp_errno;
00109 
00110         f_diag.push(d);
00111 }
00112 
00113 
00122 diagnostic::diagnostic(SQLSMALLINT handle_type, SQLHANLE handle)
00123 {
00124         SQLSMALLINT     record;
00125 
00126         // get all the possible state records
00127         for(record = 1;; ++record) {
00128                 diag_t d;
00129                 if(!get_string(handle_type, handle, record, SQL_DIAG_SERVER_NAME, d.f_server)) {
00130                         return;
00131                 }
00132                 if(!get_string(handle_type, handle, record, SQL_DIAG_CONNECTION_NAME, d.f_connection)) {
00133                         return;
00134                 }
00135                 if(!get_string(handle_type, handle, record, SQL_DIAG_MESSAGE_TEXT, d.f_message)) {
00136                         return;
00137                 }
00138                 if(!get_integer(handle_type, handle, record, , d.f_native_errno)) {
00139                         return;
00140                 }
00141                 if(!get_string(handle_type, handle, record, SQL_DIAG_SQLSTATE, d.f_odbc_state)) {
00142                         return;
00143                 }
00144                 f_diag.push(d);
00145         }
00146 }
00147 
00148 
00211 bool diagnostic::get_string(SQLSMALLINT handle_type, SQLHANLE handle,
00212                 SQLSMALLINT record, SQLSMALLINT identifier, std::string& string);
00213 {
00214         SQLCHAR         buffer[1024];
00215         SQLSMALLINT     size;
00216         SQLRETURN       return_code;
00217 
00218         // make the string empty by default
00219         string.clear();
00220 
00221         // retrieve the diagnostic
00222         size = sizeof(buffer);  // as far as I know, size should be an OUT only...
00223         return_code = SQLGetDiagField(handle_type, handle, record,
00224                 identifier, buffer, sizeof(buffer), &size);
00225 
00226         switch(return_code) {
00227         case SQL_SUCCESS:
00228         case SQL_SUCCESS_WITH_INFO:     // truncation occured... more than 1kb message?!
00229                 break;
00230 
00231         // at this time, we do not need to have this seperate
00232         //case SQL_NO_DATA: -- we're done
00233         //      return false;
00234 
00235         default:
00236         // SQL_INVALID_HANDLE or SQL_ERROR
00237                 // some error occured
00238                 // TODO: what do we do here?!
00239                 return false;
00240 
00241         }
00242         // make 100% sure that the string is null terminated
00243         buffer[sizeof(buffer) - 1] = '\0';
00244         string = buffer;
00245 
00246         return true;
00247 }
00248 
00249 
00269 bool diagnostic::get_integer(SQLSMALLINT handle_type, SQLHANLE handle,
00270                 SQLSMALLINT record, SQLSMALLINT identifier, SQLINTEGER& integer)
00271 {
00272         SQLSMALLINT     size;
00273         SQLRETURN       return_code;
00274 
00275         // set some default value, just in case
00276         integer = 0;
00277 
00278         size = sizeof(integer);
00279         return_code = SQLGetDiagField(handle_type, handle, record,
00280                 identifier, integer, sizeof(integer), &size);
00281         switch(return_code) {
00282         case SQL_SUCCESS:
00283         case SQL_SUCCESS_WITH_INFO:     // truncation occured... more than 1kb message?!
00284                 break;
00285 
00286         // at this time, we do not need to have this seperate
00287         //case SQL_NO_DATA: -- we're done
00288         //      return false;
00289 
00290         default:
00291         // SQL_INVALID_HANDLE or SQL_ERROR
00292                 // some error occured
00293                 // TODO: what do we do here?!
00294                 return false;
00295 
00296         }
00297 
00298         return true;
00299 }
00300 
00301 
00322 }       // namespace odbcpp
00323 
00324 
00325 

Generated on Sat May 31 22:23:22 2008 for odbcpp by  doxygen 1.4.7