Remove warnings
[anna.git] / source / dbos / Accesor.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 <anna/core/tracing/Logger.hpp>
10
11 #include <anna/dbms/Database.hpp>
12 #include <anna/dbms/Connection.hpp>
13 #include <anna/dbms/Statement.hpp>
14 #include <anna/dbms/ResultCode.hpp>
15
16 #include <anna/dbos/Accesor.hpp>
17 #include <anna/dbos/StorageArea.hpp>
18
19 using namespace std;
20 using namespace anna;
21
22 /*virtual*/
23 dbos::Accesor::~Accesor() {
24   if(a_statement != NULL && a_database != NULL)
25     a_database->releaseStatement(a_statement);
26 }
27
28 //------------------------------------------------------------------------------------------
29 // Transfiere la informacion del medio fisico al 'Loader' concreto.
30 //
31 // (1) Ojo!! Ejecuta la sentencia SQL y carga el primer registro en el area de intercambio
32 //     Slo habr�que invocar al 'fetch' para coger los siguientes registros'
33 //------------------------------------------------------------------------------------------
34 bool dbos::Accesor::load(dbms::Connection* connection, const dbos::StorageArea* ssaa)
35 throw(RuntimeException, dbms::DatabaseException) {
36
37   if(connection == NULL) {
38     std::string msg(ssaa->asString());
39     msg += " | ";
40     msg += asString();
41     msg += " | Cannot execute this method with dbms::Connection == NULL";
42     throw RuntimeException(msg, ANNA_FILE_LOCATION);
43   }
44
45   dbms::Statement* statement(getStatement());
46
47   if(statement == NULL) {
48     std::string msg(ssaa->asString());
49     msg += " | ";
50     msg += asString();
51     msg += " | Has no SQL sentence associated";
52     throw RuntimeException(msg, ANNA_FILE_LOCATION);
53   }
54
55   LOGDEBUG(
56     string msg("dbos::Accesor::load | ");
57     msg += ssaa->asString();
58     msg += " | ";
59     msg += asString();
60     Logger::debug(msg, ANNA_FILE_LOCATION);
61   );
62   dbms::ResultCode resultCode = connection->execute(statement);    // (1)
63
64   if(resultCode.notFound() == true) {
65     if(a_emodeIsNull == true) {
66       if(ssaa->getErrorCode() != StorageArea::NoExceptionWhenNotFound) {
67         std::string msg(ssaa->asString());
68         msg += " | ";
69         msg += asString();
70         msg += " | Register not found";
71         RuntimeException ex(msg, ANNA_FILE_LOCATION);
72         ex.setErrorCode(ssaa->getErrorCode());
73         throw ex;
74       } else
75         return false;
76     } else {
77       std::string msg(ssaa->asString());
78       msg += " | ";
79       msg += asString();
80       msg += " | Register not found";
81
82       if(a_exceptionMode == Exception::Mode::Ignore) {
83         Logger::debug(msg, ANNA_FILE_LOCATION);
84         return false;
85       }
86
87       if(a_exceptionMode == Exception::Mode::Throw) {
88         RuntimeException ex(msg, ANNA_FILE_LOCATION);
89         ex.setErrorCode(ssaa->getErrorCode());
90         throw ex;
91       } else if(Logger::isActive(Logger::Warning)) {
92         Logger::warning(msg, ANNA_FILE_LOCATION);
93       }
94     }
95   }
96
97   if(resultCode.successful() == false) {
98     string msg(ssaa->getName());
99     msg += " | ";
100     msg += asString();
101     throw dbms::DatabaseException(msg, resultCode, ANNA_FILE_LOCATION);
102   }
103
104   return statement->fetch();
105 }