21
21
#include "config.h"
22
22
#include <boost/lexical_cast.hpp>
23
#include <drizzled/field/timestamp.h>
23
#include <drizzled/field/epoch.h>
24
24
#include <drizzled/error.h>
25
25
#include <drizzled/tztime.h>
26
26
#include <drizzled/table.h>
78
81
course is non-standard.) In most cases user won't notice any change, only
79
82
exception is different behavior of old/new timestamps during ALTER TABLE.
81
Field_timestamp::Field_timestamp(unsigned char *ptr_arg,
83
unsigned char *null_ptr_arg,
84
unsigned char null_bit_arg,
85
enum utype unireg_check_arg,
86
const char *field_name_arg,
88
const CHARSET_INFO * const cs)
90
DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
84
Epoch::Epoch(unsigned char *ptr_arg,
86
unsigned char *null_ptr_arg,
87
unsigned char null_bit_arg,
88
enum utype unireg_check_arg,
89
const char *field_name_arg,
90
drizzled::TableShare *share,
91
const drizzled::CHARSET_INFO * const cs) :
93
DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
96
/* For 4.0 MYD and 4.0 InnoDB compatibility */
97
flags|= UNSIGNED_FLAG;
98
99
unireg_check= unireg_check_arg;
99
100
if (! share->getTimestampField() && unireg_check != NONE)
109
Field_timestamp::Field_timestamp(bool maybe_null_arg,
110
const char *field_name_arg,
111
const CHARSET_INFO * const cs)
112
:Field_str((unsigned char*) NULL,
113
DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
114
maybe_null_arg ? (unsigned char*) "": 0,
110
Epoch::Epoch(bool maybe_null_arg,
111
const char *field_name_arg,
112
const CHARSET_INFO * const cs) :
113
Field_str((unsigned char*) NULL,
114
DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
115
maybe_null_arg ? (unsigned char*) "": 0,
119
/* For 4.0 MYD and 4.0 InnoDB compatibility */
120
flags|= UNSIGNED_FLAG;
121
120
if (unireg_check != TIMESTAMP_DN_FIELD)
122
121
flags|= ON_UPDATE_NOW_FLAG;
128
127
Returns value indicating during which operations this TIMESTAMP field
129
128
should be auto-set to current timestamp.
131
timestamp_auto_set_type Field_timestamp::get_auto_set_type() const
130
timestamp_auto_set_type Epoch::get_auto_set_type() const
133
132
switch (unireg_check)
159
int Field_timestamp::store(const char *from,
161
const CHARSET_INFO * const )
158
int Epoch::store(const char *from,
160
const CHARSET_INFO * const )
163
162
Timestamp temporal;
165
164
ASSERT_COLUMN_MARKED_FOR_WRITE;
167
if (! temporal.from_string(from, (size_t) len))
166
if (not temporal.from_string(from, (size_t) len))
169
168
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
174
temporal.to_time_t(&tmp);
173
temporal.to_time_t(tmp);
176
store_timestamp(tmp);
180
int Field_timestamp::store(double from)
179
int Epoch::store(double from)
182
181
ASSERT_COLUMN_MARKED_FOR_WRITE;
193
192
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
196
return Field_timestamp::store((int64_t) rint(from), false);
195
return Epoch::store((int64_t) rint(from), false);
199
int Field_timestamp::store(int64_t from, bool)
198
int Epoch::store(int64_t from, bool)
201
200
ASSERT_COLUMN_MARKED_FOR_WRITE;
218
temporal.to_time_t(&tmp);
220
store_timestamp(tmp);
217
temporal.to_time_t(tmp);
224
double Field_timestamp::val_real(void)
224
double Epoch::val_real(void)
226
return (double) Field_timestamp::val_int();
226
return (double) Epoch::val_int();
229
int64_t Field_timestamp::val_int(void)
229
int64_t Epoch::val_int(void)
233
233
ASSERT_COLUMN_MARKED_FOR_READ;
235
#ifdef WORDS_BIGENDIAN
236
if (getTable() && getTable()->getShare()->db_low_byte_first)
237
temp= uint8korr(ptr);
240
int64_tget(temp, ptr);
242
237
Timestamp temporal;
243
238
(void) temporal.from_time_t((time_t) temp);
251
String *Field_timestamp::val_str(String *val_buffer, String *)
246
String *Epoch::val_str(String *val_buffer, String *)
255
250
int to_len= field_length + 1;
257
252
val_buffer->alloc(to_len);
258
253
to= (char *) val_buffer->ptr();
260
#ifdef WORDS_BIGENDIAN
261
if (getTable() && getTable()->getShare()->db_low_byte_first)
262
temp= uint8korr(ptr);
265
int64_tget(temp, ptr);
267
257
val_buffer->set_charset(&my_charset_bin); /* Safety */
277
267
return val_buffer;
280
bool Field_timestamp::get_date(DRIZZLE_TIME *ltime, uint32_t)
270
bool Epoch::get_date(type::Time *ltime, uint32_t)
284
#ifdef WORDS_BIGENDIAN
285
if (getTable() && getTable()->getShare()->db_low_byte_first)
286
temp= uint8korr(ptr);
289
int64_tget(temp, ptr);
291
276
memset(ltime, 0, sizeof(*ltime));
293
278
Timestamp temporal;
294
279
(void) temporal.from_time_t((time_t) temp);
296
/* @TODO Goodbye the below code when DRIZZLE_TIME is finally gone.. */
281
/* @TODO Goodbye the below code when type::Time is finally gone.. */
298
283
ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME;
299
284
ltime->year= temporal.years();
309
bool Field_timestamp::get_time(DRIZZLE_TIME *ltime)
311
return Field_timestamp::get_date(ltime,0);
314
int Field_timestamp::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
317
#ifdef WORDS_BIGENDIAN
318
if (getTable() && getTable()->getShare()->db_low_byte_first)
326
int64_tget(a, a_ptr);
327
int64_tget(b, b_ptr);
329
return ((uint64_t) a < (uint64_t) b) ? -1 : ((uint64_t) a > (uint64_t) b) ? 1 : 0;
333
void Field_timestamp::sort_string(unsigned char *to,uint32_t )
294
bool Epoch::get_time(type::Time *ltime)
296
return Epoch::get_date(ltime,0);
299
int Epoch::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
303
unpack_num(a, a_ptr);
304
unpack_num(b, b_ptr);
306
return (a < b) ? -1 : (a > b) ? 1 : 0;
310
void Epoch::sort_string(unsigned char *to,uint32_t )
335
312
#ifdef WORDS_BIGENDIAN
336
313
if (!getTable() || !getTable()->getShare()->db_low_byte_first)
361
void Field_timestamp::sql_type(String &res) const
338
void Epoch::sql_type(String &res) const
363
340
res.set_ascii(STRING_WITH_LEN("timestamp"));
366
void Field_timestamp::set_time()
343
void Epoch::set_time()
368
345
Session *session= getTable() ? getTable()->in_use : current_session;
369
346
time_t tmp= session->query_start();
371
store_timestamp(tmp);
374
void Field_timestamp::set_default()
351
void Epoch::set_default()
376
353
if (getTable()->timestamp_field == this &&
377
354
unireg_check != TIMESTAMP_UN_FIELD)
380
360
Field::set_default();
383
long Field_timestamp::get_timestamp(bool *null_value)
364
long Epoch::get_timestamp(bool *null_value)
385
366
if ((*null_value= is_null()))
387
#ifdef WORDS_BIGENDIAN
388
if (getTable() && getTable()->getShare()->db_low_byte_first)
389
return sint8korr(ptr);
392
int64_tget(tmp, ptr);
370
return unpack_num(tmp);
396
void Field_timestamp::store_timestamp(int64_t timestamp)
373
size_t Epoch::max_string_length()
398
#ifdef WORDS_BIGENDIAN
399
if (getTable() && getTable()->getShare()->db_low_byte_first)
401
int8store(ptr, timestamp);
405
int64_tstore(ptr, timestamp);
375
return sizeof(uint64_t);
378
} /* namespace field */
408
379
} /* namespace drizzled */