~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/epoch.cc

Merge with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
#include "config.h"
22
22
#include <boost/lexical_cast.hpp>
23
 
#include <drizzled/field/timestamp.h>
 
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
               uint32_t,
 
86
               unsigned char *null_ptr_arg,
 
87
               unsigned char null_bit_arg,
 
88
               enum utype unireg_check_arg,
 
89
               const char *field_name_arg,
 
90
               drizzled::TableShare *share,
 
91
               const drizzled::CHARSET_INFO * const cs) :
 
92
  Field_str(ptr_arg,
 
93
            DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
 
94
            null_ptr_arg,
 
95
            null_bit_arg,
 
96
            field_name_arg,
 
97
            cs)
95
98
{
96
99
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
97
100
  flags|= UNSIGNED_FLAG;
106
109
  }
107
110
}
108
111
 
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)
 
112
Epoch::Epoch(bool maybe_null_arg,
 
113
             const char *field_name_arg,
 
114
             const CHARSET_INFO * const cs) :
 
115
  Field_str((unsigned char*) NULL,
 
116
            DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
 
117
            maybe_null_arg ? (unsigned char*) "": 0,
 
118
            0,
 
119
            field_name_arg,
 
120
            cs)
118
121
{
119
122
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
120
123
  flags|= UNSIGNED_FLAG;
128
131
  Returns value indicating during which operations this TIMESTAMP field
129
132
  should be auto-set to current timestamp.
130
133
*/
131
 
timestamp_auto_set_type Field_timestamp::get_auto_set_type() const
 
134
timestamp_auto_set_type Epoch::get_auto_set_type() const
132
135
{
133
136
  switch (unireg_check)
134
137
  {
156
159
  }
157
160
}
158
161
 
159
 
int Field_timestamp::store(const char *from,
 
162
int Epoch::store(const char *from,
160
163
                           uint32_t len,
161
164
                           const CHARSET_INFO * const )
162
165
{
164
167
 
165
168
  ASSERT_COLUMN_MARKED_FOR_WRITE;
166
169
 
167
 
  if (! temporal.from_string(from, (size_t) len))
 
170
  if (not temporal.from_string(from, (size_t) len))
168
171
  {
169
172
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
170
173
    return 1;
171
174
  }
172
175
 
173
 
  time_t tmp;
174
 
  temporal.to_time_t(&tmp);
 
176
  uint64_t tmp;
 
177
  temporal.to_time_t((time_t*)&tmp);
175
178
 
176
 
  store_timestamp(tmp);
 
179
  pack_num(tmp);
177
180
  return 0;
178
181
}
179
182
 
180
 
int Field_timestamp::store(double from)
 
183
int Epoch::store(double from)
181
184
{
182
185
  ASSERT_COLUMN_MARKED_FOR_WRITE;
183
186
 
193
196
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
194
197
    return 2;
195
198
  }
196
 
  return Field_timestamp::store((int64_t) rint(from), false);
 
199
  return Epoch::store((int64_t) rint(from), false);
197
200
}
198
201
 
199
 
int Field_timestamp::store(int64_t from, bool)
 
202
int Epoch::store(int64_t from, bool)
200
203
{
201
204
  ASSERT_COLUMN_MARKED_FOR_WRITE;
202
205
 
214
217
    return 2;
215
218
  }
216
219
 
217
 
  time_t tmp;
218
 
  temporal.to_time_t(&tmp);
219
 
 
220
 
  store_timestamp(tmp);
 
220
  uint64_t tmp;
 
221
  temporal.to_time_t((time_t*)&tmp);
 
222
 
 
223
  pack_num(tmp);
 
224
 
221
225
  return 0;
222
226
}
223
227
 
224
 
double Field_timestamp::val_real(void)
 
228
double Epoch::val_real(void)
225
229
{
226
 
  return (double) Field_timestamp::val_int();
 
230
  return (double) Epoch::val_int();
227
231
}
228
232
 
229
 
int64_t Field_timestamp::val_int(void)
 
233
int64_t Epoch::val_int(void)
230
234
{
231
235
  uint64_t temp;
232
236
 
233
237
  ASSERT_COLUMN_MARKED_FOR_READ;
234
238
 
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);
 
239
  unpack_num(temp);
241
240
 
242
241
  Timestamp temporal;
243
242
  (void) temporal.from_time_t((time_t) temp);
248
247
  return result;
249
248
}
250
249
 
251
 
String *Field_timestamp::val_str(String *val_buffer, String *)
 
250
String *Epoch::val_str(String *val_buffer, String *)
252
251
{
253
 
  uint64_t temp;
 
252
  uint64_t temp= 0;
254
253
  char *to;
255
254
  int to_len= field_length + 1;
256
255
 
257
256
  val_buffer->alloc(to_len);
258
257
  to= (char *) val_buffer->ptr();
259
258
 
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);
 
259
  unpack_num(temp);
266
260
 
267
261
  val_buffer->set_charset(&my_charset_bin);     /* Safety */
268
262
 
277
271
  return val_buffer;
278
272
}
279
273
 
280
 
bool Field_timestamp::get_date(DRIZZLE_TIME *ltime, uint32_t)
 
274
bool Epoch::get_date(DRIZZLE_TIME *ltime, uint32_t)
281
275
{
282
276
  uint64_t temp;
283
277
 
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);
 
278
  unpack_num(temp);
290
279
  
291
280
  memset(ltime, 0, sizeof(*ltime));
292
281
 
306
295
  return 0;
307
296
}
308
297
 
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 )
 
298
bool Epoch::get_time(DRIZZLE_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 )
334
315
{
335
316
#ifdef WORDS_BIGENDIAN
336
317
  if (!getTable() || !getTable()->getShare()->db_low_byte_first)
358
339
  }
359
340
}
360
341
 
361
 
void Field_timestamp::sql_type(String &res) const
 
342
void Epoch::sql_type(String &res) const
362
343
{
363
344
  res.set_ascii(STRING_WITH_LEN("timestamp"));
364
345
}
365
346
 
366
 
void Field_timestamp::set_time()
 
347
void Epoch::set_time()
367
348
{
368
349
  Session *session= getTable() ? getTable()->in_use : current_session;
369
350
  time_t tmp= session->query_start();
370
351
  set_notnull();
371
 
  store_timestamp(tmp);
 
352
  pack_num(tmp);
372
353
}
373
354
 
374
 
void Field_timestamp::set_default()
 
355
void Epoch::set_default()
375
356
{
376
357
  if (getTable()->timestamp_field == this &&
377
358
      unireg_check != TIMESTAMP_UN_FIELD)
 
359
  {
378
360
    set_time();
 
361
  }
379
362
  else
 
363
  {
380
364
    Field::set_default();
 
365
  }
381
366
}
382
367
 
383
 
long Field_timestamp::get_timestamp(bool *null_value)
 
368
long Epoch::get_timestamp(bool *null_value)
384
369
{
385
370
  if ((*null_value= is_null()))
386
371
    return 0;
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;
 
372
 
 
373
  uint64_t tmp;
 
374
  return unpack_num(tmp);
394
375
}
395
376
 
396
 
void Field_timestamp::store_timestamp(int64_t timestamp)
 
377
size_t Epoch::max_string_length()
397
378
{
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);
 
379
  return sizeof(uint64_t);
406
380
}
407
381
 
 
382
} /* namespace field */
408
383
} /* namespace drizzled */