Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / source / dbms / Date.cpp
diff --git a/source/dbms/Date.cpp b/source/dbms/Date.cpp
new file mode 100644 (file)
index 0000000..5c9afaf
--- /dev/null
@@ -0,0 +1,149 @@
+// 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 <string.h>
+
+#include <anna/config/defines.hpp>
+#include <anna/core/functions.hpp>
+
+#include <anna/dbms/Date.hpp>
+
+using namespace std;
+using namespace anna;
+using namespace anna::dbms;
+
+Date::Date(const bool isNulleable, const char* format) :
+  Data(Type::Date, MaxDateSize, isNulleable) {
+  Data::setBuffer(a_buffer);
+  a_buffer [0] = 0;
+  a_format = (format == NULL) ? NULL : strdup(format);
+  anna_memset(&a_value, 0, sizeof(a_value));
+}
+
+Date::Date(const Data::Type::_v type,  const bool isNulleable, const char* format) :
+  Data(type, MaxDateSize, isNulleable) {
+  Data::setBuffer(a_buffer);
+  a_buffer [0] = 0;
+  a_format = (format == NULL) ? NULL : strdup(format);
+  anna_memset(&a_value, 0, sizeof(a_value));
+}
+
+Date::Date(const Date& other) :
+  Data(other) {
+  Data::setBuffer(a_buffer);
+  a_buffer [0] = 0;
+  a_format = (other.a_format == NULL) ? NULL : strdup(other.a_format);
+  anna_memcpy(&a_value, &other.a_value, sizeof(a_value));
+}
+
+Date::~Date() {
+  if(a_format != NULL)
+    free(a_format);
+}
+
+const char* dbms::Date::getCStringValue() const
+throw() {
+  const char* format;
+
+  if((format = a_format) == NULL)
+    format = "%d/%m/%Y %H:%M:%S";
+
+  return (strftime(const_cast <Date*>(this)->a_buffer, MaxDateSize, format, &a_value) == 0) ? NULL : a_buffer;
+}
+
+Date& Date::operator = (const Date & other)
+throw(RuntimeException) {
+  if(this != &other) {
+    if(other.isNull() == true) {
+      setNull(true);
+      anna_memset(&a_value, 0, sizeof(a_value));
+    } else {
+      setNull(false);
+      anna_memcpy(&a_value, &other.a_value, sizeof(a_value));
+    }
+  }
+
+  return *this;
+}
+
+void Date::setValue(const char* str)
+throw(RuntimeException) {
+  if(a_format == NULL) {
+    string msg(asString());
+    msg += "  | anna::dbms::Data::setValue (const char*) requires format especification";
+    throw RuntimeException(msg, ANNA_FILE_LOCATION);
+  }
+
+  tm aux;
+  char* r = strptime(str, a_format, &aux);
+
+  if(r == NULL) {
+    string msg(asString());
+    msg += " | String: ";
+    msg += str;
+    msg += " | Can't be interpreted as valid date";
+    throw RuntimeException(msg, ANNA_FILE_LOCATION);
+  }
+
+  Data::setNull(false);
+  anna_memcpy(&a_value, &aux, sizeof(a_value));
+}
+
+void Date::setValue(const Second &second)
+throw(RuntimeException) {
+  tm* aux = localtime((time_t*) & second);
+
+  if(aux == NULL) {
+    string msg(asString());
+    msg += functions::asText(" | Second: ", (int) second);
+    msg += " | Can't be interpreted as valid date";
+    throw RuntimeException(msg, ANNA_FILE_LOCATION);
+  }
+
+  Data::setNull(false);
+  anna_memcpy(&a_value, aux, sizeof(a_value));
+}
+
+void dbms::Date::set(const char* what, int& variable, const int value, const int min, const int max)
+throw(RuntimeException) {
+  if(value < min) {
+    string msg(what);
+    msg += functions::asText(" must be greater than or equal to ", min);
+    throw RuntimeException(msg, ANNA_FILE_LOCATION);
+  }
+
+  if(value > max && max != -1) {
+    string msg(what);
+    msg += functions::asText(" must be less than or equal to ", max);
+    throw RuntimeException(msg, ANNA_FILE_LOCATION);
+  }
+
+  Data::setNull(false);
+  variable = value;
+}
+
+string dbms::Date::asString() const
+throw() {
+  const char* cstring;
+  string result("dbms::Date { ");
+  result += dbms::Data::asString();
+  result += " | Format: ";
+  result += (a_format == NULL) ? "<null>" : a_format;
+  result += " | Value: ";
+
+  if(Data::isNull() == false) {
+    if((cstring = getCStringValue()) == NULL)
+      result += "<not valid>";
+    else
+      result += cstring;
+  } else
+    result += "<null>";
+
+  return result += " }";
+}
+