22 #include <boost/lexical_cast.hpp>
23 #include <drizzled/field/epoch.h>
24 #include <drizzled/error.h>
25 #include <drizzled/table.h>
26 #include <drizzled/session.h>
27 #include <drizzled/session/times.h>
28 #include <drizzled/current_session.h>
80 unsigned char *null_ptr_arg,
81 unsigned char null_bit_arg,
82 enum utype unireg_check_arg,
83 const char *field_name_arg,
92 unireg_check= unireg_check_arg;
93 if (! share->getTimestampField() && unireg_check != NONE)
96 share->setTimestampField(
this);
97 flags|= FUNCTION_DEFAULT_FLAG;
98 if (unireg_check != TIMESTAMP_DN_FIELD)
99 flags|= ON_UPDATE_NOW_FLAG;
104 const char *field_name_arg) :
107 maybe_null_arg ? (unsigned char*)
"": 0,
112 if (unireg_check != TIMESTAMP_DN_FIELD)
113 flags|= ON_UPDATE_NOW_FLAG;
124 switch (unireg_check)
126 case TIMESTAMP_DN_FIELD:
127 return TIMESTAMP_AUTO_SET_ON_INSERT;
128 case TIMESTAMP_UN_FIELD:
129 return TIMESTAMP_AUTO_SET_ON_UPDATE;
130 case TIMESTAMP_OLD_FIELD:
136 assert(getTable()->timestamp_field ==
this);
138 case TIMESTAMP_DNUN_FIELD:
139 return TIMESTAMP_AUTO_SET_ON_BOTH;
146 return TIMESTAMP_NO_AUTO_SET;
150 int Epoch::store(
const char *from,
156 ASSERT_COLUMN_MARKED_FOR_WRITE;
160 my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
167 uint64_t time_tmp= tmp;
172 int Epoch::store(
double from)
174 ASSERT_COLUMN_MARKED_FOR_WRITE;
176 uint64_t from_tmp= (uint64_t)from;
179 if (not temporal.from_int64_t(from_tmp))
182 std::string tmp(boost::lexical_cast<std::string>(from));
184 my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
189 temporal.to_time_t(tmp);
191 uint64_t tmp_micro= tmp;
197 int Epoch::store_decimal(
const type::Decimal *value)
205 int Epoch::store(int64_t from,
bool)
207 ASSERT_COLUMN_MARKED_FOR_WRITE;
214 if (not temporal.from_int64_t(from))
217 std::string tmp(boost::lexical_cast<std::string>(from));
219 my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
224 temporal.to_time_t(tmp);
232 double Epoch::val_real(
void)
const
234 return (
double) Epoch::val_int();
237 int64_t Epoch::val_int(
void)
const
241 ASSERT_COLUMN_MARKED_FOR_READ;
246 (void) temporal.from_time_t((time_t) temp);
250 temporal.to_int64_t(&result);
254 String *Epoch::val_str(String *val_buffer, String *)
const
260 val_buffer->alloc(to_len);
261 to= (
char *) val_buffer->ptr();
265 val_buffer->set_charset(&my_charset_bin);
268 (void) temporal.from_time_t((time_t) temp);
271 rlen= temporal.to_string(to, to_len);
272 assert(rlen < to_len);
274 val_buffer->length(rlen);
278 bool Epoch::get_date(type::Time <ime, uint32_t)
const
281 type::epoch_t time_temp;
288 ltime.store(time_temp);
293 bool Epoch::get_time(type::Time <ime)
const
295 return Epoch::get_date(ltime, 0);
298 int Epoch::cmp(
const unsigned char *a_ptr,
const unsigned char *b_ptr)
302 unpack_num(a, a_ptr);
303 unpack_num(b, b_ptr);
305 return (a < b) ? -1 : (a > b) ? 1 : 0;
309 void Epoch::sort_string(
unsigned char *to,uint32_t )
311 #ifdef WORDS_BIGENDIAN
312 if (!getTable() || !getTable()->getShare()->db_low_byte_first)
337 void Epoch::set_time()
339 Session *session= getTable() ? getTable()->
in_use : current_session;
340 time_t tmp= session->times.getCurrentTimestampEpoch();
343 pack_num(static_cast<uint32_t>(tmp));
346 void Epoch::set_default()
348 if (getTable()->timestamp_field ==
this &&
349 unireg_check != TIMESTAMP_UN_FIELD)
355 Field::set_default();
359 long Epoch::get_timestamp(
bool *null_value)
const
361 if ((*null_value= is_null()))
365 return unpack_num(tmp);
368 size_t Epoch::max_string_length()
370 return sizeof(uint64_t);