1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
39 #include <anna/config/defines.hpp>
40 #include <anna/core/functions.hpp>
42 #include <anna/dbms/Date.hpp>
46 using namespace anna::dbms;
48 Date::Date(const bool isNulleable, const char* format) :
49 Data(Type::Date, MaxDateSize, isNulleable) {
50 Data::setBuffer(a_buffer);
52 a_format = (format == NULL) ? NULL : strdup(format);
53 anna_memset(&a_value, 0, sizeof(a_value));
56 Date::Date(const Data::Type::_v type, const bool isNulleable, const char* format) :
57 Data(type, MaxDateSize, isNulleable) {
58 Data::setBuffer(a_buffer);
60 a_format = (format == NULL) ? NULL : strdup(format);
61 anna_memset(&a_value, 0, sizeof(a_value));
64 Date::Date(const Date& other) :
66 Data::setBuffer(a_buffer);
68 a_format = (other.a_format == NULL) ? NULL : strdup(other.a_format);
69 anna_memcpy(&a_value, &other.a_value, sizeof(a_value));
77 const char* dbms::Date::getCStringValue() const
81 if((format = a_format) == NULL)
82 format = "%d/%m/%Y %H:%M:%S";
84 return (strftime(const_cast <Date*>(this)->a_buffer, MaxDateSize, format, &a_value) == 0) ? NULL : a_buffer;
87 Date& Date::operator = (const Date & other)
88 throw(RuntimeException) {
90 if(other.isNull() == true) {
92 anna_memset(&a_value, 0, sizeof(a_value));
95 anna_memcpy(&a_value, &other.a_value, sizeof(a_value));
102 void Date::setValue(const char* str)
103 throw(RuntimeException) {
104 if(a_format == NULL) {
105 string msg(asString());
106 msg += " | anna::dbms::Data::setValue (const char*) requires format especification";
107 throw RuntimeException(msg, ANNA_FILE_LOCATION);
111 char* r = strptime(str, a_format, &aux);
114 string msg(asString());
115 msg += " | String: ";
117 msg += " | Can't be interpreted as valid date";
118 throw RuntimeException(msg, ANNA_FILE_LOCATION);
121 Data::setNull(false);
122 anna_memcpy(&a_value, &aux, sizeof(a_value));
125 void Date::setValue(const Second &second)
126 throw(RuntimeException) {
127 tm* aux = localtime((time_t*) & second);
130 string msg(asString());
131 msg += functions::asText(" | Second: ", (int) second);
132 msg += " | Can't be interpreted as valid date";
133 throw RuntimeException(msg, ANNA_FILE_LOCATION);
136 Data::setNull(false);
137 anna_memcpy(&a_value, aux, sizeof(a_value));
140 void dbms::Date::set(const char* what, int& variable, const int value, const int min, const int max)
141 throw(RuntimeException) {
144 msg += functions::asText(" must be greater than or equal to ", min);
145 throw RuntimeException(msg, ANNA_FILE_LOCATION);
148 if(value > max && max != -1) {
150 msg += functions::asText(" must be less than or equal to ", max);
151 throw RuntimeException(msg, ANNA_FILE_LOCATION);
154 Data::setNull(false);
158 string dbms::Date::asString() const
161 string result("dbms::Date { ");
162 result += dbms::Data::asString();
163 result += " | Format: ";
164 result += (a_format == NULL) ? "<null>" : a_format;
165 result += " | Value: ";
167 if(Data::isNull() == false) {
168 if((cstring = getCStringValue()) == NULL)
169 result += "<not valid>";
175 return result += " }";