2ddf7e639cb351ecfc18ed8282a4b4f603213167
[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   bool result = false;
37
38   if(connection == NULL) {
39     std::string msg(ssaa->asString());
40     msg += " | ";
41     msg += asString();
42     msg += " | Cannot execute this method with dbms::Connection == NULL";
43     throw RuntimeException(msg, ANNA_FILE_LOCATION);
44   }
45
46   dbms::Statement* statement(getStatement());
47
48   if(statement == NULL) {
49     std::string msg(ssaa->asString());
50     msg += " | ";
51     msg += asString();
52     msg += " | Has no SQL sentence associated";
53     throw RuntimeException(msg, ANNA_FILE_LOCATION);
54   }
55
56   LOGDEBUG(
57     string msg("dbos::Accesor::load | ");
58     msg += ssaa->asString();
59     msg += " | ";
60     msg += asString();
61     Logger::debug(msg, ANNA_FILE_LOCATION);
62   );
63   dbms::ResultCode resultCode = connection->execute(statement);    // (1)
64
65   if(resultCode.notFound() == true) {
66     if(a_emodeIsNull == true) {
67       if(ssaa->getErrorCode() != StorageArea::NoExceptionWhenNotFound) {
68         std::string msg(ssaa->asString());
69         msg += " | ";
70         msg += asString();
71         msg += " | Register not found";
72         RuntimeException ex(msg, ANNA_FILE_LOCATION);
73         ex.setErrorCode(ssaa->getErrorCode());
74         throw ex;
75       } else
76         return false;
77     } else {
78       std::string msg(ssaa->asString());
79       msg += " | ";
80       msg += asString();
81       msg += " | Register not found";
82
83       if(a_exceptionMode == Exception::Mode::Ignore) {
84         Logger::debug(msg, ANNA_FILE_LOCATION);
85         return false;
86       }
87
88       if(a_exceptionMode == Exception::Mode::Throw) {
89         RuntimeException ex(msg, ANNA_FILE_LOCATION);
90         ex.setErrorCode(ssaa->getErrorCode());
91         throw ex;
92       } else if(Logger::isActive(Logger::Warning)) {
93         Logger::warning(msg, ANNA_FILE_LOCATION);
94       }
95     }
96   }
97
98   if(resultCode.successful() == false) {
99     string msg(ssaa->getName());
100     msg += " | ";
101     msg += asString();
102     throw dbms::DatabaseException(msg, resultCode, ANNA_FILE_LOCATION);
103   }
104
105   return statement->fetch();
106 }