Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / source / dbms / ResultCode.cpp
diff --git a/source/dbms/ResultCode.cpp b/source/dbms/ResultCode.cpp
new file mode 100644 (file)
index 0000000..453f78b
--- /dev/null
@@ -0,0 +1,104 @@
+// 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/functions.hpp>
+
+#include <anna/dbms/ResultCode.hpp>
+
+using namespace std;
+using namespace anna::dbms;
+
+bool ResultCode::notFound() const
+throw(anna::RuntimeException) {
+  if(a_errorDecoder == NULL) {
+    string msg(asString());
+    msg += " | Has no decoder for associated error";
+    throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
+  }
+
+  return a_errorDecoder->notFound(a_errorCode);
+}
+
+bool ResultCode::successful() const
+throw(anna::RuntimeException) {
+  if(a_errorDecoder == NULL) {
+    string msg(asString());
+    msg += " | Has no decoder for associated error";
+    throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
+  }
+
+  return a_errorDecoder->successful(a_errorCode);
+}
+
+bool ResultCode::locked() const
+throw(anna::RuntimeException) {
+  if(a_errorDecoder == NULL) {
+    string msg(asString());
+    msg += " | Has no decoder for associated error";
+    throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
+  }
+
+  return a_errorDecoder->locked(a_errorCode);
+}
+
+bool ResultCode::lostConnection() const
+throw(anna::RuntimeException) {
+  if(a_errorDecoder == NULL) {
+    string msg(asString());
+    msg += " | Has no decoder for associated error";
+    throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
+  }
+
+  return a_errorDecoder->lostConnection(a_errorCode);
+}
+
+//
+// No usamos la std::string porque la gran mayoría de las veces la ejecución de las sentencias SQL será
+// correcta => no hará falta reservar ninguna memoria.
+//
+void ResultCode::copy(const char* text)
+throw() {
+  if(text == NULL) {
+    if(a_errorText != NULL) {
+      free(a_errorText);
+      a_errorText = NULL;
+    }
+  } else {
+    char* aux;
+
+    if((aux = anna_strchr((char*) text, '\n')) != NULL)
+      * aux = 0;
+
+    const int textLen = anna_strlen(text);
+
+    if(a_errorText == NULL)
+      a_errorText = strdup(text);
+    else if(anna_strlen(a_errorText) >= textLen)
+      anna_strcpy(a_errorText, text);
+    else {
+      free(a_errorText);
+      a_errorText = strdup(text);
+    }
+  }
+}
+
+std::string ResultCode::asString() const
+throw() {
+  std::string result("dbms::ResultCode { Error: ");
+  result += functions::asString(a_errorCode);
+  result += " | Error: ";
+  result += (a_errorText == NULL) ? "(null)" : a_errorText;
+  result += " | Correct: ";
+
+  if(a_errorDecoder != NULL)
+    result += functions::asString(successful());
+  else
+    result += "<No ErrorDecoder>";
+
+  return result += " }";
+}