~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/timestamp.cc

Merge Joe, plus I updated the tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
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
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
 
21
 
21
22
#include "config.h"
22
 
#include <boost/lexical_cast.hpp>
23
 
#include <drizzled/field/epoch.h>
 
23
#include <drizzled/field/timestamp.h>
24
24
#include <drizzled/error.h>
25
25
#include <drizzled/tztime.h>
26
26
#include <drizzled/table.h>
35
35
namespace drizzled
36
36
{
37
37
 
38
 
namespace field
39
 
{
40
 
 
41
38
/**
42
39
  TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to
43
40
  2038-01-01 00:00:00 UTC stored as number of seconds since Unix
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.
83
80
 */
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) :
90
 
  Field_str(ptr_arg,
91
 
            MicroTimestamp::MAX_STRING_LENGTH - 1, /* no \0 */
92
 
            null_ptr_arg,
93
 
            null_bit_arg,
94
 
            field_name_arg,
95
 
            &my_charset_bin)
 
81
Field_timestamp::Field_timestamp(unsigned char *ptr_arg,
 
82
                                 uint32_t,
 
83
                                 unsigned char *null_ptr_arg,
 
84
                                 unsigned char null_bit_arg,
 
85
                                 enum utype unireg_check_arg,
 
86
                                 const char *field_name_arg,
 
87
                                 TableShare *share,
 
88
                                 const CHARSET_INFO * const cs)
 
89
  :Field_str(ptr_arg,
 
90
             DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
 
91
             null_ptr_arg,
 
92
             null_bit_arg,
 
93
             field_name_arg,
 
94
             cs)
96
95
{
 
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)
99
100
  {
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;
105
106
  }
106
107
}
107
108
 
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,
113
 
            0,
114
 
            field_name_arg,
115
 
            &my_charset_bin)
 
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,
 
115
             0,
 
116
             field_name_arg,
 
117
             cs)
116
118
{
 
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;
119
123
}
124
128
  Returns value indicating during which operations this TIMESTAMP field
125
129
  should be auto-set to current timestamp.
126
130
*/
127
 
timestamp_auto_set_type Epoch::get_auto_set_type() const
 
131
timestamp_auto_set_type Field_timestamp::get_auto_set_type() const
128
132
{
129
133
  switch (unireg_check)
130
134
  {
138
142
      function should be called only for first of them (i.e. the one
139
143
      having auto-set property).
140
144
    */
141
 
    assert(getTable()->timestamp_field == this);
 
145
    assert(table->timestamp_field == this);
142
146
    /* Fall-through */
143
147
  case TIMESTAMP_DNUN_FIELD:
144
148
    return TIMESTAMP_AUTO_SET_ON_BOTH;
152
156
  }
153
157
}
154
158
 
155
 
int Epoch::store(const char *from,
156
 
                 uint32_t len,
157
 
                 const CHARSET_INFO * const )
 
159
int Field_timestamp::store(const char *from,
 
160
                           uint32_t len,
 
161
                           const CHARSET_INFO * const )
158
162
{
159
163
  Timestamp temporal;
160
164
 
161
165
  ASSERT_COLUMN_MARKED_FOR_WRITE;
162
166
 
163
 
  if (not temporal.from_string(from, (size_t) len))
 
167
  if (! temporal.from_string(from, (size_t) len))
164
168
  {
165
 
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
 
169
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
166
170
    return 1;
167
171
  }
168
172
 
169
173
  time_t tmp;
170
 
  temporal.to_time_t(tmp);
 
174
  temporal.to_time_t(&tmp);
171
175
 
172
 
  uint64_t time_tmp= tmp;
173
 
  pack_num(time_tmp);
 
176
  store_timestamp(tmp);
174
177
  return 0;
175
178
}
176
179
 
177
 
int Epoch::store(double from)
 
180
int Field_timestamp::store(double from)
178
181
{
179
182
  ASSERT_COLUMN_MARKED_FOR_WRITE;
180
183
 
181
 
  uint64_t from_tmp= (uint64_t)from;
182
 
 
183
 
  Timestamp temporal;
184
 
  if (not temporal.from_int64_t(from_tmp))
 
184
  if (from < 0 || from > 99991231235959.0)
185
185
  {
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;
 
188
    std::string tmp;
 
189
    ss.precision(18); /* 18 places should be fine for error display of double input. */
 
190
    ss << from; ss >> tmp;
188
191
 
189
 
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
192
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
190
193
    return 2;
191
194
  }
192
 
 
193
 
  time_t tmp;
194
 
  temporal.to_time_t(tmp);
195
 
 
196
 
  uint64_t tmp_micro= tmp;
197
 
  pack_num(tmp_micro);
198
 
 
199
 
  return 0;
200
 
}
201
 
 
202
 
int Epoch::store_decimal(const type::Decimal *value)
203
 
{
204
 
  double tmp;
205
 
  value->convert(tmp);
206
 
 
207
 
  return store(tmp);
208
 
}
209
 
 
210
 
int Epoch::store(int64_t from, bool)
 
195
  return Field_timestamp::store((int64_t) rint(from), false);
 
196
}
 
197
 
 
198
int Field_timestamp::store(int64_t from, bool)
211
199
{
212
200
  ASSERT_COLUMN_MARKED_FOR_WRITE;
213
201
 
216
204
   * if unable to create a valid DateTime.  
217
205
   */
218
206
  Timestamp temporal;
219
 
  if (not temporal.from_int64_t(from))
 
207
  if (! temporal.from_int64_t(from))
220
208
  {
221
 
    /* Convert the integer to a string using boost::lexical_cast */
222
 
    std::string tmp(boost::lexical_cast<std::string>(from));
 
209
    /* Convert the integer to a string using stringstream */
 
210
    std::stringstream ss;
 
211
    std::string tmp;
 
212
    ss << from; ss >> tmp;
223
213
 
224
 
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
214
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
225
215
    return 2;
226
216
  }
227
217
 
228
218
  time_t tmp;
229
 
  temporal.to_time_t(tmp);
230
 
 
231
 
  uint64_t tmp64= tmp;
232
 
  pack_num(tmp64);
233
 
 
 
219
  temporal.to_time_t(&tmp);
 
220
 
 
221
  store_timestamp(tmp);
234
222
  return 0;
235
223
}
236
224
 
237
 
double Epoch::val_real(void)
 
225
double Field_timestamp::val_real(void)
238
226
{
239
 
  return (double) Epoch::val_int();
 
227
  return (double) Field_timestamp::val_int();
240
228
}
241
229
 
242
 
int64_t Epoch::val_int(void)
 
230
int64_t Field_timestamp::val_int(void)
243
231
{
244
 
  uint64_t temp;
 
232
  uint32_t temp;
245
233
 
246
234
  ASSERT_COLUMN_MARKED_FOR_READ;
247
235
 
248
 
  unpack_num(temp);
 
236
#ifdef WORDS_BIGENDIAN
 
237
  if (table && table->s->db_low_byte_first)
 
238
    temp= uint4korr(ptr);
 
239
  else
 
240
#endif
 
241
    longget(temp, ptr);
249
242
 
250
243
  Timestamp temporal;
251
244
  (void) temporal.from_time_t((time_t) temp);
256
249
  return result;
257
250
}
258
251
 
259
 
String *Epoch::val_str(String *val_buffer, String *)
 
252
String *Field_timestamp::val_str(String *val_buffer, String *)
260
253
{
261
 
  uint64_t temp= 0;
 
254
  uint32_t temp;
262
255
  char *to;
263
256
  int to_len= field_length + 1;
264
257
 
265
258
  val_buffer->alloc(to_len);
266
259
  to= (char *) val_buffer->ptr();
267
260
 
268
 
  unpack_num(temp);
 
261
#ifdef WORDS_BIGENDIAN
 
262
  if (table && table->s->db_low_byte_first)
 
263
    temp= uint4korr(ptr);
 
264
  else
 
265
#endif
 
266
    longget(temp, ptr);
269
267
 
270
268
  val_buffer->set_charset(&my_charset_bin);     /* Safety */
271
269
 
280
278
  return val_buffer;
281
279
}
282
280
 
283
 
bool Epoch::get_date(type::Time &ltime, uint32_t)
 
281
bool Field_timestamp::get_date(DRIZZLE_TIME *ltime, uint32_t)
284
282
{
285
 
  uint64_t temp;
286
 
  type::Time::epoch_t time_temp;
 
283
  uint32_t temp;
287
284
 
288
 
  unpack_num(temp);
289
 
  time_temp= temp;
 
285
#ifdef WORDS_BIGENDIAN
 
286
  if (table && table->s->db_low_byte_first)
 
287
    temp= uint4korr(ptr);
 
288
  else
 
289
#endif
 
290
    longget(temp, ptr);
290
291
  
291
 
  ltime.reset();
292
 
 
293
 
  ltime.store(time_temp);
 
292
  memset(ltime, 0, sizeof(*ltime));
 
293
 
 
294
  Timestamp temporal;
 
295
  (void) temporal.from_time_t((time_t) temp);
 
296
 
 
297
  /* @TODO Goodbye the below code when DRIZZLE_TIME is finally gone.. */
 
298
 
 
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();
294
306
 
295
307
  return 0;
296
308
}
297
309
 
298
 
bool Epoch::get_time(type::Time &ltime)
299
 
{
300
 
  return Epoch::get_date(ltime, 0);
301
 
}
302
 
 
303
 
int Epoch::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
304
 
{
305
 
  uint64_t a,b;
306
 
 
307
 
  unpack_num(a, a_ptr);
308
 
  unpack_num(b, b_ptr);
309
 
 
310
 
  return (a < b) ? -1 : (a > b) ? 1 : 0;
311
 
}
312
 
 
313
 
 
314
 
void Epoch::sort_string(unsigned char *to,uint32_t )
315
 
{
316
 
#ifdef WORDS_BIGENDIAN
317
 
  if (!getTable() || !getTable()->getShare()->db_low_byte_first)
 
310
bool Field_timestamp::get_time(DRIZZLE_TIME *ltime)
 
311
{
 
312
  return Field_timestamp::get_date(ltime,0);
 
313
}
 
314
 
 
315
int Field_timestamp::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
316
{
 
317
  int32_t a,b;
 
318
#ifdef WORDS_BIGENDIAN
 
319
  if (table && table->s->db_low_byte_first)
 
320
  {
 
321
    a=sint4korr(a_ptr);
 
322
    b=sint4korr(b_ptr);
 
323
  }
 
324
  else
 
325
#endif
 
326
  {
 
327
  longget(a,a_ptr);
 
328
  longget(b,b_ptr);
 
329
  }
 
330
  return ((uint32_t) a < (uint32_t) b) ? -1 : ((uint32_t) a > (uint32_t) b) ? 1 : 0;
 
331
}
 
332
 
 
333
 
 
334
void Field_timestamp::sort_string(unsigned char *to,uint32_t )
 
335
{
 
336
#ifdef WORDS_BIGENDIAN
 
337
  if (!table || !table->s->db_low_byte_first)
318
338
  {
319
339
    to[0] = ptr[0];
320
340
    to[1] = ptr[1];
321
341
    to[2] = ptr[2];
322
342
    to[3] = ptr[3];
323
 
    to[4] = ptr[4];
324
 
    to[5] = ptr[5];
325
 
    to[6] = ptr[6];
326
 
    to[7] = ptr[7];
327
343
  }
328
344
  else
329
345
#endif
330
346
  {
331
 
    to[0] = ptr[7];
332
 
    to[1] = ptr[6];
333
 
    to[2] = ptr[5];
334
 
    to[3] = ptr[4];
335
 
    to[4] = ptr[3];
336
 
    to[5] = ptr[2];
337
 
    to[6] = ptr[1];
338
 
    to[7] = ptr[0];
 
347
    to[0] = ptr[3];
 
348
    to[1] = ptr[2];
 
349
    to[2] = ptr[1];
 
350
    to[3] = ptr[0];
339
351
  }
340
352
}
341
353
 
342
 
void Epoch::sql_type(String &res) const
 
354
void Field_timestamp::sql_type(String &res) const
343
355
{
344
356
  res.set_ascii(STRING_WITH_LEN("timestamp"));
345
357
}
346
358
 
347
 
void Epoch::set_time()
 
359
void Field_timestamp::set_time()
348
360
{
349
 
  Session *session= getTable() ? getTable()->in_use : current_session;
350
 
  time_t tmp= session->getCurrentTimestampEpoch();
351
 
 
 
361
  Session *session= table ? table->in_use : current_session;
 
362
  long tmp= (long) session->query_start();
352
363
  set_notnull();
353
 
  pack_num(static_cast<uint32_t>(tmp));
 
364
  store_timestamp(tmp);
354
365
}
355
366
 
356
 
void Epoch::set_default()
 
367
void Field_timestamp::set_default()
357
368
{
358
 
  if (getTable()->timestamp_field == this &&
 
369
  if (table->timestamp_field == this &&
359
370
      unireg_check != TIMESTAMP_UN_FIELD)
360
 
  {
361
371
    set_time();
362
 
  }
363
372
  else
364
 
  {
365
373
    Field::set_default();
366
 
  }
367
374
}
368
375
 
369
 
long Epoch::get_timestamp(bool *null_value)
 
376
long Field_timestamp::get_timestamp(bool *null_value)
370
377
{
371
378
  if ((*null_value= is_null()))
372
379
    return 0;
373
 
 
374
 
  uint64_t tmp;
375
 
  return unpack_num(tmp);
 
380
#ifdef WORDS_BIGENDIAN
 
381
  if (table && table->s->db_low_byte_first)
 
382
    return sint4korr(ptr);
 
383
#endif
 
384
  long tmp;
 
385
  longget(tmp,ptr);
 
386
  return tmp;
376
387
}
377
388
 
378
 
size_t Epoch::max_string_length()
 
389
void Field_timestamp::store_timestamp(time_t timestamp)
379
390
{
380
 
  return sizeof(uint64_t);
 
391
#ifdef WORDS_BIGENDIAN
 
392
  if (table && table->s->db_low_byte_first)
 
393
  {
 
394
    int4store(ptr,timestamp);
 
395
  }
 
396
  else
 
397
#endif
 
398
    longstore(ptr,(uint32_t) timestamp);
381
399
}
382
400
 
383
 
} /* namespace field */
384
401
} /* namespace drizzled */