~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/timestamp.cc

Moved base64.h to mysys.

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
4
 *  Copyright (C) 2008 MySQL
22
22
#pragma implementation                          // gcc: Class implementation
23
23
#endif
24
24
 
25
 
#include <drizzled/server_includes.h>
26
25
#include <drizzled/field/timestamp.h>
27
26
 
28
27
/**
69
68
  exception is different behavior of old/new timestamps during ALTER TABLE.
70
69
 */
71
70
 
72
 
Field_timestamp::Field_timestamp(unsigned char *ptr_arg,
 
71
Field_timestamp::Field_timestamp(uchar *ptr_arg,
73
72
                                 uint32_t len_arg __attribute__((unused)),
74
 
                                 unsigned char *null_ptr_arg, unsigned char null_bit_arg,
 
73
                                 uchar *null_ptr_arg, uchar null_bit_arg,
75
74
                                 enum utype unireg_check_arg,
76
75
                                 const char *field_name_arg,
77
76
                                 TABLE_SHARE *share,
78
 
                                 const CHARSET_INFO * const cs)
 
77
                                 CHARSET_INFO *cs)
79
78
  :Field_str(ptr_arg, MAX_DATETIME_WIDTH, null_ptr_arg, null_bit_arg,
80
79
             unireg_check_arg, field_name_arg, cs)
81
80
{
82
81
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
83
 
  flags|= UNSIGNED_FLAG;
 
82
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
84
83
  if (!share->timestamp_field && unireg_check != NONE)
85
84
  {
86
85
    /* This timestamp has auto-update */
94
93
 
95
94
Field_timestamp::Field_timestamp(bool maybe_null_arg,
96
95
                                 const char *field_name_arg,
97
 
                                 const CHARSET_INFO * const cs)
98
 
  :Field_str((unsigned char*) 0, MAX_DATETIME_WIDTH,
99
 
             maybe_null_arg ? (unsigned char*) "": 0, 0,
 
96
                                 CHARSET_INFO *cs)
 
97
  :Field_str((uchar*) 0, MAX_DATETIME_WIDTH,
 
98
             maybe_null_arg ? (uchar*) "": 0, 0,
100
99
             NONE, field_name_arg, cs)
101
100
{
102
101
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
103
 
  flags|= UNSIGNED_FLAG;
 
102
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
104
103
    if (unireg_check != TIMESTAMP_DN_FIELD)
105
104
      flags|= ON_UPDATE_NOW_FLAG;
106
105
}
142
141
 
143
142
 
144
143
int Field_timestamp::store(const char *from,
145
 
                           uint32_t len,
146
 
                           const CHARSET_INFO * const cs __attribute__((unused)))
 
144
                           uint len,
 
145
                           CHARSET_INFO *cs __attribute__((unused)))
147
146
{
148
 
  DRIZZLE_TIME l_time;
 
147
  MYSQL_TIME l_time;
149
148
  my_time_t tmp= 0;
150
149
  int error;
151
150
  bool have_smth_to_conv;
154
153
 
155
154
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
156
155
  have_smth_to_conv= (str_to_datetime(from, len, &l_time, 1, &error) >
157
 
                      DRIZZLE_TIMESTAMP_ERROR);
 
156
                      MYSQL_TIMESTAMP_ERROR);
158
157
 
159
158
  if (error || !have_smth_to_conv)
160
159
  {
161
160
    error= 1;
162
 
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
163
 
                         from, len, DRIZZLE_TIMESTAMP_DATETIME, 1);
 
161
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
162
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
164
163
  }
165
164
 
166
165
  /* Only convert a correct date (not a zero date) */
168
167
  {
169
168
    if (!(tmp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
170
169
    {
171
 
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
170
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
172
171
                           ER_WARN_DATA_OUT_OF_RANGE,
173
 
                           from, len, DRIZZLE_TIMESTAMP_DATETIME, !error);
 
172
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
174
173
      error= 1;
175
174
    }
176
175
    else if (in_dst_time_gap)
177
176
    {
178
 
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
177
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
179
178
                           ER_WARN_INVALID_TIMESTAMP,
180
 
                           from, len, DRIZZLE_TIMESTAMP_DATETIME, !error);
 
179
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
181
180
      error= 1;
182
181
    }
183
182
  }
191
190
  int error= 0;
192
191
  if (nr < 0 || nr > 99991231235959.0)
193
192
  {
194
 
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
193
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
195
194
                         ER_WARN_DATA_OUT_OF_RANGE,
196
 
                         nr, DRIZZLE_TIMESTAMP_DATETIME);
 
195
                         nr, MYSQL_TIMESTAMP_DATETIME);
197
196
    nr= 0;                                      // Avoid overflow on buff
198
197
    error= 1;
199
198
  }
205
204
int Field_timestamp::store(int64_t nr,
206
205
                           bool unsigned_val __attribute__((unused)))
207
206
{
208
 
  DRIZZLE_TIME l_time;
 
207
  MYSQL_TIME l_time;
209
208
  my_time_t timestamp= 0;
210
209
  int error;
211
210
  bool in_dst_time_gap;
213
212
 
214
213
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
215
214
  int64_t tmp= number_to_datetime(nr, &l_time, (thd->variables.sql_mode &
216
 
                                                 MODE_NO_ZERO_DATE), &error);
217
 
  if (tmp == INT64_C(-1))
 
215
                                                 MODE_NO_ZERO_DATE) |
 
216
                                   MODE_NO_ZERO_IN_DATE, &error);
 
217
  if (tmp == -1LL)
218
218
  {
219
219
    error= 2;
220
220
  }
223
223
  {
224
224
    if (!(timestamp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
225
225
    {
226
 
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
226
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
227
227
                           ER_WARN_DATA_OUT_OF_RANGE,
228
 
                           nr, DRIZZLE_TIMESTAMP_DATETIME, 1);
 
228
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
229
229
      error= 1;
230
230
    }
231
231
    if (in_dst_time_gap)
232
232
    {
233
 
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
233
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
234
234
                           ER_WARN_INVALID_TIMESTAMP,
235
 
                           nr, DRIZZLE_TIMESTAMP_DATETIME, 1);
 
235
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
236
236
      error= 1;
237
237
    }
238
238
  } else if (error)
239
 
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
240
 
                         ER_WARN_DATA_TRUNCATED,
241
 
                         nr, DRIZZLE_TIMESTAMP_DATETIME, 1);
 
239
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
240
                         WARN_DATA_TRUNCATED,
 
241
                         nr, MYSQL_TIMESTAMP_DATETIME, 1);
242
242
 
243
243
  store_timestamp(timestamp);
244
244
  return error;
252
252
int64_t Field_timestamp::val_int(void)
253
253
{
254
254
  uint32_t temp;
255
 
  DRIZZLE_TIME time_tmp;
 
255
  MYSQL_TIME time_tmp;
256
256
  THD  *thd= table ? table->in_use : current_thd;
257
257
 
258
258
  thd->time_zone_used= 1;
268
268
  
269
269
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp, (my_time_t)temp);
270
270
  
271
 
  return time_tmp.year * INT64_C(10000000000) +
272
 
         time_tmp.month * INT64_C(100000000) +
273
 
         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 +
274
273
         time_tmp.minute * 100 + time_tmp.second;
275
274
}
276
275
 
278
277
String *Field_timestamp::val_str(String *val_buffer, String *val_ptr)
279
278
{
280
279
  uint32_t temp, temp2;
281
 
  DRIZZLE_TIME time_tmp;
 
280
  MYSQL_TIME time_tmp;
282
281
  THD *thd= table ? table->in_use : current_thd;
283
282
  char *to;
284
283
 
347
346
}
348
347
 
349
348
 
350
 
bool Field_timestamp::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
 
349
bool Field_timestamp::get_date(MYSQL_TIME *ltime, uint fuzzydate)
351
350
{
352
351
  long temp;
353
352
  THD *thd= table ? table->in_use : current_thd;
362
361
  {                                   /* Zero time is "000000" */
363
362
    if (fuzzydate & TIME_NO_ZERO_DATE)
364
363
      return 1;
365
 
    memset(ltime, 0, sizeof(*ltime));
 
364
    bzero((char*) ltime,sizeof(*ltime));
366
365
  }
367
366
  else
368
367
  {
371
370
  return 0;
372
371
}
373
372
 
374
 
bool Field_timestamp::get_time(DRIZZLE_TIME *ltime)
 
373
bool Field_timestamp::get_time(MYSQL_TIME *ltime)
375
374
{
376
375
  return Field_timestamp::get_date(ltime,0);
377
376
}
379
378
 
380
379
bool Field_timestamp::send_binary(Protocol *protocol)
381
380
{
382
 
  DRIZZLE_TIME tm;
 
381
  MYSQL_TIME tm;
383
382
  Field_timestamp::get_date(&tm, 0);
384
383
  return protocol->store(&tm);
385
384
}
386
385
 
387
386
 
388
 
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)
389
388
{
390
389
  int32_t a,b;
391
390
#ifdef WORDS_BIGENDIAN
404
403
}
405
404
 
406
405
 
407
 
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)))
408
407
{
409
408
#ifdef WORDS_BIGENDIAN
410
409
  if (!table || !table->s->db_low_byte_first)