~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/epoch.cc

  • Committer: Stewart Smith
  • Date: 2011-01-14 05:18:23 UTC
  • mto: (2086.1.3 build)
  • mto: This revision was merged to the branch mainline in revision 2087.
  • Revision ID: stewart@flamingspork.com-20110114051823-14fyn2kvg8pc5a15
\r and trailing whitespace removed.

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
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
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
 
 
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>
35
35
namespace drizzled
36
36
{
37
37
 
 
38
namespace field
 
39
{
 
40
 
38
41
/**
39
42
  TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to
40
43
  2038-01-01 00:00:00 UTC stored as number of seconds since Unix
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.
80
83
 */
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)
 
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)
95
96
{
96
 
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
97
 
  flags|= UNSIGNED_FLAG;
98
97
  unireg_check= unireg_check_arg;
99
 
  if (! share->timestamp_field && unireg_check != NONE)
 
98
  if (! share->getTimestampField() && unireg_check != NONE)
100
99
  {
101
100
    /* This timestamp has auto-update */
102
 
    share->timestamp_field= this;
103
 
    flags|= TIMESTAMP_FLAG;
 
101
    share->setTimestampField(this);
 
102
    flags|= FUNCTION_DEFAULT_FLAG;
104
103
    if (unireg_check != TIMESTAMP_DN_FIELD)
105
104
      flags|= ON_UPDATE_NOW_FLAG;
106
105
  }
107
106
}
108
107
 
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)
 
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)
118
116
{
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;
123
119
}
128
124
  Returns value indicating during which operations this TIMESTAMP field
129
125
  should be auto-set to current timestamp.
130
126
*/
131
 
timestamp_auto_set_type Field_timestamp::get_auto_set_type() const
 
127
timestamp_auto_set_type Epoch::get_auto_set_type() const
132
128
{
133
129
  switch (unireg_check)
134
130
  {
142
138
      function should be called only for first of them (i.e. the one
143
139
      having auto-set property).
144
140
    */
145
 
    assert(table->timestamp_field == this);
 
141
    assert(getTable()->timestamp_field == this);
146
142
    /* Fall-through */
147
143
  case TIMESTAMP_DNUN_FIELD:
148
144
    return TIMESTAMP_AUTO_SET_ON_BOTH;
156
152
  }
157
153
}
158
154
 
159
 
int Field_timestamp::store(const char *from,
160
 
                           uint32_t len,
161
 
                           const CHARSET_INFO * const )
 
155
int Epoch::store(const char *from,
 
156
                 uint32_t len,
 
157
                 const CHARSET_INFO * const )
162
158
{
163
159
  Timestamp temporal;
164
160
 
165
161
  ASSERT_COLUMN_MARKED_FOR_WRITE;
166
162
 
167
 
  if (! temporal.from_string(from, (size_t) len))
 
163
  if (not temporal.from_string(from, (size_t) len))
168
164
  {
169
165
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
170
166
    return 1;
171
167
  }
172
168
 
173
169
  time_t tmp;
174
 
  temporal.to_time_t(&tmp);
 
170
  temporal.to_time_t(tmp);
175
171
 
176
 
  store_timestamp(tmp);
 
172
  uint64_t time_tmp= tmp;
 
173
  pack_num(time_tmp);
177
174
  return 0;
178
175
}
179
176
 
180
 
int Field_timestamp::store(double from)
 
177
int Epoch::store(double from)
181
178
{
182
179
  ASSERT_COLUMN_MARKED_FOR_WRITE;
183
180
 
187
184
    std::stringstream ss;
188
185
    std::string tmp;
189
186
    ss.precision(18); /* 18 places should be fine for error display of double input. */
190
 
    ss << from; ss >> tmp;
 
187
    ss << from; 
 
188
    ss >> tmp;
191
189
 
192
190
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
193
191
    return 2;
194
192
  }
195
 
  return Field_timestamp::store((int64_t) rint(from), false);
 
193
  return Epoch::store((int64_t) rint(from), false);
196
194
}
197
195
 
198
 
int Field_timestamp::store(int64_t from, bool)
 
196
int Epoch::store(int64_t from, bool)
199
197
{
200
198
  ASSERT_COLUMN_MARKED_FOR_WRITE;
201
199
 
206
204
  Timestamp temporal;
207
205
  if (! temporal.from_int64_t(from))
208
206
  {
209
 
    /* Convert the integer to a string using stringstream */
210
 
    std::stringstream ss;
211
 
    std::string tmp;
212
 
    ss << from; ss >> tmp;
 
207
    /* Convert the integer to a string using boost::lexical_cast */
 
208
    std::string tmp(boost::lexical_cast<std::string>(from));
213
209
 
214
210
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
215
211
    return 2;
216
212
  }
217
213
 
218
214
  time_t tmp;
219
 
  temporal.to_time_t(&tmp);
220
 
 
221
 
  store_timestamp(tmp);
 
215
  temporal.to_time_t(tmp);
 
216
 
 
217
  uint64_t tmp64= tmp;
 
218
  pack_num(tmp64);
 
219
 
222
220
  return 0;
223
221
}
224
222
 
225
 
double Field_timestamp::val_real(void)
 
223
double Epoch::val_real(void)
226
224
{
227
 
  return (double) Field_timestamp::val_int();
 
225
  return (double) Epoch::val_int();
228
226
}
229
227
 
230
 
int64_t Field_timestamp::val_int(void)
 
228
int64_t Epoch::val_int(void)
231
229
{
232
 
  uint32_t temp;
 
230
  uint64_t temp;
233
231
 
234
232
  ASSERT_COLUMN_MARKED_FOR_READ;
235
233
 
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);
 
234
  unpack_num(temp);
242
235
 
243
236
  Timestamp temporal;
244
237
  (void) temporal.from_time_t((time_t) temp);
249
242
  return result;
250
243
}
251
244
 
252
 
String *Field_timestamp::val_str(String *val_buffer, String *)
 
245
String *Epoch::val_str(String *val_buffer, String *)
253
246
{
254
 
  uint32_t temp;
 
247
  uint64_t temp= 0;
255
248
  char *to;
256
249
  int to_len= field_length + 1;
257
250
 
258
251
  val_buffer->alloc(to_len);
259
252
  to= (char *) val_buffer->ptr();
260
253
 
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);
 
254
  unpack_num(temp);
267
255
 
268
256
  val_buffer->set_charset(&my_charset_bin);     /* Safety */
269
257
 
278
266
  return val_buffer;
279
267
}
280
268
 
281
 
bool Field_timestamp::get_date(DRIZZLE_TIME *ltime, uint32_t)
 
269
bool Epoch::get_date(type::Time *ltime, uint32_t)
282
270
{
283
 
  uint32_t temp;
 
271
  uint64_t temp;
284
272
 
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);
 
273
  unpack_num(temp);
291
274
  
292
275
  memset(ltime, 0, sizeof(*ltime));
293
276
 
294
277
  Timestamp temporal;
295
278
  (void) temporal.from_time_t((time_t) temp);
296
279
 
297
 
  /* @TODO Goodbye the below code when DRIZZLE_TIME is finally gone.. */
 
280
  /* @TODO Goodbye the below code when type::Time is finally gone.. */
298
281
 
299
282
  ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME;
300
283
  ltime->year= temporal.years();
307
290
  return 0;
308
291
}
309
292
 
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)
 
293
bool Epoch::get_time(type::Time *ltime)
 
294
{
 
295
  return Epoch::get_date(ltime,0);
 
296
}
 
297
 
 
298
int Epoch::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
299
{
 
300
  uint64_t a,b;
 
301
 
 
302
  unpack_num(a, a_ptr);
 
303
  unpack_num(b, b_ptr);
 
304
 
 
305
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
306
}
 
307
 
 
308
 
 
309
void Epoch::sort_string(unsigned char *to,uint32_t )
 
310
{
 
311
#ifdef WORDS_BIGENDIAN
 
312
  if (!getTable() || !getTable()->getShare()->db_low_byte_first)
338
313
  {
339
314
    to[0] = ptr[0];
340
315
    to[1] = ptr[1];
341
316
    to[2] = ptr[2];
342
317
    to[3] = ptr[3];
 
318
    to[4] = ptr[4];
 
319
    to[5] = ptr[5];
 
320
    to[6] = ptr[6];
 
321
    to[7] = ptr[7];
343
322
  }
344
323
  else
345
324
#endif
346
325
  {
347
 
    to[0] = ptr[3];
348
 
    to[1] = ptr[2];
349
 
    to[2] = ptr[1];
350
 
    to[3] = ptr[0];
 
326
    to[0] = ptr[7];
 
327
    to[1] = ptr[6];
 
328
    to[2] = ptr[5];
 
329
    to[3] = ptr[4];
 
330
    to[4] = ptr[3];
 
331
    to[5] = ptr[2];
 
332
    to[6] = ptr[1];
 
333
    to[7] = ptr[0];
351
334
  }
352
335
}
353
336
 
354
 
void Field_timestamp::sql_type(String &res) const
 
337
void Epoch::sql_type(String &res) const
355
338
{
356
339
  res.set_ascii(STRING_WITH_LEN("timestamp"));
357
340
}
358
341
 
359
 
void Field_timestamp::set_time()
 
342
void Epoch::set_time()
360
343
{
361
 
  Session *session= table ? table->in_use : current_session;
362
 
  long tmp= (long) session->query_start();
 
344
  Session *session= getTable() ? getTable()->in_use : current_session;
 
345
  time_t tmp= session->getCurrentTimestampEpoch();
 
346
 
363
347
  set_notnull();
364
 
  store_timestamp(tmp);
 
348
  pack_num(static_cast<uint32_t>(tmp));
365
349
}
366
350
 
367
 
void Field_timestamp::set_default()
 
351
void Epoch::set_default()
368
352
{
369
 
  if (table->timestamp_field == this &&
 
353
  if (getTable()->timestamp_field == this &&
370
354
      unireg_check != TIMESTAMP_UN_FIELD)
 
355
  {
371
356
    set_time();
 
357
  }
372
358
  else
 
359
  {
373
360
    Field::set_default();
 
361
  }
374
362
}
375
363
 
376
 
long Field_timestamp::get_timestamp(bool *null_value)
 
364
long Epoch::get_timestamp(bool *null_value)
377
365
{
378
366
  if ((*null_value= is_null()))
379
367
    return 0;
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;
 
368
 
 
369
  uint64_t tmp;
 
370
  return unpack_num(tmp);
387
371
}
388
372
 
389
 
void Field_timestamp::store_timestamp(time_t timestamp)
 
373
size_t Epoch::max_string_length()
390
374
{
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);
 
375
  return sizeof(uint64_t);
399
376
}
400
377
 
 
378
} /* namespace field */
401
379
} /* namespace drizzled */