84bb2a10ff898627a1c6c8abef1cd2c67e693e1e
[anna.git] / source / dbms.oracle / ResultCode.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <stdlib.h>
10 #include <stdio.h>
11
12 #include <oci.h>
13
14 #include <anna/config/defines.hpp>
15 #include <anna/core/tracing/Logger.hpp>
16
17 #include <anna/dbms.oracle/ResultCode.hpp>
18
19 using namespace anna;
20 using namespace anna::dbms;
21
22 oracle::ResultCode::ErrorDecoder oracle::ResultCode::st_errorDecoder;
23
24 oracle::ResultCode::ResultCode(const int status, OCIError* error) :
25   dbms::ResultCode(0, NULL, &st_errorDecoder) {
26   char errorText [dbms::ResultCode::MaxErrorLen];
27   int errorCode = status;
28
29   if(status == OCI_SUCCESS) {
30     dbms::ResultCode::set(OCI_SUCCESS, NULL);
31     return;
32   } else if(status == OCI_SUCCESS_WITH_INFO) {
33     OCIErrorGet(error, (ub4) 1, (text*) 0, &errorCode, (unsigned char*) errorText, (ub4) sizeof(errorText), OCI_HTYPE_ERROR);
34     dbms::ResultCode::set(OCI_SUCCESS, errorText);
35     LOGINFORMATION(Logger::information(asString(), ANNA_FILE_LOCATION));
36     return;
37   }
38
39   char* aux;
40
41   switch(status) {
42   case OCI_ERROR:
43     OCIErrorGet(error, (ub4) 1, (text*) 0, &errorCode, (unsigned char*) errorText, (ub4) sizeof(errorText), OCI_HTYPE_ERROR);
44     break;
45   case OCI_NEED_DATA: anna_strcpy(errorText, "OCI | Need data"); break;
46   case OCI_NO_DATA: anna_strcpy(errorText, "OCI | Data not found"); break;
47   case OCI_INVALID_HANDLE: anna_strcpy(errorText, "OCI | Invalid handle"); break;
48   default: sprintf(errorText, "OCI | Error code: %d", status); break;
49   }
50
51   dbms::ResultCode::set(errorCode, errorText);
52 }
53
54 bool oracle::ResultCode::ErrorDecoder::notFound(const int errorCode) const
55 throw() {
56   return errorCode == OCI_NO_DATA;
57 }
58
59 bool oracle::ResultCode::ErrorDecoder::successful(const int errorCode) const
60 throw() {
61   return errorCode == OCI_SUCCESS;
62 }
63
64 bool oracle::ResultCode::ErrorDecoder::lostConnection(const int errorCode) const
65 throw() {
66   return errorCode == 28 || errorCode == 3113 || errorCode == 3114 || errorCode == 1012 || errorCode == 12570 || errorCode == 12571;
67 }