00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022 #include <boost/lexical_cast.hpp>
00023
00024 #include <drizzled/field/date.h>
00025 #include <drizzled/error.h>
00026 #include <drizzled/table.h>
00027 #include <drizzled/temporal.h>
00028 #include <drizzled/session.h>
00029 #include <drizzled/time_functions.h>
00030 #include <drizzled/current_session.h>
00031 #include <drizzled/system_variables.h>
00032
00033 #include <math.h>
00034
00035 #include <sstream>
00036 #include <string>
00037
00038 namespace drizzled
00039 {
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 int Field_date::store(const char *from,
00066 uint32_t len,
00067 const charset_info_st * const )
00068 {
00069
00070
00071
00072
00073
00074
00075 ASSERT_COLUMN_MARKED_FOR_WRITE;
00076 DateTime temporal;
00077 if (! temporal.from_string(from, (size_t) len))
00078 {
00079 my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), from);
00080 return 2;
00081 }
00082
00083 uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
00084 int4store(ptr, int_value);
00085 return 0;
00086 }
00087
00088 int Field_date::store(double from)
00089 {
00090 ASSERT_COLUMN_MARKED_FOR_WRITE;
00091 if (from < 0.0 || from > 99991231235959.0)
00092 {
00093
00094 std::stringstream ss;
00095 std::string tmp;
00096 ss.precision(18);
00097 ss << from; ss >> tmp;
00098
00099 my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
00100 return 2;
00101 }
00102 return Field_date::store((int64_t) rint(from), false);
00103 }
00104
00105 int Field_date::store(int64_t from, bool)
00106 {
00107
00108
00109
00110
00111 ASSERT_COLUMN_MARKED_FOR_WRITE;
00112 DateTime temporal;
00113 if (! temporal.from_int64_t(from))
00114 {
00115
00116 std::string tmp(boost::lexical_cast<std::string>(from));
00117
00118 my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
00119 return 2;
00120 }
00121
00122
00123 uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
00124 int4store(ptr, int_value);
00125
00126 return 0;
00127 }
00128
00129 int Field_date::store_time(type::Time <ime,
00130 type::timestamp_t time_type)
00131 {
00132 long tmp;
00133 int error= 0;
00134 if (time_type == type::DRIZZLE_TIMESTAMP_DATE || time_type == type::DRIZZLE_TIMESTAMP_DATETIME)
00135 {
00136 tmp= ltime.year*10000 + ltime.month*100 + ltime.day;
00137
00138 Session *session= getTable() ? getTable()->in_use : current_session;
00139 type::cut_t cut_error= type::VALID;
00140 if (ltime.check(tmp != 0,
00141 (TIME_FUZZY_DATE |
00142 (session->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), cut_error))
00143 {
00144 char buff[type::Time::MAX_STRING_LENGTH];
00145 String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
00146 ltime.convert(str, type::DRIZZLE_TIMESTAMP_DATE);
00147 set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
00148 str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
00149 }
00150
00151 error= static_cast<int>(cut_error);
00152
00153 if (not error && ltime.time_type != type::DRIZZLE_TIMESTAMP_DATE &&
00154 (ltime.hour || ltime.minute || ltime.second || ltime.second_part))
00155 {
00156 char buff[type::Time::MAX_STRING_LENGTH];
00157 String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
00158 ltime.convert(str);
00159 set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE,
00160 ER_WARN_DATA_TRUNCATED,
00161 str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
00162 error= 3;
00163 }
00164 }
00165 else
00166 {
00167 tmp=0;
00168 error= 1;
00169 set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
00170 }
00171
00172 int4store(ptr,tmp);
00173
00174 return error;
00175 }
00176
00177 double Field_date::val_real(void) const
00178 {
00179 return (double) Field_date::val_int();
00180 }
00181
00182 int64_t Field_date::val_int(void) const
00183 {
00184 uint32_t j;
00185
00186 ASSERT_COLUMN_MARKED_FOR_READ;
00187
00188 j= uint4korr(ptr);
00189
00190 return (int64_t) j;
00191 }
00192
00193 String *Field_date::val_str(String *val_buffer, String *) const
00194 {
00195 val_buffer->alloc(field_length);
00196 val_buffer->length(field_length);
00197 uint32_t tmp=(uint32_t) uint4korr(ptr);
00198 int32_t part;
00199 char *pos=(char*) val_buffer->ptr()+10;
00200
00201 ASSERT_COLUMN_MARKED_FOR_READ;
00202
00203
00204 *pos--=0;
00205 part=(int32_t) (tmp % 100);
00206 *pos--= (char) ('0'+part%10);
00207 *pos--= (char) ('0'+part/10);
00208 *pos--= '-';
00209 part=(int32_t) (tmp/100%100);
00210 *pos--= (char) ('0'+part%10);
00211 *pos--= (char) ('0'+part/10);
00212 *pos--= '-';
00213 part=(int32_t) (tmp/10000);
00214 *pos--= (char) ('0'+part%10); part/=10;
00215 *pos--= (char) ('0'+part%10); part/=10;
00216 *pos--= (char) ('0'+part%10); part/=10;
00217 *pos= (char) ('0'+part);
00218 return val_buffer;
00219 }
00220
00221 bool Field_date::get_date(type::Time <ime, uint32_t fuzzydate) const
00222 {
00223 uint32_t tmp=(uint32_t) uint4korr(ptr);
00224 ltime.day= (int) (tmp%100);
00225 ltime.month= (int) (tmp/100%100);
00226 ltime.year= (int) (tmp/10000);
00227 ltime.time_type= type::DRIZZLE_TIMESTAMP_DATE;
00228 ltime.hour= ltime.minute= ltime.second= ltime.second_part= ltime.neg= 0;
00229
00230 return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime.month || !ltime.day)) ?
00231 1 : 0);
00232 }
00233
00234 bool Field_date::get_time(type::Time <ime) const
00235 {
00236 return Field_date::get_date(ltime ,0);
00237 }
00238
00239 int Field_date::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
00240 {
00241 uint32_t a,b;
00242 a=(uint32_t) uint4korr(a_ptr);
00243 b=(uint32_t) uint4korr(b_ptr);
00244 return (a < b) ? -1 : (a > b) ? 1 : 0;
00245 }
00246
00247 void Field_date::sort_string(unsigned char *to,uint32_t )
00248 {
00249 to[0] = ptr[3];
00250 to[1] = ptr[2];
00251 to[2] = ptr[1];
00252 to[3] = ptr[0];
00253 }
00254
00255 }