~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/timestamp.cc

  • Committer: Monty Taylor
  • Date: 2008-09-15 17:24:04 UTC
  • Revision ID: monty@inaugust.com-20080915172404-ygh6hiyu0q7qpa9x
Removed strndup calls.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
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 MySQL
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
#ifdef USE_PRAGMA_IMPLEMENTATION
 
22
#pragma implementation                          // gcc: Class implementation
 
23
#endif
21
24
 
22
25
#include <drizzled/server_includes.h>
23
26
#include <drizzled/field/timestamp.h>
24
 
#include <drizzled/error.h>
25
 
#include <drizzled/tztime.h>
26
 
#include CMATH_H
27
 
 
28
 
#if defined(CMATH_NAMESPACE)
29
 
using namespace CMATH_NAMESPACE;
30
 
#endif
31
27
 
32
28
/**
33
29
  TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to 
73
69
  exception is different behavior of old/new timestamps during ALTER TABLE.
74
70
 */
75
71
 
76
 
Field_timestamp::Field_timestamp(unsigned char *ptr_arg,
 
72
Field_timestamp::Field_timestamp(uchar *ptr_arg,
77
73
                                 uint32_t len_arg __attribute__((unused)),
78
 
                                 unsigned char *null_ptr_arg, unsigned char null_bit_arg,
 
74
                                 uchar *null_ptr_arg, uchar null_bit_arg,
79
75
                                 enum utype unireg_check_arg,
80
76
                                 const char *field_name_arg,
81
77
                                 TABLE_SHARE *share,
99
95
Field_timestamp::Field_timestamp(bool maybe_null_arg,
100
96
                                 const char *field_name_arg,
101
97
                                 const CHARSET_INFO * const cs)
102
 
  :Field_str((unsigned char*) 0, MAX_DATETIME_WIDTH,
103
 
             maybe_null_arg ? (unsigned char*) "": 0, 0,
 
98
  :Field_str((uchar*) 0, MAX_DATETIME_WIDTH,
 
99
             maybe_null_arg ? (uchar*) "": 0, 0,
104
100
             NONE, field_name_arg, cs)
105
101
{
106
102
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
146
142
 
147
143
 
148
144
int Field_timestamp::store(const char *from,
149
 
                           uint32_t len,
 
145
                           uint len,
150
146
                           const CHARSET_INFO * const cs __attribute__((unused)))
151
147
{
152
148
  DRIZZLE_TIME l_time;
154
150
  int error;
155
151
  bool have_smth_to_conv;
156
152
  bool in_dst_time_gap;
157
 
  Session *session= table ? table->in_use : current_session;
 
153
  THD *thd= table ? table->in_use : current_thd;
158
154
 
159
155
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
160
156
  have_smth_to_conv= (str_to_datetime(from, len, &l_time, 1, &error) >
170
166
  /* Only convert a correct date (not a zero date) */
171
167
  if (have_smth_to_conv && l_time.month)
172
168
  {
173
 
    if (!(tmp= TIME_to_timestamp(session, &l_time, &in_dst_time_gap)))
 
169
    if (!(tmp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
174
170
    {
175
171
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
176
172
                           ER_WARN_DATA_OUT_OF_RANGE,
213
209
  my_time_t timestamp= 0;
214
210
  int error;
215
211
  bool in_dst_time_gap;
216
 
  Session *session= table ? table->in_use : current_session;
 
212
  THD *thd= table ? table->in_use : current_thd;
217
213
 
218
214
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
219
 
  int64_t tmp= number_to_datetime(nr, &l_time, (session->variables.sql_mode &
 
215
  int64_t tmp= number_to_datetime(nr, &l_time, (thd->variables.sql_mode &
220
216
                                                 MODE_NO_ZERO_DATE), &error);
221
 
  if (tmp == INT64_C(-1))
 
217
  if (tmp == -1LL)
222
218
  {
223
219
    error= 2;
224
220
  }
225
221
 
226
222
  if (!error && tmp)
227
223
  {
228
 
    if (!(timestamp= TIME_to_timestamp(session, &l_time, &in_dst_time_gap)))
 
224
    if (!(timestamp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
229
225
    {
230
226
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
231
227
                           ER_WARN_DATA_OUT_OF_RANGE,
257
253
{
258
254
  uint32_t temp;
259
255
  DRIZZLE_TIME time_tmp;
260
 
  Session  *session= table ? table->in_use : current_session;
 
256
  THD  *thd= table ? table->in_use : current_thd;
261
257
 
262
 
  session->time_zone_used= 1;
 
258
  thd->time_zone_used= 1;
263
259
#ifdef WORDS_BIGENDIAN
264
260
  if (table && table->s->db_low_byte_first)
265
261
    temp=uint4korr(ptr);
270
266
  if (temp == 0L)                               // No time
271
267
    return(0);                                  /* purecov: inspected */
272
268
  
273
 
  session->variables.time_zone->gmt_sec_to_TIME(&time_tmp, (my_time_t)temp);
 
269
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp, (my_time_t)temp);
274
270
  
275
 
  return time_tmp.year * INT64_C(10000000000) +
276
 
         time_tmp.month * INT64_C(100000000) +
277
 
         time_tmp.day * 1000000 + time_tmp.hour * 10000 +
 
271
  return time_tmp.year * 10000000000LL + time_tmp.month * 100000000LL +
 
272
         time_tmp.day * 1000000L + time_tmp.hour * 10000L +
278
273
         time_tmp.minute * 100 + time_tmp.second;
279
274
}
280
275
 
283
278
{
284
279
  uint32_t temp, temp2;
285
280
  DRIZZLE_TIME time_tmp;
286
 
  Session *session= table ? table->in_use : current_session;
 
281
  THD *thd= table ? table->in_use : current_thd;
287
282
  char *to;
288
283
 
289
284
  val_buffer->alloc(field_length+1);
290
285
  to= (char*) val_buffer->ptr();
291
286
  val_buffer->length(field_length);
292
287
 
293
 
  session->time_zone_used= 1;
 
288
  thd->time_zone_used= 1;
294
289
#ifdef WORDS_BIGENDIAN
295
290
  if (table && table->s->db_low_byte_first)
296
291
    temp=uint4korr(ptr);
305
300
  }
306
301
  val_buffer->set_charset(&my_charset_bin);     // Safety
307
302
  
308
 
  session->variables.time_zone->gmt_sec_to_TIME(&time_tmp,(my_time_t)temp);
 
303
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp,(my_time_t)temp);
309
304
 
310
305
  temp= time_tmp.year % 100;
311
306
  if (temp < YY_PART_YEAR - 1)
351
346
}
352
347
 
353
348
 
354
 
bool Field_timestamp::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
 
349
bool Field_timestamp::get_date(DRIZZLE_TIME *ltime, uint fuzzydate)
355
350
{
356
351
  long temp;
357
 
  Session *session= table ? table->in_use : current_session;
358
 
  session->time_zone_used= 1;
 
352
  THD *thd= table ? table->in_use : current_thd;
 
353
  thd->time_zone_used= 1;
359
354
#ifdef WORDS_BIGENDIAN
360
355
  if (table && table->s->db_low_byte_first)
361
356
    temp=uint4korr(ptr);
370
365
  }
371
366
  else
372
367
  {
373
 
    session->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)temp);
 
368
    thd->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)temp);
374
369
  }
375
370
  return 0;
376
371
}
389
384
}
390
385
 
391
386
 
392
 
int Field_timestamp::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
387
int Field_timestamp::cmp(const uchar *a_ptr, const uchar *b_ptr)
393
388
{
394
389
  int32_t a,b;
395
390
#ifdef WORDS_BIGENDIAN
408
403
}
409
404
 
410
405
 
411
 
void Field_timestamp::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
 
406
void Field_timestamp::sort_string(uchar *to,uint length __attribute__((unused)))
412
407
{
413
408
#ifdef WORDS_BIGENDIAN
414
409
  if (!table || !table->s->db_low_byte_first)
437
432
 
438
433
void Field_timestamp::set_time()
439
434
{
440
 
  Session *session= table ? table->in_use : current_session;
441
 
  long tmp= (long) session->query_start();
 
435
  THD *thd= table ? table->in_use : current_thd;
 
436
  long tmp= (long) thd->query_start();
442
437
  set_notnull();
443
438
  store_timestamp(tmp);
444
439
}