~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/timestamp.cc

  • Committer: Monty Taylor
  • Date: 2010-11-08 18:26:08 UTC
  • mto: This revision was merged to the branch mainline in revision 1931.
  • Revision ID: mordred@inaugust.com-20101108182608-lci86acl7r53sbi3
Replaced auto_ptr with scoped_ptr.

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
 
#include <config.h>
22
 
 
 
21
#include "config.h"
23
22
#include <boost/lexical_cast.hpp>
24
 
#include <drizzled/field/epoch.h>
 
23
#include <drizzled/field/timestamp.h>
25
24
#include <drizzled/error.h>
26
25
#include <drizzled/tztime.h>
27
26
#include <drizzled/table.h>
28
27
#include <drizzled/session.h>
29
 
#include <drizzled/current_session.h>
30
28
 
31
29
#include <math.h>
32
30
 
33
31
#include <sstream>
34
32
 
35
 
#include <drizzled/temporal.h>
 
33
#include "drizzled/temporal.h"
36
34
 
37
35
namespace drizzled
38
36
{
39
37
 
40
 
namespace field
41
 
{
42
 
 
43
38
/**
44
39
  TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to
45
40
  2038-01-01 00:00:00 UTC stored as number of seconds since Unix
83
78
  course is non-standard.) In most cases user won't notice any change, only
84
79
  exception is different behavior of old/new timestamps during ALTER TABLE.
85
80
 */
86
 
  Epoch::Epoch(unsigned char *ptr_arg,
87
 
               unsigned char *null_ptr_arg,
88
 
               unsigned char null_bit_arg,
89
 
               enum utype unireg_check_arg,
90
 
               const char *field_name_arg,
91
 
               drizzled::TableShare *share) :
92
 
  Field_str(ptr_arg,
93
 
            MicroTimestamp::MAX_STRING_LENGTH - 1, /* no \0 */
94
 
            null_ptr_arg,
95
 
            null_bit_arg,
96
 
            field_name_arg,
97
 
            &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)
98
95
{
 
96
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
 
97
  flags|= UNSIGNED_FLAG;
99
98
  unireg_check= unireg_check_arg;
100
99
  if (! share->getTimestampField() && unireg_check != NONE)
101
100
  {
102
101
    /* This timestamp has auto-update */
103
102
    share->setTimestampField(this);
104
 
    flags|= FUNCTION_DEFAULT_FLAG;
 
103
    flags|= TIMESTAMP_FLAG;
105
104
    if (unireg_check != TIMESTAMP_DN_FIELD)
106
105
      flags|= ON_UPDATE_NOW_FLAG;
107
106
  }
108
107
}
109
108
 
110
 
Epoch::Epoch(bool maybe_null_arg,
111
 
             const char *field_name_arg) :
112
 
  Field_str((unsigned char*) NULL,
113
 
            MicroTimestamp::MAX_STRING_LENGTH - 1, /* no \0 */
114
 
            maybe_null_arg ? (unsigned char*) "": 0,
115
 
            0,
116
 
            field_name_arg,
117
 
            &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)
118
118
{
 
119
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
 
120
  flags|= UNSIGNED_FLAG;
119
121
  if (unireg_check != TIMESTAMP_DN_FIELD)
120
122
    flags|= ON_UPDATE_NOW_FLAG;
121
123
}
126
128
  Returns value indicating during which operations this TIMESTAMP field
127
129
  should be auto-set to current timestamp.
128
130
*/
129
 
timestamp_auto_set_type Epoch::get_auto_set_type() const
 
131
timestamp_auto_set_type Field_timestamp::get_auto_set_type() const
130
132
{
131
133
  switch (unireg_check)
132
134
  {
154
156
  }
155
157
}
156
158
 
157
 
int Epoch::store(const char *from,
158
 
                 uint32_t len,
159
 
                 const CHARSET_INFO * const )
 
159
int Field_timestamp::store(const char *from,
 
160
                           uint32_t len,
 
161
                           const CHARSET_INFO * const )
160
162
{
161
163
  Timestamp temporal;
162
164
 
163
165
  ASSERT_COLUMN_MARKED_FOR_WRITE;
164
166
 
165
 
  if (not temporal.from_string(from, (size_t) len))
 
167
  if (! temporal.from_string(from, (size_t) len))
166
168
  {
167
 
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
 
169
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
168
170
    return 1;
169
171
  }
170
172
 
171
173
  time_t tmp;
172
 
  temporal.to_time_t(tmp);
 
174
  temporal.to_time_t(&tmp);
173
175
 
174
 
  uint64_t time_tmp= tmp;
175
 
  pack_num(time_tmp);
 
176
  store_timestamp(tmp);
176
177
  return 0;
177
178
}
178
179
 
179
 
int Epoch::store(double from)
 
180
int Field_timestamp::store(double from)
180
181
{
181
182
  ASSERT_COLUMN_MARKED_FOR_WRITE;
182
183
 
183
 
  uint64_t from_tmp= (uint64_t)from;
184
 
 
185
 
  Timestamp temporal;
186
 
  if (not temporal.from_int64_t(from_tmp))
 
184
  if (from < 0 || from > 99991231235959.0)
187
185
  {
188
 
    /* Convert the integer to a string using boost::lexical_cast */
189
 
    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; 
 
191
    ss >> tmp;
190
192
 
191
 
    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());
192
194
    return 2;
193
195
  }
194
 
 
195
 
  time_t tmp;
196
 
  temporal.to_time_t(tmp);
197
 
 
198
 
  uint64_t tmp_micro= tmp;
199
 
  pack_num(tmp_micro);
200
 
 
201
 
  return 0;
202
 
}
203
 
 
204
 
int Epoch::store_decimal(const type::Decimal *value)
205
 
{
206
 
  double tmp;
207
 
  value->convert(tmp);
208
 
 
209
 
  return store(tmp);
210
 
}
211
 
 
212
 
int Epoch::store(int64_t from, bool)
 
196
  return Field_timestamp::store((int64_t) rint(from), false);
 
197
}
 
198
 
 
199
int Field_timestamp::store(int64_t from, bool)
213
200
{
214
201
  ASSERT_COLUMN_MARKED_FOR_WRITE;
215
202
 
218
205
   * if unable to create a valid DateTime.  
219
206
   */
220
207
  Timestamp temporal;
221
 
  if (not temporal.from_int64_t(from))
 
208
  if (! temporal.from_int64_t(from))
222
209
  {
223
210
    /* Convert the integer to a string using boost::lexical_cast */
224
211
    std::string tmp(boost::lexical_cast<std::string>(from));
225
212
 
226
 
    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());
227
214
    return 2;
228
215
  }
229
216
 
230
217
  time_t tmp;
231
 
  temporal.to_time_t(tmp);
232
 
 
233
 
  uint64_t tmp64= tmp;
234
 
  pack_num(tmp64);
235
 
 
 
218
  temporal.to_time_t(&tmp);
 
219
 
 
220
  store_timestamp(tmp);
236
221
  return 0;
237
222
}
238
223
 
239
 
double Epoch::val_real(void) const
 
224
double Field_timestamp::val_real(void)
240
225
{
241
 
  return (double) Epoch::val_int();
 
226
  return (double) Field_timestamp::val_int();
242
227
}
243
228
 
244
 
int64_t Epoch::val_int(void) const
 
229
int64_t Field_timestamp::val_int(void)
245
230
{
246
231
  uint64_t temp;
247
232
 
248
233
  ASSERT_COLUMN_MARKED_FOR_READ;
249
234
 
250
 
  unpack_num(temp);
 
235
#ifdef WORDS_BIGENDIAN
 
236
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
237
    temp= uint8korr(ptr);
 
238
  else
 
239
#endif
 
240
    int64_tget(temp, ptr);
251
241
 
252
242
  Timestamp temporal;
253
243
  (void) temporal.from_time_t((time_t) temp);
258
248
  return result;
259
249
}
260
250
 
261
 
String *Epoch::val_str(String *val_buffer, String *) const
 
251
String *Field_timestamp::val_str(String *val_buffer, String *)
262
252
{
263
 
  uint64_t temp= 0;
 
253
  uint64_t temp;
264
254
  char *to;
265
255
  int to_len= field_length + 1;
266
256
 
267
257
  val_buffer->alloc(to_len);
268
258
  to= (char *) val_buffer->ptr();
269
259
 
270
 
  unpack_num(temp);
 
260
#ifdef WORDS_BIGENDIAN
 
261
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
262
    temp= uint8korr(ptr);
 
263
  else
 
264
#endif
 
265
    int64_tget(temp, ptr);
271
266
 
272
267
  val_buffer->set_charset(&my_charset_bin);     /* Safety */
273
268
 
282
277
  return val_buffer;
283
278
}
284
279
 
285
 
bool Epoch::get_date(type::Time &ltime, uint32_t) const
 
280
bool Field_timestamp::get_date(DRIZZLE_TIME *ltime, uint32_t)
286
281
{
287
282
  uint64_t temp;
288
 
  type::Time::epoch_t time_temp;
289
283
 
290
 
  unpack_num(temp);
291
 
  time_temp= temp;
 
284
#ifdef WORDS_BIGENDIAN
 
285
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
286
    temp= uint8korr(ptr);
 
287
  else
 
288
#endif
 
289
    int64_tget(temp, ptr);
292
290
  
293
 
  ltime.reset();
294
 
 
295
 
  ltime.store(time_temp);
 
291
  memset(ltime, 0, sizeof(*ltime));
 
292
 
 
293
  Timestamp temporal;
 
294
  (void) temporal.from_time_t((time_t) temp);
 
295
 
 
296
  /* @TODO Goodbye the below code when DRIZZLE_TIME is finally gone.. */
 
297
 
 
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();
296
305
 
297
306
  return 0;
298
307
}
299
308
 
300
 
bool Epoch::get_time(type::Time &ltime) const
301
 
{
302
 
  return Epoch::get_date(ltime, 0);
303
 
}
304
 
 
305
 
int Epoch::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
306
 
{
307
 
  uint64_t a,b;
308
 
 
309
 
  unpack_num(a, a_ptr);
310
 
  unpack_num(b, b_ptr);
311
 
 
312
 
  return (a < b) ? -1 : (a > b) ? 1 : 0;
313
 
}
314
 
 
315
 
 
316
 
void Epoch::sort_string(unsigned char *to,uint32_t )
 
309
bool Field_timestamp::get_time(DRIZZLE_TIME *ltime)
 
310
{
 
311
  return Field_timestamp::get_date(ltime,0);
 
312
}
 
313
 
 
314
int Field_timestamp::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
315
{
 
316
  int64_t a,b;
 
317
#ifdef WORDS_BIGENDIAN
 
318
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
319
  {
 
320
    a=sint8korr(a_ptr);
 
321
    b=sint8korr(b_ptr);
 
322
  }
 
323
  else
 
324
#endif
 
325
  {
 
326
    int64_tget(a, a_ptr);
 
327
    int64_tget(b, b_ptr);
 
328
  }
 
329
  return ((uint64_t) a < (uint64_t) b) ? -1 : ((uint64_t) a > (uint64_t) b) ? 1 : 0;
 
330
}
 
331
 
 
332
 
 
333
void Field_timestamp::sort_string(unsigned char *to,uint32_t )
317
334
{
318
335
#ifdef WORDS_BIGENDIAN
319
336
  if (!getTable() || !getTable()->getShare()->db_low_byte_first)
341
358
  }
342
359
}
343
360
 
344
 
void Epoch::sql_type(String &res) const
 
361
void Field_timestamp::sql_type(String &res) const
345
362
{
346
363
  res.set_ascii(STRING_WITH_LEN("timestamp"));
347
364
}
348
365
 
349
 
void Epoch::set_time()
 
366
void Field_timestamp::set_time()
350
367
{
351
368
  Session *session= getTable() ? getTable()->in_use : current_session;
352
 
  time_t tmp= session->getCurrentTimestampEpoch();
353
 
 
 
369
  time_t tmp= session->query_start();
354
370
  set_notnull();
355
 
  pack_num(static_cast<uint32_t>(tmp));
 
371
  store_timestamp(tmp);
356
372
}
357
373
 
358
 
void Epoch::set_default()
 
374
void Field_timestamp::set_default()
359
375
{
360
376
  if (getTable()->timestamp_field == this &&
361
377
      unireg_check != TIMESTAMP_UN_FIELD)
362
 
  {
363
378
    set_time();
364
 
  }
365
379
  else
366
 
  {
367
380
    Field::set_default();
368
 
  }
369
381
}
370
382
 
371
 
long Epoch::get_timestamp(bool *null_value) const
 
383
long Field_timestamp::get_timestamp(bool *null_value)
372
384
{
373
385
  if ((*null_value= is_null()))
374
386
    return 0;
375
 
 
376
 
  uint64_t tmp;
377
 
  return unpack_num(tmp);
 
387
#ifdef WORDS_BIGENDIAN
 
388
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
389
    return sint8korr(ptr);
 
390
#endif
 
391
  int64_t tmp;
 
392
  int64_tget(tmp, ptr);
 
393
  return tmp;
378
394
}
379
395
 
380
 
size_t Epoch::max_string_length()
 
396
void Field_timestamp::store_timestamp(int64_t timestamp)
381
397
{
382
 
  return sizeof(uint64_t);
 
398
#ifdef WORDS_BIGENDIAN
 
399
  if (getTable() && getTable()->getShare()->db_low_byte_first)
 
400
  {
 
401
    int8store(ptr, timestamp);
 
402
  }
 
403
  else
 
404
#endif
 
405
    int64_tstore(ptr, timestamp);
383
406
}
384
407
 
385
 
} /* namespace field */
386
408
} /* namespace drizzled */