~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/field/date.cc

Merge/fix in FAQ.

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
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include <config.h>
22
 
#include <boost/lexical_cast.hpp>
23
 
 
24
 
#include <drizzled/field/date.h>
25
 
#include <drizzled/error.h>
26
 
#include <drizzled/table.h>
27
 
#include <drizzled/temporal.h>
28
 
#include <drizzled/session.h>
29
 
#include <drizzled/time_functions.h>
30
 
#include <drizzled/current_session.h>
31
 
 
32
 
#include <math.h>
33
 
 
34
 
#include <sstream>
35
 
#include <string>
36
 
 
37
 
namespace drizzled
38
 
{
39
 
 
 
21
#ifdef USE_PRAGMA_IMPLEMENTATION
 
22
#pragma implementation                          // gcc: Class implementation
 
23
#endif
 
24
 
 
25
#include "date.h"
40
26
 
41
27
/****************************************************************************
42
 
** Drizzle date type stored in 3 bytes
 
28
** The new date type
 
29
** This is identical to the old date type, but stored on 3 bytes instead of 4
43
30
** In number context: YYYYMMDD
44
31
****************************************************************************/
45
32
 
47
34
  Store string into a date field
48
35
 
49
36
  SYNOPSIS
50
 
    Field_date::store()
 
37
    Field_newdate::store()
51
38
    from                Date string
52
39
    len                 Length of date field
53
40
    cs                  Character set (not used)
61
48
       nearly-identical class Field_date doesn't ever return 3 from its
62
49
       store function.
63
50
*/
64
 
int Field_date::store(const char *from,
65
 
                         uint32_t len,
66
 
                         const CHARSET_INFO * const )
67
 
{
68
 
  /* 
69
 
   * Try to create a DateTime from the supplied string.  Throw an error
70
 
   * if unable to create a valid DateTime.  A DateTime is used so that
71
 
   * automatic conversion from the higher-storage DateTime can be used
72
 
   * and matches on datetime format strings can occur.
73
 
   */
74
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
75
 
  DateTime temporal;
76
 
  if (! temporal.from_string(from, (size_t) len))
77
 
  {
78
 
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), from);
79
 
    return 2;
80
 
  }
81
 
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
82
 
  uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
83
 
  int4store(ptr, int_value);
84
 
  return 0;
85
 
}
86
 
 
87
 
int Field_date::store(double from)
88
 
{
89
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
90
 
  if (from < 0.0 || from > 99991231235959.0)
91
 
  {
92
 
    /* Convert the double to a string using stringstream */
93
 
    std::stringstream ss;
94
 
    std::string tmp;
95
 
    ss.precision(18); /* 18 places should be fine for error display of double input. */
96
 
    ss << from; ss >> tmp;
97
 
 
98
 
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
99
 
    return 2;
100
 
  }
101
 
  return Field_date::store((int64_t) rint(from), false);
102
 
}
103
 
 
104
 
int Field_date::store(int64_t from, bool)
105
 
{
106
 
  /* 
107
 
   * Try to create a DateTime from the supplied integer.  Throw an error
108
 
   * if unable to create a valid DateTime.  
109
 
   */
110
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
111
 
  DateTime temporal;
112
 
  if (! temporal.from_int64_t(from))
113
 
  {
114
 
    /* Convert the integer to a string using boost::lexical_cast */
115
 
    std::string tmp(boost::lexical_cast<std::string>(from)); 
116
 
 
117
 
    my_error(ER_INVALID_DATE_VALUE, MYF(ME_FATALERROR), tmp.c_str());
118
 
    return 2;
119
 
  }
120
 
 
121
 
  /* Create the stored integer format. @TODO This should go away. Should be up to engine... */
122
 
  uint32_t int_value= (temporal.years() * 10000) + (temporal.months() * 100) + temporal.days();
123
 
  int4store(ptr, int_value);
124
 
 
125
 
  return 0;
126
 
}
127
 
 
128
 
int Field_date::store_time(type::Time &ltime,
129
 
                           type::timestamp_t time_type)
 
51
 
 
52
int Field_newdate::store(const char *from,
 
53
                         uint len,
 
54
                         CHARSET_INFO *cs __attribute__((__unused__)))
 
55
{
 
56
  long tmp;
 
57
  MYSQL_TIME l_time;
 
58
  int error;
 
59
  THD *thd= table ? table->in_use : current_thd;
 
60
  enum enum_mysql_timestamp_type ret;
 
61
  if ((ret= str_to_datetime(from, len, &l_time,
 
62
                            (TIME_FUZZY_DATE |
 
63
                             (thd->variables.sql_mode &
 
64
                              (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
65
                               MODE_INVALID_DATES))),
 
66
                            &error)) <= MYSQL_TIMESTAMP_ERROR)
 
67
  {
 
68
    tmp= 0;
 
69
    error= 2;
 
70
  }
 
71
  else
 
72
  {
 
73
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
 
74
    if (!error && (ret != MYSQL_TIMESTAMP_DATE) &&
 
75
        (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
 
76
      error= 3;                                 // Datetime was cut (note)
 
77
  }
 
78
 
 
79
  if (error)
 
80
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
 
81
                         MYSQL_ERROR::WARN_LEVEL_WARN,
 
82
                         WARN_DATA_TRUNCATED,
 
83
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
 
84
 
 
85
  int3store(ptr, tmp);
 
86
  return error;
 
87
}
 
88
 
 
89
 
 
90
int Field_newdate::store(double nr)
 
91
{
 
92
  if (nr < 0.0 || nr > 99991231235959.0)
 
93
  {
 
94
    int3store(ptr,(int32) 0);
 
95
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
 
96
                         WARN_DATA_TRUNCATED, nr, MYSQL_TIMESTAMP_DATE);
 
97
    return 1;
 
98
  }
 
99
  return Field_newdate::store((int64_t) rint(nr), false);
 
100
}
 
101
 
 
102
 
 
103
int Field_newdate::store(int64_t nr,
 
104
                         bool unsigned_val __attribute__((__unused__)))
 
105
{
 
106
  MYSQL_TIME l_time;
 
107
  int64_t tmp;
 
108
  int error;
 
109
  THD *thd= table ? table->in_use : current_thd;
 
110
  if (number_to_datetime(nr, &l_time,
 
111
                         (TIME_FUZZY_DATE |
 
112
                          (thd->variables.sql_mode &
 
113
                           (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
114
                            MODE_INVALID_DATES))),
 
115
                         &error) == -1LL)
 
116
  {
 
117
    tmp= 0L;
 
118
    error= 2;
 
119
  }
 
120
  else
 
121
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
 
122
 
 
123
  if (!error && l_time.time_type != MYSQL_TIMESTAMP_DATE &&
 
124
      (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
 
125
    error= 3;
 
126
 
 
127
  if (error)
 
128
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
 
129
                         MYSQL_ERROR::WARN_LEVEL_WARN,
 
130
                         error == 2 ? 
 
131
                         ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED,
 
132
                         nr,MYSQL_TIMESTAMP_DATE, 1);
 
133
 
 
134
  int3store(ptr,tmp);
 
135
  return error;
 
136
}
 
137
 
 
138
 
 
139
int Field_newdate::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
130
140
{
131
141
  long tmp;
132
142
  int error= 0;
133
 
  if (time_type == type::DRIZZLE_TIMESTAMP_DATE || time_type == type::DRIZZLE_TIMESTAMP_DATETIME)
 
143
  if (time_type == MYSQL_TIMESTAMP_DATE ||
 
144
      time_type == MYSQL_TIMESTAMP_DATETIME)
134
145
  {
135
 
    tmp= ltime.year*10000 + ltime.month*100 + ltime.day;
136
 
 
137
 
    Session *session= getTable() ? getTable()->in_use : current_session;
138
 
    type::cut_t cut_error= type::VALID;
139
 
    if (ltime.check(tmp != 0,
140
 
                     (TIME_FUZZY_DATE |
141
 
                      (session->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES))), cut_error))
 
146
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
 
147
    if (check_date(ltime, tmp != 0,
 
148
                   (TIME_FUZZY_DATE |
 
149
                    (current_thd->variables.sql_mode &
 
150
                     (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
 
151
                      MODE_INVALID_DATES))), &error))
142
152
    {
143
 
      char buff[type::Time::MAX_STRING_LENGTH];
144
 
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
145
 
      ltime.convert(str, type::DRIZZLE_TIMESTAMP_DATE);
146
 
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
147
 
                           str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
 
153
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
154
      String str(buff, sizeof(buff), &my_charset_latin1);
 
155
      make_date((DATE_TIME_FORMAT *) 0, ltime, &str);
 
156
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
 
157
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
148
158
    }
149
 
 
150
 
    error= static_cast<int>(cut_error);
151
 
 
152
 
    if (not error && ltime.time_type != type::DRIZZLE_TIMESTAMP_DATE &&
153
 
        (ltime.hour || ltime.minute || ltime.second || ltime.second_part))
 
159
    if (!error && ltime->time_type != MYSQL_TIMESTAMP_DATE &&
 
160
        (ltime->hour || ltime->minute || ltime->second || ltime->second_part))
154
161
    {
155
 
      char buff[type::Time::MAX_STRING_LENGTH];
156
 
      String str(buff, sizeof(buff), &my_charset_utf8_general_ci);
157
 
      ltime.convert(str);
158
 
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE,
159
 
                           ER_WARN_DATA_TRUNCATED,
160
 
                           str.ptr(), str.length(), type::DRIZZLE_TIMESTAMP_DATE, 1);
 
162
      char buff[MAX_DATE_STRING_REP_LENGTH];
 
163
      String str(buff, sizeof(buff), &my_charset_latin1);
 
164
      make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
 
165
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_NOTE,
 
166
                           WARN_DATA_TRUNCATED,
 
167
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
161
168
      error= 3;
162
169
    }
163
170
  }
165
172
  {
166
173
    tmp=0;
167
174
    error= 1;
168
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
 
175
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
169
176
  }
170
 
 
171
 
  int4store(ptr,tmp);
172
 
 
 
177
  int3store(ptr,tmp);
173
178
  return error;
174
179
}
175
180
 
176
 
double Field_date::val_real(void) const
177
 
{
178
 
  return (double) Field_date::val_int();
179
 
}
180
 
 
181
 
int64_t Field_date::val_int(void) const
182
 
{
183
 
  uint32_t j;
184
 
 
185
 
  ASSERT_COLUMN_MARKED_FOR_READ;
186
 
 
187
 
  j= uint4korr(ptr);
188
 
 
 
181
 
 
182
bool Field_newdate::send_binary(Protocol *protocol)
 
183
{
 
184
  MYSQL_TIME tm;
 
185
  Field_newdate::get_date(&tm,0);
 
186
  return protocol->store_date(&tm);
 
187
}
 
188
 
 
189
 
 
190
double Field_newdate::val_real(void)
 
191
{
 
192
  return (double) Field_newdate::val_int();
 
193
}
 
194
 
 
195
 
 
196
int64_t Field_newdate::val_int(void)
 
197
{
 
198
  ulong j= uint3korr(ptr);
 
199
  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
189
200
  return (int64_t) j;
190
201
}
191
202
 
192
 
String *Field_date::val_str(String *val_buffer, String *) const
 
203
 
 
204
String *Field_newdate::val_str(String *val_buffer,
 
205
                               String *val_ptr __attribute__((unused)))
193
206
{
194
207
  val_buffer->alloc(field_length);
195
208
  val_buffer->length(field_length);
196
 
  uint32_t tmp=(uint32_t) uint4korr(ptr);
197
 
  int32_t part;
 
209
  uint32 tmp=(uint32) uint3korr(ptr);
 
210
  int part;
198
211
  char *pos=(char*) val_buffer->ptr()+10;
199
212
 
200
 
  ASSERT_COLUMN_MARKED_FOR_READ;
201
 
 
202
213
  /* Open coded to get more speed */
203
214
  *pos--=0;                                     // End NULL
204
 
  part=(int32_t) (tmp % 100);
205
 
  *pos--= (char) ('0'+part%10);
206
 
  *pos--= (char) ('0'+part/10);
207
 
  *pos--= '-';
208
 
  part=(int32_t) (tmp/100%100);
209
 
  *pos--= (char) ('0'+part%10);
210
 
  *pos--= (char) ('0'+part/10);
211
 
  *pos--= '-';
212
 
  part=(int32_t) (tmp/10000);
 
215
  part=(int) (tmp & 31);
 
216
  *pos--= (char) ('0'+part%10);
 
217
  *pos--= (char) ('0'+part/10);
 
218
  *pos--= '-';
 
219
  part=(int) (tmp >> 5 & 15);
 
220
  *pos--= (char) ('0'+part%10);
 
221
  *pos--= (char) ('0'+part/10);
 
222
  *pos--= '-';
 
223
  part=(int) (tmp >> 9);
213
224
  *pos--= (char) ('0'+part%10); part/=10;
214
225
  *pos--= (char) ('0'+part%10); part/=10;
215
226
  *pos--= (char) ('0'+part%10); part/=10;
217
228
  return val_buffer;
218
229
}
219
230
 
220
 
bool Field_date::get_date(type::Time &ltime, uint32_t fuzzydate) const
 
231
 
 
232
bool Field_newdate::get_date(MYSQL_TIME *ltime,uint fuzzydate)
221
233
{
222
 
  uint32_t tmp=(uint32_t) uint4korr(ptr);
223
 
  ltime.day=            (int) (tmp%100);
224
 
  ltime.month=  (int) (tmp/100%100);
225
 
  ltime.year=           (int) (tmp/10000);
226
 
  ltime.time_type= type::DRIZZLE_TIMESTAMP_DATE;
227
 
  ltime.hour= ltime.minute= ltime.second= ltime.second_part= ltime.neg= 0;
228
 
 
229
 
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime.month || !ltime.day)) ?
 
234
  uint32 tmp=(uint32) uint3korr(ptr);
 
235
  ltime->day=   tmp & 31;
 
236
  ltime->month= (tmp >> 5) & 15;
 
237
  ltime->year=  (tmp >> 9);
 
238
  ltime->time_type= MYSQL_TIMESTAMP_DATE;
 
239
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
 
240
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ?
230
241
          1 : 0);
231
242
}
232
243
 
233
 
bool Field_date::get_time(type::Time &ltime) const
 
244
 
 
245
bool Field_newdate::get_time(MYSQL_TIME *ltime)
234
246
{
235
 
  return Field_date::get_date(ltime ,0);
 
247
  return Field_newdate::get_date(ltime,0);
236
248
}
237
249
 
238
 
int Field_date::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
250
 
 
251
int Field_newdate::cmp(const uchar *a_ptr, const uchar *b_ptr)
239
252
{
240
 
  uint32_t a,b;
241
 
  a=(uint32_t) uint4korr(a_ptr);
242
 
  b=(uint32_t) uint4korr(b_ptr);
 
253
  uint32 a,b;
 
254
  a=(uint32) uint3korr(a_ptr);
 
255
  b=(uint32) uint3korr(b_ptr);
243
256
  return (a < b) ? -1 : (a > b) ? 1 : 0;
244
257
}
245
258
 
246
 
void Field_date::sort_string(unsigned char *to,uint32_t )
 
259
 
 
260
void Field_newdate::sort_string(uchar *to,uint length __attribute__((unused)))
247
261
{
248
 
  to[0] = ptr[3];
249
 
  to[1] = ptr[2];
250
 
  to[2] = ptr[1];
251
 
  to[3] = ptr[0];
 
262
  to[0] = ptr[2];
 
263
  to[1] = ptr[1];
 
264
  to[2] = ptr[0];
252
265
}
253
266
 
254
 
void Field_date::sql_type(String &res) const
 
267
 
 
268
void Field_newdate::sql_type(String &res) const
255
269
{
256
270
  res.set_ascii(STRING_WITH_LEN("date"));
257
271
}
258
272
 
259
 
} /* namespace drizzled */