Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / source / dbms / Sentence.cpp
diff --git a/source/dbms/Sentence.cpp b/source/dbms/Sentence.cpp
new file mode 100644 (file)
index 0000000..34dfaf3
--- /dev/null
@@ -0,0 +1,99 @@
+// 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/xml/Node.hpp>
+#include <anna/xml/Attribute.hpp>
+
+#include <anna/dbms/Database.hpp>
+#include <anna/dbms/Connection.hpp>
+#include <anna/dbms/Sentence.hpp>
+#include <anna/dbms/Statement.hpp>
+
+using namespace std;
+using namespace anna;
+
+const string& dbms::Sentence::getName() const
+throw() {
+  static string empty;
+  return (a_dbStatement == NULL) ? empty : a_dbStatement->getName();
+}
+
+void dbms::Sentence::initialize(dbms::Database& database)
+throw(RuntimeException) {
+  Guard guard(this, "anna::dbms::Sentence (initialize)");
+  a_dbStatement = do_initialize(database);
+}
+
+//-------------------------------------------------------------------------------------
+// Ojo!! No activamos la seccion critica en este metodo porque debera estar
+// activa externamente ... para recoger datos multiples, etc, etc.
+//-------------------------------------------------------------------------------------
+dbms::ResultCode dbms::Sentence::execute(dbms::Connection& connection, dbms::Statement* statement)
+throw(RuntimeException) {
+  using namespace anna::dbms;
+  ResultCode result;
+
+  try {
+    result = connection.execute(statement);
+
+    if(result.successful() == false) {
+      if(a_mode == Mode::SilentWhenNotFound && result.notFound() == true)
+        return result;
+
+      throw DatabaseException(result, ANNA_FILE_LOCATION);
+    }
+  } catch(DatabaseException& edb) {
+    string msg("Sentence: ");
+    msg += getName();
+    msg += " | ";
+    msg += edb.getText();
+    throw RuntimeException(msg, ANNA_FILE_LOCATION);
+  }
+
+  return result;
+}
+
+bool dbms::Sentence::fetch()
+throw(RuntimeException) {
+  bool result = false;
+
+  try {
+    result = a_dbStatement->fetch();
+  } catch(dbms::DatabaseException& edb) {
+    throw RuntimeException(edb);
+  }
+
+  return result;
+}
+
+string dbms::Sentence::asString() const
+/*virtual*/
+throw() {
+  string result("dbms::Sentence { Mode: ");
+  result += (a_mode == Mode::SilentWhenNotFound) ? "SilentWhenNotFound" : "ExceptionWhenNotFound";
+  result += " | ";
+
+  if(a_dbStatement != NULL)
+    result += a_dbStatement->asString();
+  else
+    result += "dbms::Statement: <null>";
+
+  return result += " }";
+}
+
+/*virtual*/
+xml::Node* dbms::Sentence::asXML(xml::Node* parent) const
+throw() {
+  xml::Node* result = parent->createChild("dbms.Sentence");
+  result->createAttribute("Mode", (a_mode == Mode::SilentWhenNotFound) ? "SilentWhenNotFound" : "ExceptionWhenNotFound");
+
+  if(a_dbStatement)
+    a_dbStatement->asXML(result);
+
+  return result;
+}