1
1
/* -*- mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
4
* Copyright (C) 2008 Sun Microsystems
6
6
* This program is free software; you can redistribute it and/or modify
7
7
* it under the terms of the GNU General Public License as published by
81
78
course is non-standard.) In most cases user won't notice any change, only
82
79
exception is different behavior of old/new timestamps during ALTER TABLE.
84
Epoch::Epoch(unsigned char *ptr_arg,
85
unsigned char *null_ptr_arg,
86
unsigned char null_bit_arg,
87
enum utype unireg_check_arg,
88
const char *field_name_arg,
89
drizzled::TableShare *share) :
91
MicroTimestamp::MAX_STRING_LENGTH - 1, /* no \0 */
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 */,
96
/* For 4.0 MYD and 4.0 InnoDB compatibility */
97
flags|= UNSIGNED_FLAG;
97
98
unireg_check= unireg_check_arg;
98
99
if (! share->getTimestampField() && unireg_check != NONE)
100
101
/* This timestamp has auto-update */
101
102
share->setTimestampField(this);
102
flags|= FUNCTION_DEFAULT_FLAG;
103
flags|= TIMESTAMP_FLAG;
103
104
if (unireg_check != TIMESTAMP_DN_FIELD)
104
105
flags|= ON_UPDATE_NOW_FLAG;
108
Epoch::Epoch(bool maybe_null_arg,
109
const char *field_name_arg) :
110
Field_str((unsigned char*) NULL,
111
MicroTimestamp::MAX_STRING_LENGTH - 1, /* no \0 */
112
maybe_null_arg ? (unsigned char*) "": 0,
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,
119
/* For 4.0 MYD and 4.0 InnoDB compatibility */
120
flags|= UNSIGNED_FLAG;
117
121
if (unireg_check != TIMESTAMP_DN_FIELD)
118
122
flags|= ON_UPDATE_NOW_FLAG;
155
int Epoch::store(const char *from,
157
const CHARSET_INFO * const )
159
int Field_timestamp::store(const char *from,
161
const CHARSET_INFO * const )
159
163
Timestamp temporal;
161
165
ASSERT_COLUMN_MARKED_FOR_WRITE;
163
if (not temporal.from_string(from, (size_t) len))
167
if (! temporal.from_string(from, (size_t) len))
165
my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
169
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
170
temporal.to_time_t(tmp);
174
temporal.to_time_t(&tmp);
172
uint64_t time_tmp= tmp;
176
store_timestamp(tmp);
177
int Epoch::store(double from)
180
int Field_timestamp::store(double from)
179
182
ASSERT_COLUMN_MARKED_FOR_WRITE;
181
uint64_t from_tmp= (uint64_t)from;
184
if (not temporal.from_int64_t(from_tmp))
184
if (from < 0 || from > 99991231235959.0)
186
/* Convert the integer to a string using boost::lexical_cast */
187
std::string tmp(boost::lexical_cast<std::string>(from));
186
/* Convert the double to a string using stringstream */
187
std::stringstream ss;
189
ss.precision(18); /* 18 places should be fine for error display of double input. */
189
my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
193
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
194
temporal.to_time_t(tmp);
196
uint64_t tmp_micro= tmp;
202
int Epoch::store_decimal(const type::Decimal *value)
210
int Epoch::store(int64_t from, bool)
196
return Field_timestamp::store((int64_t) rint(from), false);
199
int Field_timestamp::store(int64_t from, bool)
212
201
ASSERT_COLUMN_MARKED_FOR_WRITE;
216
205
* if unable to create a valid DateTime.
218
207
Timestamp temporal;
219
if (not temporal.from_int64_t(from))
208
if (! temporal.from_int64_t(from))
221
210
/* Convert the integer to a string using boost::lexical_cast */
222
211
std::string tmp(boost::lexical_cast<std::string>(from));
224
my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
213
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
229
temporal.to_time_t(tmp);
218
temporal.to_time_t(&tmp);
220
store_timestamp(tmp);
237
double Epoch::val_real(void)
224
double Field_timestamp::val_real(void)
239
return (double) Epoch::val_int();
226
return (double) Field_timestamp::val_int();
242
int64_t Epoch::val_int(void)
229
int64_t Field_timestamp::val_int(void)
246
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);
250
242
Timestamp temporal;
251
243
(void) temporal.from_time_t((time_t) temp);
259
String *Epoch::val_str(String *val_buffer, String *)
251
String *Field_timestamp::val_str(String *val_buffer, String *)
263
255
int to_len= field_length + 1;
265
257
val_buffer->alloc(to_len);
266
258
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);
270
267
val_buffer->set_charset(&my_charset_bin); /* Safety */
280
277
return val_buffer;
283
bool Epoch::get_date(type::Time <ime, uint32_t)
280
bool Field_timestamp::get_date(DRIZZLE_TIME *ltime, uint32_t)
286
type::Time::epoch_t time_temp;
284
#ifdef WORDS_BIGENDIAN
285
if (getTable() && getTable()->getShare()->db_low_byte_first)
286
temp= uint8korr(ptr);
289
int64_tget(temp, ptr);
293
ltime.store(time_temp);
291
memset(ltime, 0, sizeof(*ltime));
294
(void) temporal.from_time_t((time_t) temp);
296
/* @TODO Goodbye the below code when DRIZZLE_TIME is finally gone.. */
298
ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME;
299
ltime->year= temporal.years();
300
ltime->month= temporal.months();
301
ltime->day= temporal.days();
302
ltime->hour= temporal.hours();
303
ltime->minute= temporal.minutes();
304
ltime->second= temporal.seconds();
298
bool Epoch::get_time(type::Time <ime)
300
return Epoch::get_date(ltime, 0);
303
int Epoch::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
307
unpack_num(a, a_ptr);
308
unpack_num(b, b_ptr);
310
return (a < b) ? -1 : (a > b) ? 1 : 0;
314
void Epoch::sort_string(unsigned char *to,uint32_t )
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 )
316
335
#ifdef WORDS_BIGENDIAN
317
336
if (!getTable() || !getTable()->getShare()->db_low_byte_first)
342
void Epoch::sql_type(String &res) const
361
void Field_timestamp::sql_type(String &res) const
344
363
res.set_ascii(STRING_WITH_LEN("timestamp"));
347
void Epoch::set_time()
366
void Field_timestamp::set_time()
349
368
Session *session= getTable() ? getTable()->in_use : current_session;
350
time_t tmp= session->getCurrentTimestampEpoch();
369
time_t tmp= session->query_start();
353
pack_num(static_cast<uint32_t>(tmp));
371
store_timestamp(tmp);
356
void Epoch::set_default()
374
void Field_timestamp::set_default()
358
376
if (getTable()->timestamp_field == this &&
359
377
unireg_check != TIMESTAMP_UN_FIELD)
365
380
Field::set_default();
369
long Epoch::get_timestamp(bool *null_value)
383
long Field_timestamp::get_timestamp(bool *null_value)
371
385
if ((*null_value= is_null()))
375
return unpack_num(tmp);
387
#ifdef WORDS_BIGENDIAN
388
if (getTable() && getTable()->getShare()->db_low_byte_first)
389
return sint8korr(ptr);
392
int64_tget(tmp, ptr);
378
size_t Epoch::max_string_length()
396
void Field_timestamp::store_timestamp(int64_t timestamp)
380
return sizeof(uint64_t);
398
#ifdef WORDS_BIGENDIAN
399
if (getTable() && getTable()->getShare()->db_low_byte_first)
401
int8store(ptr, timestamp);
405
int64_tstore(ptr, timestamp);
383
} /* namespace field */
384
408
} /* namespace drizzled */