18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
21
#include "config.h"
23
#include <drizzled/field/timestamp.h>
22
#include <boost/lexical_cast.hpp>
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,
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 */
96
/* For 4.0 MYD and 4.0 InnoDB compatibility */
97
flags|= UNSIGNED_FLAG;
98
97
unireg_check= unireg_check_arg;
99
98
if (! share->getTimestampField() && unireg_check != NONE)
101
100
/* This timestamp has auto-update */
102
101
share->setTimestampField(this);
103
flags|= TIMESTAMP_FLAG;
102
flags|= FUNCTION_DEFAULT_FLAG;
104
103
if (unireg_check != TIMESTAMP_DN_FIELD)
105
104
flags|= ON_UPDATE_NOW_FLAG;
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,
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,
119
/* For 4.0 MYD and 4.0 InnoDB compatibility */
120
flags|= UNSIGNED_FLAG;
121
117
if (unireg_check != TIMESTAMP_DN_FIELD)
122
118
flags|= ON_UPDATE_NOW_FLAG;
159
int Field_timestamp::store(const char *from,
161
const CHARSET_INFO * const )
155
int Epoch::store(const char *from,
157
const CHARSET_INFO * const )
163
159
Timestamp temporal;
165
161
ASSERT_COLUMN_MARKED_FOR_WRITE;
167
if (! temporal.from_string(from, (size_t) len))
163
if (not temporal.from_string(from, (size_t) len))
169
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
165
my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
174
temporal.to_time_t(&tmp);
170
temporal.to_time_t(tmp);
176
store_timestamp(tmp);
172
uint64_t time_tmp= tmp;
180
int Field_timestamp::store(double from)
177
int Epoch::store(double from)
182
179
ASSERT_COLUMN_MARKED_FOR_WRITE;
184
if (from < 0 || from > 99991231235959.0)
181
uint64_t from_tmp= (uint64_t)from;
184
if (not temporal.from_int64_t(from_tmp))
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. */
190
ss << from; ss >> tmp;
186
/* Convert the integer to a string using boost::lexical_cast */
187
std::string tmp(boost::lexical_cast<std::string>(from));
192
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
189
my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
195
return Field_timestamp::store((int64_t) rint(from), false);
198
int Field_timestamp::store(int64_t from, bool)
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)
200
212
ASSERT_COLUMN_MARKED_FOR_WRITE;
204
216
* if unable to create a valid DateTime.
206
218
Timestamp temporal;
207
if (! temporal.from_int64_t(from))
219
if (not temporal.from_int64_t(from))
209
/* Convert the integer to a string using stringstream */
210
std::stringstream ss;
212
ss << from; ss >> tmp;
221
/* Convert the integer to a string using boost::lexical_cast */
222
std::string tmp(boost::lexical_cast<std::string>(from));
214
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
224
my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
219
temporal.to_time_t(&tmp);
221
store_timestamp(tmp);
229
temporal.to_time_t(tmp);
225
double Field_timestamp::val_real(void)
237
double Epoch::val_real(void)
227
return (double) Field_timestamp::val_int();
239
return (double) Epoch::val_int();
230
int64_t Field_timestamp::val_int(void)
242
int64_t Epoch::val_int(void)
234
246
ASSERT_COLUMN_MARKED_FOR_READ;
236
#ifdef WORDS_BIGENDIAN
237
if (getTable() && getTable()->s->db_low_byte_first)
238
temp= uint4korr(ptr);
243
250
Timestamp temporal;
244
251
(void) temporal.from_time_t((time_t) temp);
252
String *Field_timestamp::val_str(String *val_buffer, String *)
259
String *Epoch::val_str(String *val_buffer, String *)
256
263
int to_len= field_length + 1;
258
265
val_buffer->alloc(to_len);
259
266
to= (char *) val_buffer->ptr();
261
#ifdef WORDS_BIGENDIAN
262
if (getTable() && getTable()->s->db_low_byte_first)
263
temp= uint4korr(ptr);
268
270
val_buffer->set_charset(&my_charset_bin); /* Safety */
278
280
return val_buffer;
281
bool Field_timestamp::get_date(DRIZZLE_TIME *ltime, uint32_t)
283
bool Epoch::get_date(type::Time <ime, uint32_t)
286
type::Time::epoch_t time_temp;
285
#ifdef WORDS_BIGENDIAN
286
if (getTable() && getTable()->s->db_low_byte_first)
287
temp= uint4korr(ptr);
292
memset(ltime, 0, sizeof(*ltime));
295
(void) temporal.from_time_t((time_t) temp);
297
/* @TODO Goodbye the below code when DRIZZLE_TIME is finally gone.. */
299
ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME;
300
ltime->year= temporal.years();
301
ltime->month= temporal.months();
302
ltime->day= temporal.days();
303
ltime->hour= temporal.hours();
304
ltime->minute= temporal.minutes();
305
ltime->second= temporal.seconds();
293
ltime.store(time_temp);
310
bool Field_timestamp::get_time(DRIZZLE_TIME *ltime)
312
return Field_timestamp::get_date(ltime,0);
315
int Field_timestamp::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
318
#ifdef WORDS_BIGENDIAN
319
if (getTable() && getTable()->s->db_low_byte_first)
330
return ((uint32_t) a < (uint32_t) b) ? -1 : ((uint32_t) a > (uint32_t) b) ? 1 : 0;
334
void Field_timestamp::sort_string(unsigned char *to,uint32_t )
336
#ifdef WORDS_BIGENDIAN
337
if (!getTable() || !getTable()->s->db_low_byte_first)
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 )
316
#ifdef WORDS_BIGENDIAN
317
if (!getTable() || !getTable()->getShare()->db_low_byte_first)
354
void Field_timestamp::sql_type(String &res) const
342
void Epoch::sql_type(String &res) const
356
344
res.set_ascii(STRING_WITH_LEN("timestamp"));
359
void Field_timestamp::set_time()
347
void Epoch::set_time()
361
349
Session *session= getTable() ? getTable()->in_use : current_session;
362
long tmp= (long) session->query_start();
350
time_t tmp= session->getCurrentTimestampEpoch();
364
store_timestamp(tmp);
353
pack_num(static_cast<uint32_t>(tmp));
367
void Field_timestamp::set_default()
356
void Epoch::set_default()
369
358
if (getTable()->timestamp_field == this &&
370
359
unireg_check != TIMESTAMP_UN_FIELD)
373
365
Field::set_default();
376
long Field_timestamp::get_timestamp(bool *null_value)
369
long Epoch::get_timestamp(bool *null_value)
378
371
if ((*null_value= is_null()))
380
#ifdef WORDS_BIGENDIAN
381
if (getTable() && getTable()->s->db_low_byte_first)
382
return sint4korr(ptr);
375
return unpack_num(tmp);
389
void Field_timestamp::store_timestamp(time_t timestamp)
378
size_t Epoch::max_string_length()
391
#ifdef WORDS_BIGENDIAN
392
if (getTable() && getTable()->s->db_low_byte_first)
394
int4store(ptr,timestamp);
398
longstore(ptr,(uint32_t) timestamp);
380
return sizeof(uint64_t);
383
} /* namespace field */
401
384
} /* namespace drizzled */