Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / source / dbos / Accesor.cpp
diff --git a/source/dbos/Accesor.cpp b/source/dbos/Accesor.cpp
new file mode 100644 (file)
index 0000000..f77d2aa
--- /dev/null
@@ -0,0 +1,105 @@
+// ANNA - Anna is Not Nothingness Anymore                                                         //
+//                                                                                                //
+// (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
+//                                                                                                //
+// See project site at http://redmine.teslayout.com/projects/anna-suite                           //
+// See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
+
+
+#include <anna/core/tracing/Logger.hpp>
+
+#include <anna/dbms/Database.hpp>
+#include <anna/dbms/Connection.hpp>
+#include <anna/dbms/Statement.hpp>
+#include <anna/dbms/ResultCode.hpp>
+
+#include <anna/dbos/Accesor.hpp>
+#include <anna/dbos/StorageArea.hpp>
+
+using namespace std;
+using namespace anna;
+
+/*virtual*/
+dbos::Accesor::~Accesor() {
+  if(a_statement != NULL && a_database != NULL)
+    a_database->releaseStatement(a_statement);
+}
+
+//------------------------------------------------------------------------------------------
+// Transfiere la informacion del medio fisico al 'Loader' concreto.
+//
+// (1) Ojo!! Ejecuta la sentencia SQL y carga el primer registro en el area de intercambio
+//     Slo habr�que invocar al 'fetch' para coger los siguientes registros'
+//------------------------------------------------------------------------------------------
+bool dbos::Accesor::load(dbms::Connection* connection, const dbos::StorageArea* ssaa)
+throw(RuntimeException, dbms::DatabaseException) {
+
+  if(connection == NULL) {
+    std::string msg(ssaa->asString());
+    msg += " | ";
+    msg += asString();
+    msg += " | Cannot execute this method with dbms::Connection == NULL";
+    throw RuntimeException(msg, ANNA_FILE_LOCATION);
+  }
+
+  dbms::Statement* statement(getStatement());
+
+  if(statement == NULL) {
+    std::string msg(ssaa->asString());
+    msg += " | ";
+    msg += asString();
+    msg += " | Has no SQL sentence associated";
+    throw RuntimeException(msg, ANNA_FILE_LOCATION);
+  }
+
+  LOGDEBUG(
+    string msg("dbos::Accesor::load | ");
+    msg += ssaa->asString();
+    msg += " | ";
+    msg += asString();
+    Logger::debug(msg, ANNA_FILE_LOCATION);
+  );
+  dbms::ResultCode resultCode = connection->execute(statement);    // (1)
+
+  if(resultCode.notFound() == true) {
+    if(a_emodeIsNull == true) {
+      if(ssaa->getErrorCode() != StorageArea::NoExceptionWhenNotFound) {
+        std::string msg(ssaa->asString());
+        msg += " | ";
+        msg += asString();
+        msg += " | Register not found";
+        RuntimeException ex(msg, ANNA_FILE_LOCATION);
+        ex.setErrorCode(ssaa->getErrorCode());
+        throw ex;
+      } else
+        return false;
+    } else {
+      std::string msg(ssaa->asString());
+      msg += " | ";
+      msg += asString();
+      msg += " | Register not found";
+
+      if(a_exceptionMode == Exception::Mode::Ignore) {
+        Logger::debug(msg, ANNA_FILE_LOCATION);
+        return false;
+      }
+
+      if(a_exceptionMode == Exception::Mode::Throw) {
+        RuntimeException ex(msg, ANNA_FILE_LOCATION);
+        ex.setErrorCode(ssaa->getErrorCode());
+        throw ex;
+      } else if(Logger::isActive(Logger::Warning)) {
+        Logger::warning(msg, ANNA_FILE_LOCATION);
+      }
+    }
+  }
+
+  if(resultCode.successful() == false) {
+    string msg(ssaa->getName());
+    msg += " | ";
+    msg += asString();
+    throw dbms::DatabaseException(msg, resultCode, ANNA_FILE_LOCATION);
+  }
+
+  return statement->fetch();
+}